private TestContainer getContainer(ApplicationHandler application, TestContainerFactory tcf) { if (application == null) { throw new IllegalArgumentException("The application cannot be null"); } return tcf.create(getBaseUri(), application); }
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); } }
/** * Returns an instance of {@link TestContainerFactory} class. This instance can be set by a * constructor ({@link #JerseyTest(org.glassfish.jersey.test.spi.TestContainerFactory)}, as an * application {@link Providers Provider} or the {@link TestContainerFactory} class can be set as * a {@value org.glassfish.jersey.test.TestProperties#CONTAINER_FACTORY} property. * * @return an instance of {@link TestContainerFactory} class. * @throws TestContainerException if the initialization of {@link TestContainerFactory} instance * is not successful. */ protected TestContainerFactory getTestContainerFactory() throws TestContainerException { if (testContainerFactory == null) { if (testContainerFactoryClass == null) { final String tcfClassName = getProperty(TestProperties.CONTAINER_FACTORY); if ((tcfClassName == null)) { Set<TestContainerFactory> testContainerFactories = Providers.getProviders(application.getServiceLocator(), TestContainerFactory.class); if (testContainerFactories.size() >= 1) { // if default factory is present, use it. for (TestContainerFactory tcFactory : testContainerFactories) { if (tcFactory.getClass().getName().equals(TestProperties.DEFAULT_CONTAINER_FACTORY)) { LOGGER.log( Level.CONFIG, "Found multiple TestContainerFactory implementations, using default {0}", tcFactory.getClass().getName()); testContainerFactoryClass = tcFactory.getClass(); // is this necessary? return tcFactory; } } if (testContainerFactories.size() != 1) { LOGGER.log( Level.WARNING, "Found multiple TestContainerFactory implementations, using {0}", testContainerFactories.iterator().next().getClass().getName()); } testContainerFactoryClass = testContainerFactories.iterator().next().getClass(); return testContainerFactories.iterator().next(); } } else { final Class<Object> tfClass = AccessController.doPrivileged(ReflectionHelper.classForNamePA(tcfClassName, null)); if (tfClass == null) { throw new TestContainerException( "The default test container factory class name, " + tcfClassName + ", cannot be loaded"); } try { testContainerFactoryClass = tfClass.asSubclass(TestContainerFactory.class); } catch (ClassCastException ex) { throw new TestContainerException( "The default test container factory class, " + tcfClassName + ", is not an instance of TestContainerFactory", ex); } } } try { return testContainerFactoryClass.newInstance(); } catch (Exception ex) { throw new TestContainerException( "The default test container factory, " + testContainerFactoryClass + ", could not be instantiated", ex); } } return testContainerFactory; }