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