private List<ConfigurationPlaceholderResolver> loadAndOrderPlaceholderResolvers() { final List<ConfigurationPlaceholderResolver> configurationPlaceholderResolvers = new ArrayList<ConfigurationPlaceholderResolver>(serviceLoaderInstance.get().all(ConfigurationPlaceholderResolver.class)); Collections.sort(configurationPlaceholderResolvers, new Comparator<ConfigurationPlaceholderResolver>() { public int compare(ConfigurationPlaceholderResolver firstResolver, ConfigurationPlaceholderResolver secondResolver) { Integer a = firstResolver.precedence(); Integer b = secondResolver.precedence(); return b.compareTo(a); } }); return configurationPlaceholderResolvers; }
/** * Finds custom implementations of {@link TransactionEnabler} SPI * including the default one which will be first on the list. */ public List<TransactionEnabler> getTransactionEnablers() { List<TransactionEnabler> transactionEnablers = new ArrayList<TransactionEnabler>(); transactionEnablers.add(new AnnotationBasedTransactionEnabler()); transactionEnablers.addAll(serviceLoader.all(TransactionEnabler.class)); return transactionEnablers; } }
@Override public <X> Collection<X> all(Class<X> serviceClass) { return serviceLoaderInstance.get().all(serviceClass); } });
private List<Archive<?>> loadAuxiliaryArchives(DeploymentDescription deployment) { List<Archive<?>> archives = new ArrayList<Archive<?>>(); // load based on the Containers ClassLoader Collection<AuxiliaryArchiveAppender> archiveAppenders = serviceLoader.get().all(AuxiliaryArchiveAppender.class); for (AuxiliaryArchiveAppender archiveAppender : archiveAppenders) { Archive<?> auxiliaryArchive = archiveAppender.createAuxiliaryArchive(); if (auxiliaryArchive != null) { archives.add(auxiliaryArchive); } } return archives; }
public void registerInstantiators() { @SuppressWarnings("rawtypes") List<Instantiator> list = new ArrayList<Instantiator>(serviceLoader.get().all(Instantiator.class)); Collections.sort(list, PrecedenceComparator.getReversedOrder()); for (Instantiator<?, ?> instantiator : list) { Class<?> type = getFirstGenericParameterType(instantiator.getClass(), Instantiator.class); if (type != null) { droneRegistry.get().registerInstantiatorFor(type, instantiator); } } }
@Before public void addServiceLoaderAndLogCapturer() throws Exception { resourceProviders.add(resourceProvider); Mockito.when(serviceLoader.all(ResourceProvider.class)).thenReturn(resourceProviders); Mockito.when(resourceProvider.canProvide(Object.class)).thenReturn(true); bind(ApplicationScoped.class, ServiceLoader.class, serviceLoader); attachLogCapturer(); }
@Override protected void beforeStartManager(Manager manager) { final ServiceLoader serviceLoader = Mockito.mock(ServiceLoader.class); startContexts(manager); final ConfigurationPlaceholderResolver configurationSysPropResolver = new SystemPropertiesConfigurationPlaceholderResolver(); final ConfigurationPlaceholderResolver classpathConfigurationPlaceholderResolver = new ClasspathConfigurationPlaceholderResolver(); Mockito.when(serviceLoader.all(ConfigurationPlaceholderResolver.class)) .thenReturn(Arrays.asList(classpathConfigurationPlaceholderResolver, configurationSysPropResolver)); bind(SuiteScoped.class, ServiceLoader.class, serviceLoader); }
public void enrichInstances(List<Object> toEnrich) { Collection<TestEnricher> testEnrichers = serviceLoader.get().all(TestEnricher.class); for (Object instance : toEnrich) { enrichmentEvent.fire(new BeforeEnrichment(instance)); for (TestEnricher enricher : testEnrichers) { enricher.enrich(instance); } enrichmentEvent.fire(new AfterEnrichment(instance)); } }
public void enrichInstances(List<Object> toEnrich) { Collection<TestEnricher> testEnrichers = serviceLoader.get().all(TestEnricher.class); for (Object instance : toEnrich) { enrichmentEvent.fire(new BeforeEnrichment(instance)); for (TestEnricher enricher : testEnrichers) { enricher.enrich(instance); } enrichmentEvent.fire(new AfterEnrichment(instance)); } }
@Before public void addServiceLoader() throws Exception { resourceProvider = getResourceProvider(); injector.get().inject(resourceProvider); List<ResourceProvider> resourceProviders = Arrays.asList(resourceProvider); Mockito.when(serviceLoader.all(ResourceProvider.class)).thenReturn(resourceProviders); bind(ApplicationScoped.class, ServiceLoader.class, serviceLoader); }
@Test public void shouldTransformException() throws Exception { TestExceptionDeployThrower.shouldThrow = new IllegalStateException(); Mockito.when(serviceLoader.all(DeploymentExceptionTransformer.class)) .thenReturn(Collections.singletonList(transformer)); Mockito.when(transformer.transform(TestExceptionDeployThrower.shouldThrow)) .thenReturn(new IllegalArgumentException()); fire(new DeployDeployment( container, new Deployment(new DeploymentDescription("test", ShrinkWrap.create(JavaArchive.class)) .setExpectedException(IllegalArgumentException.class)))); }
@Before public void injectConfigurationRegistrar() { ConfigurationPlaceholderResolver configurationSysPropResolver = new SystemPropertiesConfigurationPlaceholderResolver(); Mockito.when(serviceLoader.all(ConfigurationPlaceholderResolver.class)) .thenReturn(Arrays.asList(configurationSysPropResolver)); bind(SuiteScoped.class, ServiceLoader.class, serviceLoader); registrar = injectorInst.get().inject(new ConfigurationRegistrar()); }
@Test public void shouldBeAbleToLoadAllEvenIfNonRegistered() throws Exception { ServiceRegistry registry = new ServiceRegistry(injector.get(), new LinkedHashMap<Class<?>, Set<Class<?>>>()); Collection<FakeService> services = registry.getServiceLoader().all(FakeService.class); Assert.assertNotNull(services); Assert.assertEquals( "Verify no services were loaded", 0, services.size()); }
@Test @SuppressWarnings("rawtypes") public void shouldSetDefaultDefinedProtocolAsDefault() { String protocolName = "default-protocol"; when(protocol.getDescription()).thenReturn(new ProtocolDescription(protocolName)); when(serviceLoader.all(Protocol.class)).thenReturn(Collections.singletonList((Protocol) protocol)); fire(createDescriptor(protocolName)); ProtocolDefinition protocol = verifyRegistryProtocol(protocolName); Assert.assertTrue(protocol.isDefaultProtocol()); }
@Test @SuppressWarnings("rawtypes") public void shouldBindFoundProtocolsToRegistry() throws Exception { String protocolName = "protocol"; when(protocol.getDescription()).thenReturn(new ProtocolDescription(protocolName)); when(serviceLoader.all(Protocol.class)).thenReturn(Collections.singletonList((Protocol) protocol)); fire(createDescriptor()); ProtocolDefinition protocol = verifyRegistryProtocol(protocolName); Assert.assertFalse(protocol.isDefaultProtocol()); }
@Test public void shouldSwallawExceptionIfExpectedAndDeploymentExceptionIsFieldOfThrownAndCauseOfOther() throws Exception { IllegalArgumentException recursiveException = new IllegalArgumentException(new MyDeploymentException( "My special exception", new NullPointerException())); TestExceptionDeployThrower.shouldThrow = recursiveException; Mockito.when(serviceLoader.all(DeploymentExceptionTransformer.class)) .thenReturn(Collections.singletonList(transformer)); Mockito.when(transformer.transform(TestExceptionDeployThrower.shouldThrow)).thenReturn( ((MyDeploymentException) recursiveException.getCause()).getDeploymentException()); fire(new DeployDeployment(container, new Deployment(new DeploymentDescription("test", ShrinkWrap.create(JavaArchive.class)).setExpectedException(NullPointerException.class)))); }
@Test public void shouldSwallawExceptionIfExpectedAndDeploymentExceptionIsFieldOfThrown() throws Exception { MyDeploymentException myException = new MyDeploymentException("My special exception", new NullPointerException()); TestExceptionDeployThrower.shouldThrow = myException; Mockito.when(serviceLoader.all(DeploymentExceptionTransformer.class)) .thenReturn(Collections.singletonList(transformer)); Mockito.when(transformer.transform(TestExceptionDeployThrower.shouldThrow)).thenReturn( myException.getDeploymentException()); fire(new DeployDeployment(container, new Deployment(new DeploymentDescription("test", ShrinkWrap.create(JavaArchive.class)).setExpectedException(NullPointerException.class)))); }
@Test(expected = DeploymentException.class) public void shouldRethrowExceptionIfWrongExpectedType() throws Exception { TestExceptionDeployThrower.shouldThrow = new DeploymentException("Could not handle ba", new NullPointerException()); Mockito.when(serviceLoader.all(DeploymentExceptionTransformer.class)) .thenReturn(Collections.singletonList(transformer)); fire(new DeployDeployment( container, new Deployment(new DeploymentDescription("test", ShrinkWrap.create(JavaArchive.class)) .setExpectedException(IllegalArgumentException.class)))); }
@Test public void shouldCallDeploymentTransformersWithEmptyCauseException() throws Exception { TestExceptionDeployThrower.shouldThrow = new DeploymentException("Could not handle ba", null); Mockito.when(serviceLoader.all(DeploymentExceptionTransformer.class)) .thenReturn(Collections.singletonList(transformer)); fire(new DeployDeployment( container, new Deployment(new DeploymentDescription("test", ShrinkWrap.create(JavaArchive.class)) .setExpectedException(DeploymentException.class)))); Mockito.verify(transformer, Mockito.times(1)).transform(TestExceptionDeployThrower.shouldThrow); }
public void enrich(@Observes Before event) throws Exception { Object instance = event.getTestInstance(); Method method = event.getTestMethod(); enrichmentEvent.fire(new BeforeEnrichment(instance, method)); Collection<TestEnricher> testEnrichers = serviceLoader.get().all(TestEnricher.class); for (TestEnricher enricher : testEnrichers) { enricher.enrich(instance); } enrichmentEvent.fire(new AfterEnrichment(instance, method)); } }