JCR-4563: test OSGi bundles

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/trunk@1877390 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/jackrabbit-it-osgi/README.md b/jackrabbit-it-osgi/README.md
new file mode 100644
index 0000000..3d9d387
--- /dev/null
+++ b/jackrabbit-it-osgi/README.md
@@ -0,0 +1,30 @@
+<!--
+   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.
+  -->
+Jackrabbit Integration tests for OSGi deployments
+==
+
+This modules validates basic Jackrabbit functionality in an OSGi environment.
+
+The Jackrabbit modules under test are not picked up from the reactor build. Instead,
+they are read from the local repository and copied in a well-known location
+using the maven-assembly-plugin.
+
+In order to test a change made in a different module, you would need to:
+
+* Run `mvn install` in the changed module
+* Run `mvn assembly:single` in this module
+* Re-run the test(s) in this module
diff --git a/jackrabbit-it-osgi/pom.xml b/jackrabbit-it-osgi/pom.xml
new file mode 100644
index 0000000..5b91611
--- /dev/null
+++ b/jackrabbit-it-osgi/pom.xml
@@ -0,0 +1,169 @@
+<?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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd ">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.jackrabbit</groupId>
+    <artifactId>jackrabbit-parent</artifactId>
+    <version>2.21.1-SNAPSHOT</version>
+    <relativePath>../jackrabbit-parent/pom.xml</relativePath>
+  </parent>
+
+  <artifactId>jackrabbit-it-osgi</artifactId>
+  <name>Jackrabbit Integration Tests for OSGi deployments</name>
+
+  <properties>
+    <skip.deployment>true</skip.deployment>
+    <pax.exam.version>3.4.0</pax.exam.version>
+  </properties>
+
+  <build>
+    <plugins>
+      <plugin>
+        <artifactId>maven-assembly-plugin</artifactId>
+        <configuration>
+          <descriptors>
+            <descriptor>test-bundles.xml</descriptor>
+          </descriptors>
+          <finalName>test</finalName>
+          <attach>false</attach>
+        </configuration>
+        <executions>
+          <execution>
+            <phase>pre-integration-test</phase>
+            <goals>
+              <goal>single</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <artifactId>maven-failsafe-plugin</artifactId>
+        <configuration>
+          <systemPropertyVariables>
+            <org.ops4j.pax.logging.DefaultServiceLog.level>
+              WARN
+            </org.ops4j.pax.logging.DefaultServiceLog.level>
+          </systemPropertyVariables>
+        </configuration>
+      </plugin>
+    </plugins>
+    <testResources>
+      <testResource>
+        <directory>src/test/resources</directory>
+        <filtering>true</filtering>
+      </testResource>
+    </testResources>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>javax.jcr</groupId>
+      <artifactId>jcr</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpcore-osgi</artifactId>
+      <version>4.4.13</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpclient-osgi</artifactId>
+      <version>4.5.12</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>jackrabbit-webdav</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.jackrabbit</groupId>
+      <artifactId>jackrabbit-jcr-server</artifactId>
+      <version>${project.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>commons-fileupload</groupId>
+      <artifactId>commons-fileupload</artifactId>
+      <version>1.4</version>
+    </dependency>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>javax.servlet-api</artifactId>
+      <version>3.1.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>ch.qos.logback</groupId>
+      <artifactId>logback-core</artifactId>
+      <version>${logback.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>ch.qos.logback</groupId>
+      <artifactId>logback-classic</artifactId>
+      <version>${logback.version}</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-junit4</artifactId>
+      <version>${pax.exam.version}</version>
+      <scope>test</scope>
+      <exclusions>
+        <exclusion>
+          <groupId>org.osgi</groupId>
+          <artifactId>org.osgi.core</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.geronimo.specs</groupId>
+      <artifactId>geronimo-atinject_1.0_spec</artifactId>
+      <version>1.2</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-container-forked</artifactId>
+      <version>${pax.exam.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.exam</groupId>
+      <artifactId>pax-exam-link-mvn</artifactId>
+      <version>${pax.exam.version}</version>
+      <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>
+  </dependencies>
+
+</project>
diff --git a/jackrabbit-it-osgi/src/test/java/org/apache/jackrabbit/oak/osgi/OSGiIT.java b/jackrabbit-it-osgi/src/test/java/org/apache/jackrabbit/oak/osgi/OSGiIT.java
new file mode 100644
index 0000000..1f9384d
--- /dev/null
+++ b/jackrabbit-it-osgi/src/test/java/org/apache/jackrabbit/oak/osgi/OSGiIT.java
@@ -0,0 +1,132 @@
+/*
+ * 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.jackrabbit.oak.osgi;
+
+import static org.junit.Assert.assertEquals;
+import static org.ops4j.pax.exam.CoreOptions.bundle;
+import static org.ops4j.pax.exam.CoreOptions.frameworkProperty;
+import static org.ops4j.pax.exam.CoreOptions.junitBundles;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.CoreOptions.systemProperties;
+import static org.ops4j.pax.exam.CoreOptions.vmOption;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.MalformedURLException;
+import java.net.URISyntaxException;
+
+import javax.inject.Inject;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.CoreOptions;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.options.DefaultCompositeOption;
+import org.ops4j.pax.exam.options.SystemPropertyOption;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.Version;
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class OSGiIT {
+
+    @Configuration
+    public Option[] configuration() throws IOException, URISyntaxException {
+        return CoreOptions.options(
+                junitBundles(),
+                mavenBundle("org.apache.felix", "org.apache.felix.scr", "2.0.12"),
+                mavenBundle("org.apache.felix", "org.apache.felix.jaas", "1.0.2"),
+                mavenBundle("org.osgi", "org.osgi.dto", "1.0.0"),
+                mavenBundle("org.apache.felix", "org.apache.felix.configadmin", "1.8.16"),
+                mavenBundle("org.apache.felix", "org.apache.felix.fileinstall", "3.2.6"),
+                mavenBundle("org.ops4j.pax.logging", "pax-logging-api", "1.7.2"),
+                frameworkProperty("repository.home").value("target"),
+                systemProperties(new SystemPropertyOption("felix.fileinstall.dir").value(getConfigDir())),
+                jarBundles(),
+                jpmsOptions());
+    }
+
+    private Option jpmsOptions(){
+        DefaultCompositeOption composite = new DefaultCompositeOption();
+        if (Version.parseVersion(System.getProperty("java.specification.version")).getMajor() > 1){
+            if (java.nio.file.Files.exists(java.nio.file.FileSystems.getFileSystem(URI.create("jrt:/")).getPath("modules", "java.se.ee"))){
+                composite.add(vmOption("--add-modules=java.se.ee"));
+            }
+            composite.add(vmOption("--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.lang=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.lang.invoke=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.io=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.net=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.nio=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.util=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.util.jar=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.util.regex=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/java.util.zip=ALL-UNNAMED"));
+            composite.add(vmOption("--add-opens=java.base/sun.nio.ch=ALL-UNNAMED"));
+        }
+        return composite;
+    }
+
+    private String getConfigDir(){
+        return new File(new File("src", "test"), "config").getAbsolutePath();
+    }
+
+    private Option jarBundles() throws MalformedURLException {
+        DefaultCompositeOption composite = new DefaultCompositeOption();
+        for (File bundle : new File("target", "test-bundles").listFiles()) {
+            if (bundle.getName().endsWith(".jar") && bundle.isFile()) {
+                composite.add(bundle(bundle.toURI().toURL().toString()));
+            }
+        }
+        return composite;
+    }
+
+    @Inject
+    private BundleContext context;
+
+    @Test
+    public void bundleStates() {
+        for (Bundle bundle : context.getBundles()) {
+            assertEquals(
+                String.format("Bundle %s not active. have a look at the logs", bundle.toString()), 
+                Bundle.ACTIVE, bundle.getState());
+        }
+    }
+
+    @Test
+    public void listBundles() {
+        for (Bundle bundle : context.getBundles()) {
+            System.out.println(bundle);
+        }
+    }
+
+    @Test
+    public void listServices() throws InvalidSyntaxException {
+        for (ServiceReference<?> reference
+                : context.getAllServiceReferences(null, null)) {
+            System.out.println(reference);
+        }
+    }
+}
diff --git a/jackrabbit-it-osgi/src/test/resources/exam.properties b/jackrabbit-it-osgi/src/test/resources/exam.properties
new file mode 100644
index 0000000..de6ab19
--- /dev/null
+++ b/jackrabbit-it-osgi/src/test/resources/exam.properties
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+
+#Disable the default bundles provisioned under pax logging
+pax.exam.logging=none
\ No newline at end of file
diff --git a/jackrabbit-it-osgi/src/test/resources/logback-test.xml b/jackrabbit-it-osgi/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..7a9abc4
--- /dev/null
+++ b/jackrabbit-it-osgi/src/test/resources/logback-test.xml
@@ -0,0 +1,39 @@
+        <!--
+           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="console" class="ch.qos.logback.core.ConsoleAppender">
+    <encoder>
+        <pattern>%date{HH:mm:ss.SSS} %-5level %-40([%thread] %F:%L) %msg%n</pattern>
+    </encoder>
+</appender>
+
+<appender name="file" class="ch.qos.logback.core.FileAppender">
+    <file>target/unit-tests.log</file>
+    <encoder>
+        <pattern>%date{HH:mm:ss.SSS} %-5level %-40([%thread] %F:%L) %msg%n</pattern>
+    </encoder>
+</appender>
+
+<root level="INFO">
+    <!--
+    <appender-ref ref="console"/>
+    -->
+    <appender-ref ref="file"/>
+</root>
+
+</configuration>
diff --git a/jackrabbit-it-osgi/test-bundles.xml b/jackrabbit-it-osgi/test-bundles.xml
new file mode 100644
index 0000000..f2f0441
--- /dev/null
+++ b/jackrabbit-it-osgi/test-bundles.xml
@@ -0,0 +1,47 @@
+<!--
+   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.
+  -->
+<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
+          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
+  <id>bundles</id>
+  <formats>
+    <format>dir</format>
+  </formats>
+  <includeBaseDirectory>false</includeBaseDirectory>
+  <dependencySets>
+    <dependencySet>
+      <outputDirectory/>
+      <outputFileNameMapping>${artifact.artifactId}.jar</outputFileNameMapping>
+      <scope>test</scope>
+      <includes>
+        <include>javax.jcr:jcr</include>
+        <include>javax.servlet:javax.servlet-api</include>
+        <include>commons-io:commons-io</include>
+        <include>commons-collections:commons-collections</include>
+        <include>commons-fileupload:commons-fileupload</include>
+        <include>org.apache.jackrabbit:jackrabbit-jcr-commons</include>
+        <include>org.apache.jackrabbit:jackrabbit-spi</include>
+        <include>org.apache.jackrabbit:jackrabbit-spi-commons</include>
+        <include>org.apache.tika:tika-core</include>
+        <include>org.apache.httpcomponents:httpclient-osgi</include>
+        <include>org.apache.httpcomponents:httpcore-osgi</include>
+        <include>org.apache.jackrabbit:jackrabbit-webdav</include>
+        <include>org.apache.jackrabbit:jackrabbit-jcr-server</include>
+      </includes>
+    </dependencySet>
+  </dependencySets>
+</assembly>
diff --git a/pom.xml b/pom.xml
index d25a86a..0a2684c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,6 +58,7 @@
     <module>jackrabbit-jcr-client</module>
     <module>jackrabbit-standalone-components</module>
     <module>jackrabbit-standalone</module>
+    <module>jackrabbit-it-osgi</module>
   </modules>
 
   <scm>