Exemplo n.º 1
0
  /** Tests that a service that comes from a factory that fails in provide */
  @Test
  public void testFactorServiceFailsInProvide() {
    ServiceLocator locator = LocatorHelper.getServiceLocator(RecordingErrorService.class);

    FactoryDescriptors fds =
        BuilderHelper.link(FactoryFailsInProvideService.class)
            .to(SimpleService.class.getName())
            .in(Singleton.class.getName())
            .buildFactory(Singleton.class.getName());

    ServiceLocatorUtilities.addFactoryDescriptors(locator, fds);

    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));
  }
Exemplo n.º 2
0
  /** Tests that a service that is created by a factory where the factory dispose method fails */
  @Test
  public void testFactorServiceFailsInDispose() {
    ServiceLocator locator = LocatorHelper.getServiceLocator(RecordingErrorService.class);

    FactoryDescriptors fds =
        BuilderHelper.link(FactoryFailsInDisposeService.class)
            .to(SimpleService.class.getName())
            .in(Singleton.class.getName())
            .buildFactory(Singleton.class.getName());

    ServiceLocatorUtilities.addFactoryDescriptors(locator, fds);

    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));
  }