Search in sources :

Example 1 with IterableProvider

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

the class ProvidesTest method testProvidesLifecycleFromInstanceField.

/**
 * Verifies the lifecycle a service obtained from an instance field that is
 * annotated with {@link Provides}.
 */
@Test
public void testProvidesLifecycleFromInstanceField() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycleFactory.class, ProvidesLifecycleDependency.class);
    IterableProvider<ProvidesLifecycleFromInstanceField> provider = locator.getService(new TypeLiteral<IterableProvider<ProvidesLifecycleFromInstanceField>>() {
    }.getType());
    ServiceHandle<ProvidesLifecycleFromInstanceField> handle = provider.getHandle();
    ProvidesLifecycleFromInstanceField root = handle.getService();
    assertFalse(root.wasStarted());
    assertTrue(root.factory.wasStarted());
    assertFalse(root.dependency.wasStarted());
    assertFalse(root.wasStopped());
    assertFalse(root.factory.wasStopped());
    assertFalse(root.dependency.wasStopped());
    handle.close();
    assertFalse(root.wasStopped());
    assertTrue(root.factory.wasStopped());
    assertFalse(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)

Example 2 with IterableProvider

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

the class ProvidesTest method testProvidesNullFromStaticMethod.

/**
 * Verifies that {@code null} may be provided from a static method annotated
 * with {@link Provides}.
 */
@Test
public void testProvidesNullFromStaticMethod() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesNull.class);
    IterableProvider<NullFromStaticMethod> provider = locator.getService(new TypeLiteral<IterableProvider<NullFromStaticMethod>>() {
    }.getType());
    try (ServiceHandle<NullFromStaticMethod> handle = provider.getHandle()) {
        assertNull(handle.getService());
    }
}
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 3 with IterableProvider

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

the class ProvidesTest method testProvidesLifecycleFromStaticField.

/**
 * Verifies the lifecycle of a service obtained from a static field that is
 * annotated with {@link Provides}.
 */
@Test
public void testProvidesLifecycleFromStaticField() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycleFactory.class, ProvidesLifecycleDependency.class);
    IterableProvider<ProvidesLifecycleFromStaticField> provider = locator.getService(new TypeLiteral<IterableProvider<ProvidesLifecycleFromStaticField>>() {
    }.getType());
    ServiceHandle<ProvidesLifecycleFromStaticField> handle = provider.getHandle();
    ProvidesLifecycleFromStaticField root = handle.getService();
    assertFalse(root.wasStarted());
    assertFalse(root.factory.wasStarted());
    assertFalse(root.dependency.wasStarted());
    assertFalse(root.wasStopped());
    assertFalse(root.factory.wasStopped());
    assertFalse(root.dependency.wasStopped());
    handle.close();
    assertFalse(root.wasStopped());
    assertFalse(root.factory.wasStopped());
    assertFalse(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)

Example 4 with IterableProvider

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

the class ProvidesTest method testStaticFieldProvidesPerLookupLifecycleWithHandle.

/**
 * 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} field and the service is per-lookup, and the
 * service is retrieved through a {@link ServiceHandle}.
 */
@Test
public void testStaticFieldProvidesPerLookupLifecycleWithHandle() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycle.class);
    IterableProvider<ProvidedPerLookupStaticFieldWithLifecycleWithHandle> serviceProvider = locator.getService(new TypeLiteral<IterableProvider<ProvidedPerLookupStaticFieldWithLifecycleWithHandle>>() {
    }.getType());
    ServiceHandle<ProvidedPerLookupStaticFieldWithLifecycleWithHandle> serviceHandle = serviceProvider.getHandle();
    ProvidedPerLookupStaticFieldWithLifecycleWithHandle serviceWithHandle = serviceHandle.getService();
    assertNotNull(serviceWithHandle);
    assertFalse(serviceWithHandle.wasStarted());
    assertFalse(serviceWithHandle.wasStopped());
    serviceHandle.close();
    assertFalse(serviceWithHandle.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 5 with IterableProvider

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

the class ProvidesTest method testProvidesLifecycleFromInstanceMethod.

/**
 * Verifies the lifecycle of a service obtained from an instance method that
 * is annotated with {@link Provides}.
 */
@Test
public void testProvidesLifecycleFromInstanceMethod() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycleFactory.class, ProvidesLifecycleDependency.class);
    IterableProvider<ProvidesLifecycleFromInstanceMethod> provider = locator.getService(new TypeLiteral<IterableProvider<ProvidesLifecycleFromInstanceMethod>>() {
    }.getType());
    ServiceHandle<ProvidesLifecycleFromInstanceMethod> handle = provider.getHandle();
    ProvidesLifecycleFromInstanceMethod root = handle.getService();
    assertFalse(root.wasStarted());
    assertTrue(root.factory.wasStarted());
    assertTrue(root.dependency.wasStarted());
    assertFalse(root.wasStopped());
    assertFalse(root.factory.wasStopped());
    assertFalse(root.dependency.wasStopped());
    handle.close();
    assertTrue(root.wasStopped());
    assertTrue(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