/** * An extending class must implement the {@link #configure()} method to * provide an application descriptor. * * @throws TestContainerException if the default test container factory * cannot be obtained, or the application descriptor is not * supported by the test container factory. */ public SpringContextJerseyTest() throws TestContainerException { ResourceConfig config = getResourceConfig(configure()); config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER)); if (isLogRecordingEnabled()) { registerLogHandler(); } this.application = new ApplicationHandler(config); this.tc = getContainer(application, getTestContainerFactory()); if (isLogRecordingEnabled()) { loggedStartupRecords.addAll(loggedRuntimeRecords); loggedRuntimeRecords.clear(); unregisterLogHandler(); } }
/** * Construct a new instance with a test container factory. * <p/> * An extending class must implement the {@link #configure()} method to * provide an application descriptor. * * @param testContainerFactory the test container factory to use for testing. * @throws TestContainerException if the application descriptor is not * supported by the test container factory. */ public SpringContextJerseyTest(TestContainerFactory testContainerFactory) { setTestContainerFactory(testContainerFactory); ResourceConfig config = getResourceConfig(configure()); config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER)); if (isLogRecordingEnabled()) { registerLogHandler(); } this.application = new ApplicationHandler(config); this.tc = getContainer(application, testContainerFactory); if (isLogRecordingEnabled()) { loggedStartupRecords.addAll(loggedRuntimeRecords); loggedRuntimeRecords.clear(); unregisterLogHandler(); } }
/** * Test the application binder. */ @Test public void testBinder() { ResourceConfig config = new ResourceConfig(); config.register(TestFeature.class); // Make sure it's registered Assert.assertTrue(config.isRegistered(TestFeature.class)); // Create a fake application. ApplicationHandler handler = new ApplicationHandler(config); ServiceRegistry serviceRegistry = handler .getServiceLocator().getService(ServiceRegistry.class); Assert.assertNotNull(serviceRegistry); // Make sure it's reading from the same place. Dialect d = new MetadataSources(serviceRegistry) .buildMetadata().getDatabase().getDialect(); Assert.assertTrue(d instanceof H2Dialect); // Make sure it's a singleton... ServiceRegistry serviceRegistry2 = handler .getServiceLocator().getService(ServiceRegistry.class); Assert.assertSame(serviceRegistry, serviceRegistry2); }
public HttpServer(String serverIdentifier, HttpConfiguration http, Timer timer, ApplicationHandler applicationHandler) { PooledByteBufAllocator allocator = new PooledByteBufAllocator(http.isDirectMemoryBacked()); this.serverIdentifier = serverIdentifier; this.host = http.getHost(); this.port = http.getPort(); this.requestProcessingExecutor = Executors.newFixedThreadPool(http.getNumRequestProcessingThreads(), Threads.newNamedThreadFactory(serverIdentifier + "-requests-%d")); this.bossEventLoopGroup = new NioEventLoopGroup(com.aerofs.baseline.http.Constants.DEFAULT_NUM_BOSS_THREADS, Threads.newNamedThreadFactory(serverIdentifier + "-nio-boss-%d")); this.workEventLoopGroup = new NioEventLoopGroup(http.getNumNetworkThreads(), Threads.newNamedThreadFactory(serverIdentifier + "-nio-work-%d")); this.bootstrap = new ServerBootstrap(); this.bootstrap .group(bossEventLoopGroup, workEventLoopGroup) .channel(NioServerSocketChannel.class) .childHandler(new AcceptedChannelInitializer(http, applicationHandler, URI.create(String.format("http://%s:%s/", host, port)), requestProcessingExecutor, timer)) .option(ALLOCATOR, allocator) .option(SO_BACKLOG, http.getMaxAcceptQueueSize()) .childOption(AUTO_READ, false) .childOption(ALLOCATOR, allocator); }
/** * Constructor * * @param baseUri * @param context */ public CdiAwareInMemoryTestContainer(final URI baseUri, final DeploymentContext context) { this.baseUri = UriBuilder.fromUri(baseUri).path(context.getContextPath()).build(); if (CdiAwareInMemoryTestContainer.LOGGER.isLoggable(Level.INFO)) { CdiAwareInMemoryTestContainer.LOGGER .info("Creating InMemoryTestContainer configured at the base URI " + this.baseUri); } // TODO: Hack is here... // HandlerWrapper cdiHandlerWrapper = new CdiAwareHandlerWrapper(); // cdiHandlerWrapper.setHandler(this.server.getHandler()); // this.server.setHandler(cdiHandlerWrapper); this.appHandler = new ApplicationHandler(context.getResourceConfig()); }
@BeforeMethod public void setup() { applicationMapping = mock(ApplicationMapping.class); request = mock(FullHttpRequest.class); response = mock(FullHttpResponse.class); when(request.getMethod()).thenReturn(HttpMethod.GET); when(request.getUri()).thenReturn("/test"); when(request.headers()).thenReturn(new DefaultHttpHeaders()); when(request.content()).thenReturn(mock(ByteBuf.class)); when(request.getProtocolVersion()).thenReturn(HttpVersion.HTTP_1_1); when(response.getStatus()).thenReturn(HttpResponseStatus.PROCESSING); application = mock(Application.class); context = mock(MessageContext.class); router = spy(new Router(applicationMapping)); doReturn(new ApplicationHandler()).when(router).getApplicationHandler(application); when(application.getPath()).thenReturn(URI.create("/app")); when(context.getRequest()).thenReturn(request); when(context.getResponse()).thenReturn(response); when(context.getApplication()).thenReturn(application); when(context.getBaseUri()).thenReturn(URI.create("http://localhost:8080")); when(applicationMapping.resolve(request)).thenReturn(application); }
/** * Creates an instance of {@link Client}. * <p/> * Checks whether TestContainer provides ClientConfig instance and * if not, empty new {@link org.glassfish.jersey.client.ClientConfig} instance * will be used to create new client instance. * <p/> * This method is called exactly once when JerseyTest is created. * * @param tc instance of {@link TestContainer} * @param applicationHandler instance of {@link ApplicationHandler} * @return A Client instance. */ protected Client getClient(TestContainer tc, ApplicationHandler applicationHandler) { ClientConfig cc = tc.getClientConfig(); if (cc == null) { cc = new ClientConfig(); } //check if logging is required if (isEnabled(TestProperties.LOG_TRAFFIC)) { cc.register(new LoggingFilter(LOGGER, isEnabled(TestProperties.DUMP_ENTITY))); } configureClient(cc); return ClientBuilder.newClient(cc); }
/** * Private constructor for a LambdaContainer. Sets the application object, sets the ApplicationHandler, * and initializes the application using the <code>onStartup</code> method. * @param requestReader A request reader instance * @param responseWriter A response writer instance * @param securityContextWriter A security context writer object * @param exceptionHandler An exception handler * @param jaxRsApplication The JaxRs application */ public JerseyLambdaContainerHandler(RequestReader<RequestType, ContainerRequest> requestReader, ResponseWriter<JerseyResponseWriter, ResponseType> responseWriter, SecurityContextWriter<RequestType> securityContextWriter, ExceptionHandler<ResponseType> exceptionHandler, Application jaxRsApplication) { super(requestReader, responseWriter, securityContextWriter, exceptionHandler); this.jaxRsApplication = jaxRsApplication; this.applicationHandler = new ApplicationHandler(jaxRsApplication); applicationHandler.onStartup(this); }
/** * Shuts down and restarts the application handler in the current container. The <code>ApplicationHandler</code> * object is re-initialized with the <code>Application</code> object initially set in the <code>LambdaContainer.getInstance()</code> * call. */ public void reload() { applicationHandler.onShutdown(this); this.applicationHandler = new ApplicationHandler(jaxRsApplication); applicationHandler.onReload(this); applicationHandler.onStartup(this); }
/** * Restarts the application handler and configures a different <code>Application</code> object. The new application * resets the one currently configured in the container. * @param resourceConfig An initialized Application */ public void reload(ResourceConfig resourceConfig) { applicationHandler.onShutdown(this); this.jaxRsApplication = resourceConfig; this.applicationHandler = new ApplicationHandler(resourceConfig); applicationHandler.onReload(this); applicationHandler.onStartup(this); }
@Test public void reload_ConfigGiven_ShouldReloadNewAppHandler() { ResourceConfig config = new ApplicationHandler().getConfiguration(); ApplicationHandler newAppHandler = mock(ApplicationHandler.class); doReturn(newAppHandler).when(container).createNewApplicationHandler(any()); container.reload(config); verify(newAppHandler, times(1)).onReload(container); }
@Test public void reload_ConfigGiven_ShouldStartNewAppHandler() { ResourceConfig config = new ApplicationHandler().getConfiguration(); ApplicationHandler newAppHandler = mock(ApplicationHandler.class); doReturn(newAppHandler).when(container).createNewApplicationHandler(any()); container.reload(config); verify(newAppHandler, times(1)).onStartup(container); }
@Test public void reload_ConfigGiven_ShouldResetAppHandler() { ResourceConfig config = new ApplicationHandler().getConfiguration(); ApplicationHandler newAppHandler = mock(ApplicationHandler.class); doReturn(newAppHandler).when(container).createNewApplicationHandler(any()); container.reload(config); assertSame(newAppHandler, container.getApplicationHandler()); }
protected <T> T getService(Class<T> clazz) { return new ApplicationHandler(configure().register(new GenericBinder<T>() { public Class<T> getType() { return clazz; } })).getServiceLocator().getService(clazz); }
/** * Construct a new instance with an application descriptor that defines * how the test container is configured. * * @param jaxrsApplication an application describing how to configure the * test container. * @throws TestContainerException if the default test container factory * cannot be obtained, or the application descriptor is not * supported by the test container factory. */ public SpringContextJerseyTest(Application jaxrsApplication) throws TestContainerException { ResourceConfig config = getResourceConfig(jaxrsApplication); config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER)); if (isLogRecordingEnabled()) { registerLogHandler(); } this.application = new ApplicationHandler(config); this.tc = getContainer(application, getTestContainerFactory()); if (isLogRecordingEnabled()) { loggedStartupRecords.addAll(loggedRuntimeRecords); loggedRuntimeRecords.clear(); unregisterLogHandler(); } }
/** * Construct a new instance with an {@link Application} class. * * @param jaxrsApplicationClass an application describing how to configure the * test container. * @throws TestContainerException if the default test container factory * cannot be obtained, or the application descriptor is not * supported by the test container factory. */ public SpringContextJerseyTest(Class<? extends Application> jaxrsApplicationClass) throws TestContainerException { ResourceConfig config = ResourceConfig.forApplicationClass(jaxrsApplicationClass); config.register(new ServiceFinderBinder<TestContainerFactory>(TestContainerFactory.class, null, RuntimeType.SERVER)); if (isLogRecordingEnabled()) { registerLogHandler(); } this.application = new ApplicationHandler(config); this.tc = getContainer(application, getTestContainerFactory()); if (isLogRecordingEnabled()) { loggedStartupRecords.addAll(loggedRuntimeRecords); loggedRuntimeRecords.clear(); unregisterLogHandler(); } }
private TestContainer getContainer(ApplicationHandler application, TestContainerFactory tcf) { if (application == null) { throw new IllegalArgumentException("The application cannot be null"); } return tcf.create(getBaseUri(), application); }
@Override public void onAfterShutdown(Object obj) { if (container instanceof NettyRestHandlerContainer) { NettyRestHandlerContainer restHandlerContainer = ((NettyRestHandlerContainer) container); ApplicationHandler applicationHandler = restHandlerContainer.getApplicationHandler(); ContainerLifecycleListener lifecycleListener = ConfigHelper.getContainerLifecycleListener(applicationHandler); lifecycleListener.onShutdown(container); } }
@Override public void onAfterStart(Object obj) { if (container instanceof NettyRestHandlerContainer) { NettyRestHandlerContainer restHandlerContainer = ((NettyRestHandlerContainer) container); ApplicationHandler applicationHandler = restHandlerContainer.getApplicationHandler(); ContainerLifecycleListener lifecycleListener = ConfigHelper.getContainerLifecycleListener(applicationHandler); lifecycleListener.onStartup(container); } }
@Override public <T> T createContainer(Class<T> type, ApplicationHandler application) throws ProcessingException { if (type != NettyRestHandlerContainer.class && (type == null || !ChannelHandler.class.isAssignableFrom(type))) { return null; } return type.cast(new NettyRestHandlerContainer(application)); }
/** * Setup the application handler for this test. */ @Before public void setup() { ResourceConfig config = new ResourceConfig(); config.register(TestFeature.class); handler = new ApplicationHandler(config); locator = handler.getServiceLocator(); }
@Override public TestContainer create(URI baseUri, ApplicationHandler application) throws IllegalArgumentException { URI uri = UriBuilder.fromUri(baseUri).port(baseUri.getPort() + getCount()).build(); System.out.println("Uri: " + uri); System.out.println("App: " + application.getConfiguration().getApplication().getClass().getName()); return new MyTestContainer(uri, application); }
public void init(String url) throws Exception { URI uri = URI.create(url); ApplicationHandler h = new ApplicationHandler(TestApplication.class); WebContainerFactory factory = new WebContainerFactory(); factory.enableEncrementPort(false); container = factory.create(uri,h); container.start(); }
@Override public void onShutdown(final Container container) { final ApplicationHandler handler = container.getApplicationHandler(); final InjectionManager injectionManager = handler.getInjectionManager(); // Call @PreDestroy method on Application. injectionManager.preDestroy(getWrappedApplication(handler.getConfiguration())); // Shutdown ServiceLocator. injectionManager.shutdown(); }
public AcceptedChannelInitializer(HttpConfiguration http, ApplicationHandler applicationHandler, URI baseUri, Executor applicationExecutor, io.netty.util.Timer timer) { this.http = http; this.applicationHandler = applicationHandler; this.baseUri = baseUri; this.applicationExecutor = applicationExecutor; this.timer = timer; }