示例#1
0
  /**
   * Tests that a service that fails in the constructor has the error passed to the error service
   */
  @Test
  public void testServiceFailsInConstructor() {
    ServiceLocator locator =
        LocatorHelper.getServiceLocator(
            RecordingErrorService.class, ServiceFailsInConstructor.class);

    ActiveDescriptor<?> serviceDescriptor =
        locator.getBestDescriptor(
            BuilderHelper.createContractFilter(ServiceFailsInConstructor.class.getName()));
    Assert.assertNotNull(serviceDescriptor);

    try {
      locator.getService(ServiceFailsInConstructor.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));
  }
示例#2
0
  /** 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));
  }
示例#3
0
  /** 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);
  }
示例#4
0
  /**
   * 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);
  }
示例#5
0
  /**
   * Tests that a service that fails but tells HK2 to NOT report the failure to the error handler
   * service
   */
  @Test
  public void testSilentFailureInPostConstruct() {
    ServiceLocator locator =
        LocatorHelper.getServiceLocator(
            RecordingErrorService.class, ServiceDirectsNoErrorService.class);

    ActiveDescriptor<?> serviceDescriptor =
        locator.getBestDescriptor(
            BuilderHelper.createContractFilter(ServiceDirectsNoErrorService.class.getName()));
    Assert.assertNotNull(serviceDescriptor);

    try {
      locator.getService(ServiceDirectsNoErrorService.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(0, errors.size());
  }
示例#6
0
  /** 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());
  }