This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git
commit 4b2d40b7b986d45dd648c16fc4748a3818db09f8 Author: Robert Munteanu <[email protected]> AuthorDate: Wed Jun 5 11:34:02 2019 +0200 Migrate to Junit 5 for more fine-grained timeouts and exception checks. --- url-connection-agent/pom.xml | 10 ++++--- .../org/apache/sling/uca/impl/IntegrationTest.java | 31 ++++++++++++++-------- .../java/org/apache/sling/uca/impl/ServerRule.java | 29 +++++++++++--------- .../sling/uca/impl/UrlTimeoutTransformerTest.java | 16 ++++++----- 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/url-connection-agent/pom.xml b/url-connection-agent/pom.xml index e44e7ba..bc5a25f 100644 --- a/url-connection-agent/pom.xml +++ b/url-connection-agent/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/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java index b3d6816..d9a1d7c 100644 --- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java +++ b/url-connection-agent/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/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java index 6bcad85..c047afd 100644 --- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java +++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java @@ -32,21 +32,28 @@ import org.eclipse.jetty.server.Request; 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 Server server; + private static final int LOCAL_PORT = 12312; - private int localPort = 12312; + public static int getLocalPort() { + return LOCAL_PORT; + } + + private Server server; + @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 @@ class ServerRule extends ExternalResource { 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 @@ class ServerRule extends ExternalResource { server.start(); } - + @Override - protected void after() { + public void afterAll(ExtensionContext context) throws Exception { if ( server != null ) try { server.stop(); @@ -114,8 +121,4 @@ class ServerRule extends ExternalResource { e.printStackTrace(); } } - - public int getLocalPort() { - return localPort; - } } \ No newline at end of file diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java index 774b420..3847ef2 100644 --- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/UrlTimeoutTransformerTest.java +++ b/url-connection-agent/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 @@ public class UrlTimeoutTransformerTest { private URLTimeoutTransformer transformer; - @Before + @BeforeEach public void initFields() { transformer = new URLTimeoutTransformer(1, 1); } @@ -37,11 +39,13 @@ public class UrlTimeoutTransformerTest { 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") + ); } }
