Skip to content

Commit 87408ac

Browse files
Migrate to JUnit 5 - avoid using TestCase (#11547)
* Migrate to JUnit 5 - avoid using TestCase Only simply migration to remove usage of JUnit 3 - TestCase More improvements to tests can be done in the future.
1 parent c2e4be2 commit 87408ac

File tree

211 files changed

+3045
-2091
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

211 files changed

+3045
-2091
lines changed

apache-maven/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ under the License.
9696
<groupId>org.fusesource.jansi</groupId>
9797
<artifactId>jansi</artifactId>
9898
</dependency>
99+
<dependency>
100+
<groupId>org.junit.jupiter</groupId>
101+
<artifactId>junit-jupiter-api</artifactId>
102+
<scope>test</scope>
103+
</dependency>
99104
</dependencies>
100105

101106
<pluginRepositories>

apache-maven/src/test/java/org/apache/maven/settings/GlobalSettingsTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,32 @@
1919
package org.apache.maven.settings;
2020

2121
import java.io.File;
22-
import java.io.FileInputStream;
2322
import java.io.InputStreamReader;
2423
import java.io.Reader;
2524
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Files;
2626

27-
import junit.framework.TestCase;
2827
import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
28+
import org.junit.jupiter.api.Test;
29+
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
2931

3032
/**
3133
* Tests that the global settings.xml shipped with the distribution is in good state.
3234
*
3335
* @author Benjamin Bentmann
3436
*/
35-
public class GlobalSettingsTest extends TestCase {
37+
class GlobalSettingsTest {
3638

37-
public void testValidGlobalSettings() throws Exception {
39+
@Test
40+
void testValidGlobalSettings() throws Exception {
3841
String basedir = System.getProperty("basedir", System.getProperty("user.dir"));
3942

4043
File globalSettingsFile = new File(basedir, "src/conf/settings.xml");
41-
assertTrue(globalSettingsFile.getAbsolutePath(), globalSettingsFile.isFile());
44+
assertTrue(globalSettingsFile.isFile(), globalSettingsFile.getAbsolutePath());
4245

43-
try (Reader reader = new InputStreamReader(new FileInputStream(globalSettingsFile), StandardCharsets.UTF_8)) {
46+
try (Reader reader =
47+
new InputStreamReader(Files.newInputStream(globalSettingsFile.toPath()), StandardCharsets.UTF_8)) {
4448
new SettingsXpp3Reader().read(reader);
4549
}
4650
}

maven-artifact/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ under the License.
3535
<groupId>org.codehaus.plexus</groupId>
3636
<artifactId>plexus-utils</artifactId>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.junit.jupiter</groupId>
40+
<artifactId>junit-jupiter-api</artifactId>
41+
<scope>test</scope>
42+
</dependency>
3843
</dependencies>
3944

4045
<build>

maven-artifact/src/test/java/org/apache/maven/artifact/ArtifactUtilsTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,24 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25-
import junit.framework.TestCase;
2625
import org.apache.maven.artifact.versioning.VersionRange;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2730

2831
/**
2932
* Tests {@link ArtifactUtils}.
3033
*
3134
* @author Benjamin Bentmann
3235
*/
33-
public class ArtifactUtilsTest extends TestCase {
36+
public class ArtifactUtilsTest {
3437

3538
private Artifact newArtifact(String aid) {
3639
return new DefaultArtifact("group", aid, VersionRange.createFromVersion("1.0"), "test", "jar", "tests", null);
3740
}
3841

42+
@Test
3943
public void testIsSnapshot() {
4044
assertEquals(false, ArtifactUtils.isSnapshot(null));
4145
assertEquals(false, ArtifactUtils.isSnapshot(""));
@@ -46,6 +50,7 @@ public void testIsSnapshot() {
4650
assertEquals(false, ArtifactUtils.isSnapshot("1.2.3-20090413X094722-2"));
4751
}
4852

53+
@Test
4954
public void testToSnapshotVersion() {
5055
assertEquals("1.2.3", ArtifactUtils.toSnapshotVersion("1.2.3"));
5156
assertEquals("1.2.3-SNAPSHOT", ArtifactUtils.toSnapshotVersion("1.2.3-SNAPSHOT"));
@@ -56,6 +61,7 @@ public void testToSnapshotVersion() {
5661
/**
5762
* Tests that the ordering of the map resembles the ordering of the input collection of artifacts.
5863
*/
64+
@Test
5965
public void testArtifactMapByVersionlessIdOrdering() throws Exception {
6066
List<Artifact> list = new ArrayList<>();
6167
list.add(newArtifact("b"));

maven-artifact/src/test/java/org/apache/maven/artifact/DefaultArtifactTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
*/
1919
package org.apache.maven.artifact;
2020

21-
import junit.framework.TestCase;
2221
import org.apache.maven.artifact.handler.ArtifactHandlerMock;
2322
import org.apache.maven.artifact.versioning.VersionRange;
23+
import org.junit.jupiter.api.BeforeEach;
2424

25-
public class DefaultArtifactTest extends TestCase {
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
28+
public class DefaultArtifactTest {
2629

2730
private DefaultArtifact artifact;
2831

@@ -43,8 +46,8 @@ public class DefaultArtifactTest extends TestCase {
4346

4447
private ArtifactHandlerMock artifactHandler;
4548

49+
@BeforeEach
4650
protected void setUp() throws Exception {
47-
super.setUp();
4851
artifactHandler = new ArtifactHandlerMock();
4952
versionRange = VersionRange.createFromVersion(version);
5053
artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, classifier, artifactHandler);

maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import java.nio.file.attribute.BasicFileAttributes;
2828
import java.util.regex.Pattern;
2929

30-
import org.junit.Test;
30+
import org.junit.jupiter.api.Test;
3131

32-
import static org.junit.Assert.assertEquals;
33-
import static org.junit.Assert.fail;
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.fail;
3434

3535
public class ComparableVersionIT {
3636

@@ -52,7 +52,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
5252
});
5353

5454
try {
55-
assertEquals("Unexpected exit code", 0, p.waitFor());
55+
assertEquals(0, p.waitFor(), "Unexpected exit code");
5656
} catch (InterruptedException e) {
5757
fail(e.getMessage());
5858
}

maven-artifact/src/test/java/org/apache/maven/artifact/versioning/ComparableVersionTest.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,28 @@
2020

2121
import java.util.Locale;
2222

23-
import junit.framework.TestCase;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertTrue;
2427

2528
/**
2629
* Test ComparableVersion.
2730
*
2831
* @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
2932
*/
3033
@SuppressWarnings("unchecked")
31-
public class ComparableVersionTest extends TestCase {
34+
public class ComparableVersionTest {
3235
private Comparable newComparable(String version) {
3336
ComparableVersion ret = new ComparableVersion(version);
3437
String canonical = ret.getCanonical();
3538
String parsedCanonical = new ComparableVersion(canonical).getCanonical();
3639

3740
System.out.println("canonical( " + version + " ) = " + canonical);
3841
assertEquals(
39-
"canonical( " + version + " ) = " + canonical + " -> canonical: " + parsedCanonical,
4042
canonical,
41-
parsedCanonical);
43+
parsedCanonical,
44+
"canonical( " + version + " ) = " + canonical + " -> canonical: " + parsedCanonical);
4245

4346
return ret;
4447
}
@@ -83,20 +86,20 @@ private void checkVersionsOrder(String[] versions) {
8386
Comparable low = c[i - 1];
8487
for (int j = i; j < versions.length; j++) {
8588
Comparable high = c[j];
86-
assertTrue("expected " + low + " < " + high, low.compareTo(high) < 0);
87-
assertTrue("expected " + high + " > " + low, high.compareTo(low) > 0);
89+
assertTrue(low.compareTo(high) < 0, "expected " + low + " < " + high);
90+
assertTrue(high.compareTo(low) > 0, "expected " + high + " > " + low);
8891
}
8992
}
9093
}
9194

9295
private void checkVersionsEqual(String v1, String v2) {
9396
Comparable c1 = newComparable(v1);
9497
Comparable c2 = newComparable(v2);
95-
assertTrue("expected " + v1 + " == " + v2, c1.compareTo(c2) == 0);
96-
assertTrue("expected " + v2 + " == " + v1, c2.compareTo(c1) == 0);
97-
assertTrue("expected same hashcode for " + v1 + " and " + v2, c1.hashCode() == c2.hashCode());
98-
assertTrue("expected " + v1 + ".equals( " + v2 + " )", c1.equals(c2));
99-
assertTrue("expected " + v2 + ".equals( " + v1 + " )", c2.equals(c1));
98+
assertTrue(c1.compareTo(c2) == 0, "expected " + v1 + " == " + v2);
99+
assertTrue(c2.compareTo(c1) == 0, "expected " + v2 + " == " + v1);
100+
assertTrue(c1.hashCode() == c2.hashCode(), "expected same hashcode for " + v1 + " and " + v2);
101+
assertTrue(c1.equals(c2), "expected " + v1 + ".equals( " + v2 + " )");
102+
assertTrue(c2.equals(c1), "expected " + v2 + ".equals( " + v1 + " )");
100103
}
101104

102105
private void checkVersionsArrayEqual(String[] array) {
@@ -111,18 +114,21 @@ private void checkVersionsArrayEqual(String[] array) {
111114
private void checkVersionsOrder(String v1, String v2) {
112115
Comparable c1 = newComparable(v1);
113116
Comparable c2 = newComparable(v2);
114-
assertTrue("expected " + v1 + " < " + v2, c1.compareTo(c2) < 0);
115-
assertTrue("expected " + v2 + " > " + v1, c2.compareTo(c1) > 0);
117+
assertTrue(c1.compareTo(c2) < 0, "expected " + v1 + " < " + v2);
118+
assertTrue(c2.compareTo(c1) > 0, "expected " + v2 + " > " + v1);
116119
}
117120

121+
@Test
118122
public void testVersionsQualifier() {
119123
checkVersionsOrder(VERSIONS_QUALIFIER);
120124
}
121125

126+
@Test
122127
public void testVersionsNumber() {
123128
checkVersionsOrder(VERSIONS_NUMBER);
124129
}
125130

131+
@Test
126132
public void testVersionsEqual() {
127133
newComparable("1.0-alpha");
128134
checkVersionsEqual("1", "1");
@@ -175,6 +181,7 @@ public void testVersionsEqual() {
175181
checkVersionsEqual("1m3", "1MILESTONE3");
176182
}
177183

184+
@Test
178185
public void testVersionComparing() {
179186
checkVersionsOrder("1", "2");
180187
checkVersionsOrder("1.5", "2");
@@ -212,6 +219,7 @@ public void testVersionComparing() {
212219
* see Netbeans issues <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=240845">240845</a> and
213220
* <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=226100">226100</a>
214221
*/
222+
@Test
215223
public void testMng5568() {
216224
String a = "6.1.0";
217225
String b = "6.1.0rc3";
@@ -225,6 +233,7 @@ public void testMng5568() {
225233
/**
226234
* Test <a href="https://jira.apache.org/jira/browse/MNG-6572">MNG-6572</a> optimization.
227235
*/
236+
@Test
228237
public void testMng6572() {
229238
String a = "20190126.230843"; // resembles a SNAPSHOT
230239
String b = "1234567890.12345"; // 10 digit number
@@ -243,6 +252,7 @@ public void testMng6572() {
243252
* Test all versions are equal when starting with many leading zeroes regardless of string length
244253
* (related to MNG-6572 optimization)
245254
*/
255+
@Test
246256
public void testVersionEqualWithLeadingZeroes() {
247257
// versions with string lengths from 1 to 19
248258
String[] arr = new String[] {
@@ -274,6 +284,7 @@ public void testVersionEqualWithLeadingZeroes() {
274284
* Test all "0" versions are equal when starting with many leading zeroes regardless of string length
275285
* (related to MNG-6572 optimization)
276286
*/
287+
@Test
277288
public void testVersionZeroEqualWithLeadingZeroes() {
278289
// versions with string lengths from 1 to 19
279290
String[] arr = new String[] {
@@ -305,6 +316,7 @@ public void testVersionZeroEqualWithLeadingZeroes() {
305316
* Test <a href="https://issues.apache.org/jira/browse/MNG-6964">MNG-6964</a> edge cases
306317
* for qualifiers that start with "-0.", which was showing A == C and B == C but A &lt; B.
307318
*/
319+
@Test
308320
public void testMng6964() {
309321
String a = "1-0.alpha";
310322
String b = "1-0.beta";
@@ -315,6 +327,7 @@ public void testMng6964() {
315327
checkVersionsOrder(a, b); // Should still be true
316328
}
317329

330+
@Test
318331
public void testLocaleIndependent() {
319332
Locale orig = Locale.getDefault();
320333
Locale[] locales = {Locale.ENGLISH, new Locale("tr"), Locale.getDefault()};
@@ -328,20 +341,22 @@ public void testLocaleIndependent() {
328341
}
329342
}
330343

344+
@Test
331345
public void testReuse() {
332346
ComparableVersion c1 = new ComparableVersion("1");
333347
c1.parseVersion("2");
334348

335349
Comparable c2 = newComparable("2");
336350

337-
assertEquals("reused instance should be equivalent to new instance", c1, c2);
351+
assertEquals(c1, c2, "reused instance should be equivalent to new instance");
338352
}
339353

340354
/**
341355
* Test <a href="https://issues.apache.org/jira/browse/MNG-7644">MNG-7644</a> edge cases
342356
* 1.0.0.RC1 &lt; 1.0.0-RC2 and more generally:
343357
* 1.0.0.X1 &lt; 1.0.0-X2 for any string X
344358
*/
359+
@Test
345360
public void testMng7644() {
346361
for (String x : new String[] {"abc", "alpha", "a", "beta", "b", "def", "milestone", "m", "RC"}) {
347362
// 1.0.0.X1 < 1.0.0-X2 for any string x

0 commit comments

Comments
 (0)