Search in sources :

Example 1 with TypeLiteral

use of org.glassfish.hk2.api.TypeLiteral in project soapstone by alfasoftware.

the class TestSoapstoneService method testGetAListOfThings.

/**
 * Test that we get type information when returning a list of some supertype
 */
@Test
public void testGetAListOfThings() throws JsonProcessingException {
    String response = target().path("path/getAListOfThings").request().get(String.class);
    JavaType returnType = OBJECT_MAPPER.constructType(new TypeLiteral<List<WebService.SuperClass>>() {
    }.getType());
    List<WebService.SuperClass> list = OBJECT_MAPPER.readerFor(returnType).readValue(response);
    assertThat(list, containsInAnyOrder(asList(instanceOf(WebService.SuperClass.SubClass1.class), instanceOf(WebService.SuperClass.SubClass2.class))));
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) TypeLiteral(org.glassfish.hk2.api.TypeLiteral) WebService(org.alfasoftware.soapstone.testsupport.WebService) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 2 with TypeLiteral

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

the class TypeLiteralTest method testParameterizedLiteral.

/**
 * Tests the type return from a TypeLiteral
 */
@Test
public void testParameterizedLiteral() {
    TypeLiteral<List<String>> tl = new TypeLiteral<List<String>>() {
    };
    Type t = tl.getType();
    Assert.assertTrue(t instanceof ParameterizedType);
    ParameterizedType pt = (ParameterizedType) t;
    Assert.assertSame(List.class, pt.getRawType());
    Assert.assertEquals(1, pt.getActualTypeArguments().length);
    Assert.assertSame(String.class, pt.getActualTypeArguments()[0]);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeLiteral(org.glassfish.hk2.api.TypeLiteral) List(java.util.List) Test(org.junit.Test)

Example 3 with TypeLiteral

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

the class ProvidesTest method testProvidesGenericTypeParametersInstanceMethodToField.

/**
 * Verifies that generic type parameters are carried through a {@link
 * Provides} chain starting from an instance method and ending with a generic
 * {@link Provides} field.
 */
@Test
public void testProvidesGenericTypeParametersInstanceMethodToField() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, GenericTypeParameterChains.class);
    ProvidesGenericField<Value7> providerA = locator.getService(new TypeLiteral<ProvidesGenericField<Value7>>() {
    }.getType());
    assertNotNull(providerA);
    assertNotNull(providerA.value);
    assertEquals(Value7.class, providerA.value.getClass());
    Value7 providedA = locator.getService(Value7.class);
    assertNotNull(providedA);
    assertEquals(Value7.class, providedA.getClass());
    ProvidesGenericField<Value8> providerB = locator.getService(new TypeLiteral<ProvidesGenericField<Value8>>() {
    }.getType());
    assertNotNull(providerB);
    assertNotNull(providerB.value);
    assertEquals(Value8.class, providerB.value.getClass());
    Value8 providedB = locator.getService(Value8.class);
    assertNotNull(providedB);
    assertEquals(Value8.class, providedB.getClass());
}
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 TypeLiteral

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

the class ProvidesTest method testProvidesGenericTypeParametersInstanceFieldToMethod.

/**
 * Verifies that generic type parameters are carried through a {@link
 * Provides} chain starting from an instance field and ending with a generic
 * {@link Provides} method.
 */
@Test
public void testProvidesGenericTypeParametersInstanceFieldToMethod() {
    ServiceLocator locator = createAndPopulateServiceLocator();
    ServiceLocatorUtilities.addClasses(locator, ProvidesListener.class, GenericTypeParameterChains.class);
    ProvidesGenericMethod<Value11> providerA = locator.getService(new TypeLiteral<ProvidesGenericMethod<Value11>>() {
    }.getType());
    assertNotNull(providerA);
    assertNotNull(providerA.getValue());
    assertEquals(Value11.class, providerA.getValue().getClass());
    Value11 providedA = locator.getService(Value11.class);
    assertNotNull(providedA);
    assertEquals(Value11.class, providedA.getClass());
    ProvidesGenericMethod<Value12> providerB = locator.getService(new TypeLiteral<ProvidesGenericMethod<Value12>>() {
    }.getType());
    assertNotNull(providerB);
    assertNotNull(providerB.getValue());
    assertEquals(Value12.class, providerB.getValue().getClass());
    Value12 providedB = locator.getService(Value12.class);
    assertNotNull(providedB);
    assertEquals(Value12.class, providedB.getClass());
}
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 TypeLiteral

use of org.glassfish.hk2.api.TypeLiteral 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)

Aggregations

Test (org.junit.Test)38 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)37 TypeLiteral (org.glassfish.hk2.api.TypeLiteral)31 ServiceLocatorUtilities.createAndPopulateServiceLocator (org.glassfish.hk2.utilities.ServiceLocatorUtilities.createAndPopulateServiceLocator)28 Type (java.lang.reflect.Type)5 AbstractBinder (org.glassfish.hk2.utilities.binding.AbstractBinder)4 Singleton (jakarta.inject.Singleton)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Optional (java.util.Optional)2 DynamicConfiguration (org.glassfish.hk2.api.DynamicConfiguration)2 Factory (org.glassfish.hk2.api.Factory)2 TestModule (org.glassfish.hk2.tests.locator.utilities.TestModule)2 MediaTypes (com.atomgraph.core.MediaTypes)1 DataManagerFactory (com.atomgraph.core.factory.DataManagerFactory)1 QueryProvider (com.atomgraph.core.io.QueryProvider)1 ResultSetProvider (com.atomgraph.core.io.ResultSetProvider)1 UpdateRequestProvider (com.atomgraph.core.io.UpdateRequestProvider)1 Service (com.atomgraph.core.model.Service)1