@Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, RestApp.class, LoadCustomClient.class).jar(); }
public AbstractRandomPortConfigSource(String configProperty, String configSourceName, int portStart, int portRange, int portMaxFail) { this.configSourceName = configSourceName; this.properties = singletonMap(configProperty, findServerPort(portStart, portRange, portMaxFail)); }
@Override public Object lookup(ArquillianResource arquillianResource, Annotation... annotations) { try { return URI.class.cast(super.lookup(arquillianResource, annotations)).toURL(); } catch (MalformedURLException e) { throw new RuntimeException("Unable to create URL", e); } }
@RunWith(Arquillian.class) @EnableRandomWebServerPort public abstract class ListenerTest { public static JavaArchive createArchive(Class<?>... classes) { String property = System.getProperty(Bootstrapper.class.getName()); return ShrinkWrap.create(JavaArchive.class) .addClasses(DefaultServlet.class, HammockRuntime.class, MessageProvider.class, DefaultListener.class, WebServerConfiguration.class, StartWebServer.class) .addClasses(classes) .addAsServiceProvider(Bootstrapper.class.getName(), property) .addAsManifestResource(new FileAsset(new File("src/main/resources/META-INF/beans.xml")), "beans.xml"); } @Test public void shouldInvokeListener() throws Exception { assertThat(DefaultListener.INITIALIZED).isTrue(); } }
private String findServerPort(int portStart, int portRange, int portMaxFail) { int openPortTry = 0; Optional<ServerSocket> serverPort = Optional.empty(); while (!serverPort.isPresent()) { if (openPortTry++ > portMaxFail) { throw new IllegalStateException("No openable server ports found, reached try limit; " + portMaxFail); } serverPort = openServerSocket(nextRandomServerPort(portStart, portRange)); } try { return Integer.toString(serverPort.get().getLocalPort()); } finally { try { serverPort.get().close(); } catch (IOException ex) { // ignore } } }
@Override public void process(Archive<?> archive, TestClass testClass) { EnableRandomWebServerPort annotation = testClass.getJavaClass().getAnnotation(EnableRandomWebServerPort.class); JavaArchive jar = archive.as(JavaArchive.class) .addPackage("io.astefanutti.metrics.cdi") .addAsResource(TOMCAT_BASE, "hammock.properties") .addAsManifestResource(BEANS_XML, "beans.xml"); if(annotation != null) { if (annotation.enableSecure()) { jar.addAsServiceProviderAndClasses(ConfigSource.class, RandomWebServerPort.class, RandomWebServerSecuredPort.class); } else { jar.addAsServiceProviderAndClasses(ConfigSource.class, RandomWebServerPort.class); } } } }
@Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, CustomRestApp.class).jar(); }
@RunWith(Arquillian.class) @EnableRandomWebServerPort public class CustomURITest { @Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, CustomRestApp.class).jar(); } @ArquillianResource private URI uri; @Test public void shouldBeAbleToRetrieveRestEndpoint() throws Exception { get(uri + "/custom/rest").then().assertThat().statusCode(200) .body(is("Hello, World!")); get(uri + "/rest").then().assertThat().statusCode(404); } }
@Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, CustomRestApp.class).jar(); }
@RunWith(Arquillian.class) @EnableRandomWebServerPort public class CustomURITest { @Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, CustomRestApp.class).jar(); } @ArquillianResource private URI uri; @Test public void shouldBeAbleToRetrieveRestEndpoint() throws Exception { get(uri + "/custom/rest").then().assertThat().statusCode(200) .body(is("Hello, World!")); get(uri + "/rest").then().assertThat().statusCode(404); } }
@Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, RestApp.class, LoadCustomClient.class).jar(); }
@RunWith(Arquillian.class) @EnableRandomWebServerPort public class BaseURITest { @Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, RestApp.class, LoadCustomClient.class).jar(); } @Inject private LoadCustomClient loadCustomClient; @ArquillianResource private URI uri; @Test public void shouldBeAbleToRetrieveRestEndpoint() throws Exception { get(uri + "/rest").then().assertThat().statusCode(200) .body(is("Hello, World!")); } @Test public void shouldLoadAndDestroyClient() { loadCustomClient.verifyLoadCustomClient(); } }
@Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class).jar() .addAsServiceProviderAndClasses(ConfigSource.class, URIConfigSource.class, RandomPortConfigSource .class); }
@RunWith(Arquillian.class) @EnableRandomWebServerPort public class BaseURITest { @Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class, RestApp.class, LoadCustomClient.class).jar(); } @Inject private LoadCustomClient loadCustomClient; @ArquillianResource private URI uri; @Test public void shouldBeAbleToRetrieveRestEndpoint() throws Exception { get(uri + "/rest").then().assertThat().statusCode(200) .body(is("Hello, World!")); } @Test public void shouldLoadAndDestroyClient() { loadCustomClient.verifyLoadCustomClient(); } }
@Deployment public static JavaArchive createArchive() { return new HammockArchive().classes(RestController.class).jar() .addAsServiceProviderAndClasses(ConfigSource.class, URIConfigSource.class, RandomWebServerPort .class); }
@RunWith(Arquillian.class) @EnableRandomWebServerPort(enableSecure=true) public abstract class FilterTest { public static JavaArchive createArchive(Class<?>...classes) {
@RunWith(Arquillian.class) @EnableRandomWebServerPort(enableSecure=true) public abstract class ServletTest { public static JavaArchive createArchive(Class<?>... classes) {