tests: properly isolate all tests that rely on ephemeral network ports
Project: http://git-wip-us.apache.org/repos/asf/zest-java/repo Commit: http://git-wip-us.apache.org/repos/asf/zest-java/commit/0cd5a150 Tree: http://git-wip-us.apache.org/repos/asf/zest-java/tree/0cd5a150 Diff: http://git-wip-us.apache.org/repos/asf/zest-java/diff/0cd5a150 Branch: refs/heads/develop Commit: 0cd5a1508217654cd0dbe70864e705fc71b874b6 Parents: 4b7d771 Author: Paul Merlin <[email protected]> Authored: Sat Nov 19 23:48:35 2016 +0100 Committer: Paul Merlin <[email protected]> Committed: Sat Nov 19 23:48:35 2016 +0100 ---------------------------------------------------------------------- .../apache/zest/test/util/FreePortFinder.java | 28 +++++---- .../zest/library/http/AbstractJettyTest.java | 1 - .../library/http/AbstractSecureJettyTest.java | 1 - .../library/http/JettyJMXStatisticsTest.java | 3 +- .../zest/library/http/JettyServiceTest.java | 7 ++- .../http/MutualSecureJettyServiceTest.java | 11 +++- .../library/http/SecureJettyServiceTest.java | 10 +++- .../http/VirtualHostJettyServiceTest.java | 47 +++++++-------- .../ContextResourceClientFactoryTest.java | 30 ++++++---- .../rest/client/ContinuousIntegrationTest.java | 7 ++- .../zest/library/rest/admin/RestTest.java | 15 +---- .../zest/library/servlet/ServletTest.java | 2 +- .../library/shiro/web/WebHttpShiroTest.java | 41 +++++-------- .../library/shiro/web/WebRealmServiceTest.java | 62 ++++++++------------ .../library/shiro/web/WebServletShiroTest.java | 2 +- 15 files changed, 127 insertions(+), 140 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java ---------------------------------------------------------------------- diff --git a/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java b/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java index b0eb6d1..5f2e00a 100644 --- a/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java +++ b/core/testsupport/src/main/java/org/apache/zest/test/util/FreePortFinder.java @@ -84,24 +84,28 @@ public class FreePortFinder checkFreePort( address, port ); return true; } - catch( IOException ex ) + catch( UncheckedIOException ex ) { return false; } } - public static void checkFreePortOnLocalHost( int port ) throws IOException + public static void checkFreePortOnLocalHost( int port ) { checkFreePort( getLocalHostUnchecked(), port ); } - public static void checkFreePort( InetAddress address, int port ) throws IOException + public static void checkFreePort( InetAddress address, int port ) { ServerSocket server = null; try { server = new ServerSocket( port, 1, address ); } + catch( IOException ex ) + { + throw new UncheckedIOException( ex ); + } finally { if( server != null ) @@ -124,8 +128,12 @@ public class FreePortFinder return findFreePort( getLocalHostUnchecked() ); } + public static int findFreePortOnLoopback() + { + return findFreePort( InetAddress.getLoopbackAddress() ); + } + public static int findFreePort( InetAddress address ) - throws IOException { FreePortPredicate check = new FreePortPredicate( address ); // Randomly choose MAX_PORT_CHECKS ports from the least used ranges @@ -134,8 +142,8 @@ public class FreePortFinder .boxed() .collect( collectingAndThen( toList(), collected -> { - shuffle( collected ); - return collected.stream(); + shuffle( collected ); + return collected.stream(); } ) ) .limit( MAX_PORT_CHECKS ) .mapToInt( Integer::intValue ) @@ -145,18 +153,16 @@ public class FreePortFinder { IOException exception = new IOException( "Unable to find a free port on " + address ); check.errors.build().forEach( exception::addSuppressed ); - return exception; + return new UncheckedIOException( exception ); } ); } public static int findFreePortInRangeOnLocalhost( int lowerBound, int higherBound ) - throws IOException { return findFreePortInRange( getLocalHostUnchecked(), lowerBound, higherBound ); } public static int findFreePortInRange( InetAddress address, int lowerBound, int higherBound ) - throws IOException { if( higherBound - lowerBound < 0 ) { @@ -169,7 +175,7 @@ public class FreePortFinder String message = "Unable to find a free port in range " + lowerBound + '-' + higherBound + " on " + address; IOException exception = new IOException( message ); check.errors.build().forEach( exception::addSuppressed ); - return exception; + return new UncheckedIOException( exception ); } ); } @@ -192,7 +198,7 @@ public class FreePortFinder checkFreePort( address, candidate ); return true; } - catch( IOException ex ) + catch( UncheckedIOException ex ) { errors.add( ex ); return false; http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java index 3cec81b..6f8c373 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractJettyTest.java @@ -38,7 +38,6 @@ import org.apache.zest.test.AbstractZestTest; public abstract class AbstractJettyTest extends AbstractZestTest { - protected static final int HTTP_PORT = 8041; protected CloseableHttpClient defaultHttpClient; protected ResponseHandler<String> stringResponseHandler = new ResponseHandler<String>() { http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java index def298e..9ca254f 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/AbstractSecureJettyTest.java @@ -49,7 +49,6 @@ import org.junit.rules.TemporaryFolder; public abstract class AbstractSecureJettyTest extends AbstractJettyTest { - protected static final int HTTPS_PORT = 8441; protected static final String KS_PASSWORD = "changeit"; protected static final String CLIENT_KEYSTORE_FILENAME = "zest-lib-http-unittests-client-cert.jceks"; protected static final String SERVER_KEYSTORE_FILENAME = "zest-lib-http-unittests-server-cert.jceks"; http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/http/src/test/java/org/apache/zest/library/http/JettyJMXStatisticsTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/JettyJMXStatisticsTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/JettyJMXStatisticsTest.java index 7d737a6..37e8ccf 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/JettyJMXStatisticsTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/JettyJMXStatisticsTest.java @@ -19,6 +19,7 @@ */ package org.apache.zest.library.http; +import org.apache.zest.test.util.FreePortFinder; import org.junit.Test; import org.apache.zest.api.common.Visibility; import org.apache.zest.bootstrap.AssemblyException; @@ -45,7 +46,7 @@ public class JettyJMXStatisticsTest JettyConfiguration config = configModule.forMixin( JettyConfiguration.class ).declareDefaults(); config.hostName().set( "127.0.0.1" ); - config.port().set( 8441 ); + config.port().set( FreePortFinder.findFreePortOnLoopback() ); config.statistics().set( Boolean.TRUE ); // Set statistics default to TRUE in configuration // Hello world servlet related assembly http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java index 6e6e7df..54f2135 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/JettyServiceTest.java @@ -21,6 +21,7 @@ package org.apache.zest.library.http; import java.util.Iterator; import org.apache.http.client.methods.HttpGet; +import org.apache.zest.test.util.FreePortFinder; import org.junit.Test; import org.apache.zest.api.common.Visibility; import org.apache.zest.api.service.ServiceReference; @@ -40,6 +41,7 @@ import static org.apache.zest.library.http.Servlets.serve; public final class JettyServiceTest extends AbstractJettyTest { + private final int httpPort = FreePortFinder.findFreePortOnLoopback(); @Override public final void assemble( ModuleAssembly module ) @@ -55,7 +57,7 @@ public final class JettyServiceTest // Set HTTP port as JettyConfiguration default JettyConfiguration config = configModule.forMixin( JettyConfiguration.class ).declareDefaults(); config.hostName().set( "127.0.0.1" ); - config.port().set( HTTP_PORT ); + config.port().set( httpPort ); // Serve /helloWorld with HelloWorldServletService addServlets( serve( "/helloWorld" ).with( HelloWorldServletService.class ) ).to( module ); @@ -81,7 +83,8 @@ public final class JettyServiceTest JettyService jettyService = serviceRef.get(); assertNotNull( jettyService ); - String output = defaultHttpClient.execute( new HttpGet( "http://127.0.0.1:8041/helloWorld" ), stringResponseHandler ); + String output = defaultHttpClient.execute( new HttpGet( "http://127.0.0.1:" + httpPort + "/helloWorld" ), + stringResponseHandler ); assertEquals( "Hello World", output ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/http/src/test/java/org/apache/zest/library/http/MutualSecureJettyServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/MutualSecureJettyServiceTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/MutualSecureJettyServiceTest.java index caedeb3..c8f2657 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/MutualSecureJettyServiceTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/MutualSecureJettyServiceTest.java @@ -21,6 +21,7 @@ package org.apache.zest.library.http; import java.io.IOException; import org.apache.http.client.methods.HttpGet; +import org.apache.zest.test.util.FreePortFinder; import org.junit.Test; import org.apache.zest.api.common.Visibility; import org.apache.zest.bootstrap.AssemblyException; @@ -34,6 +35,8 @@ import static org.apache.zest.library.http.Servlets.serve; public class MutualSecureJettyServiceTest extends AbstractSecureJettyTest { + private final int httpsPort = FreePortFinder.findFreePortOnLoopback(); + @Override public void assemble( ModuleAssembly module ) throws AssemblyException @@ -44,7 +47,7 @@ public class MutualSecureJettyServiceTest // START SNIPPET: config SecureJettyConfiguration config = configModule.forMixin( SecureJettyConfiguration.class ).declareDefaults(); config.hostName().set( "127.0.0.1" ); - config.port().set( HTTPS_PORT ); + config.port().set( httpsPort ); config.keystorePath().set( getKeyStoreFile( SERVER_KEYSTORE_FILENAME ).getAbsolutePath() ); config.keystoreType().set( "JCEKS" ); @@ -65,7 +68,8 @@ public class MutualSecureJettyServiceTest throws IOException { // As we set wantClientAuth we can request without a client certificate ... - String output = trustHttpClient.execute( new HttpGet( "https://127.0.0.1:8441/hello" ), stringResponseHandler ); + String output = trustHttpClient.execute( new HttpGet( "https://127.0.0.1:" + httpsPort + "/hello" ), + stringResponseHandler ); assertEquals( "Hello World", output ); } @@ -74,7 +78,8 @@ public class MutualSecureJettyServiceTest throws IOException { // ... and with one - String output = mutualHttpClient.execute( new HttpGet( "https://127.0.0.1:8441/hello" ), stringResponseHandler ); + String output = mutualHttpClient.execute( new HttpGet( "https://127.0.0.1:" + httpsPort + "/hello" ), + stringResponseHandler ); assertEquals( "Hello Mutual World", output ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java b/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java index 159dfaf..930414e 100644 --- a/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java +++ b/libraries/http/src/test/java/org/apache/zest/library/http/SecureJettyServiceTest.java @@ -23,6 +23,7 @@ import java.io.IOException; import javax.net.ssl.SSLHandshakeException; import org.apache.http.NoHttpResponseException; import org.apache.http.client.methods.HttpGet; +import org.apache.zest.test.util.FreePortFinder; import org.junit.Test; import org.apache.zest.api.common.Visibility; import org.apache.zest.bootstrap.AssemblyException; @@ -40,6 +41,8 @@ import static org.apache.zest.library.http.Servlets.serve; public class SecureJettyServiceTest extends AbstractSecureJettyTest { + private final int httpsPort = FreePortFinder.findFreePortOnLoopback(); + @Override public void assemble( ModuleAssembly module ) throws AssemblyException @@ -70,7 +73,7 @@ public class SecureJettyServiceTest public void testNoSSL() throws IOException { - HttpGet get = new HttpGet( "http://127.0.0.1:8441/hello" ); + HttpGet get = new HttpGet( "http://127.0.0.1:" + httpsPort + "/hello" ); defaultHttpClient.execute( get ); fail( "We could reach the HTTPS connector using a HTTP url, that's no good" ); } @@ -80,7 +83,7 @@ public class SecureJettyServiceTest public void testNoTruststore() throws IOException { - defaultHttpClient.execute( new HttpGet( "https://127.0.0.1:8441/hello" ) ); + defaultHttpClient.execute( new HttpGet( "https://127.0.0.1:" + httpsPort + "/hello" ) ); fail( "We could reach the HTTPS connector without proper truststore, this should not happen" ); } @@ -88,7 +91,8 @@ public class SecureJettyServiceTest public void testTrust() throws IOException, InterruptedException { - String output = trustHttpClient.execute( new HttpGet( "https://127.0.0.1:8441/hello" ), stringResponseHandler ); + String output = trustHttpClient.execute( new HttpGet( "https://127.0.0.1:" + httpsPort + "/hello" ), + stringResponseHandler ); assertEquals( "Hello World", output ); } } http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/http/src/vhost-test/java/org/apache/zest/library/http/VirtualHostJettyServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/http/src/vhost-test/java/org/apache/zest/library/http/VirtualHostJettyServiceTest.java b/libraries/http/src/vhost-test/java/org/apache/zest/library/http/VirtualHostJettyServiceTest.java index 1bf526c..4a433f2 100644 --- a/libraries/http/src/vhost-test/java/org/apache/zest/library/http/VirtualHostJettyServiceTest.java +++ b/libraries/http/src/vhost-test/java/org/apache/zest/library/http/VirtualHostJettyServiceTest.java @@ -21,43 +21,28 @@ package org.apache.zest.library.http; import java.io.IOException; import org.apache.http.client.methods.HttpGet; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; import org.apache.zest.api.common.Visibility; import org.apache.zest.bootstrap.AssemblyException; import org.apache.zest.bootstrap.ModuleAssembly; import org.apache.zest.library.http.dns.LocalManagedDns; import org.apache.zest.test.EntityTestAssembler; +import org.apache.zest.test.util.FreePortFinder; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; -import static org.junit.Assert.assertEquals; import static org.apache.zest.library.http.Servlets.addServlets; import static org.apache.zest.library.http.Servlets.serve; import static org.apache.zest.test.util.Assume.assumeNoIbmJdk; +import static org.junit.Assert.assertEquals; -// TODO Compilation fails with Java 9 -// TODO Move this to a separate source set that's enabled only when Java < 9 public class VirtualHostJettyServiceTest extends AbstractJettyTest { private static final String HOST1 = "host1.http.library.zest"; private static final String HOST2 = "host2.http.library.zest"; - @Override - public void assemble( ModuleAssembly module ) - throws AssemblyException - { - ModuleAssembly configModule = module; - new EntityTestAssembler().assemble( configModule ); - new JettyServiceAssembler().withConfig( configModule, Visibility.layer ).assemble( module ); - - SecureJettyConfiguration config = configModule.forMixin( SecureJettyConfiguration.class ).declareDefaults(); - config.hostName().set( "127.0.0.1" ); - config.port().set( HTTP_PORT ); - config.virtualHosts().set( HOST1 + "," + HOST2 ); - - addServlets( serve( "/hello" ).with( HelloWorldServletService.class ) ).to( module ); - } + private final int httpPort = FreePortFinder.findFreePortOnLoopback(); @BeforeClass public static void beforeVirtualHostsClass() @@ -74,16 +59,32 @@ public class VirtualHostJettyServiceTest LocalManagedDns.removeName( HOST2 ); } + @Override + public void assemble( ModuleAssembly module ) + throws AssemblyException + { + ModuleAssembly configModule = module; + new EntityTestAssembler().assemble( configModule ); + new JettyServiceAssembler().withConfig( configModule, Visibility.layer ).assemble( module ); + + SecureJettyConfiguration config = configModule.forMixin( SecureJettyConfiguration.class ).declareDefaults(); + config.hostName().set( "127.0.0.1" ); + config.port().set( httpPort ); + config.virtualHosts().set( HOST1 + "," + HOST2 ); + + addServlets( serve( "/hello" ).with( HelloWorldServletService.class ) ).to( module ); + } + @Test public void test() throws IOException { // Available on HOST1 and HOST2 - String output = defaultHttpClient.execute( new HttpGet( "http://" + HOST1 + ":8041/hello" ), + String output = defaultHttpClient.execute( new HttpGet( "http://" + HOST1 + ":" + httpPort + "/hello" ), stringResponseHandler ); assertEquals( "Hello World", output ); - output = defaultHttpClient.execute( new HttpGet( "http://" + HOST2 + ":8041/hello" ), + output = defaultHttpClient.execute( new HttpGet( "http://" + HOST2 + ":" + httpPort + "/hello" ), stringResponseHandler ); assertEquals( "Hello World", output ); } http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContextResourceClientFactoryTest.java ---------------------------------------------------------------------- diff --git a/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContextResourceClientFactoryTest.java b/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContextResourceClientFactoryTest.java index a19187e..45ed1b6 100644 --- a/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContextResourceClientFactoryTest.java +++ b/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContextResourceClientFactoryTest.java @@ -22,15 +22,6 @@ package org.apache.zest.library.rest.client; import java.io.File; import java.io.IOException; import java.util.Collections; -import org.apache.zest.api.type.HasTypes; -import org.apache.zest.api.unitofwork.UnitOfWorkFactory; -import org.apache.zest.api.usecase.UsecaseBuilder; -import org.apache.zest.api.value.ValueBuilderFactory; -import org.hamcrest.CoreMatchers; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; import org.apache.zest.api.common.Optional; import org.apache.zest.api.common.UseDefaults; import org.apache.zest.api.composite.TransientComposite; @@ -42,10 +33,14 @@ import org.apache.zest.api.property.Property; import org.apache.zest.api.structure.Application; import org.apache.zest.api.structure.ApplicationDescriptor; import org.apache.zest.api.structure.Module; +import org.apache.zest.api.type.HasTypes; import org.apache.zest.api.unitofwork.ConcurrentEntityModificationException; import org.apache.zest.api.unitofwork.UnitOfWorkCallback; import org.apache.zest.api.unitofwork.UnitOfWorkCompletionException; +import org.apache.zest.api.unitofwork.UnitOfWorkFactory; +import org.apache.zest.api.usecase.UsecaseBuilder; import org.apache.zest.api.value.ValueBuilder; +import org.apache.zest.api.value.ValueBuilderFactory; import org.apache.zest.api.value.ValueComposite; import org.apache.zest.bootstrap.AssemblyException; import org.apache.zest.bootstrap.ModuleAssembly; @@ -76,7 +71,13 @@ import org.apache.zest.library.rest.server.assembler.RestServerAssembler; import org.apache.zest.library.rest.server.restlet.NullCommandResult; import org.apache.zest.library.rest.server.spi.CommandResult; import org.apache.zest.test.AbstractZestTest; +import org.apache.zest.test.util.FreePortFinder; import org.apache.zest.valueserialization.orgjson.OrgJsonValueSerializationAssembler; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.restlet.Client; import org.restlet.Request; import org.restlet.Response; @@ -93,8 +94,10 @@ import org.restlet.security.MapVerifier; import org.restlet.security.User; import org.restlet.service.MetadataService; -import static org.apache.zest.bootstrap.ImportedServiceDeclaration.*; -import static org.apache.zest.library.rest.client.api.HandlerCommand.*; +import static org.apache.zest.bootstrap.ImportedServiceDeclaration.NEW_OBJECT; +import static org.apache.zest.library.rest.client.api.HandlerCommand.command; +import static org.apache.zest.library.rest.client.api.HandlerCommand.query; +import static org.apache.zest.library.rest.client.api.HandlerCommand.refresh; public class ContextResourceClientFactoryTest extends AbstractZestTest @@ -141,7 +144,8 @@ public class ContextResourceClientFactoryTest public void startWebServer() throws Exception { - server = new Server( Protocol.HTTP, 8888 ); + int port = FreePortFinder.findFreePortOnLoopback(); + server = new Server( Protocol.HTTP, port ); ContextRestlet restlet = objectFactory.newObject( ContextRestlet.class, new org.restlet.Context() ); ChallengeAuthenticator guard = new ChallengeAuthenticator(null, ChallengeScheme.HTTP_BASIC, "testRealm"); @@ -190,7 +194,7 @@ public class ContextResourceClientFactoryTest //END SNIPPET: client-create2 //START SNIPPET: client-create3 - Reference ref = new Reference( "http://localhost:8888/" ); + Reference ref = new Reference( "http://localhost:" + port + '/' ); crc = contextResourceClientFactory.newClient( ref ); //END SNIPPET: client-create3 } http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContinuousIntegrationTest.java ---------------------------------------------------------------------- diff --git a/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContinuousIntegrationTest.java b/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContinuousIntegrationTest.java index 4f024f5..e994742 100644 --- a/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContinuousIntegrationTest.java +++ b/libraries/rest-client/src/test/java/org/apache/zest/library/rest/client/ContinuousIntegrationTest.java @@ -19,7 +19,7 @@ */ package org.apache.zest.library.rest.client; -import org.apache.zest.api.composite.TransientBuilderFactory; +import org.apache.zest.test.util.FreePortFinder; import org.hamcrest.CoreMatchers; import org.junit.After; import org.junit.Assert; @@ -116,7 +116,8 @@ public class ContinuousIntegrationTest public void startWebServer() throws Exception { - server = new Server( Protocol.HTTP, 8888 ); + int port = FreePortFinder.findFreePortOnLoopback(); + server = new Server( Protocol.HTTP, port ); ContextRestlet restlet = objectFactory.newObject( ContextRestlet.class, new org.restlet.Context() ); ChallengeAuthenticator guard = new ChallengeAuthenticator( null, ChallengeScheme.HTTP_BASIC, "testRealm" ); @@ -167,7 +168,7 @@ public class ContinuousIntegrationTest //END SNIPPET: client-create2 //START SNIPPET: client-create3 - Reference ref = new Reference( "http://localhost:8888/" ); + Reference ref = new Reference( "http://localhost:" + port + '/' ); crc = contextResourceClientFactory.newClient( ref ); //END SNIPPET: client-create3 } http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java ---------------------------------------------------------------------- diff --git a/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java b/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java index 0bec7e8..d3f1577 100644 --- a/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java +++ b/libraries/rest/src/test/java/org/apache/zest/library/rest/admin/RestTest.java @@ -21,7 +21,6 @@ package org.apache.zest.library.rest.admin; import java.io.IOException; -import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; @@ -68,19 +67,7 @@ import static org.junit.Assert.assertThat; public class RestTest extends AbstractZestTest { - private static final int ADMIN_PORT; - - static - { - try - { - ADMIN_PORT = FreePortFinder.findFreePortOnLocalHost(); - } - catch( IOException ex ) - { - throw new UncheckedIOException( ex ); - } - } + private static final int ADMIN_PORT = FreePortFinder.findFreePortOnLoopback(); @Override protected ApplicationDescriptor newApplication() http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/servlet/src/test/java/org/apache/zest/library/servlet/ServletTest.java ---------------------------------------------------------------------- diff --git a/libraries/servlet/src/test/java/org/apache/zest/library/servlet/ServletTest.java b/libraries/servlet/src/test/java/org/apache/zest/library/servlet/ServletTest.java index a0dda4e..561b7bc 100644 --- a/libraries/servlet/src/test/java/org/apache/zest/library/servlet/ServletTest.java +++ b/libraries/servlet/src/test/java/org/apache/zest/library/servlet/ServletTest.java @@ -88,7 +88,7 @@ public class ServletTest public void test() throws Exception { - int port = FreePortFinder.findFreePortOnLocalHost(); + int port = FreePortFinder.findFreePortOnLoopback(); Server server = new Server( port ); try { http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebHttpShiroTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebHttpShiroTest.java b/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebHttpShiroTest.java index 95d9dca..5ede664 100644 --- a/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebHttpShiroTest.java +++ b/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebHttpShiroTest.java @@ -19,7 +19,6 @@ */ package org.apache.zest.library.shiro.web; -import java.io.IOException; import org.junit.Test; import org.apache.zest.api.common.Visibility; import org.apache.zest.bootstrap.AssemblyException; @@ -41,33 +40,25 @@ public class WebHttpShiroTest public void assemble( ModuleAssembly module ) throws AssemblyException { - try - { - ModuleAssembly configModule = module; - new EntityTestAssembler().assemble( configModule ); - // START SNIPPET: assembly - new JettyServiceAssembler().withConfig( configModule, Visibility.layer ).assemble( module ); - // END SNIPPET: assembly + ModuleAssembly configModule = module; + new EntityTestAssembler().assemble( configModule ); + // START SNIPPET: assembly + new JettyServiceAssembler().withConfig( configModule, Visibility.layer ).assemble( module ); + // END SNIPPET: assembly - port = FreePortFinder.findFreePortOnLocalHost(); - JettyConfiguration config = module.forMixin( JettyConfiguration.class ).declareDefaults(); - config.hostName().set( "127.0.0.1" ); - config.port().set( port ); + port = FreePortFinder.findFreePortOnLoopback(); + JettyConfiguration config = module.forMixin( JettyConfiguration.class ).declareDefaults(); + config.hostName().set( "127.0.0.1" ); + config.port().set( port ); - // START SNIPPET: assembly - new HttpShiroAssembler(). - withConfig( configModule, Visibility.layer ). - assemble( module ); - // END SNIPPET: assembly + // START SNIPPET: assembly + new HttpShiroAssembler() + .withConfig( configModule, Visibility.layer ) + .assemble( module ); + // END SNIPPET: assembly - configModule.forMixin( ShiroIniConfiguration.class ). - declareDefaults(). - iniResourcePath().set( "classpath:web-shiro.ini" ); - } - catch( IOException ex ) - { - throw new AssemblyException( "Unable to find free port to bind to", ex ); - } + configModule.forMixin( ShiroIniConfiguration.class ) + .declareDefaults().iniResourcePath().set( "classpath:web-shiro.ini" ); } @Test http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebRealmServiceTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebRealmServiceTest.java b/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebRealmServiceTest.java index 0b2aaea..3c7d89a 100644 --- a/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebRealmServiceTest.java +++ b/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebRealmServiceTest.java @@ -71,8 +71,7 @@ import static org.apache.zest.library.http.Servlets.serve; public class WebRealmServiceTest extends AbstractZestTest { - - private int port; + private final int port = FreePortFinder.findFreePortOnLoopback(); @Mixins( MyRealmMixin.class ) public interface MyRealmService @@ -84,7 +83,6 @@ public class WebRealmServiceTest extends SimpleAccountRealm implements ServiceActivation { - private final PasswordService passwordService; public MyRealmMixin() @@ -109,7 +107,6 @@ public class WebRealmServiceTest throws Exception { } - } @Mixins( MyServlet.class ) @@ -121,50 +118,39 @@ public class WebRealmServiceTest public static class MyServlet extends HttpServlet { - @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { resp.getWriter().println( "FOO" ); } - } @Override public void assemble( ModuleAssembly module ) throws AssemblyException { - try - { - ModuleAssembly configModule = module; - new EntityTestAssembler().assemble( configModule ); - // START SNIPPET: assembly - new JettyServiceAssembler().withConfig( configModule, Visibility.layer ).assemble( module ); - // END SNIPPET: assembly - - port = FreePortFinder.findFreePortOnLocalHost(); - JettyConfiguration config = module.forMixin( JettyConfiguration.class ).declareDefaults(); - config.hostName().set( "127.0.0.1" ); - config.port().set( port ); - - // START SNIPPET: assembly - new HttpShiroAssembler(). - withConfig( configModule, Visibility.layer ). - assemble( module ); - module.services( MyRealmService.class ); - // END SNIPPET: assembly - - configModule.forMixin( ShiroIniConfiguration.class ). - declareDefaults(). - iniResourcePath().set( "classpath:web-shiro.ini" ); - - addServlets( serve( "/*" ).with( MyServletService.class ) ).to( module ); - } - catch( IOException ex ) - { - throw new AssemblyException( "Unable to find free port to bind to", ex ); - } + ModuleAssembly configModule = module; + new EntityTestAssembler().assemble( configModule ); + // START SNIPPET: assembly + new JettyServiceAssembler().withConfig( configModule, Visibility.layer ).assemble( module ); + // END SNIPPET: assembly + + JettyConfiguration config = module.forMixin( JettyConfiguration.class ).declareDefaults(); + config.hostName().set( "127.0.0.1" ); + config.port().set( port ); + + // START SNIPPET: assembly + new HttpShiroAssembler() + .withConfig( configModule, Visibility.layer ) + .assemble( module ); + module.services( MyRealmService.class ); + // END SNIPPET: assembly + + configModule.forMixin( ShiroIniConfiguration.class ) + .declareDefaults().iniResourcePath().set( "classpath:web-shiro.ini" ); + + addServlets( serve( "/*" ).with( MyServletService.class ) ).to( module ); } @Test @@ -212,7 +198,8 @@ public class WebRealmServiceTest throws HttpException, IOException { AuthState authState = (AuthState) context.getAttribute( ClientContext.TARGET_AUTH_STATE ); - CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute( ClientContext.CREDS_PROVIDER ); + CredentialsProvider credsProvider = (CredentialsProvider) context + .getAttribute( ClientContext.CREDS_PROVIDER ); HttpHost targetHost = (HttpHost) context.getAttribute( ExecutionContext.HTTP_TARGET_HOST ); // If not auth scheme has been initialized yet @@ -231,5 +218,4 @@ public class WebRealmServiceTest } } } - } http://git-wip-us.apache.org/repos/asf/zest-java/blob/0cd5a150/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebServletShiroTest.java ---------------------------------------------------------------------- diff --git a/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebServletShiroTest.java b/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebServletShiroTest.java index 7eb5245..3b65158 100644 --- a/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebServletShiroTest.java +++ b/libraries/shiro-web/src/test/java/org/apache/zest/library/shiro/web/WebServletShiroTest.java @@ -36,7 +36,7 @@ public class WebServletShiroTest public void test() throws Exception { - int port = FreePortFinder.findFreePortOnLocalHost(); + int port = FreePortFinder.findFreePortOnLoopback(); Server server = new Server( port ); try {
