You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/09/22 14:15:08 UTC

svn commit: r697788 - in /servicemix/smx4/kernel/trunk/testing: ./ itests/ itests/src/ itests/src/test/ itests/src/test/java/ itests/src/test/java/org/ itests/src/test/java/org/apache/ itests/src/test/java/org/apache/servicemix/ itests/src/test/java/or...

Author: gnodet
Date: Mon Sep 22 05:15:07 2008
New Revision: 697788

URL: http://svn.apache.org/viewvc?rev=697788&view=rev
Log:
SMX4KNL-86: When running integration tests, the container classpath contains all the test bundles which may cause problems

Added:
    servicemix/smx4/kernel/trunk/testing/itests/
    servicemix/smx4/kernel/trunk/testing/itests/pom.xml
    servicemix/smx4/kernel/trunk/testing/itests/src/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/testing/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/testing/itests/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/testing/itests/SimpleTest.java
    servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/apache/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/apache/servicemix/
    servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/apache/servicemix/MANIFEST.MF
Modified:
    servicemix/smx4/kernel/trunk/testing/pom.xml
    servicemix/smx4/kernel/trunk/testing/support/src/main/java/org/apache/servicemix/kernel/testing/support/SmxKernelPlatform.java
    servicemix/smx4/kernel/trunk/testing/support/src/main/resources/org/apache/servicemix/kernel/testing/support/felix.config.properties

Added: servicemix/smx4/kernel/trunk/testing/itests/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/testing/itests/pom.xml?rev=697788&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/testing/itests/pom.xml (added)
+++ servicemix/smx4/kernel/trunk/testing/itests/pom.xml Mon Sep 22 05:15:07 2008
@@ -0,0 +1,96 @@
+<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">
+
+  <!--
+
+      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.
+  -->
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+      <groupId>org.apache.servicemix.kernel.testing</groupId>
+      <artifactId>testing</artifactId>
+      <version>1.1.0-SNAPSHOT</version>
+  </parent>
+
+  <groupId>org.apache.servicemix.kernel.testing</groupId>
+  <artifactId>org.apache.servicemix.kernel.testing.itests</artifactId>
+  <packaging>bundle</packaging>
+  <version>1.1.0-SNAPSHOT</version>
+  <name>Apache ServiceMix Kernel :: Testing - ITests</name>
+
+  <dependencies>
+      <dependency>
+          <groupId>org.apache.servicemix.kernel.testing</groupId>
+          <artifactId>org.apache.servicemix.kernel.testing.support</artifactId>
+          <version>${pom.version}</version>
+          <scope>test</scope>
+      </dependency>
+      <dependency>
+          <groupId>org.apache.servicemix.specs</groupId>
+          <artifactId>org.apache.servicemix.specs.stax-api-1.0</artifactId>
+          <version>${servicemix.specs.version}</version>
+          <scope>test</scope>
+      </dependency>
+      <dependency>
+          <groupId>org.apache.servicemix.bundles</groupId>
+          <artifactId>org.apache.servicemix.bundles.woodstox</artifactId>
+          <version>${woodstox.version}</version>
+          <scope>test</scope>
+      </dependency>
+      <dependency>
+          <groupId>xml-apis</groupId>
+          <artifactId>xml-apis</artifactId>
+          <version>2.0.2</version>
+      </dependency>
+      <dependency>
+          <groupId>xerces</groupId>
+          <artifactId>xercesImpl</artifactId>
+          <version>2.9.0</version>
+      </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <!-- generate dependencies versions -->
+      <plugin>
+        <groupId>org.apache.servicemix.tooling</groupId>
+        <artifactId>depends-maven-plugin</artifactId>
+        <version>${depends.maven.plugin.version}</version>
+        <executions>
+          <execution>
+            <id>generate-depends-file</id>
+            <goals>
+              <goal>generate-depends-file</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <configuration>
+          <instructions>
+            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+            <Import-Package>org.apache.felix*;resolution:=optional,*</Import-Package>
+            <Export-Package>${pom.artifactId}</Export-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>
\ No newline at end of file

Added: servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/testing/itests/SimpleTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/testing/itests/SimpleTest.java?rev=697788&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/testing/itests/SimpleTest.java (added)
+++ servicemix/smx4/kernel/trunk/testing/itests/src/test/java/org/apache/servicemix/kernel/testing/itests/SimpleTest.java Mon Sep 22 05:15:07 2008
@@ -0,0 +1,77 @@
+/*
+ * 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.servicemix.kernel.testing.itests;
+
+import java.util.Properties;
+
+import javax.xml.stream.XMLInputFactory;
+
+import org.apache.servicemix.kernel.testing.support.AbstractIntegrationTest;
+
+public class SimpleTest extends AbstractIntegrationTest {
+
+    private Properties dependencies;
+
+    /**
+	 * The manifest to use for the "virtual bundle" created
+	 * out of the test classes and resources in this project
+	 *
+	 * This is actually the boilerplate manifest with one additional
+	 * import-package added. We should provide a simpler customization
+	 * point for such use cases that doesn't require duplication
+	 * of the entire manifest...
+	 */
+	protected String getManifestLocation() {
+		return "classpath:org/apache/servicemix/MANIFEST.MF";
+	}
+
+	/**
+	 * The location of the packaged OSGi bundles to be installed
+	 * for this test. Values are Spring resource paths. The bundles
+	 * we want to use are part of the same multi-project maven
+	 * build as this project is. Hence we use the localMavenArtifact
+	 * helper method to find the bundles produced by the package
+	 * phase of the maven build (these tests will run after the
+	 * packaging phase, in the integration-test phase).
+	 *
+	 * JUnit, commons-logging, spring-core and the spring OSGi
+	 * test bundle are automatically included so do not need
+	 * to be specified here.
+	 */
+	protected String[] getTestBundlesNames() {
+        return new String[] {
+            getBundle("org.apache.servicemix.specs", "org.apache.servicemix.specs.stax-api-1.0"),
+		};
+	}
+
+    public void testWoodstox() throws Exception {
+        Thread.currentThread().setContextClassLoader(XMLInputFactory.class.getClassLoader());
+        System.err.println(XMLInputFactory.class.getClassLoader());
+        System.err.println(getClass().getClassLoader());
+        XMLInputFactory factory = null;
+        try {
+            factory = XMLInputFactory.newInstance();
+            fail("Factory should not have been found");
+        } catch (Throwable t) {
+            System.err.println(t.getMessage());
+        }
+        assertNull(factory);
+        installBundle("org.apache.servicemix.bundles", "org.apache.servicemix.bundles.woodstox", null, "jar");
+        assertNotNull(XMLInputFactory.newInstance());
+    }
+
+}

Added: servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/apache/servicemix/MANIFEST.MF
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/apache/servicemix/MANIFEST.MF?rev=697788&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/apache/servicemix/MANIFEST.MF (added)
+++ servicemix/smx4/kernel/trunk/testing/itests/src/test/resources/org/apache/servicemix/MANIFEST.MF Mon Sep 22 05:15:07 2008
@@ -0,0 +1,29 @@
+Manifest-Version: 1.0
+License-00: .
+License-01: Licensed to the Apache Software Foundation (ASF) under one or more
+License-02: contributor license agreements.  See the NOTICE file distributed with
+License-03: this work for additional information regarding copyright ownership.
+License-04: The ASF licenses this file to You under the Apache License, Version 2.0
+License-05: (the "License"); you may not use this file except in compliance with
+License-06: the License.  You may obtain a copy of the License at
+License-07: .
+License-08:      http://www.apache.org/licenses/LICENSE-2.0
+License-09: .
+License-10: Unless required by applicable law or agreed to in writing, software
+License-11: distributed under the License is distributed on an "AS IS" BASIS,
+License-12: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+License-13: See the License for the specific language governing permissions and
+License-14: limitations under the License.
+License-15: .
+Bundle-Name: org.apache.servicemix.testing.itests
+Bundle-SymbolicName: org.apache.servicemix.testing.itests
+Bundle-Vendor: Spring Framework
+Bundle-Activator: org.springframework.osgi.test.JUnitTestActivator
+Import-Package: junit.framework,
+ org.osgi.framework;specification-version="1.3.0",
+ org.apache.commons.logging,
+ org.springframework.core.io,
+ org.springframework.osgi.test,
+ org.apache.servicemix.kernel.testing.support,
+ org.springframework.osgi.util,
+ javax.xml.stream

Modified: servicemix/smx4/kernel/trunk/testing/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/testing/pom.xml?rev=697788&r1=697787&r2=697788&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/testing/pom.xml (original)
+++ servicemix/smx4/kernel/trunk/testing/pom.xml Mon Sep 22 05:15:07 2008
@@ -34,6 +34,7 @@
 
     <modules>
         <module>support</module>
+        <module>itests</module>
     </modules>
 
 </project>

Modified: servicemix/smx4/kernel/trunk/testing/support/src/main/java/org/apache/servicemix/kernel/testing/support/SmxKernelPlatform.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/testing/support/src/main/java/org/apache/servicemix/kernel/testing/support/SmxKernelPlatform.java?rev=697788&r1=697787&r2=697788&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/testing/support/src/main/java/org/apache/servicemix/kernel/testing/support/SmxKernelPlatform.java (original)
+++ servicemix/smx4/kernel/trunk/testing/support/src/main/java/org/apache/servicemix/kernel/testing/support/SmxKernelPlatform.java Mon Sep 22 05:15:07 2008
@@ -17,10 +17,23 @@
 package org.apache.servicemix.kernel.testing.support;
 
 import java.util.Properties;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Enumeration;
+import java.util.ArrayList;
 import java.net.URL;
+import java.net.URLClassLoader;
+import java.net.MalformedURLException;
+import java.net.URLStreamHandlerFactory;
+import java.net.URLStreamHandler;
 import java.io.File;
 import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
 import java.lang.reflect.Method;
+import java.lang.reflect.Constructor;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
@@ -29,10 +42,12 @@
 import org.springframework.util.ClassUtils;
 import org.apache.felix.main.Main;
 import org.apache.felix.framework.Felix;
+import org.apache.felix.framework.util.SecureAction;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Bundle;
+import sun.misc.CompoundEnumeration;
 
 public class SmxKernelPlatform implements OsgiPlatform {
 
@@ -46,7 +61,7 @@
 
     private BundleContext context;
 
-    private Felix platform;
+    private Object platform;
 
     private File felixStorageDir;
 
@@ -72,12 +87,73 @@
         return context;
     }
 
+    private Set<String> getJars(Class... classes) {
+        Set<String> jars = new HashSet<String>();
+        for (Class cl : classes) {
+            String name = cl.getName().replace('.', '/') + ".class";
+            URL url = (cl.getClassLoader() != null ? cl.getClassLoader() : getClass().getClassLoader()).getResource(name);
+            String path = url.toString();
+            path = path.substring(0, path.indexOf('!'));
+            jars.add(path);
+        }
+        return jars;
+    }
+
     public void start() throws Exception {
+        // Check environment.
+        // If the classloader contains JAXP providers, the META-INF infos will certainly be found
+        // by OSGi, but classes won't be found, leading to errors.
+        ClassLoader cll = Thread.currentThread().getContextClassLoader();
+        if (cll == null) {
+            cll = getClass().getClassLoader();
+        }
+        URL url1 = cll.getSystemResource("META-INF/services/javax.xml.transform.TransformerFactory");
+        URL url2 = cll.getSystemResource("META-INF/services/javax.xml.parsers.DocumentBuilderFactory");
+        List<String> additionalPackages = new ArrayList<String>();
+        /*
+        if (url1 != null) {
+            String line = new BufferedReader(new InputStreamReader(url1.openStream())).readLine();
+            line = line.substring(0, line.lastIndexOf('.'));
+            System.err.println(line);
+            additionalPackages.add(line);
+        }
+        if (url2 != null) {
+            String line = new BufferedReader(new InputStreamReader(url2.openStream())).readLine();
+            line = line.substring(0, line.lastIndexOf('.'));
+            System.err.println(line);
+            additionalPackages.add(line);
+        }
+        */
+        if (url1 != null || url2 != null) {
+            String s1 = null;
+            if (url1 != null) {
+                s1 = url1.toString();
+                s1 = s1.substring(s1.lastIndexOf(':') + 1, s1.indexOf('!'));
+            }
+            String s2 = null;
+            if (url2 != null) {
+                s2 = url2.toString();
+                s2 = s2.substring(s2.lastIndexOf(':') + 1, s2.indexOf('!'));
+            }
+            throw new Exception("An xml parser or xslt engine has been found in the classpath.\n" +
+                    "It is probably included as a transitive dependencies by Maven.\n" +
+                    "Check by running 'mvn dependency:tree' and exclude the jars from dependencies.\n" +
+                    "Offending resources:\n" +
+                    (s1 != null ? "\t" + s1 + "\n" : "") +
+                    (s2 != null ? "\t" + s2 + "\n" : ""));
+        }
 
-        platform = new Felix(getConfigurationProperties(), null);
-        platform.start();
+        Set<String> jars = getJars(Felix.class);
+        //System.out.println(jars);
+        ClassLoader classLoader = new GuardClassLoader(toURLs(jars.toArray(new String[jars.size()])), additionalPackages);
+
+        Thread.currentThread().setContextClassLoader(classLoader);
+        Class cl = classLoader.loadClass(Felix.class.getName());
+        Constructor cns = cl.getConstructor(Map.class, List.class);
+        platform = cns.newInstance(getConfigurationProperties(), null);
+        platform.getClass().getMethod("start").invoke(platform);
 
-        Bundle systemBundle = platform;
+        Bundle systemBundle = (Bundle) platform;
 
         // call getBundleContext
         final Method getContext = systemBundle.getClass().getDeclaredMethod("getBundleContext", null);
@@ -94,7 +170,7 @@
 
     public void stop() throws Exception {
         try {
-            platform.stop();
+            platform.getClass().getMethod("stop").invoke(platform);
         }
         finally {
             // remove cache folder
@@ -190,4 +266,131 @@
         // The directory is now empty so delete it
         return (success &= file.delete());
     }
+
+    private static URL[] toURLs(String[] jars) throws MalformedURLException {
+        URL[] urls = new URL[jars.length];
+        for (int i = 0; i < urls.length; i++) {
+            String s = jars[i];
+            if (s.startsWith("jar:")) {
+                s = s.substring("jar:".length());
+            }
+            urls[i] = new URL(s);
+        }
+        return urls;
+    }
+
+    public class GuardClassLoader extends URLClassLoader {
+        private Set<String> packages = new HashSet<String>();
+        private List<ClassLoader> parents = new ArrayList<ClassLoader>();
+
+        public GuardClassLoader(URL[] urls, List<String> additionalPackages) throws MalformedURLException {
+            super(urls, SmxKernelPlatform.class.getClassLoader());
+            Properties props = getConfigurationProperties();
+            String prop = props.getProperty("org.osgi.framework.system.packages");
+            String[] ps = prop.split(",");
+            for (String p : ps) {
+                String[] spack = p.split(";");
+                for (String sp : spack) {
+                    sp = sp.trim();
+                    if (!sp.startsWith("version")) {
+                        packages.add(sp);
+                    }
+                }
+            }
+            if (additionalPackages != null) {
+                packages.addAll(additionalPackages);
+            }
+            ClassLoader cl = getParent();
+            while (cl != null) {
+                parents.add(0, cl);
+                cl = cl.getParent();
+            }
+            //System.err.println("Boot packages: " + packages);
+        }
+
+        protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
+            //System.err.println("Loading class: " + name);
+            Class c = findLoadedClass(name);
+            if (c == null) {
+                String pkg = name.substring(0, name.lastIndexOf('.'));
+                if (name.startsWith("java.") || packages.contains(pkg)) {
+                    for (ClassLoader cl : parents) {
+                        try {
+                            c = cl.loadClass(name);
+                            //System.err.println("Class loaded from: " + cl.getResource(name.replace('.', '/') + ".class"));
+                            break;
+                        } catch (ClassNotFoundException e) {
+                        }
+                    }
+                    if (c == null) {
+                        throw new ClassNotFoundException(name);
+                    }
+                    //c = getParent().loadClass(name);
+                } else  {
+                    c = findClass(name);
+                }
+            }
+            if (resolve) {
+                resolveClass(c);
+            }
+            return c;
+        }
+
+        public URL getResource(String name) {
+            //System.err.println("GetResource: " + name);
+            URL url = getParent().getResource(name);
+            if (url != null && url.toString().startsWith("file:")) {
+                return url;
+            }
+            url = findResource(name);
+            System.err.println("Resource " + name + " found at " + url);
+            return url;
+            /*
+            URL u = getParent().getResource(name);
+            if (u != null) {
+                String path = u.toString();
+                int idx = path.indexOf('!');
+                if (idx > 0) {
+                    path = path.substring(0, idx);
+                    if (!jars.contains(path)) {
+                        return null;
+                    }
+                } else {
+                    idx = 0;
+                }
+            }
+            return u;
+            */
+        }
+
+        public Enumeration<URL> getResources(final String name) throws IOException {
+            //System.err.println("GetResources: " + name);
+            Enumeration[] tmp = new Enumeration[2];
+            final Enumeration<URL> e = getParent().getResources(name);
+            tmp[0] = new Enumeration<URL>() {
+                URL next = null;
+                public boolean hasMoreElements() {
+                    while (next == null && e.hasMoreElements()) {
+                        next = e.nextElement();
+                        String path = next.toString();
+                        if (!path.startsWith("file:")) {
+                            next = null;
+                        }
+                    }
+                    return next != null;
+                }
+                public URL nextElement() {
+                    return next;
+                }
+            };
+            tmp[1] = findResources(name);
+            return new CompoundEnumeration(tmp) {
+                public Object nextElement() {
+                    Object next = super.nextElement();
+                    System.err.println("Resources " + name + " found at " + next);
+                    return next;
+                }
+            };
+        }
+    }
 }

Modified: servicemix/smx4/kernel/trunk/testing/support/src/main/resources/org/apache/servicemix/kernel/testing/support/felix.config.properties
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/testing/support/src/main/resources/org/apache/servicemix/kernel/testing/support/felix.config.properties?rev=697788&r1=697787&r2=697788&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/testing/support/src/main/resources/org/apache/servicemix/kernel/testing/support/felix.config.properties (original)
+++ servicemix/smx4/kernel/trunk/testing/support/src/main/resources/org/apache/servicemix/kernel/testing/support/felix.config.properties Mon Sep 22 05:15:07 2008
@@ -30,6 +30,17 @@
  com.sun.org.apache.xerces.internal.dom, \
  com.sun.org.apache.xerces.internal.jaxp, \
  com.sun.org.apache.xerces.internal.xni, \
+ com.sun.org.apache.xerces.internal.jaxp.validation.xs, \
+ sun.net.www.protocol.doc, \
+ sun.net.www.protocol.file, \
+ sun.net.www.protocol.ftp, \
+ sun.net.www.protocol.gopher, \
+ sun.net.www.protocol.http, \
+ sun.net.www.protocol.jar, \
+ sun.net.www.protocol.mailto, \
+ sun.net.www.protocol.netdoc, \
+ sun.net.www.protocol.systemresource, \
+ sun.net.www.protocol.verbatim, \
  ${jre-${java.specification.version}}
 #org.osgi.framework.bootdelegation=sun.*,com.sun.*