Search in sources :

Example 1 with TestContainerFactory

use of org.glassfish.jersey.test.spi.TestContainerFactory in project dropwizard by dropwizard.

the class ResourceTestRule method apply.

@Override
public Statement apply(Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            DropwizardTestResourceConfig.CONFIGURATION_REGISTRY.put(configuration.getId(), configuration);
            try {
                test = new JerseyTest() {

                    @Override
                    protected TestContainerFactory getTestContainerFactory() {
                        return configuration.testContainerFactory;
                    }

                    @Override
                    protected DeploymentContext configureDeployment() {
                        return ServletDeploymentContext.builder(new DropwizardTestResourceConfig(configuration)).initParam(ServletProperties.JAXRS_APPLICATION_CLASS, DropwizardTestResourceConfig.class.getName()).initParam(DropwizardTestResourceConfig.CONFIGURATION_ID, configuration.getId()).build();
                    }

                    @Override
                    protected void configureClient(ClientConfig clientConfig) {
                        final JacksonJsonProvider jsonProvider = new JacksonJsonProvider();
                        jsonProvider.setMapper(configuration.mapper);
                        configuration.clientConfigurator.accept(clientConfig);
                        clientConfig.register(jsonProvider);
                    }
                };
                test.setUp();
                base.evaluate();
            } finally {
                DropwizardTestResourceConfig.CONFIGURATION_REGISTRY.remove(configuration.getId());
                test.tearDown();
            }
        }
    };
}
Also used : DeploymentContext(org.glassfish.jersey.test.DeploymentContext) ServletDeploymentContext(org.glassfish.jersey.test.ServletDeploymentContext) JerseyTest(org.glassfish.jersey.test.JerseyTest) Statement(org.junit.runners.model.Statement) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ClientConfig(org.glassfish.jersey.client.ClientConfig) TestContainerFactory(org.glassfish.jersey.test.spi.TestContainerFactory) InMemoryTestContainerFactory(org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory)

Example 2 with TestContainerFactory

use of org.glassfish.jersey.test.spi.TestContainerFactory in project helix by apache.

the class AbstractTestClass method getTestContainerFactory.

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
    return new TestContainerFactory() {

        @Override
        public TestContainer create(final URI baseUri, DeploymentContext deploymentContext) {
            return new TestContainer() {

                private HelixRestServer _helixRestServer;

                @Override
                public ClientConfig getClientConfig() {
                    return null;
                }

                @Override
                public URI getBaseUri() {
                    return baseUri;
                }

                @Override
                public void start() {
                    // Create namespace manifest map
                    List<HelixRestNamespace> namespaces = new ArrayList<>();
                    // Add test namespace
                    namespaces.add(new HelixRestNamespace(TEST_NAMESPACE, HelixRestNamespace.HelixMetadataStoreType.ZOOKEEPER, _zkAddrTestNS, false));
                    // Add default namesapce
                    namespaces.add(new HelixRestNamespace(ZK_ADDR));
                    try {
                        _helixRestServer = new HelixRestServer(namespaces, baseUri.getPort(), baseUri.getPath(), Arrays.<AuditLogger>asList(_auditLogger));
                        _helixRestServer.start();
                    } catch (Exception ex) {
                        throw new TestContainerException(ex);
                    }
                }

                @Override
                public void stop() {
                    _helixRestServer.shutdown();
                }
            };
        }
    };
}
Also used : DeploymentContext(org.glassfish.jersey.test.DeploymentContext) AuditLogger(org.apache.helix.rest.server.auditlog.AuditLogger) TestContainer(org.glassfish.jersey.test.spi.TestContainer) ArrayList(java.util.ArrayList) HelixRestNamespace(org.apache.helix.rest.common.HelixRestNamespace) URI(java.net.URI) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) IOException(java.io.IOException) TestContainerFactory(org.glassfish.jersey.test.spi.TestContainerFactory)

Example 3 with TestContainerFactory

use of org.glassfish.jersey.test.spi.TestContainerFactory in project jersey by jersey.

the class JerseyTest method getDefaultTestContainerFactory.

private static synchronized TestContainerFactory getDefaultTestContainerFactory() {
    if (defaultTestContainerFactoryClass == null) {
        final String factoryClassName = getSystemProperty(TestProperties.CONTAINER_FACTORY);
        if (factoryClassName != null) {
            LOGGER.log(Level.CONFIG, "Loading test container factory '{0}' specified in the '{1}' system property.", new Object[] { factoryClassName, TestProperties.CONTAINER_FACTORY });
            defaultTestContainerFactoryClass = loadFactoryClass(factoryClassName);
        } else {
            final TestContainerFactory[] factories = ServiceFinder.find(TestContainerFactory.class).toArray();
            if (factories.length > 0) {
                // if there is only one factory instance, just return it
                if (factories.length == 1) {
                    // cache the class for future reuse
                    defaultTestContainerFactoryClass = factories[0].getClass();
                    LOGGER.log(Level.CONFIG, "Using the single found TestContainerFactory service provider '{0}'", defaultTestContainerFactoryClass.getName());
                    return factories[0];
                }
                // if default factory is present, use it.
                for (final TestContainerFactory tcf : factories) {
                    if (TestProperties.DEFAULT_CONTAINER_FACTORY.equals(tcf.getClass().getName())) {
                        // cache the class for future reuse
                        defaultTestContainerFactoryClass = tcf.getClass();
                        LOGGER.log(Level.CONFIG, "Found multiple TestContainerFactory service providers, using the default found '{0}'", TestProperties.DEFAULT_CONTAINER_FACTORY);
                        return tcf;
                    }
                }
                // default factory is not in the list - log warning and return the first found factory instance
                // cache the class for future reuse
                defaultTestContainerFactoryClass = factories[0].getClass();
                LOGGER.log(Level.WARNING, "Found multiple TestContainerFactory service providers, using the first found '{0}'", defaultTestContainerFactoryClass.getName());
                return factories[0];
            }
            LOGGER.log(Level.CONFIG, "No TestContainerFactory configured, trying to load and instantiate the default implementation '{0}'", TestProperties.DEFAULT_CONTAINER_FACTORY);
            defaultTestContainerFactoryClass = loadFactoryClass(TestProperties.DEFAULT_CONTAINER_FACTORY);
        }
    }
    try {
        return defaultTestContainerFactoryClass.newInstance();
    } catch (final Exception ex) {
        throw new TestContainerException(String.format("Could not instantiate test container factory '%s'", defaultTestContainerFactoryClass.getName()), ex);
    }
}
Also used : TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) TestContainerFactory(org.glassfish.jersey.test.spi.TestContainerFactory)

Example 4 with TestContainerFactory

use of org.glassfish.jersey.test.spi.TestContainerFactory in project crnk-framework by crnk-project.

the class SecurityModuleIntTest method getTestContainerFactory.

protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
    final TestContainerFactory testContainerFactory = super.getTestContainerFactory();
    return new TestContainerFactory() {

        @Override
        public TestContainer create(URI baseUri, DeploymentContext deploymentContext) {
            TestContainer container = testContainerFactory.create(baseUri, deploymentContext);
            try {
                Field field = container.getClass().getDeclaredField("server");
                field.setAccessible(true);
                Server server = (Server) field.get(container);
                Handler handler = server.getHandler();
                SecurityHandler securityHandler = identityManager.getSecurityHandler();
                if (securityHandler.getHandler() == null) {
                    securityHandler.setHandler(handler);
                }
                server.setHandler(securityHandler);
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
            return container;
        }
    };
}
Also used : DeploymentContext(org.glassfish.jersey.test.DeploymentContext) Field(java.lang.reflect.Field) SecurityHandler(org.eclipse.jetty.security.SecurityHandler) TestContainer(org.glassfish.jersey.test.spi.TestContainer) Server(org.eclipse.jetty.server.Server) Handler(org.eclipse.jetty.server.Handler) SecurityHandler(org.eclipse.jetty.security.SecurityHandler) URI(java.net.URI) UnauthorizedException(io.crnk.core.exception.UnauthorizedException) IOException(java.io.IOException) TestContainerException(org.glassfish.jersey.test.spi.TestContainerException) ForbiddenException(io.crnk.core.exception.ForbiddenException) TestContainerFactory(org.glassfish.jersey.test.spi.TestContainerFactory)

Aggregations

TestContainerFactory (org.glassfish.jersey.test.spi.TestContainerFactory)4 DeploymentContext (org.glassfish.jersey.test.DeploymentContext)3 TestContainerException (org.glassfish.jersey.test.spi.TestContainerException)3 IOException (java.io.IOException)2 URI (java.net.URI)2 TestContainer (org.glassfish.jersey.test.spi.TestContainer)2 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)1 ForbiddenException (io.crnk.core.exception.ForbiddenException)1 UnauthorizedException (io.crnk.core.exception.UnauthorizedException)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 HelixRestNamespace (org.apache.helix.rest.common.HelixRestNamespace)1 AuditLogger (org.apache.helix.rest.server.auditlog.AuditLogger)1 SecurityHandler (org.eclipse.jetty.security.SecurityHandler)1 Handler (org.eclipse.jetty.server.Handler)1 Server (org.eclipse.jetty.server.Server)1 ClientConfig (org.glassfish.jersey.client.ClientConfig)1 JerseyTest (org.glassfish.jersey.test.JerseyTest)1 ServletDeploymentContext (org.glassfish.jersey.test.ServletDeploymentContext)1 InMemoryTestContainerFactory (org.glassfish.jersey.test.inmemory.InMemoryTestContainerFactory)1