예제 #1
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);
  }
예제 #2
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);
  }
예제 #3
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());
  }