Skip to content

Commit

Permalink
Migrate all tests to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Feb 17, 2024
1 parent fa04932 commit d251939
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 187 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {

// for TestHelpers we need junit during normal compile...
implementation 'org.junit.vintage:junit-vintage-engine:5.10.2'
implementation 'org.junit.jupiter:junit-jupiter-engine:5.10.2'

testImplementation 'org.apache.commons:commons-email:1.5'
testImplementation 'com.sun.activation:javax.activation:1.2.0' // needed for JDK 11
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.dstadler.commons.testing;

import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.ArrayList;

import static org.junit.Assert.fail;
import org.junit.jupiter.api.Test;

public class BaseMemoryVerifierTest {
@Test
Expand All @@ -25,12 +25,8 @@ public void testFailure() {
verifier.register(obj);

// should not cause an error if no allocation was registered
try {
verifier.tearDownBase();
fail("Expecting to fail here");
} catch (AssertionError e) {
// expected here
}
assertThrows(AssertionError.class,
verifier::tearDownBase);
}

private static class FailingBaseMemoryVerifier extends BaseMemoryVerifier {
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/org/dstadler/commons/testing/HeapDumpTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.dstadler.commons.testing;

import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.*;
import org.junit.jupiter.api.Test;

public class HeapDumpTest {
@Test
Expand Down Expand Up @@ -38,13 +40,12 @@ public void testDumpHeapFailsOnWrongFilename() throws IOException {
assertTrue(file.mkdirs());

try {
HeapDump.dumpHeap(file.getAbsolutePath(), true);
fail("Should fail to write the dump in this case, tried for " + file.getAbsolutePath());
} catch (IOException e) {
// expected in this case
assertThrows(IOException.class,
() -> HeapDump.dumpHeap(file.getAbsolutePath(), true),
"Should fail to write the dump in this case, tried for " + file.getAbsolutePath());
} finally {
assertTrue(file.exists());
assertTrue(file.delete());
assertTrue(file.exists(), "Failed for " + file.getAbsolutePath());
assertTrue(file.delete(), "Failed for " + file.getAbsolutePath());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.dstadler.commons.testing;

import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;

public class MemoryLeakVerifierTest {
@Test
Expand All @@ -26,12 +27,10 @@ public void testWithMemoryLeak() {

verifier.addObject(obj);

try {
verifier.assertGarbageCollected(3);
fail("Should report a memory leak here");
} catch (AssertionError e) {
TestHelpers.assertContains(e, "Object should not exist");
}
TestHelpers.assertContains(
assertThrows(AssertionError.class,
() -> verifier.assertGarbageCollected(3)),
"Object should not exist");
}

@Test
Expand Down Expand Up @@ -61,8 +60,9 @@ private void expectMemoryLeaks(File heapDumpFile, MemoryLeakVerifier verifier) {
} catch (AssertionError e) {
TestHelpers.assertContains(e, "Object should not exist");

assertTrue("HeapDumpFile was not found at " + heapDumpFile.getAbsolutePath(), heapDumpFile.exists());
assertTrue("HeapDumpFile at " + heapDumpFile.getAbsolutePath() + " could not be deleted", heapDumpFile.delete());
assertTrue(heapDumpFile.exists(), "HeapDumpFile was not found at " + heapDumpFile.getAbsolutePath());
assertTrue(heapDumpFile.delete(),
"HeapDumpFile at " + heapDumpFile.getAbsolutePath() + " could not be deleted");
}
}

Expand Down
123 changes: 56 additions & 67 deletions src/test/java/org/dstadler/commons/testing/MockRESTServerTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
package org.dstadler.commons.testing;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import org.dstadler.commons.http.NanoHTTPD;
import org.dstadler.commons.logging.jdk.LoggerFactory;
import org.dstadler.commons.net.UrlUtils;
import org.junit.After;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

import static org.junit.Assert.*;


/**
* Just verify that the host where the tests are run does what we
* expect it to in respect to hostname resolution/DNS/...
*
* @author cwat-dstadler
*/
@SuppressWarnings("Convert2Lambda") // should still compile with Java 7
public class MockRESTServerTest {
private static final Logger log = LoggerFactory.make();

@After
@AfterEach
public void tearDown() throws InterruptedException {
ThreadTestHelper.waitForThreadToFinishSubstring("NanoHTTP");
}
Expand All @@ -48,66 +48,63 @@ public void testLocalhostIP() throws Exception {
@Test
public void testIP() throws Exception {
InetAddress localHost = java.net.InetAddress.getLocalHost();
assertNotNull("Should get a local address", localHost);
assertNotNull(localHost, "Should get a local address");
String ipAddress = localHost.getHostAddress();

log.info("Had hostname: " + ipAddress + ", address-info: " + localHost);

assertNotNull("Should get a local ip-address", ipAddress);
assertNotEquals("Local ip-address should not equal localhost", "localhost", ipAddress);
assertNotNull(ipAddress, "Should get a local ip-address");
assertNotEquals("localhost", ipAddress, "Local ip-address should not equal localhost");

// Travis-CI reports 127.0.0.1 for some reason
Assume.assumeFalse("Travis-CI reports an unexpected ipAddress",
"true".equals(System.getenv("TRAVIS")) && "127.0.0.1".equals(ipAddress));
Assumptions.assumeFalse("true".equals(System.getenv("TRAVIS")) && "127.0.0.1".equals(ipAddress),
"Travis-CI reports an unexpected ipAddress");
// Github Actions reports an inaccessible hostname
Assume.assumeFalse("Github Actions report an unexpected ipAddress",
"true".equals(System.getenv("GITHUB_ACTIONS")));
Assumptions.assumeFalse("true".equals(System.getenv("GITHUB_ACTIONS")), "Github Actions report an unexpected ipAddress");

// cannot assert on startsWith("127.0.0") as e.g. lab13 reports an ip-address of 127.0.0.2
assertNotEquals("Local ip-address should not equal 127.0.0.1", "127.0.0.1", ipAddress);
assertNotEquals("127.0.0.1", ipAddress, "Local ip-address should not equal 127.0.0.1");

runWithHostname(ipAddress);
}

@Test
public void testHostname() throws Exception {
assertNotNull(java.net.InetAddress.getLocalHost());
assertNotNull(InetAddress.getLocalHost());
String hostname = java.net.InetAddress.getLocalHost().getHostName();
assertNotNull(hostname);

// Travis-CI reports 127.0.0.1 for some reason
Assume.assumeFalse("Travis-CI reports an unexpected hostname",
"true".equals(System.getenv("TRAVIS")) && "localhost".equals(hostname));
Assumptions.assumeFalse("true".equals(System.getenv("TRAVIS")) && "localhost".equals(hostname),
"Travis-CI reports an unexpected hostname");
// Github Actions reports an inaccessible hostname
Assume.assumeFalse("Github Actions report an unexpected ipAddress",
"true".equals(System.getenv("GITHUB_ACTIONS")));
Assumptions.assumeFalse("true".equals(System.getenv("GITHUB_ACTIONS")), "Github Actions report an unexpected ipAddress");

assertNotEquals("Local hostname should not equal localhost", "localhost", hostname);
assertFalse("Local hostname should not start with 127.0.0", hostname.startsWith("127.0.0"));
assertNotEquals("localhost", hostname, "Local hostname should not equal localhost");
assertFalse(hostname.startsWith("127.0.0"), "Local hostname should not start with 127.0.0");

runWithHostname(hostname);
}

@Test
public void testCanonicalHostname() throws Exception {
assertNotNull(java.net.InetAddress.getLocalHost());
assertNotNull(InetAddress.getLocalHost());
String hostname = java.net.InetAddress.getLocalHost().getCanonicalHostName();
assertNotNull(hostname);

// Travis-CI reports 127.0.0.1 for some reason
Assume.assumeFalse("Travis-CI reports an unexpected hostname",
"true".equals(System.getenv("TRAVIS")) && "localhost".equals(hostname));
Assumptions.assumeFalse("true".equals(System.getenv("TRAVIS")) && "localhost".equals(hostname),
"Travis-CI reports an unexpected hostname");
// Github Actions reports an inaccessible hostname
Assume.assumeFalse("Github Actions report an unexpected ipAddress",
"true".equals(System.getenv("GITHUB_ACTIONS")));
Assumptions.assumeFalse("true".equals(System.getenv("GITHUB_ACTIONS")), "Github Actions report an unexpected ipAddress");

assertNotEquals("localhost", hostname);
assertFalse(hostname.startsWith("127.0.0"));

runWithHostname(hostname);
}

@Ignore("Fails in some environments")
@Disabled("Fails in some environments")
@Test
public void testAllNetworkInterfaces() throws Exception {
final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
Expand All @@ -119,8 +116,8 @@ public void testAllNetworkInterfaces() throws Exception {
assertNotNull(inetAddress);
String hostname = inetAddress.getCanonicalHostName();
assertNotNull(hostname);
assertNotEquals("Had: " + hostname, "localhost", hostname);
assertFalse("Had: " + hostname, hostname.startsWith("127.0.0"));
assertNotEquals("localhost", hostname, "Had: " + hostname);
assertFalse(hostname.startsWith("127.0.0"), "Had: " + hostname);

// UrlUtils does not support IPv6 yet
if(inetAddress instanceof Inet6Address) {
Expand All @@ -141,17 +138,17 @@ private void runWithHostname(String hostname) throws IOException {
try (MockRESTServer server = new MockRESTServer(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_PLAINTEXT, "OK")) {
final String url = "http://" + hostname + ":" + server.getPort();
boolean check = UrlUtils.isAvailable(url, false, 500);
assertTrue("Expect URL to be available. " + url + ": Had: " + check + ", on Windows this might indicate that a VirtualBox related network interface is enabled",
check);
assertTrue(check,
"Expect URL to be available. " + url + ": Had: " + check + ", on Windows this might indicate that a VirtualBox related network interface is enabled");

check = UrlUtils.isAvailable(url, true, 500);
assertTrue("Expect URL to be available. " + url + ": Had: " + check, check);
assertTrue(check, "Expect URL to be available. " + url + ": Had: " + check);

String checkStr = UrlUtils.retrieveData(url, 500);
assertTrue("Expect URL to be available. " + url + ": Had: " + checkStr, checkStr.length() > 0);
assertFalse(checkStr.isEmpty(), "Expect URL to be available. " + url + ": Had: " + checkStr);

String data = UrlUtils.retrieveData(url, 500);
assertEquals("Expect URL to return 'OK'. " + url + ": Had: " + data, "OK", data);
assertEquals("OK", data, "Expect URL to return 'OK'. " + url + ": Had: " + data);
}
}

Expand All @@ -175,7 +172,8 @@ public void testExhaustPorts() {
MockRESTServer[] servers = new MockRESTServer[100];
try {
for (int i = 0; i < 100; i++) {
servers[i] = new MockRESTServer(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, "<html>" + i + "</html>");
//noinspection resource
servers[i] = new MockRESTServer(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, "<html>" + i + "</html>");
}
} catch (IOException e) {
TestHelpers.assertContains(e, "No free port found");
Expand All @@ -191,49 +189,40 @@ public void testExhaustPorts() {
@Test
public void testWithRunnable() throws IOException {
final AtomicBoolean called = new AtomicBoolean();
try (MockRESTServer server = new MockRESTServer(new Runnable() {
@Override
public void run() {
assertFalse("Should be called exactly once, but was already called before", called.get());
called.set(true);
}
}, NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, "<html>1</html>")) {
try (MockRESTServer server = new MockRESTServer(() -> {
assertFalse(called.get(), "Should be called exactly once, but was already called before");
called.set(true);
}, NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, "<html>1</html>")) {
String data = UrlUtils.retrieveData("http://localhost:" + server.getPort(), 10_000);
assertEquals("<html>1</html>", data);
}

assertTrue("Should be called now", called.get());
assertTrue(called.get(), "Should be called now");
}

@Test
public void testWithCallable() throws IOException {
final AtomicBoolean called = new AtomicBoolean();
try (MockRESTServer server = new MockRESTServer(new Callable<NanoHTTPD.Response>() {
@Override
public NanoHTTPD.Response call() {
assertFalse("Should be called exactly once, but was already called before", called.get());
called.set(true);
return new NanoHTTPD.Response(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, "<html>1</html>");
}
})) {
try (MockRESTServer server = new MockRESTServer(() -> {
assertFalse(called.get(), "Should be called exactly once, but was already called before");
called.set(true);
return new NanoHTTPD.Response(NanoHTTPD.HTTP_OK, NanoHTTPD.MIME_HTML, "<html>1</html>");
})) {
String data = UrlUtils.retrieveData("http://localhost:" + server.getPort(), 10_000);
assertEquals("<html>1</html>", data);
}

assertTrue("Should be called now", called.get());
assertTrue(called.get(), "Should be called now");
}

@Test
public void testWithCallableException() throws IOException {
final AtomicBoolean called = new AtomicBoolean();
try (MockRESTServer server = new MockRESTServer(new Callable<NanoHTTPD.Response>() {
@Override
public NanoHTTPD.Response call() {
assertFalse("Should be called exactly once, but was already called before", called.get());
called.set(true);
throw new RuntimeException("TestException");
}
})) {
try (MockRESTServer server = new MockRESTServer(() -> {
assertFalse(called.get(), "Should be called exactly once, but was already called before");
called.set(true);
throw new RuntimeException("TestException");
})) {
try {
UrlUtils.retrieveData("http://localhost:" + server.getPort(), 10_000);
fail("Should catch HTTP 500 here because of the exception");
Expand All @@ -242,6 +231,6 @@ public NanoHTTPD.Response call() {
}
}

assertTrue("Should be called now", called.get());
assertTrue(called.get(), "Should be called now");
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.dstadler.commons.testing;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

import org.junit.Test;
import org.junit.jupiter.api.Test;


/**
*
* @author dominik.stadler
*/
public class PrivateConstructorCoverageTest {

@Test
Expand All @@ -26,7 +21,7 @@ public void testExecutePrivateConstructor() throws Exception {
}
}

private abstract class MyAbstract {
private abstract static class MyAbstract {

}
}
Loading

0 comments on commit d251939

Please sign in to comment.