Search in sources :

Example 6 with ContractProvider

use of org.glassfish.jersey.model.ContractProvider in project jersey by jersey.

the class CommonConfigTest method testRegisterClassContracts.

@Test
@SuppressWarnings("unchecked")
public void testRegisterClassContracts() throws Exception {
    try {
        final Class clazz = null;
        //noinspection ConstantConditions
        config.register(clazz, ReaderInterceptor.class);
        fail("Cannot register null.");
    } catch (final IllegalArgumentException e) {
    // OK.
    }
    config.register(ComplexEmptyProvider.class, ReaderInterceptor.class, ContainerRequestFilter.class, WriterInterceptor.class);
    final ContractProvider contractProvider = config.getComponentBag().getModel(ComplexEmptyProvider.class);
    final Set<Class<?>> contracts = contractProvider.getContracts();
    assertEquals(2, contracts.size());
    assertTrue(ReaderInterceptor.class + " is not registered.", contracts.contains(ReaderInterceptor.class));
    assertTrue(ContainerRequestFilter.class + " is not registered.", contracts.contains(ContainerRequestFilter.class));
    assertFalse(WriterInterceptor.class + " should not be registered.", contracts.contains(WriterInterceptor.class));
    assertTrue(config.getInstances().isEmpty());
    assertTrue(config.getClasses().contains(ComplexEmptyProvider.class));
}
Also used : WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ContractProvider(org.glassfish.jersey.model.ContractProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Test(org.junit.Test)

Example 7 with ContractProvider

use of org.glassfish.jersey.model.ContractProvider in project jersey by jersey.

the class CommonConfigTest method testRegisterInstance.

@Test
public void testRegisterInstance() throws Exception {
    try {
        config.register(null);
        fail("Cannot register null.");
    } catch (final IllegalArgumentException e) {
    // OK.
    }
    final ComplexEmptyProvider[] ceps = new ComplexEmptyProvider[2];
    for (int i = 0; i < 2; i++) {
        ceps[i] = new ComplexEmptyProvider();
        config.register(ceps[i]);
    }
    final ContractProvider contractProvider = config.getComponentBag().getModel(ComplexEmptyProvider.class);
    final Set<Class<?>> contracts = contractProvider.getContracts();
    assertEquals(3, contracts.size());
    assertTrue(contracts.contains(ReaderInterceptor.class));
    assertTrue(contracts.contains(ContainerRequestFilter.class));
    assertTrue(contracts.contains(ExceptionMapper.class));
    assertTrue(config.isRegistered(ComplexEmptyProvider.class));
    assertTrue(config.isRegistered(ceps[0]));
    assertFalse(config.isRegistered(ceps[1]));
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ContractProvider(org.glassfish.jersey.model.ContractProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Test(org.junit.Test)

Example 8 with ContractProvider

use of org.glassfish.jersey.model.ContractProvider in project jersey by jersey.

the class CommonConfigTest method testRegisterInstanceBingingPriority.

@Test
public void testRegisterInstanceBingingPriority() throws Exception {
    try {
        config.register(null, Priorities.USER);
        fail("Cannot register null.");
    } catch (final IllegalArgumentException e) {
    // OK.
    }
    final Class<ComplexEmptyProvider> providerClass = ComplexEmptyProvider.class;
    for (final int priority : new int[] { Priorities.USER, Priorities.AUTHENTICATION }) {
        config.register(providerClass, priority);
        final CommonConfig commonConfig = config;
        final ContractProvider contractProvider = commonConfig.getComponentBag().getModel(providerClass);
        final Set<Class<?>> contracts = contractProvider.getContracts();
        // Feature is not there.
        assertEquals(3, contracts.size());
        assertTrue(contracts.contains(ReaderInterceptor.class));
        assertTrue(contracts.contains(ContainerRequestFilter.class));
        assertTrue(contracts.contains(ExceptionMapper.class));
        // All priorities are the same.
        assertEquals(Priorities.USER, contractProvider.getPriority(ReaderInterceptor.class));
        assertEquals(Priorities.USER, contractProvider.getPriority(ContainerRequestFilter.class));
        assertEquals(Priorities.USER, contractProvider.getPriority(ExceptionMapper.class));
    }
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ContractProvider(org.glassfish.jersey.model.ContractProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Test(org.junit.Test)

Example 9 with ContractProvider

use of org.glassfish.jersey.model.ContractProvider in project jersey by jersey.

the class CommonConfigTest method testRegisterClass.

@Test
public void testRegisterClass() throws Exception {
    try {
        final Class clazz = null;
        //noinspection ConstantConditions
        config.register(clazz);
        fail("Cannot register null.");
    } catch (final IllegalArgumentException e) {
    // OK.
    }
    for (int i = 0; i < 2; i++) {
        config.register(ComplexEmptyProvider.class);
    }
    final ContractProvider contractProvider = config.getComponentBag().getModel(ComplexEmptyProvider.class);
    final Set<Class<?>> contracts = contractProvider.getContracts();
    assertEquals(3, contracts.size());
    assertTrue(contracts.contains(ReaderInterceptor.class));
    assertTrue(contracts.contains(ContainerRequestFilter.class));
    assertTrue(contracts.contains(ExceptionMapper.class));
    assertTrue(config.isRegistered(ComplexEmptyProvider.class));
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ContractProvider(org.glassfish.jersey.model.ContractProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Test(org.junit.Test)

Example 10 with ContractProvider

use of org.glassfish.jersey.model.ContractProvider in project jersey by jersey.

the class CommonConfigTest method testRegisterClassInstanceBindingPriorityClash.

@Test
public void testRegisterClassInstanceBindingPriorityClash() throws Exception {
    final ComplexEmptyProvider complexEmptyProvider = new ComplexEmptyProvider();
    config.register(ComplexEmptyProvider.class, Priorities.AUTHENTICATION);
    config.register(complexEmptyProvider, Priorities.USER);
    assertTrue(config.getClasses().contains(ComplexEmptyProvider.class));
    assertFalse(config.getInstances().contains(complexEmptyProvider));
    final ComponentBag componentBag = config.getComponentBag();
    final ContractProvider contractProvider = componentBag.getModel(ComplexEmptyProvider.class);
    final Set<Class<?>> contracts = contractProvider.getContracts();
    // Feature is not there.
    assertEquals(3, contracts.size());
    assertTrue(contracts.contains(ReaderInterceptor.class));
    assertTrue(contracts.contains(ContainerRequestFilter.class));
    assertTrue(contracts.contains(ExceptionMapper.class));
    // All priorities are the same.
    assertEquals(Priorities.AUTHENTICATION, contractProvider.getPriority(ReaderInterceptor.class));
    assertEquals(Priorities.AUTHENTICATION, contractProvider.getPriority(ContainerRequestFilter.class));
    assertEquals(Priorities.AUTHENTICATION, contractProvider.getPriority(ExceptionMapper.class));
}
Also used : ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) ContractProvider(org.glassfish.jersey.model.ContractProvider) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Test(org.junit.Test)

Aggregations

ContractProvider (org.glassfish.jersey.model.ContractProvider)16 ContainerRequestFilter (javax.ws.rs.container.ContainerRequestFilter)10 Test (org.junit.Test)10 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)9 ExceptionMapper (javax.ws.rs.ext.ExceptionMapper)5 Annotation (java.lang.annotation.Annotation)4 RuntimeType (javax.ws.rs.RuntimeType)3 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)3 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Singleton (javax.inject.Singleton)2 NameBinding (javax.ws.rs.NameBinding)2 PreMatching (javax.ws.rs.container.PreMatching)2 OutputStream (java.io.OutputStream)1