Search in sources :

Example 36 with InjectionManager

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

the class InjectionManagerTest method testServiceLocatorParent.

@Test
public void testServiceLocatorParent() {
    AbstractBinder binder = new AbstractBinder() {

        @Override
        protected void configure() {
            bindAsContract(EnglishGreeting.class);
        }
    };
    ServiceLocator parentLocator = ServiceLocatorUtilities.bind(binder);
    InjectionManager injectionManager = new HK2InjectionManager();
    injectionManager.initialize(parentLocator);
    assertNotNull(injectionManager.getInstance(EnglishGreeting.class));
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 37 with InjectionManager

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

the class ContextResolverFactoryTest method setUp.

@Before
public void setUp() {
    final InjectionManager locator = Injections.createInjectionManager(new ContextResolverFactory.Binder(), new Binder());
    final ProviderBinder providerBinder = new ProviderBinder(locator);
    providerBinder.bindClasses(Collections.singleton(CustomIntegerResolverC.class));
    crf = locator.getInstance(ContextResolverFactory.class);
}
Also used : ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Before(org.junit.Before)

Example 38 with InjectionManager

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

the class JaxrsProvidersTest method testProviders.

@Test
public void testProviders() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new MessagingBinders.MessageBodyProviders(null, RuntimeType.SERVER), new Binder());
    injectionManager.register(new TestBinder(injectionManager));
    TestBinder.initProviders(injectionManager);
    RequestScope scope = injectionManager.getInstance(RequestScope.class);
    scope.runInScope(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Providers instance = injectionManager.getInstance(Providers.class);
            assertNotNull(instance);
            assertSame(JaxrsProviders.class, instance.getClass());
            assertNotNull(instance.getExceptionMapper(Throwable.class));
            assertNotNull(instance.getMessageBodyReader(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
            assertNotNull(instance.getMessageBodyWriter(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
            assertNotNull(instance.getContextResolver(String.class, MediaType.TEXT_PLAIN_TYPE));
            return null;
        }
    });
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) MessagingBinders(org.glassfish.jersey.message.internal.MessagingBinders) Providers(javax.ws.rs.ext.Providers) RequestScope(org.glassfish.jersey.process.internal.RequestScope) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 39 with InjectionManager

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

the class ProviderBinderTest method testCustomRegistration.

@Test
public void testCustomRegistration() {
    InjectionManager injectionManager = Injections.createInjectionManager();
    ProviderBinder providerBinder = new ProviderBinder(injectionManager);
    providerBinder.bindClasses(Child.class);
    providerBinder.bindClasses(NotFilterChild.class);
    ContainerRequestFilter requestFilter = getRequestFilter(injectionManager);
    ContainerRequestFilter requestFilter2 = getRequestFilter(injectionManager);
    assertEquals(requestFilter, requestFilter2);
    ContainerResponseFilter responseFilter = getResponseFilter(injectionManager);
    ContainerResponseFilter responseFilter2 = getResponseFilter(injectionManager);
    assertTrue(responseFilter == responseFilter2);
    assertTrue(responseFilter == requestFilter);
    // only one filter should be registered
    Collection<ContainerResponseFilter> filters = Providers.getCustomProviders(injectionManager, ContainerResponseFilter.class);
    assertEquals(1, filters.size());
    Child child = injectionManager.getInstance(Child.class);
    Child child2 = injectionManager.getInstance(Child.class);
    assertTrue(child != responseFilter);
    assertTrue(child == child2);
}
Also used : ProviderBinder(org.glassfish.jersey.internal.inject.ProviderBinder) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 40 with InjectionManager

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

the class CommonConfigTest method testFeatureInjections.

@Test
public void testFeatureInjections() throws Exception {
    config.register(InjectIntoFeatureClass.class).register(new InjectIntoFeatureInstance()).register(new AbstractBinder() {

        @Override
        protected void configure() {
            bind(new InjectMe());
        }
    });
    final InjectionManager injectionManager = Injections.createInjectionManager();
    config.configureMetaProviders(injectionManager);
    assertThat("Feature instance not injected", config.getProperty("instance-injected").toString(), is("true"));
    assertThat("Feature class not injected", config.getProperty("class-injected").toString(), is("true"));
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) 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