Search in sources :

Example 6 with IterableProvider

use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.

the class ProvidesTest method testInstanceFieldProvidesPerLookupLifecycle.

/**
 * Verifies that the {@link PostConstruct} and {@link PreDestroy} methods of a
 * service are invoked at the expected times when the service was registered
 * from an instance {@link Provides} field and the service is per-lookup.
 */
@Test
public void testInstanceFieldProvidesPerLookupLifecycle() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycle.class);
    ProvidedPerLookupInstanceFieldWithLifecycle serviceWithoutHandle = locator.getService(ProvidedPerLookupInstanceFieldWithLifecycle.class);
    assertNotNull(serviceWithoutHandle);
    assertFalse(serviceWithoutHandle.wasStarted());
    assertFalse(serviceWithoutHandle.wasStopped());
    assertNotSame(serviceWithoutHandle, locator.getService(ProvidedPerLookupInstanceFieldWithLifecycle.class));
    IterableProvider<ProvidedPerLookupInstanceFieldWithLifecycle> serviceProvider = locator.getService(new TypeLiteral<IterableProvider<ProvidedPerLookupInstanceFieldWithLifecycle>>() {
    }.getType());
    ServiceHandle<ProvidedPerLookupInstanceFieldWithLifecycle> serviceHandle = serviceProvider.getHandle();
    ProvidedPerLookupInstanceFieldWithLifecycle serviceWitHandle = serviceHandle.getService();
    assertNotNull(serviceWitHandle);
    assertFalse(serviceWitHandle.wasStarted());
    assertFalse(serviceWitHandle.wasStopped());
    serviceHandle.close();
    assertFalse(serviceWitHandle.wasStopped());
    locator.shutdown();
    assertFalse(serviceWithoutHandle.wasStopped());
}
Also used : ServiceLocatorUtilities.createAndPopulateServiceLocator(org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) TypeLiteral(org.glassfish.hk2.api.TypeLiteral) Test(org.junit.Test)

Example 7 with IterableProvider

use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.

the class ProvidesTest method testProvidesGenericContractTypeParameters.

/**
 * Verifies that generic type parameters are carried through a {@link
 * Provides} chain where a {@link Contract} interface is involved.
 */
@Test
public void testProvidesGenericContractTypeParameters() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesGenericContracts.class);
    IterableProvider<BoxFromGenericProvidesContract<String>> boxes = locator.getService(new TypeLiteral<IterableProvider<BoxFromGenericProvidesContract<String>>>() {
    }.getType());
    Set<String> expected = new HashSet<>(Arrays.asList("staticField", "instanceField", "staticMethod", "instanceMethod"));
    assertEquals(expected.size(), boxes.getSize());
    assertEquals(expected, StreamSupport.stream(boxes.spliterator(), false).map(box -> box.value).collect(toSet()));
}
Also used : ServiceLocatorUtilities.createAndPopulateServiceLocator(org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) TypeLiteral(org.glassfish.hk2.api.TypeLiteral) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with IterableProvider

use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.

the class ProvidesTest method testStaticMethodProvidesPerLookupLifecycle.

/**
 * Verifies that the {@link PostConstruct} and {@link PreDestroy} methods of a
 * service are invoked at the expected times when the service was registered
 * from a static {@link Provides} method and the service is per-lookup.
 */
@Test
public void testStaticMethodProvidesPerLookupLifecycle() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycle.class);
    ProvidedPerLookupStaticMethodWithLifecycle serviceWithoutHandle = locator.getService(ProvidedPerLookupStaticMethodWithLifecycle.class);
    assertNotNull(serviceWithoutHandle);
    assertFalse(serviceWithoutHandle.wasStarted());
    assertFalse(serviceWithoutHandle.wasStopped());
    assertNotSame(serviceWithoutHandle, locator.getService(ProvidedPerLookupStaticMethodWithLifecycle.class));
    IterableProvider<ProvidedPerLookupStaticMethodWithLifecycle> serviceProvider = locator.getService(new TypeLiteral<IterableProvider<ProvidedPerLookupStaticMethodWithLifecycle>>() {
    }.getType());
    ServiceHandle<ProvidedPerLookupStaticMethodWithLifecycle> serviceHandle = serviceProvider.getHandle();
    ProvidedPerLookupStaticMethodWithLifecycle serviceWitHandle = serviceHandle.getService();
    assertNotNull(serviceWitHandle);
    assertFalse(serviceWitHandle.wasStarted());
    assertFalse(serviceWitHandle.wasStopped());
    serviceHandle.close();
    assertTrue(serviceWitHandle.wasStopped());
    locator.shutdown();
    assertFalse(serviceWithoutHandle.wasStopped());
}
Also used : ServiceLocatorUtilities.createAndPopulateServiceLocator(org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) TypeLiteral(org.glassfish.hk2.api.TypeLiteral) Test(org.junit.Test)

Example 9 with IterableProvider

use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.

the class DefaultUnqualifiedTest method testIterableProviderReturnsAllEvenIfUnqualified.

/**
 * Tests that injection of IterableProvider does NOT have the
 * default Unqualified applied to it
 */
// @org.junit.Ignore
@Test
public void testIterableProviderReturnsAllEvenIfUnqualified() {
    ServiceLocator locator = LocatorHelper.getServiceLocator(BoardGameImpl.class, DollImpl.class, TrainImpl.class, ChildsRoom.class);
    locator.setDefaultUnqualified(new UnqualifiedImpl());
    ChildsRoom childsRoom = locator.getService(ChildsRoom.class);
    IterableProvider<Toy> allToys = childsRoom.getAllToys();
    Assert.assertEquals(3, allToys.getSize());
    int lcv = 0;
    for (Toy toy : allToys) {
        switch(lcv) {
            case 0:
                Assert.assertEquals(BoardGameImpl.class, toy.getClass());
                break;
            case 1:
                Assert.assertEquals(TrainImpl.class, toy.getClass());
                break;
            case 2:
                Assert.assertEquals(DollImpl.class, toy.getClass());
                break;
            default:
                Assert.fail("Should not get here");
        }
        lcv++;
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) UnqualifiedImpl(org.glassfish.hk2.utilities.UnqualifiedImpl) Test(org.junit.Test)

Example 10 with IterableProvider

use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.

the class ProvidesTest method testProvidesLifecycleFromStaticMethod.

/**
 * Verifies the lifecycle of a service obtained from a static method that is
 * annotated with {@link Provides}.
 */
@Test
public void testProvidesLifecycleFromStaticMethod() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycleFactory.class, ProvidesLifecycleDependency.class);
    IterableProvider<ProvidesLifecycleFromStaticMethod> provider = locator.getService(new TypeLiteral<IterableProvider<ProvidesLifecycleFromStaticMethod>>() {
    }.getType());
    ServiceHandle<ProvidesLifecycleFromStaticMethod> handle = provider.getHandle();
    ProvidesLifecycleFromStaticMethod root = handle.getService();
    assertFalse(root.wasStarted());
    assertFalse(root.factory.wasStarted());
    assertTrue(root.dependency.wasStarted());
    assertFalse(root.wasStopped());
    assertFalse(root.factory.wasStopped());
    assertFalse(root.dependency.wasStopped());
    handle.close();
    assertTrue(root.wasStopped());
    assertFalse(root.factory.wasStopped());
    assertTrue(root.dependency.wasStopped());
}
Also used : ServiceLocatorUtilities.createAndPopulateServiceLocator(org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) TypeLiteral(org.glassfish.hk2.api.TypeLiteral) Test(org.junit.Test)

Aggregations

ServiceLocator (org.glassfish.hk2.api.ServiceLocator)16 Test (org.junit.Test)16 TypeLiteral (org.glassfish.hk2.api.TypeLiteral)15 ServiceLocatorUtilities.createAndPopulateServiceLocator (org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator)15 HashSet (java.util.HashSet)2 UnqualifiedImpl (org.glassfish.hk2.utilities.UnqualifiedImpl)1