tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: TOMEE-1848 @Configuration support for TomEEEmbeddedSingleRunner
Date Wed, 22 Jun 2016 05:33:51 GMT
Repository: tomee
Updated Branches:
  refs/heads/master 111989beb -> 30e42e463


TOMEE-1848 @Configuration support for TomEEEmbeddedSingleRunner


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/30e42e46
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/30e42e46
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/30e42e46

Branch: refs/heads/master
Commit: 30e42e46313fbb3f952114ce0f4ad65ca92e6694
Parents: 111989b
Author: Romain manni-Bucau <rmannibucau@gmail.com>
Authored: Wed Jun 22 07:33:30 2016 +0200
Committer: Romain manni-Bucau <rmannibucau@gmail.com>
Committed: Wed Jun 22 07:33:30 2016 +0200

----------------------------------------------------------------------
 .../junit/TomEEEmbeddedSingleRunner.java        | 25 ++++++++++++++++++++
 .../embedded/SingleInstanceRunnerTest.java      |  8 +++++++
 2 files changed, 33 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/30e42e46/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedSingleRunner.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedSingleRunner.java
b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedSingleRunner.java
index 4635e2f..b223663 100644
--- a/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedSingleRunner.java
+++ b/tomee/tomee-embedded/src/main/java/org/apache/tomee/embedded/junit/TomEEEmbeddedSingleRunner.java
@@ -16,6 +16,7 @@
  */
 package org.apache.tomee.embedded.junit;
 
+import org.apache.openejb.config.sys.Openejb;
 import org.apache.openejb.testing.Application;
 import org.apache.openejb.testing.Classes;
 import org.apache.openejb.testing.ContainerProperties;
@@ -26,6 +27,7 @@ import org.apache.tomee.embedded.Container;
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.inject.OWBInjector;
 import org.apache.xbean.finder.AnnotationFinder;
+import org.apache.xbean.finder.archive.ClassesArchive;
 import org.apache.xbean.finder.archive.FileArchive;
 import org.junit.rules.MethodRule;
 import org.junit.rules.TestRule;
@@ -43,6 +45,7 @@ import java.io.IOException;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
 import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -50,11 +53,13 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.concurrent.atomic.AtomicReference;
 
 import static java.lang.annotation.ElementType.TYPE;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import static org.apache.openejb.loader.JarLocation.jarLocation;
+import static org.apache.openejb.util.Classes.ancestors;
 
 /**
  * see org.apache.tomee.embedded.SingleInstanceRunnerTest for a sample.
@@ -65,6 +70,7 @@ import static org.apache.openejb.loader.JarLocation.jarLocation;
  * Note: @Application classes are only searched in the same jar as the test.
  *
  * Model:
+ * - @Configuration: programmatic properties - note injections don't work there.
  * - @Classes: only context value is used.
  * - @ContainerProperties: to configure the container
  * - @WebResource: first value can be used to set the docBase (other values are ignored)
@@ -181,6 +187,7 @@ public class TomEEEmbeddedSingleRunner extends BlockJUnit4ClassRunner
{
                 started = true;
 
                 final Class<?> appClass = APP.get().getClass();
+                final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(ancestors(appClass)));
 
                 // setup the container config reading class annotation, using a randome http
port and deploying the classpath
                 final Configuration configuration = new Configuration();
@@ -191,6 +198,24 @@ public class TomEEEmbeddedSingleRunner extends BlockJUnit4ClassRunner
{
                     }
                 }
 
+                Openejb openejb = null;
+                final List<Method> annotatedMethods = finder.findAnnotatedMethods(org.apache.openejb.testing.Configuration.class);
+                if (annotatedMethods.size() > 1) {
+                    throw new IllegalArgumentException("Only one @Configuration is supported:
" + annotatedMethods);
+                }
+                for (final Method m : annotatedMethods) {
+                    final Object o = m.invoke(APP.get());
+                    if (Properties.class.isInstance(o)) {
+                        final Properties properties = Properties.class.cast(o);
+                        if (configuration.getProperties() == null) {
+                            configuration.setProperties(new Properties());
+                        }
+                        configuration.getProperties().putAll(properties);
+                    } else {
+                        throw new IllegalArgumentException("Unsupported " + o + " for @Configuration");
+                    }
+                }
+
                 final Collection<Closeable> postTasks = new ArrayList<>();
                 final LifecycleTasks tasks = appClass.getAnnotation(LifecycleTasks.class);
                 if (tasks != null) {

http://git-wip-us.apache.org/repos/asf/tomee/blob/30e42e46/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
----------------------------------------------------------------------
diff --git a/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
b/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
index 5d9da78..295d7cc 100644
--- a/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
+++ b/tomee/tomee-embedded/src/test/java/org/apache/tomee/embedded/SingleInstanceRunnerTest.java
@@ -22,6 +22,7 @@ import org.apache.openejb.testing.Application;
 import org.apache.openejb.testing.Classes;
 import org.apache.openejb.testing.ContainerProperties;
 import org.apache.openejb.testing.RandomPort;
+import org.apache.openejb.testng.PropertiesBuilder;
 import org.apache.tomee.embedded.junit.TomEEEmbeddedSingleRunner;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -30,6 +31,7 @@ import org.junit.runner.RunWith;
 import java.io.Closeable;
 import java.io.IOException;
 import java.net.URL;
+import java.util.Properties;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
@@ -51,6 +53,7 @@ public class SingleInstanceRunnerTest {
     public void run() {
         assertNotNull(SystemInstance.get().getComponent(Assembler.class));
         assertEquals("set", SystemInstance.get().getProperty("t"));
+        assertEquals("p", SystemInstance.get().getProperty("prog"));
         assertEquals("128463", SystemInstance.get().getProperty("my.server.port"));
         assertNotEquals(8080, app.port);
         assertTrue(app.base.toExternalForm().endsWith("/app"));
@@ -67,6 +70,11 @@ public class SingleInstanceRunnerTest {
 
         @RandomPort("http")
         private URL base;
+
+        @org.apache.openejb.testing.Configuration
+        public Properties add() {
+            return new PropertiesBuilder().p("prog", "p").build();
+        }
     }
 
     public static class MyTask implements TomEEEmbeddedSingleRunner.LifecycleTask {


Mime
View raw message