Skip to content

Commit 6203c1e

Browse files
committed
test/refactor: use kotlin file api instead of commons.io.FileUtils, more concise
1 parent fd97295 commit 6203c1e

File tree

3 files changed

+11
-23
lines changed

3 files changed

+11
-23
lines changed

library/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@
5757

5858
<dependencies>
5959
<!-- Testing libs -->
60-
<dependency>
61-
<groupId>commons-io</groupId>
62-
<artifactId>commons-io</artifactId>
63-
<scope>test</scope>
64-
</dependency>
6560
<dependency>
6661
<groupId>org.apache.commons</groupId>
6762
<artifactId>commons-lang3</artifactId>

library/src/test/java/com/alibaba/dcm/agent/DcmAgentTests.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ import io.kotest.matchers.nulls.shouldBeNull
99
import io.kotest.matchers.nulls.shouldNotBeNull
1010
import io.kotest.matchers.shouldBe
1111
import io.kotest.matchers.string.shouldContain
12-
import org.apache.commons.io.FileUtils
1312
import java.io.File
14-
15-
16-
private const val UTF8 = "UTF-8"
13+
import java.nio.charset.StandardCharsets.UTF_8
1714

1815

1916
/**
@@ -43,7 +40,7 @@ class DcmAgentTests : AnnotationSpec() {
4340
fun test_agentmain_file() {
4441
DcmAgent.agentmain("file $outputFilePath")
4542

46-
val content = FileUtils.readLines(outputFile, UTF8)
43+
val content = outputFile.readLines(UTF_8)
4744
content.first() shouldContain "No action in agent argument, do nothing!"
4845
}
4946

@@ -60,7 +57,7 @@ class DcmAgentTests : AnnotationSpec() {
6057

6158
DnsCacheManipulator.getDnsCache("bing.com")!!.ip shouldBe "1.2.3.4"
6259

63-
val content = FileUtils.readLines(outputFile, UTF8)
60+
val content = outputFile.readLines(UTF_8)
6461
content.last() shouldBe DcmAgent.DCM_AGENT_SUCCESS_MARK_LINE
6562
}
6663

@@ -157,7 +154,7 @@ class DcmAgentTests : AnnotationSpec() {
157154

158155
DnsCacheManipulator.getDnsNegativeCachePolicy() shouldBe 1110
159156

160-
val content = FileUtils.readLines(outputFile, UTF8)
157+
val content = outputFile.readLines(UTF_8)
161158
content.first() shouldContain "Error to do action setNegativePolicy"
162159
content.first() shouldContain "Action setNegativePolicy need more argument!"
163160
}
@@ -170,7 +167,7 @@ class DcmAgentTests : AnnotationSpec() {
170167

171168
DnsCacheManipulator.getDnsNegativeCachePolicy() shouldBe 1111
172169

173-
val content = FileUtils.readLines(outputFile, UTF8)
170+
val content = outputFile.readLines(UTF_8)
174171
content.first() shouldContain "Error to do action setNegativePolicy 737 HaHa"
175172
content.first() shouldContain "Too many arguments for action setNegativePolicy! arguments: [737, HaHa]"
176173
}
@@ -179,7 +176,7 @@ class DcmAgentTests : AnnotationSpec() {
179176
fun test_agentmain_unknownAction() {
180177
DcmAgent.agentmain(" unknownAction arg1 arg2 file $outputFilePath")
181178

182-
val content = FileUtils.readLines(outputFile, UTF8)
179+
val content = outputFile.readLines(UTF_8)
183180
content.first() shouldContain "No action in agent argument, do nothing!"
184181
}
185182
}

tool/src/test/java/com/alibaba/dcm/tool/DcmToolTests.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import io.kotest.core.spec.style.AnnotationSpec
55
import io.kotest.core.test.config.TestCaseConfig
66
import io.kotest.engine.spec.tempfile
77
import io.kotest.matchers.shouldBe
8-
import org.apache.commons.io.FileUtils
98
import org.apache.commons.lang3.SystemUtils
109
import org.apache.maven.artifact.versioning.ComparableVersion
1110
import java.io.File
1211
import java.net.InetAddress
13-
import kotlin.streams.toList
1412

1513
/**
1614
* https://kotest.io/docs/framework/testing-styles.html#annotation-spec
@@ -73,21 +71,19 @@ class DcmToolTests : AnnotationSpec() {
7371
if (!targetDir.exists()) return null
7472
println("Found target dir: ${targetDir.canonicalPath}")
7573

76-
return FileUtils.streamFiles(targetDir, false, "jar")
77-
.filter { isAgentJar(it) }
78-
.findFirst()
74+
return targetDir.walk()
75+
.filter { it.extension == "jar" && isAgentJar(it) }
7976
.map { it.canonicalPath }
80-
.orElse(null)
77+
.firstOrNull()
8178
}
8279

8380
private fun findAgentFileFromMavenLocal(): String? {
8481
val home: String = System.getProperty("user.home")
8582
val m2DcmLibDependencyDir = File("$home/.m2/repository/com/alibaba/dns-cache-manipulator")
8683

87-
return FileUtils.streamFiles(m2DcmLibDependencyDir, true, "jar")
88-
.filter { isAgentJar(it) }
84+
return m2DcmLibDependencyDir.walk()
85+
.filter { it.extension == "jar" && isAgentJar(it) }
8986
.map { it.canonicalPath }
90-
.toList()
9187
.maxWithOrNull(Comparator.comparing { ComparableVersion(it) })
9288
}
9389

0 commit comments

Comments
 (0)