public int getPort() { return containerInfo.getPort(); }
public String getHost() { return containerInfo.getHost(); }
@Override public void beforeEach(ExtensionContext extensionContext) throws Exception { logger().info("ContainerInfoExtension - beforeTestExecution "); containerInfo = containerInfo().apply(extensionContext); logger().info("ContainerInfoExtension - " + containerInfo); } }
@Override public void beforeEach(Method testMethod, ExtensionContext context) throws Exception { meecrowave = new Meecrowave(new Meecrowave.Builder() { { randomHttpPort(); setHost(localeIP().get()); setTomcatScanning(true); setTomcatAutoSetup(false); setHttp2(true); } }).bake(); store().apply(context).put(NetworkFunctions.SERVER_IP, meecrowave.getConfiguration().getHost()); store().apply(context).put(NetworkFunctions.SERVER_PORT, meecrowave.getConfiguration().getHttpPort()); store().apply(context).put(NetworkFunctions.SERVER_WEBAPP, "/"); }
@Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { if (ContainerInfo.class.isAssignableFrom(parameterContext.getParameter().getType())) { return containerInfo().apply(extensionContext); } else { throw new ParameterResolutionException("was not able to create ContainerInfo instance"); } }
@Override public void afterEach(Method testMethod, ExtensionContext context) throws Exception { cleanUpPort(context); cleanUpIP(context); store().apply(context).get(TSERVER_SERVER, Result.class) .flatMap((CheckedFunction<Server, String>) server -> { URI serverURI = server.getURI(); server.stop(); return serverURI.toASCIIString(); }).ifPresentOrElse(serverURI -> logger().info("Stopped server on : " + serverURI), failed -> logger().warning("no active server available to stop " + failed)); store().apply(context).remove(TSERVER_SERVER); }
@Override public void beforeEach(Method testMethod, ExtensionContext context) throws Exception { int port = preparePort(context); prepareIP(context); ((CheckedFunction<Integer, Server>) p -> new TServer().startServer(p)).apply(port) .ifPresentOrElse(ok -> { Result<Server> server = Result.success(ok); store().apply(context).put(TSERVER_SERVER, server); logger().info("Started server on port: " + port); }, failed -> { String message = "failed to start TServer for port " + port + " -> " + failed; logger().warning(message); throw new RuntimeException(message); }); }
@Override public void beforeEach(Method testMethod, ExtensionContext context) throws Exception { String localIP = localeIP().get(); final PortUtils portUtils = new PortUtils(); int nextFreePort = portUtils.nextFreePortForTest(); store().apply(context).put(NetworkFunctions.SERVER_IP, localIP); store().apply(context).put(NetworkFunctions.SERVER_PORT, nextFreePort); store().apply(context).put(NetworkFunctions.SERVER_WEBAPP, "/"); serviceResult = Result .success(new CoreUIService()) .ifPresent(service -> service.startup(new Config(localIP, nextFreePort, null))); }
@Override public void beforeEach(ExtensionContext context) throws Exception { logger().info("ServletContainerExtension - beforeEach"); containerIntializer.beforeEach(context.getTestMethod().get(), context); }
default int preparePort(ExtensionContext context) { final int port = freePort().get().ifAbsent(() -> { throw new RuntimeException("no free Port available..."); }).get(); store().apply(context).put(NetworkFunctions.SERVER_PORT, port); logger().info( "Port ServletContainerExtension - will be -> " + port); return port; }
@Override public void afterAll(ExtensionContext context) throws Exception { containerIntializer.afterAll(context.getTestClass().get(), context); }
@Override public void beforeAll(ExtensionContext context) throws Exception { containerIntializer.beforeAll(context.getTestClass().get(), context); }
static Function<ExtensionContext, ContainerInfo> containerInfo() { return extensionContext -> { final int port = (int) store().apply(extensionContext).get(NetworkFunctions.SERVER_PORT); final String host = store().apply(extensionContext).get(NetworkFunctions.SERVER_IP, String.class); return new ContainerInfo(port, host); }; } }
public ServletContainerExtension() { ServiceLoader<ContainerInitializer> serviceLoader = ServiceLoader.load(ContainerInitializer.class); List<ContainerInitializer> initializers = new ArrayList<>(); serviceLoader.forEach(initializers::add); if (initializers.isEmpty()) { throw new IllegalStateException("No implementation of ContainerInitializer found"); } if (initializers.size() != 1) { logger().warning("More than one implementation of ContainerInitializer found!"); } containerIntializer = initializers.get(0); logger().info("Using ContainerInitializer: " + containerIntializer.getClass().getName()); }
default Supplier<String> port() { return () -> String.valueOf(getContainerInfo().getPort()); }
default Supplier<String> ip() { return () -> getContainerInfo().getHost(); }
default Supplier<String> port() { return () -> String.valueOf(getContainerInfo().getPort()); }
default Supplier<String> ip() { return () -> getContainerInfo().getHost(); }