Migrate to Junit 5 for more fine-grained timeouts and exception checks.
diff --git a/pom.xml b/pom.xml
index e44e7ba..bc5a25f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -67,10 +67,6 @@
<version>3.24.0-GA</version>
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.18.v20190429</version>
@@ -81,5 +77,11 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
+ <version>5.4.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index b3d6816..d9a1d7c 100644
--- a/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -16,42 +16,51 @@
*/
package org.apache.sling.uca.impl;
-import static org.junit.Assert.fail;
+import static java.time.Duration.ofSeconds;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTimeout;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
+import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
-import org.junit.Rule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+@ExtendWith(ServerRule.class)
public class IntegrationTest {
private static final Logger LOG = LoggerFactory.getLogger(IntegrationTest.class);
-
- @Rule
- public ServerRule server = new ServerRule();
- @Test(expected = IOException.class, timeout = 5000)
+ @Test
public void connectTimeout() throws IOException {
- runTest(false);
+ SocketTimeoutException exception = assertThrows(SocketTimeoutException.class,
+ () -> assertTimeout(ofSeconds(5), () -> runTest(false))
+ );
+ assertEquals("Connect timed out", exception.getMessage());
}
- @Test(expected = IOException.class, timeout = 15000)
+ @Test
public void readTimeout() throws IOException {
- runTest(true);
+ SocketTimeoutException exception = assertThrows(SocketTimeoutException.class,
+ () -> assertTimeout(ofSeconds(10), () -> runTest(false))
+ );
+ assertEquals("Read timed out", exception.getMessage());
}
private void runTest(boolean shouldConnect) throws MalformedURLException, IOException {
- URL url = new URL("http://localhost:" + server.getLocalPort());
+
+ URL url = new URL("http://localhost:" + ServerRule.getLocalPort());
LOG.info("connecting");
URLConnection connection = url.openConnection();
// TODO - remove when running through the harness
diff --git a/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/src/test/java/org/apache/sling/uca/impl/ServerRule.java
index 6bcad85..c047afd 100644
--- a/src/test/java/org/apache/sling/uca/impl/ServerRule.java
+++ b/src/test/java/org/apache/sling/uca/impl/ServerRule.java
@@ -32,21 +32,28 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
-import org.junit.rules.ExternalResource;
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-class ServerRule extends ExternalResource {
+class ServerRule implements BeforeAllCallback, AfterAllCallback {
private static final Logger LOG = LoggerFactory.getLogger(ServerRule.class);
+ private static final int LOCAL_PORT = 12312;
+
+
+ public static int getLocalPort() {
+ return LOCAL_PORT;
+ }
+
private Server server;
- private int localPort = 12312;
-
@Override
- protected void before() throws Throwable {
- server = new Server(localPort);
+ public void beforeAll(ExtensionContext context) throws Exception {
+ server = new Server(LOCAL_PORT);
ServerConnector connector = new ServerConnector(server) {
@Override
public void accept(int acceptorID) throws IOException {
@@ -61,7 +68,7 @@
LOG.info("Accepted");
}
};
- connector.setPort(localPort);
+ connector.setPort(LOCAL_PORT);
connector.setConnectionFactories(Collections.singleton(new HttpConnectionFactory() {
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
@@ -104,9 +111,9 @@
server.start();
}
-
+
@Override
- protected void after() {
+ public void afterAll(ExtensionContext context) throws Exception {
if ( server != null )
try {
server.stop();
@@ -114,8 +121,4 @@
e.printStackTrace();
}
}
-
- public int getLocalPort() {
- return localPort;
- }
}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java b/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java
index 774b420..3847ef2 100644
--- a/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java
+++ b/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java
@@ -16,10 +16,12 @@
*/
package org.apache.sling.uca.impl;
-import static org.junit.Assert.assertNotNull;
-import org.junit.Before;
-import org.junit.Test;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import javassist.NotFoundException;
@@ -27,7 +29,7 @@
private URLTimeoutTransformer transformer;
- @Before
+ @BeforeEach
public void initFields() {
transformer = new URLTimeoutTransformer(1, 1);
}
@@ -37,11 +39,13 @@
assertNotNull(transformer.findConnectMethod("sun/net/www/protocol/http/HttpURLConnection"));
}
- @Test(expected = NotFoundException.class)
+ @Test
public void findInheritedConnectMethod() throws NotFoundException {
// do NOT look for inherited methods, as we can only rewrite the precise classes the
// retransform was triggered for
- transformer.findConnectMethod("sun/net/www/protocol/https/DelegateHttpsURLConnection");
+ assertThrows( NotFoundException.class,
+ () -> transformer.findConnectMethod("sun/net/www/protocol/https/DelegateHttpsURLConnection")
+ );
}
}