You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/11/24 09:40:27 UTC

[sling-org-apache-sling-servlets-annotations-it] 01/01: SLING-9929 - use PaxExamServer for HTTP-driven tests

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch SLING-9929
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-annotations-it.git

commit 39a5e3393a4d67b67cff4ebce0f98e2b2f4dee7b
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Tue Nov 24 10:33:46 2020 +0100

    SLING-9929 - use PaxExamServer for HTTP-driven tests
---
 pom.xml                                            | 142 +++++++++------------
 .../annotations/AnnotationsTestSupport.java        | 103 +++++++++++++++
 .../servlets/annotations/ServicePropertiesIT.java  |   2 +-
 .../annotations/ServletRegistrationIT.java         |  10 +-
 .../sling/servlets/annotations/TestSupport.java    |  69 ----------
 src/test/resources/logback.xml                     |  32 +++++
 6 files changed, 199 insertions(+), 159 deletions(-)

diff --git a/pom.xml b/pom.xml
index ece21f7..1faad96 100644
--- a/pom.xml
+++ b/pom.xml
@@ -38,9 +38,10 @@
     <!-- compile with java 7 -->
     <properties>
         <sling.java.version>8</sling.java.version>
-        <http.host>localhost</http.host>
-        <!-- start with -DkeepITServerRunning=true to allow to rerun ITs or inspect the server after the ITs have been executed there -->
-        <keepITServerRunning>false</keepITServerRunning>
+        <org.ops4j.pax.exam.version>4.13.3</org.ops4j.pax.exam.version>
+        <!-- additional options that can be passed to Pax before executing the tests -->
+        <pax.vm.options />
+        <bundle.filename>${basedir}/target/${project.build.finalName}.jar</bundle.filename>
     </properties>
 
     <build>
@@ -51,85 +52,6 @@
                 <version>5.0.0</version>
             </plugin>
             <plugin>
-                <!-- Find free ports to run our server -->
-                <groupId>org.codehaus.mojo</groupId>
-                <artifactId>build-helper-maven-plugin</artifactId>
-                <executions>
-                    <execution>
-                        <id>reserve-server-port</id>
-                        <goals>
-                            <goal>reserve-network-port</goal>
-                        </goals>
-                        <phase>pre-integration-test</phase>
-                        <configuration>
-                            <portNames>
-                                <!-- used port name must be stored in property because it must be used for the base url -->
-                                <portName>http.port</portName>
-                            </portNames>
-                        </configuration>
-                    </execution>
-                </executions>
-            </plugin>
-            <plugin>
-                <!-- the Sling instance is provisioned from the model in src/main/provisioning/it-model.txt -->
-                <groupId>org.apache.sling</groupId>
-                <artifactId>slingstart-maven-plugin</artifactId>
-                <!-- for https://issues.apache.org/jira/browse/SLING-7662 -->
-                <version>1.8.2</version>
-                <extensions>true</extensions>
-                <executions>
-                    <execution>
-                        <id>customise-starter</id>
-                        <goals>
-                            <goal>prepare-package</goal>
-                        </goals>
-                        <configuration>
-                            <modelDirectory>${project.basedir}/src/test/provisioning</modelDirectory>
-                            <usePomDependencies>true</usePomDependencies>
-                        </configuration>
-                    </execution>
-                    <execution>
-                        <id>package-starter</id>
-                        <goals>
-                            <goal>package</goal>
-                        </goals>
-                        <configuration>
-                            <attachArtifact>false</attachArtifact>
-                            <modelDirectory>${project.basedir}/src/test/provisioning</modelDirectory>
-                            <usePomDependencies>true</usePomDependencies>
-                        </configuration>
-                    </execution>
-                    <execution>
-                        <id>start-container-before-IT</id>
-                        <goals>
-                            <goal>start</goal>
-                        </goals>
-                    </execution>
-                    <execution>
-                        <id>stop-container-after-IT</id>
-                        <goals>
-                            <goal>stop</goal>
-                        </goals>
-                        <configuration>
-                            <shouldBlockUntilKeyIsPressed>${keepITServerRunning}</shouldBlockUntilKeyIsPressed>
-                        </configuration>
-                    </execution>
-                </executions>
-                <configuration>
-                    <servers>
-                        <!-- this configuration applies to both 'start' and 'stop' -->
-                        <server>
-                            <id>singleinstance</id>
-                            <port>${http.port}</port>
-                            <vmOpts>${sling.vm.options}</vmOpts>
-                            <stdOutFile>sling/logs/stdout.log</stdOutFile>
-                        </server>
-                    </servers>
-                    <!-- this configuration only applies to 'prepare-package' and 'package' -->
-                    <disableExtendingMavenClasspath>true</disableExtendingMavenClasspath>
-                </configuration>
-            </plugin>
-            <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-failsafe-plugin</artifactId>
                 <executions>
@@ -142,11 +64,15 @@
                 </executions>
                 <configuration>
                     <systemPropertyVariables>
-                        <baseUrl>http://${http.host}:${http.port}/</baseUrl>
-                        <bundleFile>${project.build.directory}/${project.build.finalName}.jar</bundleFile>
+                        <bundle.filename>${bundle.filename}</bundle.filename>
+                        <pax.vm.options>${pax.vm.options}</pax.vm.options>
                     </systemPropertyVariables>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.servicemix.tooling</groupId>
+                <artifactId>depends-maven-plugin</artifactId>
+            </plugin>
         </plugins>
     </build>
 
@@ -217,6 +143,54 @@
             <artifactId>osgi.core</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.framework</artifactId>
+            <version>6.0.3</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.paxexam</artifactId>
+            <version>3.1.0</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam</artifactId>
+            <version>${org.ops4j.pax.exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-cm</artifactId>
+            <version>${org.ops4j.pax.exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-container-forked</artifactId>
+            <version>${org.ops4j.pax.exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-junit4</artifactId>
+            <version>${org.ops4j.pax.exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.exam</groupId>
+            <artifactId>pax-exam-link-mvn</artifactId>
+            <version>${org.ops4j.pax.exam.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.ops4j.pax.url</groupId>
+            <artifactId>pax-url-wrap</artifactId>
+            <version>2.3.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
diff --git a/src/test/java/org/apache/sling/servlets/annotations/AnnotationsTestSupport.java b/src/test/java/org/apache/sling/servlets/annotations/AnnotationsTestSupport.java
new file mode 100644
index 0000000..f1efd80
--- /dev/null
+++ b/src/test/java/org/apache/sling/servlets/annotations/AnnotationsTestSupport.java
@@ -0,0 +1,103 @@
+/*
+ * 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.sling.servlets.annotations;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.servlets.annotations.services.PathBoundService;
+import org.apache.sling.testing.clients.osgi.OsgiConsoleClient;
+import org.apache.sling.testing.paxexam.TestSupport;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExamServer;
+import org.ops4j.pax.exam.options.extra.VMOption;
+
+import static org.apache.sling.testing.paxexam.SlingOptions.logback;
+import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar;
+import static org.ops4j.pax.exam.CoreOptions.composite;
+import static org.ops4j.pax.exam.CoreOptions.when;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.systemProperty;
+
+import java.net.URI;
+
+public class AnnotationsTestSupport extends TestSupport {
+
+    private final static int STARTUP_WAIT_SECONDS = 30;
+
+    protected OsgiConsoleClient CLIENT;
+    protected static int httpPort;
+
+    @ClassRule
+    public static PaxExamServer serverRule = new PaxExamServer();
+
+    public AnnotationsTestSupport() {
+        if(httpPort == 0) {
+            // findFreePort should probably be a static method
+            httpPort = findFreePort();
+        }
+    }
+
+    @Configuration
+    public Option[] configuration() throws Exception {
+
+        final String vmOpt = System.getProperty("pax.vm.options");
+        VMOption vmOption = null;
+        if (StringUtils.isNotEmpty(vmOpt)) {
+            vmOption = new VMOption(vmOpt);
+        }
+
+        final String jacocoOpt = System.getProperty("jacoco.command");
+        VMOption jacocoCommand = null;
+        if (StringUtils.isNotEmpty(jacocoOpt)) {
+            jacocoCommand = new VMOption(jacocoOpt);
+        }
+
+        final String workingDirectory = workingDirectory();
+
+        return composite(
+            // TODO not sure why the below list of bundles is different from
+            // running tests with PaxExam.class - but this setup works
+            //super.baseConfiguration(),
+
+            when(vmOption != null).useOptions(vmOption),
+            when(jacocoCommand != null).useOptions(jacocoCommand),
+
+            // For some reason, Jetty starts first on port 8080 without this
+            systemProperty("org.osgi.service.http.port").value(String.valueOf(httpPort)),
+
+            slingQuickstartOakTar(workingDirectory, httpPort),
+            testBundle("bundle.filename"),
+
+            logback(),
+            mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.log").version("1.2.4"),
+            mavenBundle().groupId("log4j").artifactId("log4j").version("1.2.17"),
+            mavenBundle().groupId("org.apache.aries.spifly").artifactId("org.apache.aries.spifly.dynamic.framework.extension").version("1.3.2"),
+            mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.webconsole.plugins.ds").version("2.1.0")
+            
+        ).getOptions();
+    }
+
+    @Before
+    public void waitForSling() throws Exception {
+        final URI url = new URI(String.format("http://localhost:%d", httpPort));
+        CLIENT = new OsgiConsoleClient(url, "admin", "admin");
+        CLIENT.waitExists("/", STARTUP_WAIT_SECONDS * 1000, 500);
+        CLIENT.waitComponentRegistered(PathBoundService.class.getName(), 10 * 1000, 500);
+    }
+}
diff --git a/src/test/java/org/apache/sling/servlets/annotations/ServicePropertiesIT.java b/src/test/java/org/apache/sling/servlets/annotations/ServicePropertiesIT.java
index 4ddbb31..70171a1 100644
--- a/src/test/java/org/apache/sling/servlets/annotations/ServicePropertiesIT.java
+++ b/src/test/java/org/apache/sling/servlets/annotations/ServicePropertiesIT.java
@@ -35,7 +35,7 @@ import org.junit.Test;
  *  are set by our annotations - this is what this class does for some of
  *  our test services.
  */
-public class ServicePropertiesIT extends TestSupport {
+public class ServicePropertiesIT extends AnnotationsTestSupport {
     
     private static String componentPath(Object nameOrId) {
         return "/system/console/components/" + nameOrId + ".json";
diff --git a/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java b/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java
index 297f599..ccdf406 100644
--- a/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java
+++ b/src/test/java/org/apache/sling/servlets/annotations/ServletRegistrationIT.java
@@ -27,7 +27,7 @@ import org.junit.Test;
  *  some of the servlet resolver tests, see also {@link ServicePropertiesIT}
  *  for another testing method.
  */
-public class ServletRegistrationIT extends TestSupport {
+public class ServletRegistrationIT extends AnnotationsTestSupport {
     
     @Test
     public void testPathBoundServlet() throws ClientException, UnsupportedEncodingException {
@@ -56,7 +56,7 @@ public class ServletRegistrationIT extends TestSupport {
         CLIENT.doGet("/content/servlettest/resourceTypeBoundServlet.html", 560);
         CLIENT.doGet("/content/servlettest/resourceTypeBoundServlet.json", 560);
         // only GET and HEAD are supposed to be working
-        CLIENT.doPut("/content/servlettest/resourceTypeBoundServlet.json", new StringEntity("some text"), Collections.emptyList(), 201);
+        CLIENT.doPut("/content/servlettest/resourceTypeBoundServlet.json", new StringEntity("some text"), Collections.emptyList(), 405);
     }
 
     @Test
@@ -65,7 +65,7 @@ public class ServletRegistrationIT extends TestSupport {
         CLIENT.doGet("/content/servlettest/resourceTypeBoundServletWithPrefix.html", 590);
         CLIENT.doGet("/content/servlettest/resourceTypeBoundServletWithPrefix.json", 590);
         // only GET and HEAD are supposed to be working
-        CLIENT.doPut("/content/servlettest/resourceTypeBoundServletWithPrefix.json", new StringEntity("some text"), Collections.emptyList(), 201);
+        CLIENT.doPut("/content/servlettest/resourceTypeBoundServletWithPrefix.json", new StringEntity("some text"), Collections.emptyList(), 405);
     }
 
     @Test
@@ -76,7 +76,7 @@ public class ServletRegistrationIT extends TestSupport {
         CLIENT.doGet("/content/servlettest/resourceTypeBoundServletWithExtension.ext2", 570);
         CLIENT.doPut("/content/servlettest/resourceTypeBoundServletWithExtension.ext2", new StringEntity("some text"), Collections.emptyList(), 571);
         // extension is considered for all methods!
-        CLIENT.doPut("/content/servlettest/resourceTypeBoundServletWithExtension.someotherext", new StringEntity("some text"), Collections.emptyList(), 201); // DEFAULT servlet
+        CLIENT.doPut("/content/servlettest/resourceTypeBoundServletWithExtension.someotherext", new StringEntity("some text"), Collections.emptyList(), 405); // DEFAULT servlet
     }
 
     @Test
@@ -90,7 +90,7 @@ public class ServletRegistrationIT extends TestSupport {
         // some non-registered selector as last selector
         CLIENT.doGet("/content/servlettest/resourceTypeBoundServletWithSelectors.selector1.selector2.someotherselector.someext", 600);
         // only GET and HEAD are supposed to be working
-        CLIENT.doPut("/content/servlettest/resourceTypeBoundServletWithSelectors.selector3.someext", new StringEntity("some text"), Collections.emptyList(), 201);
+        CLIENT.doPut("/content/servlettest/resourceTypeBoundServletWithSelectors.selector3.someext", new StringEntity("some text"), Collections.emptyList(), 405);
     }
 
     @Test
diff --git a/src/test/java/org/apache/sling/servlets/annotations/TestSupport.java b/src/test/java/org/apache/sling/servlets/annotations/TestSupport.java
deleted file mode 100644
index 6973804..0000000
--- a/src/test/java/org/apache/sling/servlets/annotations/TestSupport.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * 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.sling.servlets.annotations;
-
-import java.io.File;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-
-import org.apache.sling.testing.clients.ClientException;
-import org.apache.sling.testing.clients.osgi.OsgiConsoleClient;
-import org.junit.BeforeClass;
-
-public class TestSupport {
-    
-    static OsgiConsoleClient CLIENT;
-    
-    private static final String BUNDLE_SYMBOLICNAME = "org.apache.sling.servlets.annotations.it";
-    private static final long BUNDLE_START_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
-    private static final long SERVICE_START_TIMEOUT = TimeUnit.SECONDS.toMillis(60);
-    
-    @BeforeClass
-    public static synchronized void setupOnce() throws ClientException, InterruptedException, TimeoutException, URISyntaxException {
-        if(CLIENT != null) {
-            return;
-        }
-
-        String baseUrl = System.getProperty("baseUrl");
-        if (baseUrl == null) {
-            throw new IllegalArgumentException("IT must be started with environment variable 'baseUrl' set");
-        }
-        URI url = new URI(baseUrl);
-        CLIENT = new OsgiConsoleClient(url, "admin", "admin");
-        
-        String bundleFile = System.getProperty("bundleFile");
-        if (bundleFile == null) {
-            throw new IllegalArgumentException("IT must be started with environment variable 'bundleFile' set");
-        }
-        // deploy bundle to server
-        File file = new File(bundleFile);
-        if (!file.exists()) {
-            throw new IllegalArgumentException("Test bundle file in " + file + " does not exist!");
-        }
-        // wait until the server is fully started
-        CLIENT.waitExists("/starter/index.html", SERVICE_START_TIMEOUT, 500);
-
-        CLIENT.waitInstallBundle(file, true, -1, BUNDLE_START_TIMEOUT, 500);
-        
-        // the following method somehow fails sometimes
-        CLIENT.waitServiceRegistered("javax.servlet.Servlet", BUNDLE_SYMBOLICNAME, SERVICE_START_TIMEOUT, 500);
-        CLIENT.waitComponentRegistered("org.apache.sling.servlets.annotations.testservlets.PathBoundServlet", SERVICE_START_TIMEOUT, 500);
-        CLIENT.waitComponentRegistered("org.apache.sling.servlets.annotations.testservletfilters.SimpleServletFilter", SERVICE_START_TIMEOUT, 500);
-    }
-}
diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml
new file mode 100644
index 0000000..a603c15
--- /dev/null
+++ b/src/test/resources/logback.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<configuration>
+  <appender name="file" class="ch.qos.logback.core.FileAppender">
+    <file>target/test.log</file>
+    <append>true</append>
+    <encoder>
+      <pattern>%date level=%level thread=%thread logger=%logger sourcefile=%file line=%line %mdc message=%msg%n</pattern>
+    </encoder>
+  </appender>
+  
+  <root level="info">
+    <appender-ref ref="file"/>
+  </root>
+</configuration>