/** Tests that adding the whole annotation adds the class and string */ @Test public void testDescWithAnnotationGivenDirectly() { AbstractActiveDescriptor<?> desc = BuilderHelper.activeLink(ServiceA.class) .in(ServiceLocatorUtilities.getSingletonAnnotation()) .build(); Assert.assertEquals( desc.getScopeAsAnnotation(), ServiceLocatorUtilities.getSingletonAnnotation()); Assert.assertEquals(desc.getScopeAnnotation(), Singleton.class); Assert.assertEquals(desc.getScope(), Singleton.class.getName()); }
/** Tests that a third-party service that fails in dispose */ @Test public void testFailingInDisposeThirdPartyService() { ServiceLocator locator = LocatorHelper.getServiceLocator(RecordingErrorService.class); AlwaysFailInDisposeActiveDescriptor thirdPartyDescriptor = new AlwaysFailInDisposeActiveDescriptor(); ServiceLocatorUtilities.addOneDescriptor(locator, thirdPartyDescriptor); ActiveDescriptor<?> serviceDescriptor = locator.getBestDescriptor( BuilderHelper.createContractFilter(SimpleService.class.getName())); Assert.assertNotNull(serviceDescriptor); ServiceHandle<SimpleService> handle = locator.getServiceHandle(SimpleService.class); Assert.assertNotNull(handle); Assert.assertNotNull(handle.getService()); handle.destroy(); List<ErrorInformation> errors = locator.getService(RecordingErrorService.class).getAndClearErrors(); Assert.assertEquals(1, errors.size()); ErrorInformation ei = errors.get(0); Assert.assertEquals(ErrorType.SERVICE_DESTRUCTION_FAILURE, ei.getErrorType()); Assert.assertEquals(serviceDescriptor, ei.getDescriptor()); Assert.assertNull(ei.getInjectee()); Throwable associatedException = ei.getAssociatedException(); Assert.assertTrue(associatedException.getMessage().contains(ERROR_STRING)); }
/** Tests that a third-party service that fails in create */ @Test public void testFailingThirdPartyService() { ServiceLocator locator = LocatorHelper.getServiceLocator(RecordingErrorService.class); AlwaysFailActiveDescriptor thirdPartyDescriptor = new AlwaysFailActiveDescriptor(); ServiceLocatorUtilities.addOneDescriptor(locator, thirdPartyDescriptor); ActiveDescriptor<?> serviceDescriptor = locator.getBestDescriptor( BuilderHelper.createContractFilter(SimpleService.class.getName())); Assert.assertNotNull(serviceDescriptor); try { locator.getService(SimpleService.class); Assert.fail("Should have failed"); } catch (MultiException me) { Assert.assertTrue(me.getMessage().contains(ERROR_STRING)); } List<ErrorInformation> errors = locator.getService(RecordingErrorService.class).getAndClearErrors(); Assert.assertEquals(1, errors.size()); ErrorInformation ei = errors.get(0); Assert.assertEquals(ErrorType.SERVICE_CREATION_FAILURE, ei.getErrorType()); Assert.assertEquals(serviceDescriptor, ei.getDescriptor()); Assert.assertNull(ei.getInjectee()); Throwable associatedException = ei.getAssociatedException(); Assert.assertTrue(associatedException.getMessage().contains(ERROR_STRING)); }
public void release() { if (domDescriptor != null) { // children added via createProxy are not registered in serviceLocator ServiceLocatorUtilities.removeOneDescriptor(getHabitat(), domDescriptor, true); } listeners.clear(); }
/** Ensures we can add a scope to a service with no scope at all */ @Test public void testGiveClassWithNoScopeAScope() { ServiceLocator locator = LocatorHelper.create(); ServiceLocatorUtilities.enableLookupExceptions(locator); Descriptor desc = BuilderHelper.activeLink(NoScopeService.class) .to(NoScopeService.class) .in(ServiceLocatorUtilities.getSingletonAnnotation()) .build(); ServiceLocatorUtilities.addOneDescriptor(locator, desc); NoScopeService one = locator.getService(NoScopeService.class); NoScopeService two = locator.getService(NoScopeService.class); Assert.assertNotNull(one); Assert.assertEquals(one, two); }
/** * Ensures that you can change from one hard-coded scope on the descriptor to a different scope */ @Test public void testSwitchFromExplicitScopeToGhostedScope() { ServiceLocator locator = LocatorHelper.create(); ServiceLocatorUtilities.enableLookupExceptions(locator); ServiceLocatorUtilities.addClasses(locator, GhostedContext.class); Descriptor desc = BuilderHelper.activeLink(SingletonScopedService.class) .to(SingletonScopedService.class) .in(new GhostedScopeImpl(0)) .build(); ServiceLocatorUtilities.addOneDescriptor(locator, desc); SingletonScopedService one = locator.getService(SingletonScopedService.class); SingletonScopedService two = locator.getService(SingletonScopedService.class); Assert.assertNotNull(one); Assert.assertNotSame(one, two); }
/** * Create an unnamed {@link ServiceLocator}. * * @param binders custom the HK2 {@link Binder binders}. * @return a service locator with all the bindings. */ public static ServiceLocator createLocator(Binder... binders) { ServiceLocator locator = factory.create(null, null, generator); ServiceLocatorUtilities.enablePerThreadScope(locator); for (Binder binder : binders) { bind(locator, binder); } return locator; }
private ActiveDescriptor<Dom> addWithAlias( ServiceLocator locator, AbstractActiveDescriptor<?> descriptor, Class<?> contract, String name) { ActiveDescriptor<Dom> added = ServiceLocatorUtilities.findOneDescriptor(locator, descriptor); if (added == null) { if (ConfigBean.class.isAssignableFrom(this.getClass())) { if (!descriptor.getAdvertisedContracts().contains(ConfigBean.class.getName())) { descriptor.addContractType(ConfigBean.class); } } added = ServiceLocatorUtilities.addOneDescriptor(locator, descriptor); } AliasDescriptor<Dom> alias = new AliasDescriptor<Dom>(locator, added, contract.getName(), name); ServiceLocatorUtilities.addOneDescriptor(locator, alias); return added; }
/** Tests that we can change the value of a field in a scope with a ghost added annotation */ @Test public void testModifyExistingScopeWithDifferentValue() { ServiceLocator locator = LocatorHelper.create(); ServiceLocatorUtilities.addClasses(locator, GhostedContext.class); Descriptor desc = BuilderHelper.activeLink(GhostedServiceWithValue.class) .to(GhostedServiceWithValue.class) .in(new GhostedScopeImpl(0)) .build(); ServiceLocatorUtilities.addOneDescriptor(locator, desc); GhostedServiceWithValue ghosted = locator.getService(GhostedServiceWithValue.class); Assert.assertNotNull(ghosted); ActiveDescriptor<?> gDesck = ghosted.getDescriptor(); Annotation anno = gDesck.getScopeAsAnnotation(); Assert.assertNotNull(anno); GhostedScope gs = (GhostedScope) anno; Assert.assertEquals(0, gs.value()); }
/** * Enables the Configuration subsystem of HK2. This call is idempotent * * @param locator The non-null service locator in which to enable the configuration subsystem */ public static void enableConfigurationSystem(ServiceLocator locator) { ServiceHandle<ConfiguredByContext> alreadyThere = locator.getServiceHandle(ConfiguredByContext.class); if (alreadyThere != null) { // The assumption is that if this service is there then this is already on, don't do it again return; } ManagerUtilities.enableConfigurationHub(locator); ServiceLocatorUtilities.addClasses(locator, true, ConfiguredValidator.class); ServiceLocatorUtilities.addClasses( locator, true, ConfiguredByContext.class, ConfigurationValidationService.class, ConfiguredByInjectionResolver.class, ConfigurationListener.class, ChildInjectResolverImpl.class); // Creates demand, starts the thing off locator.getService(ConfigurationListener.class); }
/** Gets the {@link ConfigInjector} instance that can be used to inject this DOM to a bean. */ public ConfigInjector getInjector() { return ServiceLocatorUtilities.getService(habitat, model.injector); }