Search in sources :

Example 56 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project camel by apache.

the class CamelCdiDeployment method apply.

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            WeldContainer container = weld.initialize();
            context.setBeanManager(container.getBeanManager());
            try {
                base.evaluate();
            } finally {
                container.shutdown();
                context.unsetBeanManager();
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) WeldContainer(org.jboss.weld.environment.se.WeldContainer)

Example 57 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project indy by Commonjava.

the class DefaultIndyConfigFactoryTest method weldInjection_IterateIndyConfigurators.

@Test
public void weldInjection_IterateIndyConfigurators() {
    Weld weld = new Weld();
    WeldContainer container = weld.initialize();
    List<String> sections = new ArrayList<>();
    container.instance().select(IndyConfigInfo.class).iterator().forEachRemaining((instance) -> {
        String section = ConfigUtils.getSectionName(instance);
        System.out.printf("Got instance: %s with section: %s\n", instance, section);
        sections.add(section);
    });
    System.out.println(sections);
    assertThat(sections.contains(ConfigurationSectionListener.DEFAULT_SECTION), equalTo(true));
    assertThat(sections.contains("flatfiles"), equalTo(true));
    assertThat(sections.contains("ui"), equalTo(true));
    assertThat(sections.contains("storage-default"), equalTo(true));
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) ArrayList(java.util.ArrayList) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 58 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class NotificationModeTest method testObserversNotifiedSerially.

@Test
public void testObserversNotifiedSerially() throws InterruptedException {
    try (WeldContainer container = createWeld()) {
        CountDownLatch latch = new CountDownLatch(4);
        Set<String> threadNames = new CopyOnWriteArraySet<>();
        List<String> observers = new CopyOnWriteArrayList<>();
        container.event().select(PriorityMessage.class).fireAsync((id) -> {
            threadNames.add(Thread.currentThread().getName());
            observers.add(id);
            latch.countDown();
        }, NotificationOptions.builder().set(WeldNotificationOptions.MODE, NotificationMode.SERIAL).build());
        assertTrue(latch.await(2, TimeUnit.SECONDS));
        // Observers were notified using the same thread
        assertEquals(1, threadNames.size());
        assertEquals(4, observers.size());
        assertEquals("1", observers.get(0));
        assertEquals("20", observers.get(1));
        assertEquals("300", observers.get(2));
        assertEquals("last", observers.get(3));
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) PriorityMessage(org.jboss.weld.environment.se.test.event.options.mode.PriorityObservers.PriorityMessage) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) CountDownLatch(java.util.concurrent.CountDownLatch) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.Test)

Example 59 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class NotificationModeTest method testAsyncEventExecutedInDifferentThread.

@Test
public void testAsyncEventExecutedInDifferentThread() throws InterruptedException {
    try (WeldContainer container = createWeld()) {
        BlockingQueue<Message> synchronizer = new LinkedBlockingQueue<>();
        Set<String> threadNames = new CopyOnWriteArraySet<>();
        container.event().select(Message.class).fireAsync(() -> threadNames.add(Thread.currentThread().getName()), WeldNotificationOptions.withParallelMode()).thenAccept(synchronizer::add);
        Message message = synchronizer.poll(2, TimeUnit.SECONDS);
        assertNotNull(message);
        // Eeach observer was notified using a different thread
        assertEquals(2, threadNames.size());
    }
}
Also used : PriorityMessage(org.jboss.weld.environment.se.test.event.options.mode.PriorityObservers.PriorityMessage) WeldContainer(org.jboss.weld.environment.se.WeldContainer) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Example 60 with WeldContainer

use of org.jboss.weld.environment.se.WeldContainer in project core by weld.

the class NotificationModeTest method testExceptionPropagated.

@Test
public void testExceptionPropagated() throws InterruptedException {
    try (WeldContainer container = createWeld()) {
        BlockingQueue<Throwable> synchronizer = new LinkedBlockingQueue<>();
        container.event().select(Message.class).fireAsync(() -> {
            throw new IllegalStateException(NotificationModeTest.class.getName());
        }, WeldNotificationOptions.withParallelMode()).whenComplete((event, throwable) -> synchronizer.add(throwable));
        Throwable materializedThrowable = synchronizer.poll(2, TimeUnit.SECONDS);
        assertTrue(materializedThrowable instanceof CompletionException);
        Throwable[] suppressed = ((CompletionException) materializedThrowable).getSuppressed();
        assertEquals(2, suppressed.length);
        assertTrue(suppressed[0] instanceof IllegalStateException);
        assertEquals(NotificationModeTest.class.getName(), suppressed[0].getMessage());
        assertTrue(suppressed[1] instanceof IllegalStateException);
        assertEquals(NotificationModeTest.class.getName(), suppressed[1].getMessage());
    }
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) CompletionException(java.util.concurrent.CompletionException) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Aggregations

WeldContainer (org.jboss.weld.environment.se.WeldContainer)115 Weld (org.jboss.weld.environment.se.Weld)100 Test (org.junit.Test)98 CountDownLatch (java.util.concurrent.CountDownLatch)8 Bean (javax.enterprise.inject.spi.Bean)8 BeanManager (javax.enterprise.inject.spi.BeanManager)8 Type (java.lang.reflect.Type)7 URLClassLoader (java.net.URLClassLoader)7 ArrayList (java.util.ArrayList)7 URL (java.net.URL)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 Fabric8Extension (io.fabric8.cdi.Fabric8Extension)4 ParameterizedType (java.lang.reflect.ParameterizedType)4 List (java.util.List)4 CreationalContext (javax.enterprise.context.spi.CreationalContext)4 BeanManagerImpl (org.jboss.weld.manager.BeanManagerImpl)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 PrintStream (java.io.PrintStream)3 ServiceRegistry (org.jboss.weld.bootstrap.api.ServiceRegistry)3