/** 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)); }
@Test public void verifyPerThreadScoped() { ServiceHandle<KryoFactory> serviceHandle = locator.getServiceHandle(KryoFactory.class); ActiveDescriptor<KryoFactory> activeDescriptor = serviceHandle.getActiveDescriptor(); Class<? extends Annotation> scope = activeDescriptor.getScopeAnnotation(); assertThat(scope).isEqualTo(Singleton.class); }
public Object get() { if (serviceHandle == null) { return null; } Object result = serviceHandle.getService(); return result; }