Search in sources :

Example 1 with ApplicationComposers

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

the class EnsureRequestScopeThreadLocalIsCleanUpTest method ensureRequestContextCanBeRestarted.

@Test
public void ensureRequestContextCanBeRestarted() throws Exception {
    final ApplicationComposers composers = new ApplicationComposers(EnsureRequestScopeThreadLocalIsCleanUpTest.class);
    composers.before(this);
    final CdiAppContextsService contextsService = CdiAppContextsService.class.cast(WebBeansContext.currentInstance().getService(ContextsService.class));
    final Context req1 = contextsService.getCurrentContext(RequestScoped.class);
    assertNotNull(req1);
    final Context session1 = contextsService.getCurrentContext(SessionScoped.class);
    assertNotNull(session1);
    contextsService.endContext(RequestScoped.class, null);
    contextsService.startContext(RequestScoped.class, null);
    final Context req2 = contextsService.getCurrentContext(RequestScoped.class);
    assertNotSame(req1, req2);
    final Context session2 = contextsService.getCurrentContext(SessionScoped.class);
    assertSame(session1, session2);
    composers.after();
    assertNull(contextsService.getCurrentContext(RequestScoped.class));
    assertNull(contextsService.getCurrentContext(SessionScoped.class));
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) Context(javax.enterprise.context.spi.Context) 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)

Example 2 with ApplicationComposers

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

the class ThreadDumpOnDestroyTest method run.

@Test
public void run() throws Exception {
    final List<String> records = new ArrayList<>();
    final Handler h = new Handler() {

        @Override
        public synchronized void publish(final LogRecord record) {
            records.add(record.getMessage());
        }

        @Override
        public void flush() {
        // no-op
        }

        @Override
        public void close() throws SecurityException {
        // no-op
        }
    };
    new ApplicationComposers(ThreadDumpOnDestroyTest.class).evaluate(this, new Runnable() {

        @Override
        public void run() {
            Logger.getLogger("OpenEJB.startup").addHandler(h);
        }
    });
    Logger.getLogger("OpenEJB.startup").removeHandler(h);
    assertEquals("Can't destroy test in 3 seconds, giving up.", records.get(1));
    assertTrue(records.get(2).contains("\"openejb-resource-destruction-test - 1\" suspended=false state=TIMED_WAITING"));
    assertTrue(records.get(2).contains("org.apache.openejb.resource.ThreadDumpOnDestroyTest$WillFail.destroy"));
}
Also used : LogRecord(java.util.logging.LogRecord) ArrayList(java.util.ArrayList) Handler(java.util.logging.Handler) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers) Test(org.junit.Test)

Example 3 with ApplicationComposers

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

the class ApplicationRule method apply.

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

        @Override
        public void evaluate() throws Throwable {
            final Thread thread = Thread.currentThread();
            final ClassLoader old = thread.getContextClassLoader();
            final ApplicationComposers composers = new ApplicationOnlyApplicationComposers(instance);
            composers.deployApp(instance);
            try {
                statement.evaluate();
            } finally {
                composers.stopApplication();
                thread.setContextClassLoader(old);
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers)

Example 4 with ApplicationComposers

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

the class CustomObjectNameMBeanTest method run.

@Test
public void run() throws Exception {
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    mbeanState(server, false);
    new ApplicationComposers(this).evaluate(this, new Runnable() {

        @Override
        public void run() {
            try {
                mbeanState(server, true);
                assertEquals(1, server.getAttribute(new ObjectName("openejb.user.mbeans:application=openejb,group=org.apache.openejb.config,name=DefaultName"), "value"));
                assertEquals(2, server.getAttribute(new ObjectName("foo:type=bar,custom=yes"), "value"));
            } catch (final Exception e) {
                throw new IllegalStateException(e);
            }
        }
    });
    mbeanState(server, false);
}
Also used : ApplicationComposers(org.apache.openejb.testing.ApplicationComposers) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 5 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)

Aggregations

ApplicationComposers (org.apache.openejb.testing.ApplicationComposers)8 Test (org.junit.Test)6 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