use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.
the class ProvidesTest method testInstanceMethodProvidesPerLookupLifecycle.
/**
* 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} method and the service is per-lookup.
*/
@Test
public void testInstanceMethodProvidesPerLookupLifecycle() {
ServiceLocator locator = createAndPopulateServiceLocator();
ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesLifecycle.class);
ProvidedPerLookupInstanceMethodWithLifecycle serviceWithoutHandle = locator.getService(ProvidedPerLookupInstanceMethodWithLifecycle.class);
assertNotNull(serviceWithoutHandle);
assertFalse(serviceWithoutHandle.wasStarted());
assertFalse(serviceWithoutHandle.wasStopped());
assertNotSame(serviceWithoutHandle, locator.getService(ProvidedPerLookupInstanceMethodWithLifecycle.class));
IterableProvider<ProvidedPerLookupInstanceMethodWithLifecycle> serviceProvider = locator.getService(new TypeLiteral<IterableProvider<ProvidedPerLookupInstanceMethodWithLifecycle>>() {
}.getType());
ServiceHandle<ProvidedPerLookupInstanceMethodWithLifecycle> serviceHandle = serviceProvider.getHandle();
ProvidedPerLookupInstanceMethodWithLifecycle serviceWitHandle = serviceHandle.getService();
assertNotNull(serviceWitHandle);
assertFalse(serviceWitHandle.wasStarted());
assertFalse(serviceWitHandle.wasStopped());
serviceHandle.close();
assertTrue(serviceWitHandle.wasStopped());
locator.shutdown();
assertFalse(serviceWithoutHandle.wasStopped());
}
use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.
the class ProvidesTest method testProvidesOverrideMethodNoDuplicates.
/**
* Verifies that when a {@link Contract} interface has a {@link Provides}
* method, and then a concrete service class overrides that method and the
* overridden method is also implemented with {@link Provides}, that they are
* considered to be the same method and not counted as distinct providers.
*/
@Test
public void testProvidesOverrideMethodNoDuplicates() {
ServiceLocator locator = createAndPopulateServiceLocator();
ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesGenericContracts.class);
Set<String> expected = new HashSet<>(Arrays.asList("staticField", "instanceField", "staticMethod", "instanceMethod"));
IterableProvider<OverrideBoxFromGenericProvidesContract<String>> overrides = locator.getService(new TypeLiteral<IterableProvider<OverrideBoxFromGenericProvidesContract<String>>>() {
}.getType());
assertEquals(expected.size(), overrides.getSize());
assertEquals(expected, StreamSupport.stream(overrides.spliterator(), false).map(box -> box.value).collect(toSet()));
}
use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.
the class ProvidesTest method testProvidesNullFromStaticField.
/**
* Verifies that {@code null} may be provided from a static field annotated
* with {@link Provides}.
*/
@Test
public void testProvidesNullFromStaticField() {
ServiceLocator locator = createAndPopulateServiceLocator();
ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesNull.class);
IterableProvider<NullFromStaticField> provider = locator.getService(new TypeLiteral<IterableProvider<NullFromStaticField>>() {
}.getType());
try (ServiceHandle<NullFromStaticField> handle = provider.getHandle()) {
assertNull(handle.getService());
}
}
use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.
the class ProvidesTest method testNullServicesWithLifecycle.
/**
* Verifies that if a service comes from a {@link Provides} method or field
* and its type defines lifecycle methods, or the {@link Provides} annotation
* defines a custom dispose method, that no errors are thrown when the service
* is {@code null}.
*/
@Test
public void testNullServicesWithLifecycle() {
ServiceLocator locator = createAndPopulateServiceLocator();
ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesNullWithLifecycle.class);
IterableProvider<ServiceWithLifecycle> providers = locator.getService(new TypeLiteral<IterableProvider<ServiceWithLifecycle>>() {
}.getType());
assertEquals(12, providers.getSize());
for (ServiceHandle<ServiceWithLifecycle> handle : providers.handleIterator()) {
assertNull(handle.getService());
handle.close();
}
}
use of org.glassfish.hk2.api.IterableProvider in project glassfish-hk2 by eclipse-ee4j.
the class ProvidesTest method testProvidesNullFromInstanceMethod.
/**
* Verifies that {@code null} may be provided from an instance method
* annotated with {@link Provides}.
*/
@Test
public void testProvidesNullFromInstanceMethod() {
ServiceLocator locator = createAndPopulateServiceLocator();
ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, ProvidesNull.class);
IterableProvider<NullFromInstanceMethod> provider = locator.getService(new TypeLiteral<IterableProvider<NullFromInstanceMethod>>() {
}.getType());
try (ServiceHandle<NullFromInstanceMethod> handle = provider.getHandle()) {
assertNull(handle.getService());
}
}
Aggregations