You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2013/01/06 21:29:13 UTC

svn commit: r1429591 - /webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXImplementation.java

Author: veithen
Date: Sun Jan  6 20:29:13 2013
New Revision: 1429591

URL: http://svn.apache.org/viewvc?rev=1429591&view=rev
Log:
Allow tests to be run with a StAX implementation other than Woodstox (by setting the relevant system properties).

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXImplementation.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXImplementation.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXImplementation.java?rev=1429591&r1=1429590&r2=1429591&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXImplementation.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/dialect/StAXImplementation.java Sun Jan  6 20:29:13 2013
@@ -18,6 +18,9 @@
  */
 package org.apache.axiom.util.stax.dialect;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.Properties;
 
 import javax.xml.stream.FactoryConfigurationError;
@@ -40,24 +43,31 @@ public final class StAXImplementation {
         return name;
     }
 
-    public XMLInputFactory newXMLInputFactory() {
-        String className = props == null ? null : props.getProperty(XMLInputFactory.class.getName());
-        XMLInputFactory factory;
+    private Object newFactory(Class type) {
+        String className = props == null ? null : props.getProperty(type.getName());
         if (className == null) {
-            ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
-            Thread.currentThread().setContextClassLoader(classLoader);
-            try {
-                factory = XMLInputFactory.newInstance();
-            } finally {
-                Thread.currentThread().setContextClassLoader(savedClassLoader);
-            }
-        } else {
+            // We do the lookup ourselves instead of using the StAX API. This allows
+            // us to completely ignore the system properties. The Axiom build allows
+            // to use system properties to specify the StAX implementation to be used
+            // by unit tests, but this must not interfere with the dialect tests.
             try {
-                factory = (XMLInputFactory)classLoader.loadClass(className).newInstance();
-            } catch (Exception ex) {
+                BufferedReader in = new BufferedReader(new InputStreamReader(
+                        classLoader.getResourceAsStream("META-INF/services/" + type.getName()), "UTF-8"));
+                try {
+                    className = in.readLine();
+                } finally {
+                    in.close();
+                }
+            } catch (IOException ex) {
                 throw new FactoryConfigurationError(ex);
             }
         }
+        Object factory;
+        try {
+            factory = classLoader.loadClass(className).newInstance();
+        } catch (Exception ex) {
+            throw new FactoryConfigurationError(ex);
+        }
         // Check that the parser has actually been loaded from the expected class loader.
         // If the parser has been loaded from the JRE, then comparing the class loaders
         // is not reliable (because it may be null). Hence the check on ParentLastURLClassLoader.
@@ -69,6 +79,10 @@ public final class StAXImplementation {
         return factory;
     }
     
+    public XMLInputFactory newXMLInputFactory() {
+        return (XMLInputFactory)newFactory(XMLInputFactory.class);
+    }
+    
     public XMLInputFactory newNormalizedXMLInputFactory() {
         XMLInputFactory factory = newXMLInputFactory();
         if (dialect == null) {
@@ -78,29 +92,7 @@ public final class StAXImplementation {
     }
 
     public XMLOutputFactory newXMLOutputFactory() {
-        String className = props == null ? null : props.getProperty(XMLOutputFactory.class.getName());
-        XMLOutputFactory factory;
-        if (className == null) {
-            ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader();
-            Thread.currentThread().setContextClassLoader(classLoader);
-            try {
-                factory = XMLOutputFactory.newInstance();
-            } finally {
-                Thread.currentThread().setContextClassLoader(savedClassLoader);
-            }
-        } else {
-            try {
-                factory = (XMLOutputFactory)classLoader.loadClass(className).newInstance();
-            } catch (Exception ex) {
-                throw new FactoryConfigurationError(ex);
-            }
-        }
-        if (classLoader != ClassLoader.getSystemClassLoader()
-                && factory.getClass().getClassLoader() != classLoader) {
-            throw new FactoryConfigurationError("Wrong factory: got " + factory.getClass().getName()
-                    + " loaded from " + factory.getClass().getClassLoader());
-        }
-        return factory;
+        return (XMLOutputFactory)newFactory(XMLOutputFactory.class);
     }
     
     public XMLOutputFactory newNormalizedXMLOutputFactory() {