Repository: tomee Updated Branches: refs/heads/develop d62af41b4 -> d64754772
TOMEE-1494 adding run utility method to ApplicationComposers Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/d6475477 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/d6475477 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/d6475477 Branch: refs/heads/develop Commit: d647547727cd39e39f6092ac93fb25a13c1d45b3 Parents: d62af41 Author: Romain Manni-Bucau <[email protected]> Authored: Sun Jan 18 17:14:32 2015 +0100 Committer: Romain Manni-Bucau <[email protected]> Committed: Sun Jan 18 17:14:32 2015 +0100 ---------------------------------------------------------------------- .../openejb/testing/ApplicationComposers.java | 113 ++++++++++++++----- .../testing/ApplicationComposersTest.java | 45 ++++++++ .../apache/openejb/testing/app/Application.java | 68 +++++++++++ 3 files changed, 198 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/d6475477/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java index 46571ed..f130cf2 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/ApplicationComposers.java @@ -91,6 +91,7 @@ import org.apache.xbean.finder.archive.CompositeArchive; import org.apache.xbean.finder.archive.JarArchive; import org.xml.sax.InputSource; +import javax.annotation.PostConstruct; import javax.enterprise.context.ConversationScoped; import javax.enterprise.context.RequestScoped; import javax.enterprise.context.SessionScoped; @@ -102,6 +103,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -117,6 +119,7 @@ import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; import static java.util.Arrays.asList; import static org.apache.openejb.config.DeploymentFilterable.DEPLOYMENTS_CLASSPATH_PROPERTY; @@ -151,6 +154,7 @@ public final class ApplicationComposers { private MockHttpSession session; private MockServletContext servletContext; private final Collection<String> globalJndiEntries = new ArrayList<>(); + private final Collection<Runnable> afterRunnables = new ArrayList<>(); public ApplicationComposers(final Object... modules) { this(modules[0].getClass(), modules); @@ -832,6 +836,9 @@ public final class ApplicationComposers { field.set(inputTestInstance, new InitialContext(new Properties() {{ setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName()); }})); + } else if (ApplicationComposers.class.isAssignableFrom(type)) { + field.setAccessible(true); + field.set(inputTestInstance, this); } else if (ContextProvider.class.isAssignableFrom(type)) { RESTResourceFinder finder = SystemInstance.get().getComponent(RESTResourceFinder.class); if (finder == null || !ContextProvider.class.isInstance(finder)) { @@ -1070,49 +1077,55 @@ public final class ApplicationComposers { } public void after() throws Exception { - if (assembler != null) { - final ContextsService contextsService = appContext.getWebBeansContext().getContextsService(); - contextsService.endContext(SessionScoped.class, session); - contextsService.endContext(RequestScoped.class, null); - contextsService.endContext(ConversationScoped.class, null); + try { + if (assembler != null) { + final ContextsService contextsService = appContext.getWebBeansContext().getContextsService(); + contextsService.endContext(SessionScoped.class, session); + contextsService.endContext(RequestScoped.class, null); + contextsService.endContext(ConversationScoped.class, null); - try { - assembler.destroyApplication(appInfo.path); - } catch (final Exception e) { - // no-op - } + try { + assembler.destroyApplication(appInfo.path); + } catch (final Exception e) { + // no-op + } - final ContainerSystem component = SystemInstance.get().getComponent(ContainerSystem.class); + final ContainerSystem component = SystemInstance.get().getComponent(ContainerSystem.class); - if (null != component) { - final Context context = component.getJNDIContext(); + if (null != component) { + final Context context = component.getJNDIContext(); - for (final String entry : globalJndiEntries) { - context.unbind(entry); + for (final String entry : globalJndiEntries) { + context.unbind(entry); + } + } + + globalJndiEntries.clear(); + + if (mockCdiContexts()) { + try { + ScopeHelper.stopContexts(contextsService, servletContext, session); + } catch (final Exception e) { + // no-op + } } } - globalJndiEntries.clear(); + if (serviceManager != null) { - if (mockCdiContexts()) { try { - ScopeHelper.stopContexts(contextsService, servletContext, session); - } catch (final Exception e) { + serviceManager.stop(); + } catch (final RuntimeException ignored) { // no-op } } - } - if (serviceManager != null) { - - try { - serviceManager.stop(); - } catch (final RuntimeException ignored) { - // no-op + OpenEJB.destroy(); + } finally { + for (final Runnable r : afterRunnables) { + r.run(); } } - - OpenEJB.destroy(); } private <M extends NamedModule> M setId(final M module, final Method method) { @@ -1255,4 +1268,48 @@ public final class ApplicationComposers { return list; } } + + public static void run(final Class<?> type, String... args) { + final ApplicationComposers composer = new ApplicationComposers(type); + try { + Object instance; + try { + final Constructor<?> constructor = type.getConstructor(String[].class); + instance = constructor.newInstance(new Object[] { args }); + } catch (final Exception e) { + instance = type.newInstance(); + } + composer.before(instance); + + final CountDownLatch latch = new CountDownLatch(1); + composer.afterRunnables.add(new Runnable() { + @Override + public void run() { + latch.countDown(); + } + }); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + try { + composer.after(); + } catch (final Exception e) { + // no-op + } + } + }); + + for (final Method m : type.getMethods()) { + if (m.getAnnotation(PostConstruct.class) != null && m.getParameterTypes().length == 0) { + m.invoke(instance); + } + } + + latch.await(); + } catch (final InterruptedException ie) { + Thread.interrupted(); + } catch (final Exception e) { + throw new OpenEJBRuntimeException(e); + } + } } http://git-wip-us.apache.org/repos/asf/tomee/blob/d6475477/container/openejb-core/src/test/java/org/apache/openejb/testing/ApplicationComposersTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/testing/ApplicationComposersTest.java b/container/openejb-core/src/test/java/org/apache/openejb/testing/ApplicationComposersTest.java new file mode 100644 index 0000000..9bec2e3 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/testing/ApplicationComposersTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openejb.testing; + +import org.apache.openejb.testing.app.Application; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ApplicationComposersTest { + public static boolean ok = false; + + @Before + public void reset() { + ok = false; + } + + @Test + public void run() throws InterruptedException { + final Thread t = new Thread() { + @Override + public void run() { + ApplicationComposers.run(Application.class, "a", "b"); + } + }; + t.start(); + t.join(); + assertTrue(ok); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/d6475477/container/openejb-core/src/test/java/org/apache/openejb/testing/app/Application.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/test/java/org/apache/openejb/testing/app/Application.java b/container/openejb-core/src/test/java/org/apache/openejb/testing/app/Application.java new file mode 100644 index 0000000..7f18708 --- /dev/null +++ b/container/openejb-core/src/test/java/org/apache/openejb/testing/app/Application.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.openejb.testing.app; + +import org.apache.openejb.testing.AppResource; +import org.apache.openejb.testing.ApplicationComposers; +import org.apache.openejb.testing.ApplicationComposersTest; +import org.apache.openejb.testing.Classes; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; + +import static java.util.Arrays.asList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +@Classes(cdi = true, value = Application.CdiBean.class) +public class Application { + private final String[] args; + + public Application(String[] args) { + this.args = args; + } + + @AppResource + private ApplicationComposers composers; + + @Inject + private CdiBean bean; + + @PostConstruct + public void init() { + try { + assertNotNull(bean); + assertEquals("run", bean.run()); + assertNotNull(args); + assertEquals(asList("a", "b"), asList(args)); + ApplicationComposersTest.ok = true; + } finally { + try { + composers.after(); + } catch (final Exception e) { + // no-op + } + } + } + + public static class CdiBean { + String run() { + return "run"; + } + } + +}
