Repository: tomee Updated Branches: refs/heads/develop c1d8d4364 -> e79c813e2
TOMEE-1517 TomEEEmbeddedRule Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/e79c813e Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/e79c813e Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/e79c813e Branch: refs/heads/develop Commit: e79c813e2c51fb1898258e7308d72c0fcd7d8799 Parents: c1d8d43 Author: Romain Manni-Bucau <[email protected]> Authored: Sun Feb 22 20:05:25 2015 +0100 Committer: Romain Manni-Bucau <[email protected]> Committed: Sun Feb 22 20:05:25 2015 +0100 ---------------------------------------------------------------------- .../apache/openejb/junit/jee/InjectRule.java | 5 + tomee/tomee-embedded/pom.xml | 2 +- .../apache/tomee/embedded/Configuration.java | 5 + .../org/apache/tomee/embedded/Container.java | 22 ++-- .../tomee/embedded/junit/TomEEEmbeddedRule.java | 104 +++++++++++++++++++ 5 files changed, 131 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/e79c813e/container/openejb-junit/src/main/java/org/apache/openejb/junit/jee/InjectRule.java ---------------------------------------------------------------------- diff --git a/container/openejb-junit/src/main/java/org/apache/openejb/junit/jee/InjectRule.java b/container/openejb-junit/src/main/java/org/apache/openejb/junit/jee/InjectRule.java index f68df85..0c639a5 100644 --- a/container/openejb-junit/src/main/java/org/apache/openejb/junit/jee/InjectRule.java +++ b/container/openejb-junit/src/main/java/org/apache/openejb/junit/jee/InjectRule.java @@ -26,6 +26,11 @@ public class InjectRule implements TestRule { private final StartingStatement startingStatement; private final Object test; + public InjectRule(final Object test) { + this.test = test; + this.startingStatement = null; + } + public InjectRule(final Object target, final EJBContainerRule rule) { this(target, rule.getStartingStatement()); } http://git-wip-us.apache.org/repos/asf/tomee/blob/e79c813e/tomee/tomee-embedded/pom.xml ---------------------------------------------------------------------- diff --git a/tomee/tomee-embedded/pom.xml b/tomee/tomee-embedded/pom.xml index 1093246..023442c 100644 --- a/tomee/tomee-embedded/pom.xml +++ b/tomee/tomee-embedded/pom.xml @@ -235,7 +235,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <scope>test</scope> + <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jdt.core.compiler</groupId> http://git-wip-us.apache.org/repos/asf/tomee/blob/e79c813e/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Configuration.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Configuration.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Configuration.java index d4549e7..e55f1d0 100644 --- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Configuration.java +++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Configuration.java @@ -81,6 +81,11 @@ public class Configuration { return dir; } + public Configuration dir(final String dir) { + setDir(dir); + return this; + } + public void setDir(final String dir) { this.dir = dir; } http://git-wip-us.apache.org/repos/asf/tomee/blob/e79c813e/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java index 6b51e34..0618b96 100644 --- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java +++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/Container.java @@ -96,6 +96,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; @@ -103,6 +104,7 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; /** * @version $Rev$ $Date$ @@ -143,6 +145,10 @@ public class Container implements AutoCloseable { } public Container deployClasspathAsWebApp(final String context, final File docBase, final String... dependencies) { + return deployClasspathAsWebApp(context, docBase, Collections.<String>emptyList(), dependencies); + } + + public Container deployClasspathAsWebApp(final String context, final File docBase, final List<String> callers, final String... dependencies) { final List<URL> jarList = new DeploymentsResolver.ClasspathSearcher().loadUrls(Thread.currentThread().getContextClassLoader()).getUrls(); if (dependencies != null) { for (final String dep : dependencies) { @@ -164,7 +170,8 @@ public class Container implements AutoCloseable { NewLoaderLogic.applyBuiltinExcludes( new UrlSet(jarList), NewLoaderLogic.ADDITIONAL_INCLUDE == null ? null : Filters.prefixes(NewLoaderLogic.ADDITIONAL_INCLUDE.split("[ \t\n\n]*,[ \t\n\n]*"))).getUrls(), - docBase); + docBase, + callers == null || callers.isEmpty() ? null : callers.toArray(new String[callers.size()])); } catch (final MalformedURLException e) { return deployPathsAsWebapp(context, jarList, docBase); } @@ -176,7 +183,7 @@ public class Container implements AutoCloseable { throw new IllegalArgumentException("The file does not have content"); } - final List<URL> urls = new ArrayList<URL>(jarList.length); + final List<URL> urls = new ArrayList<>(jarList.length); for (final File jar : jarList) { urls.addAll(asList(jar.toURI().toURL())); } @@ -186,7 +193,7 @@ public class Container implements AutoCloseable { } } - public Container deployPathsAsWebapp(final String context, final List<URL> jarList, final File docBase) { + public Container deployPathsAsWebapp(final String context, final List<URL> jarList, final File docBase, final String... additionalCallers) { final ClassLoader loader = Thread.currentThread().getContextClassLoader(); final SystemInstance systemInstance = SystemInstance.get(); @@ -229,7 +236,7 @@ public class Container implements AutoCloseable { throw new IllegalStateException(e); } - addCallersAsEjbModule(loader, app); + addCallersAsEjbModule(loader, app, additionalCallers); systemInstance.addObserver(new StandardContextCustomizer(webModule)); @@ -243,8 +250,11 @@ public class Container implements AutoCloseable { return this; } - private static void addCallersAsEjbModule(final ClassLoader loader, final AppModule app) { - final Set<String> callers = NewLoaderLogic.callers(Filters.classes(Container.class.getName(), "org.apache.openejb.maven.plugins.TomEEEmbeddedMojo")); + private static void addCallersAsEjbModule(final ClassLoader loader, final AppModule app, final String... additionalCallers) { + final Set<String> callers = new HashSet<>(NewLoaderLogic.callers(Filters.classes(Container.class.getName(), "org.apache.openejb.maven.plugins.TomEEEmbeddedMojo"))); + if (additionalCallers != null && additionalCallers.length > 0) { + callers.addAll(asList(additionalCallers)); + } if (callers.isEmpty()) { return; } http://git-wip-us.apache.org/repos/asf/tomee/blob/e79c813e/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedRule.java ---------------------------------------------------------------------- diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedRule.java b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedRule.java new file mode 100644 index 0000000..362d004 --- /dev/null +++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedRule.java @@ -0,0 +1,104 @@ +/** + * 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.tomee.embedded.junit; + +import org.apache.openejb.util.NetworkUtil; +import org.apache.tomee.embedded.Configuration; +import org.apache.tomee.embedded.Container; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import java.io.File; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +public class TomEEEmbeddedRule implements TestRule { + private final Configuration configuration; + private final File docBase; + private final String context; + private final Collection<Object> injects = new LinkedList<>(); + + public TomEEEmbeddedRule() { + this(new Configuration().http(NetworkUtil.getNextAvailablePort()), ""); + } + + public TomEEEmbeddedRule(final Configuration configuration, final String context) { + this.configuration = configuration; + this.context = context; + + final File mvnDocBase = new File("src/main/webapp"); + if (mvnDocBase.isDirectory()) { + docBase = mvnDocBase; + } else { + docBase = null; + } + } + + public TomEEEmbeddedRule(final Configuration configuration, final String context, final File docBase) { + this.configuration = configuration; + this.docBase = docBase; + this.context = context; + } + + public TomEEEmbeddedRule injectOn(final Object instance) { + this.injects.add(instance); + return this; + } + + public TomEEEmbeddedRule stopInjectionOn(final Object instance) { + this.injects.remove(instance); + return this; + } + + public Configuration getConfiguration() { + return configuration; + } + + public int getPort() { + return configuration.getHttpPort(); + } + + @Override + public Statement apply(final Statement statement, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + try (final Container container = new Container(configuration) + .deployClasspathAsWebApp(context, docBase, toCallers())) { + for (final Object o : injects) { + container.inject(o); + } + statement.evaluate(); + } + } + }; + } + + private List<String> toCallers() { + if (injects.isEmpty()) { + return null; + } + final Collection<String> callers = new HashSet<>(); + for (final Object o : injects) { + callers.add(o.getClass().getName()); + } + return new LinkedList<>(callers); + } +}
