Skip to content

Commit f02c1d1

Browse files
Refactor JUnit4 @Rule to JUnit5 @TempDir. (#9524)
1 parent 3b4a22b commit f02c1d1

File tree

6 files changed

+34
-46
lines changed

6 files changed

+34
-46
lines changed
Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,29 @@
11
package datadog.trace.instrumentation.java.io
22

33
import datadog.trace.agent.test.InstrumentationSpecification
4-
import org.junit.Rule
5-
import org.junit.rules.TemporaryFolder
4+
import spock.lang.Shared
5+
import spock.lang.TempDir
66

7-
abstract class BaseIoCallSiteTest extends InstrumentationSpecification {
7+
import java.nio.file.Files
8+
import java.nio.file.Path
89

9-
@Rule
10-
TemporaryFolder temporaryFolder = new TemporaryFolder(parentFolder())
10+
abstract class BaseIoCallSiteTest extends InstrumentationSpecification {
11+
@Shared
12+
@TempDir
13+
Path temporaryFolder
1114

1215
@Override
1316
protected void configurePreAgent() {
1417
injectSysConfig("dd.iast.enabled", "true")
1518
}
1619

1720
protected File newFile(final String name) {
18-
return temporaryFolder.newFile(name)
21+
Path p = temporaryFolder.resolve(name)
22+
Files.createDirectories(p.getParent())
23+
return Files.createFile(p).toFile()
1924
}
2025

21-
protected File getRootFolder() {
26+
protected Path getRootFolder() {
2227
return temporaryFolder.getRoot()
2328
}
24-
25-
/**
26-
* We cannot use @TempDir from spock due to dependencies, this method tries to write to the build folder to prevent
27-
* permissions with /tmp
28-
*/
29-
private static File parentFolder() {
30-
def folder = new File(BaseIoCallSiteTest.getResource('.').toURI())
31-
while (folder.name != 'build') {
32-
folder = folder.parentFile
33-
}
34-
folder = new File(folder, 'tmp')
35-
if (!folder.exists()) {
36-
if (!folder.mkdirs()) {
37-
throw new RuntimeException('Cannot create folder ' + folder)
38-
}
39-
}
40-
return folder
41-
}
4229
}

dd-java-agent/instrumentation/java-io/src/test/groovy/datadog/trace/instrumentation/java/io/FileInputStreamCallSiteTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FileInputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
1111
setup:
1212
PathTraversalModule iastModule = Mock(PathTraversalModule)
1313
InstrumentationBridge.registerIastModule(iastModule)
14-
final path = newFile('test.txt').toString()
14+
final path = newFile('test_iast.txt').toString()
1515

1616
when:
1717
TestFileInputStreamSuite.newFileInputStream(path)
@@ -24,7 +24,7 @@ class FileInputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
2424
setup:
2525
final helper = Mock(FileLoadedRaspHelper)
2626
FileLoadedRaspHelper.INSTANCE = helper
27-
final path = newFile('test.txt').toString()
27+
final path = newFile('test_rasp.txt').toString()
2828

2929
when:
3030
TestFileInputStreamSuite.newFileInputStream(path)

dd-java-agent/instrumentation/java-io/src/test/groovy/datadog/trace/instrumentation/java/io/FileOutputStreamCallSiteTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
1313
setup:
1414
PathTraversalModule iastModule = Mock(PathTraversalModule)
1515
InstrumentationBridge.registerIastModule(iastModule)
16-
final path = newFile('test.txt').toString()
16+
final path = newFile('test_iast_1.txt').toString()
1717

1818
when:
1919
TestFileOutputStreamSuite.newFileOutputStream(path)
@@ -26,7 +26,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
2626
setup:
2727
PathTraversalModule iastModule = Mock(PathTraversalModule)
2828
InstrumentationBridge.registerIastModule(iastModule)
29-
final path = newFile('test.txt').toString()
29+
final path = newFile('test_iast_2.txt').toString()
3030

3131
when:
3232
TestFileOutputStreamSuite.newFileOutputStream(path, false)
@@ -39,7 +39,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
3939
setup:
4040
final helper = Mock(FileLoadedRaspHelper)
4141
FileLoadedRaspHelper.INSTANCE = helper
42-
final path = newFile('test.txt').toString()
42+
final path = newFile('test_rasp_1.txt').toString()
4343

4444
when:
4545
TestFileOutputStreamSuite.newFileOutputStream(path)
@@ -52,7 +52,7 @@ class FileOutputStreamCallSiteTest extends BaseIoRaspCallSiteTest {
5252
setup:
5353
final helper = Mock(FileLoadedRaspHelper)
5454
FileLoadedRaspHelper.INSTANCE = helper
55-
final path = newFile('test.txt').toString()
55+
final path = newFile('test_rasp_2.txt').toString()
5656

5757
when:
5858
TestFileOutputStreamSuite.newFileOutputStream(path, false)

dd-java-agent/instrumentation/java-io/src/test/groovy/datadog/trace/instrumentation/java/io/PathCallSiteTest.groovy

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
1111
setup:
1212
PathTraversalModule iastModule = Mock(PathTraversalModule)
1313
InstrumentationBridge.registerIastModule(iastModule)
14-
final path = 'test.txt'
14+
final path = 'test_iast.txt'
1515

1616
when:
17-
TestPathSuite.resolve(getRootFolder().toPath(), path)
17+
TestPathSuite.resolve(getRootFolder(), path)
1818

1919
then:
2020
1 * iastModule.onPathTraversal(path)
@@ -24,8 +24,8 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
2424
setup:
2525
PathTraversalModule iastModule = Mock(PathTraversalModule)
2626
InstrumentationBridge.registerIastModule(iastModule)
27-
final sibling = newFile('test1.txt').toPath()
28-
final path = 'test2.txt'
27+
final sibling = newFile('test_iast_1.txt').toPath()
28+
final path = 'test_iast_2.txt'
2929

3030
when:
3131
TestPathSuite.resolveSibling(sibling, path)
@@ -38,10 +38,10 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
3838
setup:
3939
final helper = Mock(FileLoadedRaspHelper)
4040
FileLoadedRaspHelper.INSTANCE = helper
41-
final path = 'test.txt'
41+
final path = 'test_rasp.txt'
4242

4343
when:
44-
TestPathSuite.resolve(getRootFolder().toPath(), path)
44+
TestPathSuite.resolve(getRootFolder(), path)
4545

4646
then:
4747
1 * helper.beforeFileLoaded(path)
@@ -51,8 +51,8 @@ class PathCallSiteTest extends BaseIoRaspCallSiteTest {
5151
setup:
5252
final helper = Mock(FileLoadedRaspHelper)
5353
FileLoadedRaspHelper.INSTANCE = helper
54-
final sibling = newFile('test1.txt').toPath()
55-
final path = 'test2.txt'
54+
final sibling = newFile('test_rasp_1.txt').toPath()
55+
final path = 'test_rasp_2.txt'
5656

5757
when:
5858
TestPathSuite.resolveSibling(sibling, path)

dd-java-agent/instrumentation/maven-3.2.1/src/test/groovy/datadog/trace/instrumentation/maven3/AbstractMavenTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.File;
66
import java.io.PrintStream;
7+
import java.nio.file.Path;
78
import java.util.concurrent.atomic.AtomicBoolean;
89
import java.util.concurrent.atomic.AtomicReference;
910
import java.util.function.Function;
@@ -12,16 +13,15 @@
1213
import org.apache.maven.eventspy.EventSpy;
1314
import org.apache.maven.execution.ExecutionEvent;
1415
import org.codehaus.plexus.PlexusContainer;
15-
import org.junit.ClassRule;
16-
import org.junit.rules.TemporaryFolder;
16+
import org.junit.jupiter.api.io.TempDir;
1717

1818
public abstract class AbstractMavenTest {
19-
20-
@ClassRule public static TemporaryFolder WORKING_DIRECTORY = new TemporaryFolder();
19+
@TempDir static Path WORKING_DIRECTORY;
2120

2221
protected AbstractMavenTest() {
2322
System.setProperty(
24-
"maven.multiModuleProjectDirectory", WORKING_DIRECTORY.getRoot().getAbsolutePath());
23+
"maven.multiModuleProjectDirectory",
24+
WORKING_DIRECTORY.getRoot().toAbsolutePath().toString());
2525
}
2626

2727
protected void executeMaven(
@@ -58,7 +58,8 @@ protected void customizeContainer(PlexusContainer container) {
5858
arguments[2] = goal;
5959
System.arraycopy(additionalArgs, 0, arguments, 3, additionalArgs.length);
6060

61-
mavenCli.doMain(arguments, WORKING_DIRECTORY.getRoot().getAbsolutePath(), stdOut, stderr);
61+
mavenCli.doMain(
62+
arguments, WORKING_DIRECTORY.getRoot().toAbsolutePath().toString(), stdOut, stderr);
6263

6364
Exception error = spy.handlerError.get();
6465
if (error != null) {

dd-java-agent/instrumentation/maven-3.2.1/src/test/groovy/datadog/trace/instrumentation/maven3/MavenUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private static File createToolchainsFile()
257257
Map<String, String> replacements =
258258
Collections.singletonMap("my_jdk_home_path", toolchainJdkHome.getAbsolutePath());
259259

260-
File toolchainsFile = new File(WORKING_DIRECTORY.getRoot(), "toolchains.xml");
260+
File toolchainsFile = WORKING_DIRECTORY.getRoot().resolve("toolchains.xml").toFile();
261261
try (FileWriter toolchainsFileWriter = new FileWriter(toolchainsFile)) {
262262
Template coveragesTemplate = FREEMARKER.getTemplate("sampleToolchains.ftl");
263263
coveragesTemplate.process(replacements, toolchainsFileWriter);

0 commit comments

Comments
 (0)