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

rombert pushed a commit to annotated tag org.apache.sling.testing.paxexam-0.0.4
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-paxexam.git

commit f6bd96fa69465391d9382de4fdc891d37c3930df
Author: Julian Sedding <[email protected]>
AuthorDate: Tue Jan 17 12:29:56 2017 +0000

    SLING-6473 - Create a VersionResolver that provides versions from 
provisioning model files
    
    - implementation of a ProvisioningModelVersionResolver that can
      be created by pointing to provisioning model via a URL
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/testing/org.apache.sling.testing.paxexam@1779180
 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  12 ++
 .../paxexam/ProvisioningModelVersionResolver.java  | 136 +++++++++++++++++++++
 .../ProvisioningModelVersionResolverTest.java      |  55 +++++++++
 src/test/resources/test-dependencies.txt           |   7 ++
 4 files changed, 210 insertions(+)

diff --git a/pom.xml b/pom.xml
index b5b709a..36b8580 100644
--- a/pom.xml
+++ b/pom.xml
@@ -105,6 +105,12 @@
       <artifactId>osgi.cmpn</artifactId>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.provisioning.model</artifactId>
+      <version>1.8.0</version>
+      <scope>provided</scope>
+    </dependency>
     <!-- Apache Felix -->
     <dependency>
       <groupId>org.apache.felix</groupId>
@@ -146,6 +152,12 @@
       <version>${org.ops4j.pax.exam.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <version>1.7.13</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
diff --git 
a/src/main/java/org/apache/sling/testing/paxexam/ProvisioningModelVersionResolver.java
 
b/src/main/java/org/apache/sling/testing/paxexam/ProvisioningModelVersionResolver.java
new file mode 100644
index 0000000..6cc1207
--- /dev/null
+++ 
b/src/main/java/org/apache/sling/testing/paxexam/ProvisioningModelVersionResolver.java
@@ -0,0 +1,136 @@
+/*
+ * 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.testing.paxexam;
+
+import org.apache.sling.provisioning.model.Artifact;
+import org.apache.sling.provisioning.model.ArtifactGroup;
+import org.apache.sling.provisioning.model.Feature;
+import org.apache.sling.provisioning.model.Model;
+import org.apache.sling.provisioning.model.RunMode;
+import org.apache.sling.provisioning.model.io.ModelReader;
+import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
+import org.ops4j.pax.exam.options.MavenUrlReference.VersionResolver;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * VersionResolver that reads version information from a sling provisioning 
file.
+ * One use-case is to reference Sling's launchpad, which normally references a
+ * (recent) set of compatible bundles, in order to allow running test based on
+ * the versioning information from the Sling launchpad's provisioning model.
+ */
+public class ProvisioningModelVersionResolver implements VersionResolver {
+
+    private final Model model;
+
+    /**
+     * Adds classifier "slingfeature" and type "txt" to the provided 
MavenArtifactUrlReference
+     * to simplify creation of a VersionResolver based on a slingfeature.
+     *
+     * @param reference Maven coordinates of a module that provides a 
slingfeature.
+     * @return VersionResolver instance backed by the referenced slingfeature.
+     */
+    public static VersionResolver fromSlingfeature(MavenArtifactUrlReference 
reference) {
+        final String url = 
reference.classifier("slingfeature").type("txt").getURL();
+        try {
+            return new ProvisioningModelVersionResolver(url);
+        } catch (MalformedURLException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Constructor to create a VersionResolver instance backed by a 
provisioning model referenced
+     * by the URL represented by the provided String.
+     *
+     * @param url The String representation of a URL.
+     * @throws MalformedURLException If the String representation of the URL 
is not a valid URL.
+     */
+    public ProvisioningModelVersionResolver(final String url) throws 
MalformedURLException {
+        this(toUrl(url));
+    }
+
+    /**
+     * Constructor to create a VersionResolver instance backed by a 
provisioning model referenced
+     * by the provided URL object.
+     *
+     * @param url The URL pointing the the provisioning model file.
+     */
+    public ProvisioningModelVersionResolver(final URL url) {
+        InputStream inputStream = null;
+        try {
+            inputStream = url.openStream();
+            this.model = ModelReader.read(new InputStreamReader(inputStream), 
url.toExternalForm());
+        } catch (IOException e) {
+            throw new RuntimeException("Failed to read " + 
url.toExternalForm(), e);
+        } finally {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (IOException e) {
+                    // silent
+                }
+            }
+        }
+    }
+
+    private static URL toUrl(final String url) throws MalformedURLException {
+        final boolean hasProtocolHandler = 
System.getProperty("java.protocol.handler.pkgs") != null;
+        if (!hasProtocolHandler) {
+            // enable org.ops4j.pax.url handlers by default, unless the 
property is already set
+            System.setProperty("java.protocol.handler.pkgs", 
"org.ops4j.pax.url");
+        }
+        try {
+            return new URL(url);
+        } catch (final MalformedURLException e) {
+            if ("unknown protocol: mvn".equals(e.getMessage())) {
+                // best effort: present a helpful message in case the mvn 
protocol handler is missing
+                final MalformedURLException exception = new 
MalformedURLException(e.getMessage()
+                        + " -> Consider a dependency to 
org.ops4j.pax.url:pax-url-aether");
+                exception.initCause(e);
+                throw exception;
+            }
+            throw e;
+        } finally {
+            if (!hasProtocolHandler) {
+                System.clearProperty("java.protocol.handler.pkgs");
+            }
+        }
+    }
+
+    @Override
+    public String getVersion(final String groupId, final String artifactId) {
+        for (final Feature feature : model.getFeatures()) {
+            for (final RunMode runMode : feature.getRunModes()) {
+                for (final ArtifactGroup artifacts : 
runMode.getArtifactGroups()) {
+                    for (final Artifact artifact : artifacts) {
+                        if (groupId.equals(artifact.getGroupId()) && 
artifactId.equals(artifact.getArtifactId())) {
+                            return artifact.getVersion();
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+}
diff --git 
a/src/test/java/org/apache/sling/testing/paxexam/ProvisioningModelVersionResolverTest.java
 
b/src/test/java/org/apache/sling/testing/paxexam/ProvisioningModelVersionResolverTest.java
new file mode 100644
index 0000000..6529c11
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/testing/paxexam/ProvisioningModelVersionResolverTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.testing.paxexam;
+
+import org.junit.Test;
+import org.ops4j.pax.exam.options.MavenUrlReference.VersionResolver;
+
+import static 
org.apache.sling.testing.paxexam.ProvisioningModelVersionResolver.fromSlingfeature;
+import static org.junit.Assert.assertEquals;
+import static org.ops4j.pax.exam.CoreOptions.maven;
+
+public class ProvisioningModelVersionResolverTest {
+
+    @Test
+    public void getVersionFromClasspathResource() throws Exception {
+        final VersionResolver versionResolver =
+                new 
ProvisioningModelVersionResolver(getClass().getResource("/test-dependencies.txt"));
+        assertVersion("2.6.4", "org.apache.sling", "org.apache.sling.engine", 
versionResolver);
+        assertVersion("2.4.10", "org.apache.sling", 
"org.apache.sling.servlets.resolver", versionResolver);
+        assertVersion("2.1.18", "org.apache.sling", 
"org.apache.sling.servlets.get", versionResolver);
+        assertVersion("2.3.14", "org.apache.sling", 
"org.apache.sling.servlets.post", versionResolver);
+    }
+
+    @Test
+    public void getVersionFromMavenDependency() throws Exception {
+        final VersionResolver versionResolver =
+                fromSlingfeature(maven("org.apache.sling", 
"org.apache.sling.launchpad", "8"));
+        assertVersion("2.4.4", "org.apache.sling", "org.apache.sling.engine", 
versionResolver);
+        assertVersion("2.3.8", "org.apache.sling", 
"org.apache.sling.servlets.resolver", versionResolver);
+        assertVersion("2.1.12", "org.apache.sling", 
"org.apache.sling.servlets.get", versionResolver);
+        assertVersion("2.3.8", "org.apache.sling", 
"org.apache.sling.servlets.post", versionResolver);
+    }
+
+    private void assertVersion(final String expectedVersion,
+                               final String groupId, final String artifactId, 
final VersionResolver versionResolver) {
+        final String actualVersion = versionResolver.getVersion(groupId, 
artifactId);
+        assertEquals("Version mismatch for " + groupId + ":" + artifactId, 
expectedVersion, actualVersion);
+    }
+}
\ No newline at end of file
diff --git a/src/test/resources/test-dependencies.txt 
b/src/test/resources/test-dependencies.txt
new file mode 100644
index 0000000..b418373
--- /dev/null
+++ b/src/test/resources/test-dependencies.txt
@@ -0,0 +1,7 @@
+[feature name=test]
+# Dependencies
+[artifacts]
+  org.apache.sling/org.apache.sling.engine/2.6.4
+  org.apache.sling/org.apache.sling.servlets.resolver/2.4.10
+  org.apache.sling/org.apache.sling.servlets.get/2.1.18
+  org.apache.sling/org.apache.sling.servlets.post/2.3.14

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to