Search in sources :

Example 1 with TestContainerException

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

the class JerseyTest method loadFactoryClass.

private static Class<? extends TestContainerFactory> loadFactoryClass(final String factoryClassName) {
    Class<? extends TestContainerFactory> factoryClass;
    final Class<Object> loadedClass = AccessController.doPrivileged(ReflectionHelper.classForNamePA(factoryClassName, null));
    if (loadedClass == null) {
        throw new TestContainerException(String.format("Test container factory class '%s' cannot be loaded", factoryClassName));
    }
    try {
        return loadedClass.asSubclass(TestContainerFactory.class);
    } catch (final ClassCastException ex) {
        throw new TestContainerException(String.format("Class '%s' does not implement TestContainerFactory SPI.", factoryClassName), ex);
    }
}
Also used : TestContainerException(org.glassfish.jersey.test.spi.TestContainerException)

Example 2 with TestContainerException

use of org.glassfish.jersey.test.spi.TestContainerException 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)

Aggregations

TestContainerException (org.glassfish.jersey.test.spi.TestContainerException)2 TestContainerFactory (org.glassfish.jersey.test.spi.TestContainerFactory)1