Author: veithen
Date: Wed Jul 8 20:17:51 2009
New Revision: 792290
URL: http://svn.apache.org/viewvc?rev=792290&view=rev
Log:
Extended the coverage of XMLStreamReaderComparator and fixed
OMStAXWrapper#getNamespaceContext().
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/stax/XMLStreamReaderComparator.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=792290&r1=792289&r2=792290&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 Jul 8 20:17:51 2009
@@ -19,7 +19,7 @@
package org.apache.axiom.om.impl;
-import java.util.HashMap;
+import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -1126,10 +1126,15 @@
if (state==SWITCHED){
return parser.getNamespaceContext();
}
- Map m = getAllNamespaces(getNode());
- if (getNode() != lastNode) {
- // Handle situation involving substituted node.
- m.putAll(getAllNamespaces(lastNode));
+ Map m;
+ if (currentEvent == END_DOCUMENT) {
+ m = Collections.EMPTY_MAP;
+ } else {
+ m = getAllNamespaces(getNode());
+ if (getNode() != lastNode) {
+ // Handle situation involving substituted node.
+ m.putAll(getAllNamespaces(lastNode));
+ }
}
return new NamespaceContextImpl(m);
}
@@ -1544,14 +1549,17 @@
}
private Map getAllNamespaces(OMNode contextNode) {
- if (!(contextNode instanceof OMContainer &&
- contextNode instanceof OMElement)) {
- return new HashMap();
+ if (contextNode == null) {
+ return Collections.EMPTY_MAP;
+ }
+ OMContainer context;
+ if (contextNode instanceof OMContainer) {
+ context = (OMContainer)contextNode;
+ } else {
+ context = contextNode.getParent();
}
Map nsMap = new LinkedHashMap();
- for (OMContainer context = (OMContainer) contextNode;
- context != null && !(context instanceof OMDocument);
- context = ((OMElement) context).getParent()) {
+ while (context != null && !(context instanceof OMDocument)) {
OMElement element = (OMElement) context;
Iterator i = element.getAllDeclaredNamespaces();
while (i != null && i.hasNext()) {
@@ -1567,6 +1575,7 @@
addNamespaceToMap(attr.getNamespace(), nsMap);
}
}
+ context = element.getParent();
}
return nsMap;
}
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/stax/XMLStreamReaderComparator.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/stax/XMLStreamReaderComparator.java?rev=792290&r1=792289&r2=792290&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/stax/XMLStreamReaderComparator.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/stax/XMLStreamReaderComparator.java
Wed Jul 8 20:17:51 2009
@@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Set;
+import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamReader;
import junit.framework.Assert;
@@ -47,6 +48,12 @@
private final XMLStreamReader actual;
private final LinkedList path = new LinkedList();
+ /**
+ * Set collecting all prefixes seen in the document to be able to test
+ * {...@link XMLStreamReader#getNamespaceURI(String)}.
+ */
+ private final Set prefixes = new HashSet();
+
public XMLStreamReaderComparator(XMLStreamReader expected, XMLStreamReader
actual) {
this.expected = expected;
this.actual = actual;
@@ -54,6 +61,9 @@
private String getLocation() {
StringBuffer buffer = new StringBuffer();
+ buffer.append("event type ");
+ buffer.append(StAXUtils.getEventTypeString(expected.getEventType()));
+ buffer.append("; location ");
for (Iterator it = path.iterator(); it.hasNext(); ) {
buffer.append('/');
buffer.append(it.next());
@@ -88,8 +98,7 @@
if (actualException != null) {
actualException.printStackTrace(System.out);
fail("Method " + methodName + " threw unexpected exception " +
- actualException.getClass().getName() + "; event type
was " +
- StAXUtils.getEventTypeString(expected.getEventType()));
+ actualException.getClass().getName() + " (" +
getLocation() + ")");
} else {
return new Object[] { expectedResult, actualResult };
}
@@ -97,8 +106,7 @@
if (actualException == null) {
fail("Expected " + methodName + " to throw " +
expectedException.getClass().getName() +
- ", but the method retuned normally; event type was " +
- StAXUtils.getEventTypeString(expected.getEventType()));
+ ", but the method retuned normally (" + getLocation()
+ ")");
} else {
assertEquals(expectedException.getClass(),
actualException.getClass());
}
@@ -114,9 +122,7 @@
Object[] results = invoke(methodName, paramTypes, args);
if (results != null) {
assertEquals("Return value of " + methodName + " for arguments " +
- Arrays.asList(args) + " (event type " +
- StAXUtils.getEventTypeString(expected.getEventType()) +
- "; location " + getLocation() + ")",
+ Arrays.asList(args) + " (" + getLocation() + ")",
results[0], results[1]);
return results[0];
} else {
@@ -128,27 +134,45 @@
return assertSameResult(methodName, new Class[0], new Object[0]);
}
+ private void compareNamespaceContexts(NamespaceContext expected,
NamespaceContext actual) {
+ for (Iterator it = prefixes.iterator(); it.hasNext(); ) {
+ String prefix = (String)it.next();
+ if (prefix != null) {
+ assertEquals("Namespace URI for prefix '" + prefix + "' (" +
getLocation() + ")", expected.getNamespaceURI(prefix),
actual.getNamespaceURI(prefix));
+ }
+ }
+ }
+
+ /**
+ * Add a prefix that should be used in testing the
+ * {...@link XMLStreamReader#getNamespaceURI(String)} method.
+ *
+ * @param prefix the prefix to add
+ */
+ public void addPrefix(String prefix) {
+ prefixes.add(prefix);
+ }
+
public void compare() throws Exception {
- // Collect all prefixes seen in the document to be able to test
getNamespaceURI(String)
- Set prefixes = new HashSet();
do {
int eventType =
((Integer)assertSameResult("getEventType")).intValue();
if (eventType == XMLStreamReader.START_ELEMENT) {
path.addLast(expected.getName());
}
Integer attributeCount =
(Integer)assertSameResult("getAttributeCount");
- if (attributeCount != null) {
- for (int i=0; i<attributeCount.intValue(); i++) {
- Class[] paramTypes = { Integer.TYPE };
- Object[] args = { Integer.valueOf(i) };
- assertSameResult("getAttributeLocalName", paramTypes,
args);
- assertSameResult("getAttributeName", paramTypes, args);
- assertSameResult("getAttributeNamespace", paramTypes,
args);
- prefixes.add(assertSameResult("getAttributePrefix",
paramTypes, args));
- assertSameResult("getAttributeType", paramTypes, args);
- assertSameResult("getAttributeValue", paramTypes, args);
- assertSameResult("isAttributeSpecified", paramTypes, args);
- }
+ // Test the behavior of the getAttributeXxx methods for all types
of events,
+ // to check that an appropriate exception is thrown for events
other than
+ // START_ELEMENT
+ for (int i=0; i < (attributeCount == null ? 1 :
attributeCount.intValue()); i++) {
+ Class[] paramTypes = { Integer.TYPE };
+ Object[] args = { Integer.valueOf(i) };
+ assertSameResult("getAttributeLocalName", paramTypes, args);
+ assertSameResult("getAttributeName", paramTypes, args);
+ assertSameResult("getAttributeNamespace", paramTypes, args);
+ prefixes.add(assertSameResult("getAttributePrefix",
paramTypes, args));
+ assertSameResult("getAttributeType", paramTypes, args);
+ assertSameResult("getAttributeValue", paramTypes, args);
+ assertSameResult("isAttributeSpecified", paramTypes, args);
}
assertSameResult("getLocalName");
assertSameResult("getName");
@@ -203,6 +227,8 @@
}
}
+ compareNamespaceContexts(expected.getNamespaceContext(),
actual.getNamespaceContext());
+
if (eventType == XMLStreamReader.END_ELEMENT) {
path.removeLast();
}