You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2013/10/14 11:26:32 UTC

svn commit: r1531832 - in /tomee/tomee/trunk/maven/tomee-maven-plugin: ./ src/main/java/org/apache/openejb/maven/plugin/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/openejb/ src/test/java/org/apache/op...

Author: rmannibucau
Date: Mon Oct 14 09:26:31 2013
New Revision: 1531832

URL: http://svn.apache.org/r1531832
Log:
adding a basic way to test tomee maven plugin

Added:
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Config.java
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Url.java
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/TomEEMavenPluginTest.java
Modified:
    tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml
    tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java

Modified: tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml?rev=1531832&r1=1531831&r2=1531832&view=diff
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml (original)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/pom.xml Mon Oct 14 09:26:31 2013
@@ -62,6 +62,15 @@
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
     </dependency>
+
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>apache-tomee</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+      <classifier>webprofile</classifier>
+      <type>zip</type>
+    </dependency>
   </dependencies>
 
   <properties>

Modified: tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java?rev=1531832&r1=1531831&r2=1531832&view=diff
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java (original)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/main/java/org/apache/openejb/maven/plugin/AbstractTomEEMojo.java Mon Oct 14 09:26:31 2013
@@ -1,4 +1,3 @@
-package org.apache.openejb.maven.plugin;
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  *  contributor license agreements.  See the NOTICE file distributed with
@@ -15,6 +14,7 @@ package org.apache.openejb.maven.plugin;
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  */
+package org.apache.openejb.maven.plugin;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.factory.ArtifactFactory;
@@ -119,10 +119,10 @@ public abstract class AbstractTomEEMojo 
     protected String tomeeClassifier;
 
     @Parameter(property = "tomee-plugin.shutdown", defaultValue = "8005")
-    protected int tomeeShutdownPort = 8005;
+    protected int tomeeShutdownPort;
 
     @Parameter(property = "tomee-plugin.ajp", defaultValue = "8009")
-    protected int tomeeAjpPort = 8009;
+    protected int tomeeAjpPort;
 
     @Parameter(property = "tomee-plugin.https")
     protected Integer tomeeHttpsPort;
@@ -182,7 +182,7 @@ public abstract class AbstractTomEEMojo 
     private List<String> classpaths;
 
     @Parameter(property = "tomee-plugin.quick-session", defaultValue = "true")
-    private boolean quickSession;
+    protected boolean quickSession;
 
     @Parameter(property = "tomee-plugin.force-reloadable", defaultValue = "false")
     protected boolean forceReloadable;
@@ -370,7 +370,7 @@ public abstract class AbstractTomEEMojo 
             getLog().warn("can't create '" + destParent.getPath() + "'");
         }
 
-        for (String file : files) {
+        for (final String file : files) {
             updateLib(file, destParent, defaultType);
         }
     }
@@ -827,7 +827,7 @@ public abstract class AbstractTomEEMojo 
         }
     }
 
-    private void unzip(File mvnTomEE, File catalinaBase) {
+    private void unzip(final File mvnTomEE, final File catalinaBase) {
         ZipFile in = null;
         try {
             in = new ZipFile(mvnTomEE);

Added: tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Config.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Config.java?rev=1531832&view=auto
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Config.java (added)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Config.java Mon Oct 14 09:26:31 2013
@@ -0,0 +1,27 @@
+/*
+ * 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.maven.plugin;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Config {
+}

Added: tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java?rev=1531832&view=auto
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java (added)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/TomEEMavenPluginRule.java Mon Oct 14 09:26:31 2013
@@ -0,0 +1,244 @@
+/*
+ * 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.maven.plugin;
+
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.factory.ArtifactFactory;
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.DefaultArtifactRepository;
+import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.settings.Settings;
+import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader;
+import org.apache.openejb.config.RemoteServer;
+import org.apache.openejb.loader.Files;
+import org.apache.openejb.util.NetworkUtil;
+import org.apache.openejb.util.OpenEjbVersion;
+import org.junit.rules.MethodRule;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.Statement;
+
+import java.io.File;
+import java.io.FileReader;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * Allows to write unit tests on TomEE Maven Plugin.
+ *
+ * The basic usage is:
+ *
+ *
+public class TomEEMavenPluginTest {
+  @Rule
+  public TomEEMavenPluginRule TMPRule = new TomEEMavenPluginRule();
+
+  @Url // get the base http url injected
+  private String url;
+
+  @Config
+  private String tomeeHost = "localhost";
+
+  @Test
+  public void simpleStart() throws Exception {
+    assertThat(IO.slurp(new URL(url + "/docs")), containsString("Apache Tomcat"));
+  }
+}
+ *
+ * @Url specifies you want the http url base injected (without any webapp context)
+ * @Config specifies you want to configure the backend mojo with the value specifies on the test instance
+ */
+public class TomEEMavenPluginRule implements MethodRule {
+    @Override
+    public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
+        return new RunTest(target, base);
+    }
+
+    public class RunTest extends Statement {
+        // The TestCase instance
+        private final Object testInstance;
+        private final Statement next;
+
+        public RunTest(final Object testInstance, final Statement next) {
+            this.testInstance = testInstance;
+            this.next = next;
+        }
+
+        @Override
+        public void evaluate() throws Throwable {
+            final TestTomEEMojo testMojo = newMojo();
+
+            for (final Field f : testInstance.getClass().getDeclaredFields()) {
+                if (f.getAnnotation(Url.class) != null) {
+                    f.setAccessible(true);
+                    f.set(testInstance, "http://localhost:" + testMojo.tomeeHttpPort);
+                } else if (f.getAnnotation(Config.class) != null) {
+                    f.setAccessible(true);
+
+                    final Field mojoField = AbstractTomEEMojo.class.getDeclaredField(f.getName());
+                    mojoField.setAccessible(true);
+                    f.set(mojoField, f.get(testInstance));
+                }
+            }
+
+            testMojo.runTest();
+        }
+
+        private TestTomEEMojo newMojo() {
+            return defaults(new TestTomEEMojo() {
+                @Override
+                protected void asserts() throws Throwable {
+                    next.evaluate();
+                }
+            });
+        }
+    }
+
+    protected static abstract class TestTomEEMojo extends StartTomEEMojo {
+        final AtomicReference<Throwable> ex = new AtomicReference<Throwable>();
+
+        protected abstract void asserts() throws Throwable;
+
+        public void runTest() throws Throwable {
+            execute();
+            if (ex.get() != null) {
+                throw ex.get();
+            }
+        }
+
+        protected void serverCmd(final RemoteServer server, final List<String> strings) {
+            super.serverCmd(server, strings);
+
+            // the test
+            try {
+                asserts();
+            } catch (final Throwable e) {
+                ex.set(e);
+            } finally {
+                destroy(this);
+            }
+        }
+    }
+
+    protected static <T extends AbstractTomEEMojo> T defaults(final T tomEEMojo) {
+        // settings
+        final File settingsXml = new File(System.getProperty("user.home") + "/.m2/settings.xml");
+        if (settingsXml.exists()) {
+            try {
+                final FileReader reader = new FileReader(settingsXml);
+                try {
+                    tomEEMojo.settings = new SettingsXpp3Reader().read(reader, false);
+                } finally {
+                    reader.close();
+                }
+            } catch (final Exception e) {
+                // no-op
+            }
+        }
+        if (tomEEMojo.settings == null) {
+            tomEEMojo.settings = new Settings();
+        }
+        tomEEMojo.settings.setOffline(true);
+        if (tomEEMojo.settings.getLocalRepository() == null) {
+            tomEEMojo.settings.setLocalRepository(System.getProperty("user.home") + "/.m2/repository");
+        }
+
+        // we don't deploy anything by default
+        tomEEMojo.skipCurrentProject = true;
+
+        // our well known web profile ;)
+        tomEEMojo.tomeeGroupId = "org.apache.openejb";
+        tomEEMojo.tomeeArtifactId = "apache-tomee";
+        tomEEMojo.tomeeVersion = "1" + OpenEjbVersion.get().getVersion().substring(1);
+        tomEEMojo.tomeeClassifier = "webprofile";
+        tomEEMojo.tomeeType = "zip";
+
+        // target config
+        tomEEMojo.catalinaBase = new File("target/mvn-test");
+        Files.mkdirs(tomEEMojo.catalinaBase);
+
+        // some defaults
+        tomEEMojo.simpleLog = true;
+        tomEEMojo.quickSession = true;
+
+        tomEEMojo.libDir = "lib";
+        tomEEMojo.webappDir = "webapps";
+        tomEEMojo.appDir = "apps";
+
+        tomEEMojo.bin = new File(tomEEMojo.catalinaBase.getPath() + "-bin");
+        tomEEMojo.config = new File(tomEEMojo.catalinaBase.getPath() + "-conf");
+        tomEEMojo.lib = new File(tomEEMojo.catalinaBase.getPath() + "-lib");
+
+        tomEEMojo.tomeeHttpsPort = NetworkUtil.getNextAvailablePort();
+        tomEEMojo.tomeeHttpPort = NetworkUtil.getNextAvailablePort();
+        tomEEMojo.tomeeAjpPort = NetworkUtil.getNextAvailablePort();
+        tomEEMojo.tomeeShutdownPort = NetworkUtil.getNextAvailablePort();
+        tomEEMojo.tomeeHost = "localhost";
+
+        tomEEMojo.useConsole = true;
+        tomEEMojo.checkStarted = true;
+
+        // we mock all the artifact resolution in test
+        tomEEMojo.remoteRepos = new LinkedList<ArtifactRepository>();
+        tomEEMojo.local = new DefaultArtifactRepository("local", tomEEMojo.settings.getLocalRepository(), new DefaultRepositoryLayout());
+
+        tomEEMojo.factory = ArtifactFactory.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{ArtifactFactory.class}, new InvocationHandler() {
+            @Override
+            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+                return new DefaultArtifact(
+                    tomEEMojo.tomeeGroupId,
+                    tomEEMojo.tomeeArtifactId,
+                    VersionRange.createFromVersion(tomEEMojo.tomeeVersion),
+                    "provided",
+                    tomEEMojo.tomeeType,
+                    tomEEMojo.tomeeClassifier,
+                    null) {
+                    @Override
+                    public File getFile() {
+                        return new File(
+                            tomEEMojo.settings.getLocalRepository(),
+                            getGroupId().replace('.', '/')
+                                + '/' + getArtifactId().replace('.', '/')
+                                + '/' + getVersion()
+                                + '/' + getArtifactId().replace('.', '/') + '-' + getVersion() + '-' + getClassifier() + '.' + getType());
+                    }
+                };
+            }
+        }));
+
+        tomEEMojo.resolver = ArtifactResolver.class.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ArtifactResolver.class }, new InvocationHandler() {
+            @Override
+            public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+                return null;
+            }
+        }));
+
+        return tomEEMojo;
+    }
+
+    protected static void destroy(final AbstractTomEEMojo run) {
+        if (run.server != null) {
+            run.server.destroy();
+        }
+    }
+}

Added: tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Url.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Url.java?rev=1531832&view=auto
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Url.java (added)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/Url.java Mon Oct 14 09:26:31 2013
@@ -0,0 +1,27 @@
+/*
+ * 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.maven.plugin;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface Url {
+}

Added: tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/TomEEMavenPluginTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/TomEEMavenPluginTest.java?rev=1531832&view=auto
==============================================================================
--- tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/TomEEMavenPluginTest.java (added)
+++ tomee/tomee/trunk/maven/tomee-maven-plugin/src/test/java/org/apache/openejb/maven/plugin/test/TomEEMavenPluginTest.java Mon Oct 14 09:26:31 2013
@@ -0,0 +1,41 @@
+/*
+ * 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.maven.plugin.test;
+
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.maven.plugin.TomEEMavenPluginRule;
+import org.apache.openejb.maven.plugin.Url;
+import org.junit.Rule;
+import org.junit.Test;
+
+import java.net.URL;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThat;
+
+public class TomEEMavenPluginTest {
+    @Rule
+    public TomEEMavenPluginRule TMPRule = new TomEEMavenPluginRule();
+
+    @Url
+    private String url;
+
+    @Test
+    public void simpleStart() throws Exception {
+        assertThat(IO.slurp(new URL(url + "/docs")), containsString("Apache Tomcat"));
+    }
+}