Skip to content

Commit c932984

Browse files
committed
more test
1 parent b9a8aae commit c932984

File tree

8 files changed

+153
-16
lines changed

8 files changed

+153
-16
lines changed

src/main/kotlin/components/Extensions.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package components
33
import groovy.json.StringEscapeUtils
44
import models.Configuration
55
import models.Extension
6+
import models.ResourceFile
67
import org.gradle.api.NamedDomainObjectContainer
78
import org.gradle.api.Project
89
import java.io.File
@@ -36,6 +37,30 @@ fun File.validForConfiguration(configuration: Configuration): Boolean {
3637
return valid
3738
}
3839

40+
fun File.resourceFile(configuration: Configuration): ResourceFile? {
41+
var sourceFolder = ""
42+
var validFile: File? = null
43+
var valid = false
44+
configuration.srcFolders.forEach { folder ->
45+
if (this.absolutePath.contains("/$folder/".replace("//", "/"))
46+
&& !this.absolutePath.contains("/$resourceBackup/")
47+
) {
48+
sourceFolder = folder
49+
valid = true
50+
}
51+
}
52+
if (valid) {
53+
valid = false
54+
configuration.stringFiles.forEach { file ->
55+
if (this.absolutePath.contains("/$file".replace("//", "/"))) {
56+
valid = true
57+
validFile = this
58+
}
59+
}
60+
}
61+
return if (valid) ResourceFile(validFile!!, sourceFolder, configuration.name) else null
62+
}
63+
3964
fun Project.absolutePath(): String = this.file(wrapperWindows).absolutePath.replace(
4065
wrapperWindows,
4166
emptyChar
@@ -50,3 +75,23 @@ fun Process.outputString() = this.inputStream.bufferedReader().use { it.readText
5075
fun defaultConfig(): Configuration {
5176
return Configuration("app", listOf("strings.xml"), listOf("src${File.separator}main"))
5277
}
78+
79+
fun ResourceFile.backup(projectPath: String): File {
80+
val cleanPath = "$projectPath${File.separator}$resourceBackup${File.separator}${this.module}" +
81+
"${File.separator}${this.sourceFolder}${this.file.absolutePath.split(this.sourceFolder)[1]}"
82+
.replace("${File.separator}${File.separator}", File.separator)
83+
84+
val backupFile = File(cleanPath)
85+
this.file.copyTo(backupFile)
86+
return backupFile
87+
}
88+
89+
fun File.restore(projectPath: String): File {
90+
val cleanPath = "$projectPath${File.separator}${this.absolutePath.split(resourceBackup)[1]}"
91+
.replace("${File.separator}${File.separator}", File.separator)
92+
93+
val restore = File(cleanPath)
94+
this.copyTo(restore, true)
95+
return restore
96+
}
97+

src/main/kotlin/models/Fingerprint.kt renamed to src/main/kotlin/components/Fingerprint.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
package models
2-
3-
import components.PrintUtils
4-
import components.runCommand
5-
import components.signingReportTask
1+
package components
62

73
private class Fingerprint {
84

src/main/kotlin/models/Os.kt renamed to src/main/kotlin/components/Os.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package models
1+
package components
22

33
enum class Os {
44
WINDOWS,

src/main/kotlin/components/Tasks.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package components
22

3-
import models.Os
4-
import models.getOs
5-
63
internal fun signingReportTask(): String = "${when (getOs()) {
74
Os.WINDOWS -> wrapperWindows
85
Os.OSX -> wrapperOsX

src/main/kotlin/components/XParser.kt

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,63 @@
11
package components
22

33
import models.Configuration
4+
import models.ResourceFile
5+
import models.SAttribute
6+
import models.StringEntity
7+
import org.xml.sax.InputSource
48
import java.io.File
9+
import javax.xml.parsers.DocumentBuilderFactory
510

6-
fun locateFiles(projectPath: String, configuration: Configuration): List<File> = File(projectPath).walkTopDown()
11+
fun locateFiles(projectPath: String, configuration: Configuration): List<ResourceFile> = File(projectPath).walkTopDown()
712
.filterIndexed { _, file ->
813
file.validForConfiguration(configuration)
9-
}.toList()
14+
}.map { it.resourceFile(configuration)!! }.toList()
15+
16+
fun backupFiles(projectPath: String, configuration: Configuration): List<File> {
17+
val resourceFiles = mutableListOf<File>()
18+
val files = locateFiles(projectPath, configuration)
19+
files.forEach { resource ->
20+
resourceFiles.add(resource.backup(projectPath))
21+
}
22+
return resourceFiles
23+
}
24+
25+
fun restoreFiles(projectPath: String): List<File> {
26+
val resourceFiles = File("$projectPath${File.separator}$resourceBackup").walkTopDown().toList()
27+
resourceFiles.filter { file ->
28+
!file.isDirectory
29+
}.map {
30+
it.restore(projectPath)
31+
}
32+
return resourceFiles
33+
}
34+
35+
fun parseXML(file: File, module: String, debug: Boolean): List<StringEntity> {
36+
if (debug) {
37+
val content = file.inputStream().readBytes().toString(Charsets.UTF_8)
38+
PrintUtils.print(module, content, true)
39+
}
40+
val entities = mutableListOf<StringEntity>()
41+
val builderFactory = DocumentBuilderFactory.newInstance()
42+
val docBuilder = builderFactory.newDocumentBuilder()
43+
val doc = docBuilder.parse(file.inputStream())
44+
// reading player tags
45+
val nList = doc.getElementsByTagName("string")
46+
for (i in 0 until nList.length) {
47+
val node = nList.item(i)
48+
var name = ""
49+
val attributes = mutableListOf<SAttribute>()
50+
for (a in 0 until node.attributes.length) {
51+
val attribute = node.attributes.item(a)
52+
for (n in 0 until attribute.childNodes.length) {
53+
val attr = attribute.childNodes.item(n)
54+
// can set value
55+
if (attribute.nodeName == "name") name = attr.nodeValue
56+
attributes.add(SAttribute(attribute.nodeName, attr.nodeValue))
57+
}
58+
}
59+
entities.add(StringEntity(name, attributes, node.firstChild.nodeValue))
60+
}
61+
return entities
62+
}
63+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package models
2+
3+
import java.io.File
4+
5+
data class ResourceFile(val file: File, val sourceFolder: String, val module: String)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package models
2+
3+
data class StringEntity(val name: String, val attributes: List<SAttribute>, val value: String)
4+
data class SAttribute(val name: String, val value: String)

src/test/kotlin/SCTest.kt

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import components.*
2-
import models.extractFingerprint
32
import org.junit.Test
43

54

65
class SCTest {
76

87
private val logger by logger()
98

9+
private val projectName = "KotlinSample"
1010
private val prepareTask = """
11-
rm -rf KotlinSample &&
12-
git clone https://github.com/StringCare/KotlinSample.git &&
13-
cd KotlinSample
11+
rm -rf $projectName &&
12+
git clone https://github.com/StringCare/$projectName.git &&
13+
cd $projectName
1414
""".trimIndent()
1515

1616
// gradlew task needs export ANDROID_SDK_ROOT=/Users/efrainespada/Library/Android/sdk
@@ -44,9 +44,45 @@ class SCTest {
4444
@Test
4545
fun `locate string files for default configuration`() {
4646
prepareTask.runCommand { _, _ ->
47-
assert(locateFiles("KotlinSample", defaultConfig()).isNotEmpty())
47+
assert(locateFiles(projectName, defaultConfig()).isNotEmpty())
4848
}
49+
}
50+
51+
@Test
52+
fun `backup string files`() {
53+
prepareTask.runCommand { _, _ ->
54+
assert(backupFiles(projectName, defaultConfig()).isNotEmpty())
55+
}
56+
}
57+
58+
@Test
59+
fun `restore string files`() {
60+
prepareTask.runCommand { _, _ ->
61+
assert(backupFiles(projectName, defaultConfig()).isNotEmpty())
62+
assert(restoreFiles(projectName).isNotEmpty())
63+
}
64+
}
4965

66+
@Test
67+
fun `xml parsing`() {
68+
prepareTask.runCommand { _, _ ->
69+
val files = backupFiles(projectName, defaultConfig())
70+
files.forEach {
71+
assert(parseXML(it, "app", true).isNotEmpty())
72+
}
73+
}
74+
}
75+
76+
77+
@Test
78+
fun `obfuscate string files`() {
79+
prepareTask.runCommand { _, _ ->
80+
val files = backupFiles(projectName, defaultConfig())
81+
files.forEach {
82+
val entities = parseXML(it, "app", true)
83+
84+
}
85+
}
5086
}
5187

5288
}

0 commit comments

Comments
 (0)