Search in sources :

Example 21 with InjectionManager

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

the class InjectionManagerTest method testIsRegistrable.

@Test
public void testIsRegistrable() {
    InjectionManager injectionManager = new HK2InjectionManager();
    assertTrue(injectionManager.isRegistrable(Binder.class));
    assertTrue(injectionManager.isRegistrable(AbstractBinder.class));
    assertFalse(injectionManager.isRegistrable(org.glassfish.jersey.internal.inject.AbstractBinder.class));
    assertFalse(injectionManager.isRegistrable(String.class));
}
Also used : Binder(org.glassfish.hk2.utilities.Binder) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) AbstractBinder(org.glassfish.hk2.utilities.binding.AbstractBinder) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 22 with InjectionManager

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

the class InjectionManagerTest method testRegisterBinder.

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

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

Example 23 with InjectionManager

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

the class ExceptionMapperFactoryTest method testFindMappingExtendedExceptions.

/**
     * Test spec:
     * <p/>
     * setup:<br/>
     * - have two extended exception mappers, order matters<br/>
     * - both using the same generic type (RuntimeException)<br/>
     * - first mapper return isMappable true only to IllegalArgumentException<br/>
     * - second mapper return isMappable true only to IllegalStateException<br/>
     * <br/>
     * when:<br/>
     * - {@link ExceptionMapperFactory#findMapping(Throwable)} with IllegalArgumentException instance<br/>
     * <br/>
     * then:<br/>
     * - exception mapper factory returns IllegalArgumentExceptionMapper<br/>
     * <p/>
     * why:<br/>
     * - IllegalArgumentException has the same distance (1) for both exception mappers generic type (RuntimeException),
     * but IllegalArgumentException's isMappable return true, so it is the winner
     *
     * @throws Exception unexpected - if anything goes wrong, the test fails
     */
@Test
public void testFindMappingExtendedExceptions() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new ExtendedExceptionMappers());
    final ExceptionMapperFactory mapperFactory = new ExceptionMapperFactory(injectionManager);
    final ExceptionMapper mapper = mapperFactory.findMapping(new IllegalArgumentException());
    Assert.assertTrue("IllegalArgumentExceptionMapper should be returned", mapper instanceof IllegalArgumentExceptionMapper);
}
Also used : ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ExtendedExceptionMapper(org.glassfish.jersey.spi.ExtendedExceptionMapper) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 24 with InjectionManager

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

the class ExceptionMapperFactoryTest method testFindMapping.

/**
     * Test spec:
     * <p/>
     * setup:<br/>
     * - have 3 exception mappers, order matters<br/>
     * - first is *not* extended mapper typed to RuntimeException
     * - second and third are extended mappers type to RuntimeException
     * <br/>
     * when:<br/>
     * - {@link ExceptionMapperFactory#findMapping(Throwable)} invoked with RuntimeException instance<br/>
     * then: <br/>
     * - exception mapper factory returns RuntimeExceptionMapper<br/>
     * <p/>
     * why:<br/>
     * - RuntimeException mapper has distance 0 for RuntimeException, it is not extended mapper, so it will be chosen
     * immediately, cause there is no better option possible
     *
     * @throws Exception unexpected - if anything goes wrong, the test fails
     */
@Test
public void testFindMapping() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new AllMappers());
    final ExceptionMapperFactory mapperFactory = new ExceptionMapperFactory(injectionManager);
    final ExceptionMapper<RuntimeException> mapper = mapperFactory.findMapping(new RuntimeException());
    Assert.assertTrue("RuntimeExceptionMapper should be returned", mapper instanceof RuntimeExceptionMapper);
}
Also used : InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 25 with InjectionManager

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

the class ExceptionMapperFactoryTest method testFindExtendedExceptions.

/**
     * Test spec: <br/>
     * <p/>
     * setup:<br/>
     * - have 2 extended mappers, order matters<br/>
     * - first mapper return isMappable true only to IllegalArgumentException<br/>
     * - second mapper return isMappable true only to IllegalStateException<br/>
     * <br/>
     * when:<br/>
     * - {@link ExceptionMapperFactory#find(Class)} invoked with IllegalArgumentException.class<br/>
     * then:<br/>
     * - exception mapper factory returns IllegalArgumentExceptionMapper<br/>
     * <p/>
     * why:<br/>
     * - both exception mappers have distance 1 to IllegalArgumentException, we don't have instance of the
     * IllegalArgumentException, so the isMappable check is not used and both are accepted, the later accepted is
     * the winner
     *
     * @throws Exception unexpected - if anything goes wrong, the test fails
     */
@Test
public void testFindExtendedExceptions() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new ExtendedExceptionMappers());
    final ExceptionMapperFactory mapperFactory = new ExceptionMapperFactory(injectionManager);
    final ExceptionMapper mapper = mapperFactory.find(IllegalArgumentException.class);
    Assert.assertTrue("IllegalStateExceptionMapper should be returned", mapper instanceof IllegalStateExceptionMapper);
}
Also used : ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ExtendedExceptionMapper(org.glassfish.jersey.spi.ExtendedExceptionMapper) 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