Search in sources :

Example 41 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class CommonConfigTest method testProviderOrderSemiAutomatic.

@Test
public void testProviderOrderSemiAutomatic() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.register(MidPriorityProvider.class, 50);
    config.register(LowPriorityProvider.class, 2000);
    config.register(HighPriorityProvider.class);
    ProviderBinder.bindProviders(config.getComponentBag(), injectionManager);
    final Iterable<WriterInterceptor> allProviders = Providers.getAllProviders(injectionManager, WriterInterceptor.class, new RankedComparator<>());
    final Iterator<WriterInterceptor> iterator = allProviders.iterator();
    assertEquals(MidPriorityProvider.class, iterator.next().getClass());
    assertEquals(HighPriorityProvider.class, iterator.next().getClass());
    assertEquals(LowPriorityProvider.class, iterator.next().getClass());
    assertFalse(iterator.hasNext());
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 42 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class CommonConfigTest method testProviderOrderAutomatic.

@Test
public void testProviderOrderAutomatic() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.register(MidPriorityProvider.class);
    config.register(LowPriorityProvider.class);
    config.register(HighPriorityProvider.class);
    ProviderBinder.bindProviders(config.getComponentBag(), injectionManager);
    final Iterable<WriterInterceptor> allProviders = Providers.getAllProviders(injectionManager, WriterInterceptor.class, new RankedComparator<WriterInterceptor>());
    final Iterator<WriterInterceptor> iterator = allProviders.iterator();
    assertEquals(HighPriorityProvider.class, iterator.next().getClass());
    assertEquals(MidPriorityProvider.class, iterator.next().getClass());
    assertEquals(LowPriorityProvider.class, iterator.next().getClass());
    assertFalse(iterator.hasNext());
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 43 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class CommonConfigTest method testBinderConfiguringFeature.

@Test
public void testBinderConfiguringFeature() throws Exception {
    config.register(ContractBinderFeature.class);
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.configureMetaProviders(injectionManager);
    assertTrue(config.isEnabled(ContractBinderFeature.class));
    assertEquals(1, config.getInstances().size());
    assertSame(ContractBinder.class, config.getInstances().iterator().next().getClass());
    final Contract service = injectionManager.getInstance(Contract.class);
    assertNotNull(service);
    assertSame(Service.class, service.getClass());
}
Also used : InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 44 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class CommonConfigTest method testProviderOrderDifForContracts.

@Test
@SuppressWarnings("unchecked")
public void testProviderOrderDifForContracts() throws Exception {
    final Map<Class<?>, Integer> contracts = new IdentityHashMap<Class<?>, Integer>();
    contracts.put(WriterInterceptor.class, ContractProvider.NO_PRIORITY);
    contracts.put(ReaderInterceptor.class, 2000);
    config.register(MidPriorityProvider.class, contracts);
    contracts.clear();
    contracts.put(WriterInterceptor.class, ContractProvider.NO_PRIORITY);
    contracts.put(ReaderInterceptor.class, 1000);
    config.register(LowPriorityProvider.class, contracts);
    contracts.clear();
    contracts.put(WriterInterceptor.class, ContractProvider.NO_PRIORITY);
    contracts.put(ReaderInterceptor.class, 3000);
    config.register(HighPriorityProvider.class, contracts);
    contracts.clear();
    final InjectionManager injectionManager = Injections.createInjectionManager();
    ProviderBinder.bindProviders(config.getComponentBag(), injectionManager);
    final Iterable<WriterInterceptor> writerInterceptors = Providers.getAllProviders(injectionManager, WriterInterceptor.class, new RankedComparator<WriterInterceptor>());
    final Iterator<WriterInterceptor> writerIterator = writerInterceptors.iterator();
    assertEquals(HighPriorityProvider.class, writerIterator.next().getClass());
    assertEquals(MidPriorityProvider.class, writerIterator.next().getClass());
    assertEquals(LowPriorityProvider.class, writerIterator.next().getClass());
    assertFalse(writerIterator.hasNext());
    final Iterable<ReaderInterceptor> readerInterceptors = Providers.getAllProviders(injectionManager, ReaderInterceptor.class, new RankedComparator<ReaderInterceptor>());
    final Iterator<ReaderInterceptor> readerIterator = readerInterceptors.iterator();
    assertEquals(LowPriorityProvider.class, readerIterator.next().getClass());
    assertEquals(MidPriorityProvider.class, readerIterator.next().getClass());
    assertEquals(HighPriorityProvider.class, readerIterator.next().getClass());
    assertFalse(readerIterator.hasNext());
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) IdentityHashMap(java.util.IdentityHashMap) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 45 with InjectionManager

use of org.glassfish.jersey.internal.inject.InjectionManager in project jersey by jersey.

the class BeanParamMemoryLeakTest method testBeanParam.

@Test
public void testBeanParam() throws Exception {
    initiateWebApplication(BeanParamInjectionResource.class);
    final InjectionManager injectionManager = app().getInjectionManager();
    if (!(injectionManager instanceof HK2InjectionManager)) {
        throw new RuntimeException("Bean Manager is not an injection manager");
    }
    HK2InjectionManager hk2BeanManager = (HK2InjectionManager) injectionManager;
    ServiceLocator serviceLocator = hk2BeanManager.getServiceLocator();
    // we do not expect any descriptor registered yet
    assertEquals(0, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    // now make one registered via this call
    assertEquals("one", resource("/jaxrs?q=one").getEntity());
    // make sure it got registered
    assertEquals(1, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    // make another call
    assertEquals("two", resource("/jaxrs?q=two").getEntity());
    assertEquals(1, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    // and some more
    for (int i = 0; i < 20; i++) {
        assertEquals(Integer.toString(i), resource("/jaxrs?q=" + i).getEntity());
        assertEquals(1, serviceLocator.getDescriptors(new ParameterBeanFilter()).size());
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) HK2InjectionManager(org.glassfish.jersey.hk2.HK2InjectionManager) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Aggregations

InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)50 Test (org.junit.Test)41 WadlGenerator (org.glassfish.jersey.server.wadl.WadlGenerator)10 AbstractBinder (org.glassfish.jersey.internal.inject.AbstractBinder)8 ProviderBinder (org.glassfish.jersey.internal.inject.ProviderBinder)7 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)5 File (java.io.File)4 URL (java.net.URL)4 Properties (java.util.Properties)4 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)4 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)4 MessageBodyWriter (javax.ws.rs.ext.MessageBodyWriter)3 JerseyTest (org.glassfish.jersey.test.JerseyTest)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 IdentityHashMap (java.util.IdentityHashMap)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Function (java.util.function.Function)2