Skip to content

Commit 9b3344c

Browse files
[MSHARED-1249] Switch to JUnit 5
1 parent 8dbf929 commit 9b3344c

File tree

9 files changed

+154
-159
lines changed

9 files changed

+154
-159
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@
104104

105105
<!-- testing support -->
106106
<dependency>
107-
<groupId>junit</groupId>
108-
<artifactId>junit</artifactId>
109-
<version>4.13.2</version>
107+
<groupId>org.junit.jupiter</groupId>
108+
<artifactId>junit-jupiter-api</artifactId>
109+
<version>5.9.3</version>
110110
<scope>test</scope>
111111
</dependency>
112112
<dependency>

src/test/java/org/apache/maven/shared/dependency/analyzer/ClassFileVisitorUtilsTest.java

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
package org.apache.maven.shared.dependency.analyzer;
2020

21-
import java.io.File;
22-
import java.io.FileOutputStream;
21+
import java.io.BufferedReader;
2322
import java.io.IOException;
2423
import java.io.InputStream;
24+
import java.io.InputStreamReader;
2525
import java.net.URL;
2626
import java.nio.charset.StandardCharsets;
2727
import java.nio.file.Files;
@@ -31,9 +31,8 @@
3131
import java.util.jar.JarOutputStream;
3232
import java.util.zip.ZipEntry;
3333

34-
import org.apache.commons.io.FileUtils;
35-
import org.apache.commons.io.IOUtils;
36-
import org.junit.Test;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.io.TempDir;
3736

3837
import static org.assertj.core.api.Assertions.assertThat;
3938
import static org.assertj.core.api.Assertions.fail;
@@ -44,36 +43,36 @@
4443
* @author <a href="mailto:[email protected]">Mark Hobson</a>
4544
* @see ClassFileVisitorUtils
4645
*/
47-
public class ClassFileVisitorUtilsTest {
46+
class ClassFileVisitorUtilsTest {
47+
48+
@TempDir
49+
private Path tempDir;
50+
4851
private TestVisitor visitor = new TestVisitor();
4952

5053
private static class TestVisitor implements ClassFileVisitor {
5154
final List<String> classNames = new ArrayList<>();
55+
5256
final List<String> data = new ArrayList<>();
5357

5458
@Override
5559
public void visitClass(String className, InputStream in) {
5660
classNames.add(className);
57-
try {
58-
List<String> lines = IOUtils.readLines(in, StandardCharsets.UTF_8);
59-
data.addAll(lines);
60-
} catch (IOException ex) {
61-
throw new RuntimeException(ex);
62-
}
61+
BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
62+
reader.lines().forEach(data::add);
6363
}
6464
}
6565

6666
@Test
67-
public void testAcceptJar() throws IOException {
68-
File file = File.createTempFile("test", ".jar");
69-
file.deleteOnExit();
67+
void testAcceptJar() throws IOException {
68+
Path path = Files.createTempFile(tempDir, "test", ".jar");
7069

71-
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(file))) {
70+
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(path))) {
7271
addZipEntry(out, "a/b/c.class", "class a.b.c");
7372
addZipEntry(out, "x/y/z.class", "class x.y.z");
7473
}
7574

76-
ClassFileVisitorUtils.accept(file.toURI().toURL(), visitor);
75+
ClassFileVisitorUtils.accept(path.toUri().toURL(), visitor);
7776

7877
assertThat(visitor.classNames).contains("a.b.c");
7978
assertThat(visitor.classNames).contains("x.y.z");
@@ -82,22 +81,21 @@ public void testAcceptJar() throws IOException {
8281
}
8382

8483
@Test
85-
public void testAcceptJarWithNonClassEntry() throws IOException {
86-
File file = File.createTempFile("test", ".jar");
87-
file.deleteOnExit();
84+
void testAcceptJarWithNonClassEntry() throws IOException {
85+
Path path = Files.createTempFile(tempDir, "test", ".jar");
8886

89-
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(file))) {
87+
try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(path))) {
9088
addZipEntry(out, "a/b/c.jpg", "jpeg a.b.c");
9189
}
9290

93-
ClassFileVisitorUtils.accept(file.toURI().toURL(), visitor);
91+
ClassFileVisitorUtils.accept(path.toUri().toURL(), visitor);
9492

9593
assertThat(visitor.classNames).isEmpty();
9694
}
9795

9896
@Test
99-
public void testAcceptDir() throws IOException {
100-
Path dir = Files.createTempDirectory("d-a-test");
97+
void testAcceptDir() throws IOException {
98+
Path dir = Files.createTempDirectory(tempDir, "d-a-test");
10199

102100
Path abDir = Files.createDirectories(dir.resolve("a/b"));
103101
writeToFile(abDir, "c.class", "class a.b.c");
@@ -107,34 +105,28 @@ public void testAcceptDir() throws IOException {
107105

108106
ClassFileVisitorUtils.accept(dir.toUri().toURL(), visitor);
109107

110-
FileUtils.deleteDirectory(dir.toFile());
111-
112108
assertThat(visitor.classNames).contains("a.b.c");
113109
assertThat(visitor.classNames).contains("x.y.z");
114110
assertThat(visitor.data).contains("class a.b.c");
115111
assertThat(visitor.data).contains("class x.y.z");
116112
}
117113

118114
@Test
119-
public void testAcceptDirWithNonClassFile() throws IOException {
120-
Path dir = Files.createTempDirectory("d-a-test");
115+
void testAcceptDirWithNonClassFile() throws IOException {
116+
Path dir = Files.createTempDirectory(tempDir, "d-a-test");
121117

122118
Path abDir = Files.createDirectories(dir.resolve("a/b"));
123119
writeToFile(abDir, "c.jpg", "jpeg a.b.c");
124120

125121
ClassFileVisitorUtils.accept(dir.toUri().toURL(), visitor);
126122

127-
FileUtils.deleteDirectory(dir.toFile());
128-
129123
assertThat(visitor.classNames).isEmpty();
130124
}
131125

132126
@Test
133-
public void testAcceptWithFile() throws IOException {
134-
File file = File.createTempFile("test", ".class");
135-
file.deleteOnExit();
136-
137-
URL url = file.toURI().toURL();
127+
void testAcceptWithFile() throws IOException {
128+
Path path = Files.createTempFile(tempDir, "test", ".class");
129+
URL url = path.toUri().toURL();
138130

139131
try {
140132
ClassFileVisitorUtils.accept(url, visitor);
@@ -145,7 +137,7 @@ public void testAcceptWithFile() throws IOException {
145137
}
146138

147139
@Test
148-
public void testAcceptWithUnsupportedScheme() throws IOException {
140+
void testAcceptWithUnsupportedScheme() throws IOException {
149141
URL url = new URL("http://localhost/");
150142

151143
try {

src/test/java/org/apache/maven/shared/dependency/analyzer/CollectorClassFileVisitorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.HashSet;
2222
import java.util.Set;
2323

24-
import org.junit.Before;
25-
import org.junit.Test;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
2828

@@ -32,16 +32,16 @@
3232
* @author <a href="mailto:[email protected]">Mark Hobson</a>
3333
* @see CollectorClassFileVisitor
3434
*/
35-
public class CollectorClassFileVisitorTest {
35+
class CollectorClassFileVisitorTest {
3636
private CollectorClassFileVisitor visitor;
3737

38-
@Before
39-
public void setUp() {
38+
@BeforeEach
39+
void setUp() {
4040
visitor = new CollectorClassFileVisitor();
4141
}
4242

4343
@Test
44-
public void testVisitClass() {
44+
void testVisitClass() {
4545
visitor.visitClass("a.b.c", null);
4646
visitor.visitClass("x.y.z", null);
4747

src/test/java/org/apache/maven/shared/dependency/analyzer/DefaultClassAnalyzerTest.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
import java.util.zip.ZipEntry;
3131
import java.util.zip.ZipException;
3232

33-
import org.junit.After;
34-
import org.junit.Before;
35-
import org.junit.Test;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.io.TempDir;
3636

3737
import static org.assertj.core.api.Assertions.assertThat;
3838
import static org.assertj.core.api.Assertions.fail;
@@ -43,27 +43,24 @@
4343
* @author <a href="mailto:[email protected]">Mark Hobson</a>
4444
* @see DefaultClassAnalyzer
4545
*/
46-
public class DefaultClassAnalyzerTest {
46+
class DefaultClassAnalyzerTest {
47+
48+
@TempDir
49+
private Path tempDir;
50+
4751
private Path file;
4852

49-
@Before
50-
public void setUp() throws IOException {
51-
file = Files.createTempFile("test", ".jar");
53+
@BeforeEach
54+
void setUp() throws IOException {
55+
file = Files.createTempFile(tempDir, "test", ".jar");
5256
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(file.toFile()))) {
5357
addZipEntry(out, "a/b/c.class", "class a.b.c");
5458
addZipEntry(out, "x/y/z.class", "class x.y.z");
5559
}
5660
}
5761

58-
@After
59-
public void cleanup() throws IOException {
60-
if (file != null) {
61-
Files.deleteIfExists(file);
62-
}
63-
}
64-
6562
@Test
66-
public void testAnalyzeWithJar() throws IOException {
63+
void testAnalyzeWithJar() throws IOException {
6764
Set<String> expectedClasses = new HashSet<>();
6865
expectedClasses.add("a.b.c");
6966
expectedClasses.add("x.y.z");
@@ -75,7 +72,7 @@ public void testAnalyzeWithJar() throws IOException {
7572
}
7673

7774
@Test
78-
public void testAnalyzeBadJar() throws IOException {
75+
void testAnalyzeBadJar() throws IOException {
7976
// to reproduce MDEP-143
8077
// corrupt the jar file by altering its contents
8178
ByteArrayOutputStream baos = new ByteArrayOutputStream(100);

src/test/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalysisTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.apache.maven.artifact.Artifact;
2626
import org.apache.maven.artifact.DefaultArtifact;
2727
import org.apache.maven.artifact.versioning.VersionRange;
28-
import org.junit.Test;
28+
import org.junit.jupiter.api.Test;
2929

3030
import static org.assertj.core.api.Assertions.assertThat;
3131

@@ -35,9 +35,9 @@
3535
* @author <a href="mailto:[email protected]">Mark Hobson</a>
3636
* @see ProjectDependencyAnalysis
3737
*/
38-
public class ProjectDependencyAnalysisTest {
38+
class ProjectDependencyAnalysisTest {
3939
@Test
40-
public void testConstructor() {
40+
void testConstructor() {
4141
Set<Artifact> usedDeclaredArtifacts = new HashSet<>();
4242
Set<Artifact> usedUndeclaredArtifacts = new HashSet<>();
4343
Set<Artifact> unusedDeclaredArtifacts = new HashSet<>();
@@ -52,7 +52,7 @@ public void testConstructor() {
5252
}
5353

5454
@Test
55-
public void ignoreNonCompileShouldFilterOnlyUnusedDeclare() {
55+
void ignoreNonCompileShouldFilterOnlyUnusedDeclare() {
5656
Artifact artifactCompile = aTestArtifact("test1", Artifact.SCOPE_COMPILE);
5757
Artifact artifactProvided = aTestArtifact("test2", Artifact.SCOPE_PROVIDED);
5858
Artifact artifactTest = aTestArtifact("test3", Artifact.SCOPE_TEST);

src/test/java/org/apache/maven/shared/dependency/analyzer/ProjectDependencyAnalyzerExceptionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package org.apache.maven.shared.dependency.analyzer;
2020

21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
2424

@@ -28,14 +28,14 @@
2828
* @author <a href="mailto:[email protected]">Mark Hobson</a>
2929
* @see ProjectDependencyAnalyzerException
3030
*/
31-
public class ProjectDependencyAnalyzerExceptionTest {
31+
class ProjectDependencyAnalyzerExceptionTest {
3232
@Test
33-
public void testConstructor() {
33+
void testConstructor() {
3434
assertThat(new ProjectDependencyAnalyzerException("a")).hasMessage("a");
3535
}
3636

3737
@Test
38-
public void testConstructorWithThrowable() {
38+
void testConstructorWithThrowable() {
3939
Throwable throwable = new Exception();
4040
ProjectDependencyAnalyzerException exception = new ProjectDependencyAnalyzerException("a", throwable);
4141

src/test/java/org/apache/maven/shared/dependency/analyzer/asm/ASMDependencyAnalyzerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
import java.util.Set;
2323

2424
import org.apache.maven.shared.dependency.analyzer.DependencyAnalyzer;
25-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
2626

2727
import static org.assertj.core.api.Assertions.assertThat;
2828

29-
public class ASMDependencyAnalyzerTest {
29+
class ASMDependencyAnalyzerTest {
3030
private final DependencyAnalyzer analyzer = new ASMDependencyAnalyzer();
3131

3232
@Test
33-
public void test() throws Exception {
33+
void test() throws Exception {
3434
URL jarUrl = this.getClass().getResource("/org/objectweb/asm/ClassReader.class");
3535
assertThat(jarUrl).isNotNull();
3636
String fileUrl =

0 commit comments

Comments
 (0)