Skip to content

Commit c96e318

Browse files
Migrate tests to JUnit5 (fixes)
* Make CentralSearchTest more stable
1 parent fea5a73 commit c96e318

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

core/src/test/java/org/owasp/dependencycheck/data/central/CentralSearchTest.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import org.owasp.dependencycheck.BaseTest;
77
import org.owasp.dependencycheck.data.nexus.MavenArtifact;
88

9+
import java.io.FileNotFoundException;
910
import java.io.IOException;
1011
import java.util.List;
1112

1213
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
1315
import static org.junit.jupiter.api.Assertions.assertThrows;
1416
import static org.junit.jupiter.api.Assertions.assertTrue;
1517
import static org.junit.jupiter.api.Assumptions.assumeFalse;
@@ -46,13 +48,16 @@ void testMalformedSha1() {
4648
@Test
4749
void testValidSha1() throws Exception {
4850
try {
49-
List<MavenArtifact> ma = searcher.searchSha1("9977a8d04e75609cf01badc4eb6a9c7198c4c5ea");
50-
assertEquals("org.apache.maven.plugins", ma.get(0).getGroupId(), "Incorrect group");
51-
assertEquals("maven-compiler-plugin", ma.get(0).getArtifactId(), "Incorrect artifact");
52-
assertEquals("3.1", ma.get(0).getVersion(), "Incorrect version");
53-
} catch (IOException ex) {
54-
//we hit a failure state on the CI
51+
List<MavenArtifact> ma = searcher.searchSha1("9977a8d04e75609cf01badc4eb6a9c7198c4c5ea");
52+
assertEquals("org.apache.maven.plugins", ma.get(0).getGroupId(), "Incorrect group");
53+
assertEquals("maven-compiler-plugin", ma.get(0).getArtifactId(), "Incorrect artifact");
54+
assertEquals("3.1", ma.get(0).getVersion(), "Incorrect version");
55+
} catch (IOException ex) {
56+
// abort if we hit a failure state on the CI
5557
assumeFalse(StringUtils.contains(ex.getMessage(), "Could not connect to MavenCentral"));
58+
assumeFalse(ex.getMessage().matches("^https://.+ - Server status: \\d{3} - Server reason: .+$"));
59+
60+
// otherwise fail the test
5661
throw ex;
5762
}
5863
}
@@ -62,15 +67,15 @@ void testValidSha1() throws Exception {
6267
// test it anyway
6368
@Test
6469
void testMissingSha1() {
65-
assertThrows(IOException.class, () -> {
66-
try {
67-
searcher.searchSha1("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
68-
} catch (IOException ex) {
69-
//we hit a failure state on the CI
70-
assumeFalse(StringUtils.contains(ex.getMessage(), "Could not connect to MavenCentral"));
71-
throw ex;
72-
}
73-
});
70+
IOException ex = assertThrows(IOException.class, () -> searcher.searchSha1("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
71+
72+
//abort if we hit a failure state on the CI
73+
assumeFalse(StringUtils.contains(ex.getMessage(), "Could not connect to MavenCentral"));
74+
assumeFalse(ex.getMessage().matches("^https://.+ - Server status: \\d{3} - Server reason: .+$"));
75+
76+
// otherwise assert that the exception is a FileNotFoundException
77+
assertInstanceOf(FileNotFoundException.class, ex);
78+
assertEquals("Artifact not found in Central", ex.getMessage());
7479
}
7580

7681
// This test should give us multiple results back from Central
@@ -80,8 +85,11 @@ void testMultipleReturns() throws Exception {
8085
List<MavenArtifact> ma = searcher.searchSha1("94A9CE681A42D0352B3AD22659F67835E560D107");
8186
assertTrue(ma.size() > 1);
8287
} catch (IOException ex) {
83-
//we hit a failure state on the CI
88+
// abort if we hit a failure state on the CI
8489
assumeFalse(StringUtils.contains(ex.getMessage(), "Could not connect to MavenCentral"));
90+
assumeFalse(ex.getMessage().matches("^https://.+ - Server status: \\d{3} - Server reason: .+$"));
91+
92+
// otherwise fail the test
8593
throw ex;
8694
}
8795
}

0 commit comments

Comments
 (0)