Search in sources :

Example 1 with JohnzonCdiExtension

use of org.apache.johnzon.jsonb.cdi.JohnzonCdiExtension in project meecrowave by apache.

the class MeecrowaveClientLifecycleListenerTest method autoClose.

@Test
public void autoClose() throws IOException, NoSuchFieldException, IllegalAccessException {
    try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder().randomHttpPort().includePackages(MeecrowaveClientLifecycleListenerTest.class.getName())).bake()) {
        final JohnzonCdiExtension johnzonCdiExtension = CDI.current().select(JohnzonCdiExtension.class).get();
        final Field jsonbs = JohnzonCdiExtension.class.getDeclaredField("jsonbs");
        jsonbs.setAccessible(true);
        final BeanManager beanManager = CDI.current().getBeanManager();
        final JohnzonCdiExtension extensionInstance = JohnzonCdiExtension.class.cast(beanManager.getContext(ApplicationScoped.class).get(beanManager.resolve(beanManager.getBeans(JohnzonCdiExtension.class))));
        final Collection<?> o = Collection.class.cast(jsonbs.get(extensionInstance));
        {
            // ensure server is init whatever test suite we run in
            final Client client = ClientBuilder.newClient();
            get(meecrowave, client);
            client.close();
        }
        final int origin = o.size();
        final Client client = ClientBuilder.newClient();
        final JsonbJaxrsProvider<?> provider = new JsonbJaxrsProvider<>();
        client.register(provider);
        get(meecrowave, client);
        assertEquals(origin + 1, o.size());
        client.close();
        assertEquals(origin, o.size());
    }
}
Also used : Field(java.lang.reflect.Field) JsonbJaxrsProvider(org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider) ClientBuilder(javax.ws.rs.client.ClientBuilder) Client(javax.ws.rs.client.Client) BeanManager(javax.enterprise.inject.spi.BeanManager) Meecrowave(org.apache.meecrowave.Meecrowave) JohnzonCdiExtension(org.apache.johnzon.jsonb.cdi.JohnzonCdiExtension) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)1 BeanManager (javax.enterprise.inject.spi.BeanManager)1 Client (javax.ws.rs.client.Client)1 ClientBuilder (javax.ws.rs.client.ClientBuilder)1 JsonbJaxrsProvider (org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider)1 JohnzonCdiExtension (org.apache.johnzon.jsonb.cdi.JohnzonCdiExtension)1 Meecrowave (org.apache.meecrowave.Meecrowave)1 Test (org.junit.Test)1