You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2016/11/10 21:32:05 UTC

svn commit: r1769199 - in /sling/trunk/bundles/commons/classloader: pom.xml src/test/java/org/apache/sling/commons/classloader/it/DynamicClassLoaderIT.java

Author: rombert
Date: Thu Nov 10 21:32:05 2016
New Revision: 1769199

URL: http://svn.apache.org/viewvc?rev=1769199&view=rev
Log:
SLING-6269 - DynamicClassLoaderIT fails when the
org.apache.sling.commons.osgi:2.1.0 Maven artifact is not available
locally

Pull in the required bundle using Maven.

Modified:
    sling/trunk/bundles/commons/classloader/pom.xml
    sling/trunk/bundles/commons/classloader/src/test/java/org/apache/sling/commons/classloader/it/DynamicClassLoaderIT.java

Modified: sling/trunk/bundles/commons/classloader/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/classloader/pom.xml?rev=1769199&r1=1769198&r2=1769199&view=diff
==============================================================================
--- sling/trunk/bundles/commons/classloader/pom.xml (original)
+++ sling/trunk/bundles/commons/classloader/pom.xml Thu Nov 10 21:32:05 2016
@@ -137,7 +137,7 @@
       <!-- Integration Testing with Pax Exam -->
         <dependency>
             <groupId>org.ops4j.pax.exam</groupId>
-            <artifactId>pax-exam-container-forked</artifactId>
+            <artifactId>pax-exam-container-native</artifactId>
             <version>${pax-exam.version}</version>
             <scope>test</scope>
         </dependency>
@@ -195,5 +195,12 @@
             <version>5.4.0</version>
             <scope>test</scope>
         </dependency>
+        <!-- This bundle needs to be present at run time for the Pax-Exam tests -->
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.osgi</artifactId>
+            <version>2.1.0</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: sling/trunk/bundles/commons/classloader/src/test/java/org/apache/sling/commons/classloader/it/DynamicClassLoaderIT.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/classloader/src/test/java/org/apache/sling/commons/classloader/it/DynamicClassLoaderIT.java?rev=1769199&r1=1769198&r2=1769199&view=diff
==============================================================================
--- sling/trunk/bundles/commons/classloader/src/test/java/org/apache/sling/commons/classloader/it/DynamicClassLoaderIT.java (original)
+++ sling/trunk/bundles/commons/classloader/src/test/java/org/apache/sling/commons/classloader/it/DynamicClassLoaderIT.java Thu Nov 10 21:32:05 2016
@@ -21,10 +21,11 @@ import static org.ops4j.pax.exam.Constan
 import static org.ops4j.pax.exam.CoreOptions.*;
 
 import java.io.File;
-import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.InputStream;
-import java.net.URL;
-import java.util.Hashtable;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 import javax.inject.Inject;
 
@@ -38,8 +39,7 @@ import org.ops4j.pax.exam.Configuration;
 import org.ops4j.pax.exam.junit.PaxExam;
 import org.ops4j.pax.exam.ProbeBuilder;
 import org.ops4j.pax.exam.options.AbstractDelegateProvisionOption;
-import org.ops4j.pax.url.mvn.MavenResolver;
-import org.ops4j.pax.url.mvn.MavenResolvers;
+import org.ops4j.pax.exam.options.MavenArtifactProvisionOption;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -98,12 +98,6 @@ public class DynamicClassLoaderIT {
     @Configuration
     public static Option[] configuration() {
 
-        try {
-            commonsOsgi.ensureIsDownloaded();
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-        
         final String bundleFileName = System.getProperty( BUNDLE_JAR_SYS_PROP );
         final File bundleFile = new File( bundleFileName );
         if ( !bundleFile.canRead() ) {
@@ -132,13 +126,9 @@ public class DynamicClassLoaderIT {
         // check class loader
         assertNotNull(getDynamicClassLoader());
 
-        final URL url = new URL(commonsOsgi.getURL());
-        final InputStream is = url.openStream();
-        Bundle osgiBundle = null;
-        try {
-            osgiBundle = this.bundleContext.installBundle(url.toExternalForm(), is);
-        } finally {
-            try { is.close(); } catch ( final IOException ignore) {}
+        Bundle osgiBundle;
+        try ( InputStream input = commonsOsgi.openInputStream() ) {
+            osgiBundle = this.bundleContext.installBundle(commonsOsgi.getMavenBundle().getURL(), input);
         }
         assertNotNull(osgiBundle);
         assertEquals(Bundle.INSTALLED, osgiBundle.getState());
@@ -212,7 +202,7 @@ public class DynamicClassLoaderIT {
     }
     
     /**
-     * Helper class which ensures that a Maven artifact is resolved and allows access to its contents
+     * Helper class which simplifies accesing a Maven artifact based on its coordinates
      */
     static class MavenArtifactCoordinates {
         private String groupId;
@@ -224,15 +214,34 @@ public class DynamicClassLoaderIT {
             this.artifactId = artifactId;
             this.version = version;
         }
-        
-        public void ensureIsDownloaded() throws IOException {
 
-            MavenResolver resolver = MavenResolvers.createMavenResolver(new Hashtable<String,  String>(), null);
-            resolver.resolve(groupId, artifactId, null, "jar", version);
-        }
+        public InputStream openInputStream() throws FileNotFoundException {
 
-        public String getURL() {
-            return maven(groupId, artifactId, version).getURL();
+            // note that this contains a lot of Maven-related logic, but I did not find the
+            // right set of dependencies to make this work inside the OSGi container
+            // 
+            // The tough part is making sure that this also works on Jenkins where a 
+            // private local repository is specified using -Dmaven.repo.local
+            Path localRepo = Paths.get(System.getProperty("user.home"), ".m2", "repository");
+            String overridenRepo = System.getProperty("maven.repo.local");
+            if ( overridenRepo != null ) {
+                localRepo = Paths.get(overridenRepo);
+            }
+            
+            Path artifact = Paths.get(localRepo.toString(), groupId.replace('.', File.separatorChar), artifactId, version, artifactId+"-" + version+".jar");
+            
+            if ( !artifact.toFile().exists() ) {
+                throw new RuntimeException("Artifact at " + artifact + " does not exist.");
+            }
+            
+            return new FileInputStream(artifact.toFile());
+        }
+        
+        
+        
+        public MavenArtifactProvisionOption getMavenBundle() {
+            
+            return mavenBundle(groupId, artifactId, version);
         }
     }