Search in sources :

Example 6 with ApplicationComposers

use of org.apache.openejb.testing.ApplicationComposers in project tomee by apache.

the class ProperConnectionShutdownTest method run.

@Test
@Ignore("https://issues.apache.org/jira/browse/AMQ-6051")
public void run() throws Throwable {
    final Thread[] threadsBefore = listThreads();
    final AtomicReference<Thread[]> threadWhile = new AtomicReference<>();
    // run test
    final Statement testInContainer = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            messages.sendMessage("Hello World!");
            messages.sendMessage("How are you?");
            threadWhile.set(listThreads());
            messages.sendMessage("Still spinning?");
            assertEquals(messages.receiveMessage(), "Hello World!");
            assertEquals(messages.receiveMessage(), "How are you?");
            assertEquals(messages.receiveMessage(), "Still spinning?");
        /* TODO: activate it when AMQ-6051 is fixed

                // all worked, now hold a connection
                new Thread(new Runnable() { // not daemon!
                    @Override
                    public void run() {
                        messages.blockConnection(); // oops, I forgot to close it
                    }
                }).start();
                 */
        }
    };
    new DeployApplication(this, testInContainer, new ApplicationComposers(this)).evaluate();
    // AMQ state (started) polling for transport thread is 1s
    Thread.sleep(2250);
    while (Join.join("", listThreads()).contains("ActiveMQ Session Task")) {
        // let few sec to AMQ to leave the holding task
        Thread.sleep(1000);
    }
    // ensure no connection are leaking
    final Thread[] threadsAfter = listThreads();
    int countAMQ = 0;
    int countOthers = 0;
    for (final Thread t : threadsAfter) {
        if (!t.isAlive()) {
            continue;
        }
        if (t.getName().contains("AMQ") || t.getName().toLowerCase(Locale.ENGLISH).contains("activemq")) {
            countAMQ++;
        } else {
            countOthers++;
        }
    }
    final String debugMessage = Join.join(", ", threadsAfter);
    assertEquals(debugMessage, 0, countAMQ);
    // geronimo libs spawn 2 threads we know: PoolIdleReleaseTimer and CurrentTime so we can get initial + 2 threads there
    assertTrue(debugMessage, countOthers <= threadsBefore.length + 2);
}
Also used : Statement(org.junit.runners.model.Statement) DeployApplication(org.apache.openejb.junit.DeployApplication) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with ApplicationComposers

use of org.apache.openejb.testing.ApplicationComposers in project tomee by apache.

the class ResourceEventsTest method run.

@Test
public void run() throws Exception {
    Listener.EVENTS.clear();
    new ApplicationComposers(this).evaluate(this, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            assertEquals(2, Listener.EVENTS.size());
            for (final ResourceEvent re : Listener.EVENTS) {
                assertTrue(ResourceCreated.class.isInstance(re));
                assertNotNull(re.getResource());
                assertTrue("base".equals(re.getName()) || "base2".equals(re.getName()));
            }
            Listener.EVENTS.clear();
            assertTrue(Child.class.isInstance(base));
            assertFalse(Child.class.isInstance(base2));
            assertNotNull(Child.class.cast(base).parent);
            return null;
        }
    });
    assertEquals(2, Listener.EVENTS.size());
    for (final ResourceEvent re : Listener.EVENTS) {
        assertTrue(ResourceBeforeDestroyed.class.isInstance(re));
        Object resource = re.getResource();
        if (Assembler.ResourceInstance.class.isInstance(resource)) {
            resource = Assembler.ResourceInstance.class.cast(resource).getObject();
        }
        assertNotNull(resource);
        assertTrue("base".equals(re.getName()) || "base2".equals(re.getName()));
        if ("base".equals(re.getName())) {
            assertTrue(Child.class.isInstance(resource));
        } else {
            assertFalse(Child.class.isInstance(resource));
            assertTrue(Base.class.isInstance(resource));
        }
    }
    Listener.EVENTS.clear();
    assertTrue(base2.stopped);
    assertTrue(base.stopped);
    assertTrue(Base.class.cast(Child.class.cast(base).parent).stopped);
}
Also used : Assembler(org.apache.openejb.assembler.classic.Assembler) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers) Test(org.junit.Test)

Example 8 with ApplicationComposers

use of org.apache.openejb.testing.ApplicationComposers in project tomee by apache.

the class ContainerRule method apply.

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

        @Override
        public void evaluate() throws Throwable {
            final ApplicationComposers composers = new ContainerApplicationComposers(instance);
            composers.startContainer(instance);
            try {
                statement.evaluate();
            } finally {
                composers.after();
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers)

Example 9 with ApplicationComposers

use of org.apache.openejb.testing.ApplicationComposers in project tomee by apache.

the class EnsureRequestScopeThreadLocalIsCleanUpTest method runAndCheckThreadLocal.

@Test
public void runAndCheckThreadLocal() throws Exception {
    final ApplicationComposers composers = new ApplicationComposers(EnsureRequestScopeThreadLocalIsCleanUpTest.class);
    composers.before(this);
    final CdiAppContextsService contextsService = CdiAppContextsService.class.cast(WebBeansContext.currentInstance().getService(ContextsService.class));
    assertNotNull(contextsService.getCurrentContext(RequestScoped.class));
    assertNotNull(contextsService.getCurrentContext(SessionScoped.class));
    composers.after();
    assertNull(contextsService.getCurrentContext(RequestScoped.class));
    assertNull(contextsService.getCurrentContext(SessionScoped.class));
}
Also used : SessionScoped(javax.enterprise.context.SessionScoped) ContextsService(org.apache.webbeans.spi.ContextsService) RequestScoped(javax.enterprise.context.RequestScoped) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers) Test(org.junit.Test)

Aggregations

ApplicationComposers (org.apache.openejb.testing.ApplicationComposers)9 Test (org.junit.Test)7 Statement (org.junit.runners.model.Statement)3 RequestScoped (javax.enterprise.context.RequestScoped)2 SessionScoped (javax.enterprise.context.SessionScoped)2 ContextsService (org.apache.webbeans.spi.ContextsService)2 ArrayList (java.util.ArrayList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Handler (java.util.logging.Handler)1 LogRecord (java.util.logging.LogRecord)1 Context (javax.enterprise.context.spi.Context)1 MBeanServer (javax.management.MBeanServer)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ObjectName (javax.management.ObjectName)1 Assembler (org.apache.openejb.assembler.classic.Assembler)1 DeployApplication (org.apache.openejb.junit.DeployApplication)1 WebBeansContext (org.apache.webbeans.config.WebBeansContext)1 Ignore (org.junit.Ignore)1