Author: veithen
Date: Wed Mar 18 22:46:24 2009
New Revision: 755766

URL: http://svn.apache.org/viewvc?rev=755766&view=rev
Log:
* Added some tests comparing the output produced by a native XMLStreamReader 
(Woodstox) and an OMStAXWrapper.
* Modified OMStAXWrapper to throw an IllegalStateException as required by the 
StAX specs when a method is called that is not valid for the current event.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamReaderComparator.java
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/OMStAXWrapper.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/OMStAXWrapperTestBase.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/OMStAXWrapper.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/OMStAXWrapper.java?rev=755766&r1=755765&r2=755766&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/OMStAXWrapper.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/OMStAXWrapper.java
 Wed Mar 18 22:46:24 2009
@@ -249,17 +249,17 @@
      * @see javax.xml.stream.XMLStreamReader#getPrefix()
      */
     public String getPrefix() {
-        String returnStr = null;
         if (parser != null) {
-            returnStr = parser.getPrefix();
+            return parser.getPrefix();
         } else {
             if ((currentEvent == START_ELEMENT)
                     || (currentEvent == END_ELEMENT)) {
                 OMNamespace ns = ((OMElement) getNode()).getNamespace();
-                returnStr = (ns == null) ? null : ns.getPrefix();
+                return (ns == null) ? null : ns.getPrefix();
+            } else {
+                throw new IllegalStateException();
             }
         }
-        return returnStr;
     }
 
     /**
@@ -267,7 +267,7 @@
      * @see javax.xml.stream.XMLStreamReader#getNamespaceURI()
      */
     public String getNamespaceURI() {
-        String returnStr = null;
+        String returnStr;
         if (parser != null) {
             returnStr = parser.getNamespaceURI();
         } else {
@@ -276,6 +276,8 @@
                     || (currentEvent == NAMESPACE)) {
                 OMNamespace ns = ((OMElement) getNode()).getNamespace();
                 returnStr = (ns == null) ? null : ns.getNamespaceURI();
+            } else {
+                throw new IllegalStateException();
             }
         }
         
@@ -308,17 +310,17 @@
      * @see javax.xml.stream.XMLStreamReader#getLocalName()
      */
     public String getLocalName() {
-        String returnStr = null;
         if (parser != null) {
-            returnStr = parser.getLocalName();
+            return parser.getLocalName();
         } else {
             if ((currentEvent == START_ELEMENT)
                     || (currentEvent == END_ELEMENT)
                     || (currentEvent == ENTITY_REFERENCE)) {
-                returnStr = ((OMElement) getNode()).getLocalName();
+                return ((OMElement) getNode()).getLocalName();
+            } else {
+                throw new IllegalStateException();
             }
         }
-        return returnStr;
     }
 
     /**
@@ -326,16 +328,16 @@
      * @see javax.xml.stream.XMLStreamReader#getName()
      */
     public QName getName() {
-        QName returnName = null;
         if (parser != null) {
-            returnName = parser.getName();
+            return parser.getName();
         } else {
             if ((currentEvent == START_ELEMENT)
                     || (currentEvent == END_ELEMENT)) {
-                returnName = getQName((OMElement) getNode());
+                return getQName((OMElement) getNode());
+            } else {
+                throw new IllegalStateException();
             }
         }
-        return returnName;
     }
 
     /**
@@ -357,8 +359,7 @@
         if (parser != null) {
             return parser.getTextLength();
         } else {
-            String text = getTextFromNode();
-            return text == null ? 0 : text.length();
+            return getTextFromNode().length();
         }
     }
 
@@ -395,13 +396,9 @@
             }
         } else {
             String text = getTextFromNode();
-            if (text != null) {
-                int copied = Math.min(length, text.length()-sourceStart);
-                text.getChars(sourceStart, sourceStart + copied, target, 
targetStart);
-                return copied;
-            } else {
-                return 0;
-            }
+            int copied = Math.min(length, text.length()-sourceStart);
+            text.getChars(sourceStart, sourceStart + copied, target, 
targetStart);
+            return copied;
         }
     }
 
@@ -413,8 +410,7 @@
         if (parser != null) {
             return parser.getTextCharacters();
         } else {
-            String text = getTextFromNode();
-            return text == null ? null : text.toCharArray();
+            return getTextFromNode().toCharArray();
         }
     }
 
@@ -431,15 +427,16 @@
     }
     
     private String getTextFromNode() {
-        if (hasText()) {
-            OMNode node = getNode();
-            if (node instanceof OMText) {
-                return ((OMText)node).getText();
-            } else if (node instanceof OMComment) {
-                return ((OMComment)node).getValue();
-            }
+        switch (currentEvent) {
+            case CHARACTERS:
+            case CDATA:
+            case SPACE:
+                return ((OMText)getNode()).getText();
+            case COMMENT:
+                return ((OMComment)getNode()).getValue();
+            default:
+                throw new IllegalStateException();
         }
-        return null;
     }
 
     /**
@@ -509,17 +506,17 @@
      * @see javax.xml.stream.XMLStreamReader#getNamespaceCount()
      */
     public int getNamespaceCount() {
-        int returnCount = 0;
         if (parser != null) {
-            returnCount = parser.getNamespaceCount();
+            return parser.getNamespaceCount();
         } else {
             if (isStartElement() || isEndElement()
                     || (currentEvent == NAMESPACE)) {
-                returnCount = getCount(((OMElement) getNode())
+                return getCount(((OMElement) getNode())
                         .getAllDeclaredNamespaces());
+            } else {
+                throw new IllegalStateException();
             }
         }
-        return returnCount;
     }
 
     /**

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/OMStAXWrapperTestBase.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/OMStAXWrapperTestBase.java?rev=755766&r1=755765&r2=755766&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/OMStAXWrapperTestBase.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/OMStAXWrapperTestBase.java
 Wed Mar 18 22:46:24 2009
@@ -20,6 +20,7 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.io.StringReader;
 import java.util.Arrays;
 
 import javax.xml.namespace.NamespaceContext;
@@ -36,6 +37,7 @@
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axiom.om.util.StAXUtils;
 
 public class OMStAXWrapperTestBase extends TestCase {
     protected final OMMetaFactory omMetaFactory;
@@ -200,4 +202,18 @@
     public void testGetNamespaceContextWithoutCaching() throws Exception {
         testGetNamespaceContext(false);
     }
+    
+    private void testWithComparator(String xml) throws Exception {
+        XMLStreamReader expected = StAXUtils.createXMLStreamReader(new 
StringReader(xml));
+        XMLStreamReader actual = 
AXIOMUtil.stringToOM(omMetaFactory.getOMFactory(), xml).getXMLStreamReader();
+        new XMLStreamReaderComparator(expected, actual).compare();
+    }
+    
+    public void testProcessingInstruction() throws Exception {
+        testWithComparator("<root><?pi data?></root>");
+    }
+    
+    public void testNamespaces() throws Exception {
+        testWithComparator("<a xmlns='urn:ns1' xmlns:ns2='urn:ns2' 
ns2:att='test'><ns2:b att='test'/></a>");
+    }
 }

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamReaderComparator.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamReaderComparator.java?rev=755766&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamReaderComparator.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamReaderComparator.java
 Wed Mar 18 22:46:24 2009
@@ -0,0 +1,158 @@
+/*
+ * 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.axiom.om.impl;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.Assert;
+
+import org.apache.axiom.om.util.StAXUtils;
+
+/**
+ * Helper class that compares the events produced by two {...@link 
XMLStreamReader} objects.
+ */
+public class XMLStreamReaderComparator extends Assert {
+    private final XMLStreamReader expected;
+    private final XMLStreamReader actual;
+    
+    public XMLStreamReaderComparator(XMLStreamReader expected, XMLStreamReader 
actual) {
+        this.expected = expected;
+        this.actual = actual;
+    }
+
+    private Object[] invoke(String methodName, Object[] args) throws Exception 
{
+        if (args == null) {
+            args = new Object[0];
+        }
+        Method[] methods = XMLStreamReader.class.getMethods();
+        Method method = null;
+        for (int i=0; i<methods.length; i++) {
+            Method candidate = methods[i];
+            if (candidate.getName().equals(methodName) &&
+                    candidate.getParameterTypes().length == args.length) {
+                method = candidate;
+                break;
+            }
+        }
+        if (method == null) {
+            fail("Method " + methodName + " not found");
+        }
+        
+        Object expectedResult;
+        Throwable expectedException;
+        try {
+            expectedResult = method.invoke(expected, args);
+            expectedException = null;
+        } catch (InvocationTargetException ex) {
+            expectedResult = null;
+            expectedException = ex.getCause();
+        }
+
+        Object actualResult;
+        Throwable actualException;
+        try {
+            actualResult = method.invoke(actual, args);
+            actualException = null;
+        } catch (InvocationTargetException ex) {
+            actualResult = null;
+            actualException = ex.getCause();
+        }
+        
+        if (expectedException == null) {
+            if (actualException != null) {
+                fail("Method " + methodName + " threw unexpected exception " +
+                        actualException.getClass().getName() + "; event type 
was " +
+                        StAXUtils.getEventTypeString(expected.getEventType()));
+            } else {
+                return new Object[] { expectedResult, actualResult };
+            }
+        } else {
+            if (actualException == null) {
+                fail("Expected " + methodName + " to throw " +
+                        expectedException.getClass().getName() +
+                        ", but the method retuned normally; event type was " +
+                        StAXUtils.getEventTypeString(expected.getEventType()));
+            } else {
+                assertEquals(expectedException.getClass(), 
actualException.getClass());
+            }
+        }
+        return null;
+    }
+    
+    private Object assertSameResult(String methodName, Object[] args) throws 
Exception {
+        Object[] results = invoke(methodName, args);
+        if (results != null) {
+            assertEquals("Return value of " + methodName + " (event type " +
+                        StAXUtils.getEventTypeString(expected.getEventType()) 
+ ")",
+                        results[0], results[1]);
+            return results[0];
+        } else {
+            return null;
+        }
+    }
+    
+    public void compare() throws Exception {
+        while (expected.next() != XMLStreamReader.END_DOCUMENT) {
+            actual.next();
+            Integer attributeCount = 
(Integer)assertSameResult("getAttributeCount", null);
+            if (attributeCount != null) {
+                for (int i=0; i<attributeCount.intValue(); i++) {
+                    Object[] args = { Integer.valueOf(i) };
+                    assertSameResult("getAttributeLocalName", args);
+                    assertSameResult("getAttributeName", args);
+                    assertSameResult("getAttributeNamespace", args);
+                    assertSameResult("getAttributePrefix", args);
+                    assertSameResult("getAttributeType", args);
+                    assertSameResult("getAttributeValue", args);
+                }
+            }
+            assertSameResult("getLocalName", null);
+            assertSameResult("getName", null);
+            Integer namespaceCount = 
(Integer)assertSameResult("getNamespaceCount", null);
+            if (namespaceCount != null) {
+                Map expectedNamespaces = new HashMap();
+                Map actualNamespaces = new HashMap();
+                for (int i=0; i<namespaceCount.intValue(); i++) {
+                    expectedNamespaces.put(expected.getNamespacePrefix(i),
+                            expected.getNamespaceURI(i));
+                    actualNamespaces.put(actual.getNamespacePrefix(i),
+                            actual.getNamespaceURI(i));
+                }
+//                assertEquals(expectedNamespaces, actualNamespaces);
+            }
+            assertSameResult("getNamespaceURI", null);
+            assertSameResult("getPIData", null);
+            assertSameResult("getPITarget", null);
+//            assertSameResult("getPrefix", null);
+            assertSameResult("getText", null);
+            assertSameResult("getTextLength", null);
+            assertSameResult("hasName", null);
+            assertSameResult("hasText", null);
+            assertSameResult("isCharacters", null);
+            assertSameResult("isEndElement", null);
+            assertSameResult("isStartElement", null);
+            assertSameResult("isWhiteSpace", null);
+        }
+    }
+}

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/impl/XMLStreamReaderComparator.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to