SingleApplicationComposer rule
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/4655c053 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/4655c053 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/4655c053 Branch: refs/heads/master Commit: 4655c053b4d91046131b47c2f8c3087517e1d2a3 Parents: bf1b5d0 Author: Romain manni-Bucau <rmannibu...@gmail.com> Authored: Fri Feb 12 18:48:00 2016 +0100 Committer: Romain manni-Bucau <rmannibu...@gmail.com> Committed: Fri Feb 12 18:48:00 2016 +0100 ---------------------------------------------------------------------- .../SingleApplicationComposerRunner.java | 133 ++++++++++++------- 1 file changed, 83 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/4655c053/container/openejb-core/src/main/java/org/apache/openejb/testing/SingleApplicationComposerRunner.java ---------------------------------------------------------------------- diff --git a/container/openejb-core/src/main/java/org/apache/openejb/testing/SingleApplicationComposerRunner.java b/container/openejb-core/src/main/java/org/apache/openejb/testing/SingleApplicationComposerRunner.java index 1758ec6..dcbfe9a 100644 --- a/container/openejb-core/src/main/java/org/apache/openejb/testing/SingleApplicationComposerRunner.java +++ b/container/openejb-core/src/main/java/org/apache/openejb/testing/SingleApplicationComposerRunner.java @@ -22,6 +22,9 @@ import org.apache.webbeans.inject.OWBInjector; import org.apache.xbean.finder.AnnotationFinder; import org.apache.xbean.finder.archive.FileArchive; import org.junit.rules.MethodRule; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runner.notification.RunListener; import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; @@ -41,6 +44,34 @@ public class SingleApplicationComposerRunner extends BlockJUnit4ClassRunner { private static final AtomicReference<Object> APP = new AtomicReference<>(); private static final AtomicReference<Thread> HOOK = new AtomicReference<>(); + // use when you use another runner like Parameterized of JUnit + public static class Rule implements TestRule { + private final Object test; + + public Rule(final Object test) { + this.test = test; + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + start(test.getClass()); + composerInject(test); + base.evaluate(); + } + }; + } + } + + public static class Start extends RunListener { + @Override + public void testStarted(final Description description) throws Exception { + start(null); + } + } + public static void setApp(final Object o) { APP.set(o); } @@ -57,7 +88,28 @@ public class SingleApplicationComposerRunner extends BlockJUnit4ClassRunner { public SingleApplicationComposerRunner(final Class<?> klass) throws InitializationError { super(klass); + } + + @Override + protected List<MethodRule> rules(final Object test) { + final List<MethodRule> rules = super.rules(test); + rules.add(new MethodRule() { + @Override + public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + start(getTestClass().getJavaClass()); + composerInject(target); + base.evaluate(); + } + }; + } + }); + return rules; + } + private static void start(final Class<?> marker) throws Exception { if (APP.get() == null) { final Class<?> type; final String typeStr = System.getProperty("tomee.application-composer.application"); @@ -67,9 +119,11 @@ public class SingleApplicationComposerRunner extends BlockJUnit4ClassRunner { } catch (final ClassNotFoundException e) { throw new IllegalArgumentException(e); } + } else if (marker == null) { + throw new IllegalArgumentException("set tomee.application-composer.application system property or add a marker to the rule or runner"); } else { final Iterator<Class<?>> descriptors = - new AnnotationFinder(new FileArchive(Thread.currentThread().getContextClassLoader(), jarLocation(klass)), false) + new AnnotationFinder(new FileArchive(Thread.currentThread().getContextClassLoader(), jarLocation(marker)), false) .findAnnotatedClasses(Application.class).iterator(); if (!descriptors.hasNext()) { throw new IllegalArgumentException("No descriptor class using @Application"); @@ -85,59 +139,38 @@ public class SingleApplicationComposerRunner extends BlockJUnit4ClassRunner { throw new IllegalStateException(e); } } - } - - @Override - protected List<MethodRule> rules(final Object test) { - final List<MethodRule> rules = super.rules(test); - rules.add(new MethodRule() { - @Override - public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - start(); - OWBInjector.inject(WebBeansContext.currentInstance().getBeanManagerImpl(), target, null); - composerInject(target); - base.evaluate(); - } - - private void start() throws Exception { - if (!started) { - final Object app = APP.get(); - new ApplicationComposers(app.getClass()) { - @Override - public void deployApp(final Object inputTestInstance) throws Exception { - super.deployApp(inputTestInstance); - if (!started) { - final ThreadContext previous = ThreadContext.getThreadContext(); // done here for logging - final ApplicationComposers comp = this; - final Thread hook = new Thread() { - @Override - public void run() { - try { - comp.after(); - } catch (final Exception e) { - ThreadContext.exit(previous); - throw new IllegalStateException(e); - } - } - }; - HOOK.set(hook); - Runtime.getRuntime().addShutdownHook(hook); - started = true; - } + if (!started) { + final Object app = APP.get(); + new ApplicationComposers(app.getClass()) { + @Override + public void deployApp(final Object inputTestInstance) throws Exception { + super.deployApp(inputTestInstance); + if (!started) { + final ThreadContext previous = ThreadContext.getThreadContext(); // done here for logging + final ApplicationComposers comp = this; + final Thread hook = new Thread() { + @Override + public void run() { + try { + comp.after(); + } catch (final Exception e) { + ThreadContext.exit(previous); + throw new IllegalStateException(e); } - }.before(app); - } + } + }; + HOOK.set(hook); + Runtime.getRuntime().addShutdownHook(hook); + started = true; } - }; - } - }); - return rules; + } + }.before(app); + } } - private void composerInject(final Object target) throws IllegalAccessException { + private static void composerInject(final Object target) throws IllegalAccessException { + OWBInjector.inject(WebBeansContext.currentInstance().getBeanManagerImpl(), target, null); + final Object app = APP.get(); final Class<?> aClass = target.getClass(); for (final Field f : aClass.getDeclaredFields()) {