Skip to content

Commit 816c631

Browse files
committed
test: migrate junit4 test DcmToolTest to kotest
1 parent 6c293b1 commit 816c631

File tree

6 files changed

+136
-271
lines changed

6 files changed

+136
-271
lines changed

library/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>kotest-runner-junit4-jvm</artifactId>
8787
<scope>test</scope>
8888
</dependency>
89+
<dependency>
90+
<groupId>io.kotest</groupId>
91+
<artifactId>kotest-runner-junit5-jvm</artifactId>
92+
<scope>test</scope>
93+
</dependency>
8994
<dependency>
9095
<groupId>io.kotest</groupId>
9196
<artifactId>kotest-assertions-core-jvm</artifactId>
@@ -152,6 +157,12 @@
152157
<version>${kotest.version}</version>
153158
<scope>test</scope>
154159
</dependency>
160+
<dependency>
161+
<groupId>io.kotest</groupId>
162+
<artifactId>kotest-runner-junit5-jvm</artifactId>
163+
<version>${kotest.version}</version>
164+
<scope>test</scope>
165+
</dependency>
155166
<dependency>
156167
<groupId>io.kotest</groupId>
157168
<artifactId>kotest-assertions-core-jvm</artifactId>

tool/pom.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@
8989
<optional>true</optional>
9090
</dependency>
9191
<!-- Testing libs -->
92+
<dependency>
93+
<groupId>org.apache.commons</groupId>
94+
<artifactId>commons-lang3</artifactId>
95+
<scope>test</scope>
96+
</dependency>
9297
<dependency>
9398
<groupId>io.kotest</groupId>
94-
<artifactId>kotest-runner-junit4-jvm</artifactId>
99+
<artifactId>kotest-runner-junit5-jvm</artifactId>
95100
<scope>test</scope>
96101
</dependency>
97102
<dependency>
@@ -135,6 +140,11 @@
135140
<artifactId>commons-io</artifactId>
136141
<version>2.11.0</version>
137142
</dependency>
143+
<dependency>
144+
<groupId>org.apache.commons</groupId>
145+
<artifactId>commons-lang3</artifactId>
146+
<version>3.12.0</version>
147+
</dependency>
138148

139149
<!-- QA libs -->
140150
<!--
@@ -161,7 +171,7 @@
161171
<!-- https://github.com/kotlintest/kotlintest -->
162172
<dependency>
163173
<groupId>io.kotest</groupId>
164-
<artifactId>kotest-runner-junit4-jvm</artifactId>
174+
<artifactId>kotest-runner-junit5-jvm</artifactId>
165175
<version>${kotest.version}</version>
166176
<scope>test</scope>
167177
</dependency>
@@ -279,6 +289,9 @@
279289
</execution>
280290
</executions>
281291
</plugin>
292+
<plugin>
293+
<artifactId>maven-surefire-plugin</artifactId>
294+
</plugin>
282295
<plugin>
283296
<groupId>org.sonatype.plugins</groupId>
284297
<artifactId>nexus-staging-maven-plugin</artifactId>

tool/src/test/java/com/alibaba/dcm/tool/DcmToolTest.java

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.alibaba.dcm.tool
2+
3+
import io.kotest.assertions.withClue
4+
import io.kotest.core.annotation.EnabledCondition
5+
import io.kotest.core.annotation.EnabledIf
6+
import io.kotest.core.spec.Spec
7+
import io.kotest.core.spec.style.AnnotationSpec
8+
import io.kotest.matchers.nulls.shouldNotBeNull
9+
import io.kotest.matchers.shouldBe
10+
import org.apache.commons.io.FileUtils
11+
import org.apache.commons.lang3.SystemUtils
12+
import java.io.File
13+
import java.net.InetAddress
14+
import kotlin.reflect.KClass
15+
import kotlin.streams.toList
16+
17+
/**
18+
* https://kotest.io/docs/framework/testing-styles.html#annotation-spec
19+
*/
20+
// Ignore "attach to current VM" test for jdk 9+, since java 9+ does not support
21+
// "java.io.IOException: Can not attach to current VM"
22+
@EnabledIf(Java8Only::class)
23+
class DcmToolTests : AnnotationSpec() {
24+
25+
private lateinit var agentFilePath: String
26+
27+
@BeforeAll
28+
fun prepareAgentFilePath() {
29+
val agentFile = findAgentFileFromLibProject() ?: findAgentFileFromMavenLocal()
30+
withClue("Not found agent file") {
31+
agentFile.shouldNotBeNull()
32+
}
33+
34+
agentFilePath = agentFile!!
35+
println("Found agent file: $agentFilePath")
36+
}
37+
38+
@BeforeEach
39+
fun setUp() {
40+
val outputFile = File.createTempFile("dcm-output-", ".log")
41+
FileUtils.deleteQuietly(outputFile)
42+
FileUtils.touch(outputFile)
43+
outputFile.length() shouldBe 0
44+
45+
val outputFilePath = outputFile.canonicalPath
46+
println("Prepared output file: $outputFilePath")
47+
48+
System.setProperty(DcmTool.DCM_TOOLS_TMP_FILE_KEY, outputFilePath)
49+
System.setProperty(DcmTool.DCM_TOOLS_AGENT_JAR_KEY, agentFilePath)
50+
}
51+
52+
@Test
53+
fun test_main_getPolicy() {
54+
DcmTool.main(arrayOf("-p", DcmTool.pid(), "getPolicy"))
55+
}
56+
57+
@Test
58+
fun test_main_set() {
59+
val ip = "1.1.2.2"
60+
val host = "bing.com"
61+
62+
DcmTool.main(arrayOf("-p", DcmTool.pid(), "set", host, ip))
63+
InetAddress.getByName(host).hostAddress shouldBe ip
64+
}
65+
66+
private fun findAgentFileFromLibProject(): String? {
67+
val dcmLibProjectDir = listOf("library", "../library", "../../library")
68+
.asSequence()
69+
.map { File(it) }
70+
.filter { it.exists() }
71+
.firstOrNull()
72+
?: return null
73+
74+
val targetDir = File(dcmLibProjectDir, "target")
75+
if (!targetDir.exists()) return null
76+
println("Found target dir: ${targetDir.canonicalPath}")
77+
78+
return FileUtils.streamFiles(targetDir, false, "jar")
79+
.filter { isAgentJar(it) }
80+
.findFirst()
81+
.map { it.canonicalPath }
82+
.orElse(null)
83+
}
84+
85+
private fun findAgentFileFromMavenLocal(): String? {
86+
val home = System.getProperty("user.home")
87+
val m2DcmLibDependencyDir = File("$home/.m2/repository/com/alibaba/dns-cache-manipulator")
88+
89+
return FileUtils.streamFiles(m2DcmLibDependencyDir, true, "jar")
90+
.filter { isAgentJar(it) }
91+
.map { it.canonicalPath }
92+
.toList()
93+
.maxOrNull()
94+
}
95+
96+
private fun isAgentJar(file: File): Boolean {
97+
val fileName = file.name
98+
if (!fileName.startsWith("dns-cache-manipulator")) return false
99+
100+
val replaced = fileName.replace("dns-cache-manipulator-", "").replace("-SNAPSHOT", "")
101+
return !replaced.contains("-")
102+
}
103+
}
104+
105+
/**
106+
* https://kotest.io/docs/framework/conditional/spec-annotations-conditional-evaluation.html
107+
*/
108+
class Java8Only : EnabledCondition {
109+
override fun enabled(kclass: KClass<out Spec>): Boolean = SystemUtils.IS_JAVA_1_8
110+
}

tool/src/test/java/com/alibaba/support/junit/conditional/AboveJava8.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)