Skip to content

Commit 2463f9c

Browse files
Migrate tests to JUnit5 (utils)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 6c56761 commit 2463f9c

File tree

9 files changed

+167
-176
lines changed

9 files changed

+167
-176
lines changed

utils/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Copyright (c) 2014 - Jeremy Long. All Rights Reserved.
100100
</dependency>
101101
<dependency>
102102
<groupId>org.mock-server</groupId>
103-
<artifactId>mockserver-junit-rule</artifactId>
103+
<artifactId>mockserver-junit-jupiter</artifactId>
104104
<scope>test</scope>
105105
</dependency>
106106
</dependencies>

utils/src/test/java/org/owasp/dependencycheck/utils/BaseTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package org.owasp.dependencycheck.utils;
1717

18+
import org.junit.jupiter.api.AfterEach;
19+
import org.junit.jupiter.api.BeforeEach;
20+
1821
import java.io.File;
1922
import java.net.URISyntaxException;
20-
import org.junit.After;
21-
import org.junit.Assume;
22-
import org.junit.Before;
23+
24+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2325

2426
/**
2527
*
@@ -35,15 +37,15 @@ public abstract class BaseTest {
3537
/**
3638
* Initialize the {@link Settings}.
3739
*/
38-
@Before
40+
@BeforeEach
3941
public void setUp() {
4042
settings = new Settings();
4143
}
4244

4345
/**
4446
* Clean the {@link Settings}.
4547
*/
46-
@After
48+
@AfterEach
4749
public void tearDown() {
4850
settings.cleanup(true);
4951
}
@@ -56,7 +58,7 @@ public void tearDown() {
5658
protected Settings getSettings() {
5759
return settings;
5860
}
59-
61+
6062
/**
6163
* Returns the given resource as a File using the object's class loader. The
6264
* org.junit.Assume API is used so that test cases are skipped if the
@@ -69,7 +71,7 @@ protected Settings getSettings() {
6971
public static File getResourceAsFile(Object o, String resource) {
7072
try {
7173
File f = new File(o.getClass().getClassLoader().getResource(resource).toURI().getPath());
72-
Assume.assumeTrue(String.format("%n%n[SEVERE] Unable to load resource for test case: %s%n%n", resource), f.exists());
74+
assumeTrue(f.exists(), String.format("%n%n[SEVERE] Unable to load resource for test case: %s%n%n", resource));
7375
return f;
7476
} catch (URISyntaxException e) {
7577
throw new UnsupportedOperationException(e);

utils/src/test/java/org/owasp/dependencycheck/utils/ChecksumTest.java

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,45 @@
1717
*/
1818
package org.owasp.dependencycheck.utils;
1919

20+
import org.junit.jupiter.api.Test;
21+
2022
import java.io.File;
2123
import java.io.IOException;
2224
import java.security.NoSuchAlgorithmException;
23-
import org.junit.Assert;
24-
import static org.junit.Assert.assertArrayEquals;
2525

26-
import static org.junit.Assert.assertEquals;
27-
import static org.junit.Assert.assertTrue;
28-
import static org.junit.Assert.fail;
29-
import org.junit.Test;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
3029

3130
/**
3231
*
3332
* @author Jeremy Long
3433
*/
35-
public class ChecksumTest {
34+
class ChecksumTest {
3635

3736
/**
3837
* Test of getChecksum method, of class Checksum. This checks that an
3938
* exception is thrown when an invalid path is specified.
4039
*
41-
* @throws Exception is thrown when an exception occurs.
4240
*/
4341
@Test
44-
public void testGetChecksum_FileNotFound() throws Exception {
42+
void testGetChecksum_FileNotFound() {
4543
String algorithm = "MD5";
4644
File file = new File("not a valid file");
47-
Exception exception = Assert.assertThrows(IOException.class, () -> Checksum.getChecksum(algorithm, file));
45+
Exception exception = assertThrows(IOException.class, () -> Checksum.getChecksum(algorithm, file));
4846
assertTrue(exception.getMessage().contains("not a valid file"));
4947
}
5048

5149
/**
5250
* Test of getChecksum method, of class Checksum. This checks that an
5351
* exception is thrown when an invalid algorithm is specified.
5452
*
55-
* @throws Exception is thrown when an exception occurs.
5653
*/
5754
@Test
58-
public void testGetChecksum_NoSuchAlgorithm() throws Exception {
55+
void testGetChecksum_NoSuchAlgorithm() {
5956
String algorithm = "some unknown algorithm";
6057
File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").getPath());
61-
Exception exception = Assert.assertThrows(NoSuchAlgorithmException.class, () -> Checksum.getChecksum(algorithm, file));
58+
Exception exception = assertThrows(NoSuchAlgorithmException.class, () -> Checksum.getChecksum(algorithm, file));
6259
assertTrue(exception.getMessage().contains("some unknown algorithm"));
6360
}
6461

@@ -68,7 +65,7 @@ public void testGetChecksum_NoSuchAlgorithm() throws Exception {
6865
* @throws Exception is thrown when an exception occurs.
6966
*/
7067
@Test
71-
public void testGetMD5Checksum() throws Exception {
68+
void testGetMD5Checksum() throws Exception {
7269
File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
7370
//String expResult = "F0915C5F46B8CFA283E5AD67A09B3793";
7471
String expResult = "f0915c5f46b8cfa283e5ad67a09b3793";
@@ -82,7 +79,7 @@ public void testGetMD5Checksum() throws Exception {
8279
* @throws Exception is thrown when an exception occurs.
8380
*/
8481
@Test
85-
public void testGetSHA1Checksum() throws Exception {
82+
void testGetSHA1Checksum() throws Exception {
8683
File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
8784
//String expResult = "B8A9FF28B21BCB1D0B50E24A5243D8B51766851A";
8885
String expResult = "b8a9ff28b21bcb1d0b50e24a5243d8b51766851a";
@@ -94,7 +91,7 @@ public void testGetSHA1Checksum() throws Exception {
9491
* Test of getHex method, of class Checksum.
9592
*/
9693
@Test
97-
public void testGetHex() {
94+
void testGetHex() {
9895
byte[] raw = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
9996
//String expResult = "000102030405060708090A0B0C0D0E0F10";
10097
String expResult = "000102030405060708090a0b0c0d0e0f10";
@@ -106,7 +103,7 @@ public void testGetHex() {
106103
* Test of getChecksum method, of class Checksum.
107104
*/
108105
@Test
109-
public void testGetChecksum_String_File() throws Exception {
106+
void testGetChecksum_String_File() throws Exception {
110107
String algorithm = "MD5";
111108
File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
112109
String expResult = "f0915c5f46b8cfa283e5ad67a09b3793";
@@ -121,7 +118,7 @@ public void testGetChecksum_String_File() throws Exception {
121118
* Test of getMD5Checksum method, of class Checksum.
122119
*/
123120
@Test
124-
public void testGetMD5Checksum_File() throws Exception {
121+
void testGetMD5Checksum_File() throws Exception {
125122
File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
126123
String expResult = "f0915c5f46b8cfa283e5ad67a09b3793";
127124
String result = Checksum.getMD5Checksum(file);
@@ -132,7 +129,7 @@ public void testGetMD5Checksum_File() throws Exception {
132129
* Test of getSHA1Checksum method, of class Checksum.
133130
*/
134131
@Test
135-
public void testGetSHA1Checksum_File() throws Exception {
132+
void testGetSHA1Checksum_File() throws Exception {
136133
File file = new File(this.getClass().getClassLoader().getResource("checkSumTest.file").toURI().getPath());
137134
String expResult = "b8a9ff28b21bcb1d0b50e24a5243d8b51766851a";
138135
String result = Checksum.getSHA1Checksum(file);
@@ -143,7 +140,7 @@ public void testGetSHA1Checksum_File() throws Exception {
143140
* Test of getChecksum method, of class Checksum.
144141
*/
145142
@Test
146-
public void testGetChecksum_String_byteArr() {
143+
void testGetChecksum_String_byteArr() {
147144
String algorithm = "SHA1";
148145
byte[] bytes = {-16, -111, 92, 95, 70, -72, -49, -94, -125, -27, -83, 103, -96, -101, 55, -109};
149146
String expResult = "89268a389a97f0bfba13d3ff2370d8ad436e36f6";
@@ -155,7 +152,7 @@ public void testGetChecksum_String_byteArr() {
155152
* Test of getMD5Checksum method, of class Checksum.
156153
*/
157154
@Test
158-
public void testGetMD5Checksum_String() {
155+
void testGetMD5Checksum_String() {
159156
String text = "test string";
160157
String expResult = "6f8db599de986fab7a21625b7916589c";
161158
String result = Checksum.getMD5Checksum(text);
@@ -166,7 +163,7 @@ public void testGetMD5Checksum_String() {
166163
* Test of getSHA1Checksum method, of class Checksum.
167164
*/
168165
@Test
169-
public void testGetSHA1Checksum_String() {
166+
void testGetSHA1Checksum_String() {
170167
String text = "test string";
171168
String expResult = "661295c9cbf9d6b2f6428414504a8deed3020641";
172169
String result = Checksum.getSHA1Checksum(text);

utils/src/test/java/org/owasp/dependencycheck/utils/DownloaderIT.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@
1717
*/
1818
package org.owasp.dependencycheck.utils;
1919

20+
import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
21+
import org.apache.hc.core5.http.HttpEntity;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
2025
import java.io.File;
2126
import java.io.IOException;
2227
import java.io.InputStream;
2328
import java.net.URL;
2429

25-
import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
26-
import org.apache.hc.core5.http.HttpEntity;
27-
import org.junit.Test;
28-
2930
import static java.nio.charset.StandardCharsets.UTF_8;
30-
import static org.junit.Assert.assertTrue;
31-
import org.junit.Before;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
3232

3333
/**
3434
*
3535
* @author Jeremy Long
3636
*/
37-
public class DownloaderIT extends BaseTest {
37+
class DownloaderIT extends BaseTest {
3838

3939
/**
4040
* Initialize the {@link Settings}.
4141
*/
42-
@Before
42+
@BeforeEach
4343
@Override
4444
public void setUp() {
4545
super.setUp();
@@ -51,7 +51,7 @@ public void setUp() {
5151
* @throws Exception thrown when an exception occurs.
5252
*/
5353
@Test
54-
public void testFetchFile() throws Exception {
54+
void testFetchFile() throws Exception {
5555
final String str = getSettings().getString(Settings.KEYS.ENGINE_VERSION_CHECK_URL, "https://dependency-check.github.io/DependencyCheck/current.txt");
5656
URL url = new URL(str);
5757
File outputPath = new File("target/current.txt");
@@ -66,9 +66,9 @@ public void testFetchFile() throws Exception {
6666
* @throws Exception thrown when an exception occurs.
6767
*/
6868
@Test
69-
public void testfetchAndHandleContent() throws Exception {
69+
void testfetchAndHandleContent() throws Exception {
7070
URL url = new URL(getSettings().getString(Settings.KEYS.ENGINE_VERSION_CHECK_URL));
71-
AbstractHttpClientResponseHandler<String> versionHandler = new AbstractHttpClientResponseHandler<String>() {
71+
AbstractHttpClientResponseHandler<String> versionHandler = new AbstractHttpClientResponseHandler<>() {
7272
@Override
7373
public String handleEntity(HttpEntity entity) throws IOException {
7474
try (InputStream in = entity.getContent()) {

utils/src/test/java/org/owasp/dependencycheck/utils/ExpectedObjectInputStreamTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@
1717
*/
1818
package org.owasp.dependencycheck.utils;
1919

20+
import org.junit.jupiter.api.Test;
21+
2022
import java.io.BufferedOutputStream;
2123
import java.io.ByteArrayInputStream;
2224
import java.io.ByteArrayOutputStream;
2325
import java.io.IOException;
2426
import java.io.ObjectOutputStream;
2527
import java.util.ArrayList;
2628
import java.util.List;
27-
import static org.junit.Assert.fail;
28-
import org.junit.Test;
29-
import static org.junit.Assert.fail;
29+
30+
import static org.junit.jupiter.api.Assertions.assertThrows;
31+
import static org.junit.jupiter.api.Assertions.fail;
3032

3133
/**
3234
*
3335
* @author jeremy
3436
*/
35-
public class ExpectedObjectInputStreamTest {
37+
class ExpectedObjectInputStreamTest {
3638

3739
/**
3840
* Test of resolveClass method, of class ExpectedObjectInputStream.
3941
*/
4042
@Test
41-
public void testResolveClass() {
43+
void testResolveClass() {
4244
List<SimplePojo> data = new ArrayList<>();
4345
data.add(new SimplePojo());
4446
try (ByteArrayOutputStream mem = new ByteArrayOutputStream();
@@ -57,21 +59,19 @@ public void testResolveClass() {
5759
/**
5860
* Test of resolveClass method, of class ExpectedObjectInputStream.
5961
*/
60-
@Test(expected = java.io.InvalidClassException.class)
61-
public void testResolveClassException() throws Exception {
62+
@Test
63+
void testResolveClassException() throws Exception {
6264
List<SimplePojo> data = new ArrayList<>();
6365
data.add(new SimplePojo());
64-
6566
ByteArrayOutputStream mem = new ByteArrayOutputStream();
6667
byte[] buf;
6768
try (ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(mem))) {
68-
out.writeObject(data);
69-
out.flush();
70-
buf = mem.toByteArray();
71-
}
69+
out.writeObject(data);
70+
out.flush();
71+
buf = mem.toByteArray();
72+
}
7273
ByteArrayInputStream in = new ByteArrayInputStream(buf);
73-
7474
ExpectedObjectInputStream instance = new ExpectedObjectInputStream(in, "java.util.ArrayList", "org.owasp.dependencycheck.utils.SimplePojo");
75-
instance.readObject();
75+
assertThrows(java.io.InvalidClassException.class, instance::readObject);
7676
}
7777
}

0 commit comments

Comments
 (0)