ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1739812 - in /webservices/axiom/trunk/testing/xml-truth/src: main/java/org/apache/axiom/truth/xml/ site/markdown/ test/java/org/apache/axiom/truth/xml/
Date Mon, 18 Apr 2016 19:56:32 GMT
Author: veithen
Date: Mon Apr 18 19:56:32 2016
New Revision: 1739812

URL: http://svn.apache.org/viewvc?rev=1739812&view=rev
Log:
Add support for XMLStreamReader objects to xml-truth.

Added:
    webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/Parsable.java
  (with props)
    webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLStreamReaderProvider.java
  (with props)
    webservices/axiom/trunk/testing/xml-truth/src/test/java/org/apache/axiom/truth/xml/StAXTraverserTest.java
  (with props)
Modified:
    webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXTraverser.java
    webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXXML.java
    webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLTruth.java
    webservices/axiom/trunk/testing/xml-truth/src/site/markdown/index.md.vm

Added: webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/Parsable.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/Parsable.java?rev=1739812&view=auto
==============================================================================
--- webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/Parsable.java
(added)
+++ webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/Parsable.java
Mon Apr 18 19:56:32 2016
@@ -0,0 +1,42 @@
+/*
+ * 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.truth.xml;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import com.ctc.wstx.api.WstxInputProperties;
+import com.ctc.wstx.stax.WstxInputFactory;
+
+abstract class Parsable extends XMLStreamReaderProvider {
+
+    @Override
+    final XMLStreamReader getXMLStreamReader(boolean expandEntityReferences) throws XMLStreamException
{
+        WstxInputFactory factory = new WstxInputFactory();
+        factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.valueOf(expandEntityReferences));
+        factory.setProperty(WstxInputFactory.P_AUTO_CLOSE_INPUT, Boolean.TRUE);
+        factory.setProperty(WstxInputFactory.P_REPORT_PROLOG_WHITESPACE, Boolean.TRUE);
+        factory.setProperty(WstxInputFactory.P_REPORT_CDATA, Boolean.TRUE);
+        factory.setProperty(WstxInputProperties.P_MIN_TEXT_SEGMENT, Integer.MAX_VALUE);
+        return createXMLStreamReader(factory);
+    }
+    
+    abstract XMLStreamReader createXMLStreamReader(XMLInputFactory factory) throws XMLStreamException;
+}

Propchange: webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/Parsable.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXTraverser.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXTraverser.java?rev=1739812&r1=1739811&r2=1739812&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXTraverser.java
(original)
+++ webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXTraverser.java
Mon Apr 18 19:56:32 2016
@@ -34,41 +34,67 @@ import com.google.common.base.Strings;
 
 final class StAXTraverser implements Traverser {
     private final XMLStreamReader reader;
+    private int depth;
+    private boolean atStart;
 
     StAXTraverser(XMLStreamReader reader) {
         this.reader = reader;
+        switch (reader.getEventType()) {
+            case XMLStreamReader.START_DOCUMENT:
+                depth = -1;
+                break;
+            case XMLStreamReader.START_ELEMENT:
+                depth = 0;
+                atStart = true;
+                break;
+            default:
+                throw new IllegalStateException("The reader must be positioned at a START_DOCUMENT
or START_ELEMENT event");
+        }
     }
     
     @Override
     public Event next() throws TraverserException {
         try {
-            if (reader.hasNext()) {
-                switch (reader.next()) {
-                    case XMLStreamReader.DTD:
-                        return Event.DOCUMENT_TYPE;
-                    case XMLStreamReader.START_ELEMENT:
-                        return Event.START_ELEMENT;
-                    case XMLStreamReader.END_ELEMENT:
-                        return Event.END_ELEMENT;
-                    case XMLStreamReader.CHARACTERS:
-                        return Event.TEXT;
-                    case XMLStreamReader.SPACE:
-                        return Event.WHITESPACE;
-                    case XMLStreamReader.ENTITY_REFERENCE:
-                        return Event.ENTITY_REFERENCE;
-                    case XMLStreamReader.COMMENT:
-                        return Event.COMMENT;
-                    case XMLStreamReader.CDATA:
-                        return Event.CDATA_SECTION;
-                    case XMLStreamReader.PROCESSING_INSTRUCTION:
-                        return Event.PROCESSING_INSTRUCTION;
-                    case XMLStreamReader.END_DOCUMENT:
-                        return null;
-                    default:
-                        throw new IllegalStateException();
+            int event;
+            if (depth == 0) {
+                if (atStart) {
+                    atStart = false;
+                    event = XMLStreamReader.START_ELEMENT;
+                } else {
+                    event = XMLStreamReader.END_DOCUMENT;
                 }
             } else {
-                return null;
+                event = reader.hasNext() ? reader.next() : XMLStreamReader.END_DOCUMENT;
+            }
+            switch (event) {
+                case XMLStreamReader.DTD:
+                    return Event.DOCUMENT_TYPE;
+                case XMLStreamReader.START_ELEMENT:
+                    if (depth != -1) {
+                        depth++;
+                    }
+                    return Event.START_ELEMENT;
+                case XMLStreamReader.END_ELEMENT:
+                    if (depth != -1) {
+                        depth--;
+                    }
+                    return Event.END_ELEMENT;
+                case XMLStreamReader.CHARACTERS:
+                    return Event.TEXT;
+                case XMLStreamReader.SPACE:
+                    return Event.WHITESPACE;
+                case XMLStreamReader.ENTITY_REFERENCE:
+                    return Event.ENTITY_REFERENCE;
+                case XMLStreamReader.COMMENT:
+                    return Event.COMMENT;
+                case XMLStreamReader.CDATA:
+                    return Event.CDATA_SECTION;
+                case XMLStreamReader.PROCESSING_INSTRUCTION:
+                    return Event.PROCESSING_INSTRUCTION;
+                case XMLStreamReader.END_DOCUMENT:
+                    return null;
+                default:
+                    throw new IllegalStateException();
             }
         } catch (XMLStreamException ex) {
             throw new TraverserException(ex);

Modified: webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXXML.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXXML.java?rev=1739812&r1=1739811&r2=1739812&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXXML.java
(original)
+++ webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/StAXXML.java
Mon Apr 18 19:56:32 2016
@@ -18,37 +18,30 @@
  */
 package org.apache.axiom.truth.xml;
 
-import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamReader;
 
 import org.apache.axiom.truth.xml.spi.Traverser;
 import org.apache.axiom.truth.xml.spi.TraverserException;
 import org.apache.axiom.truth.xml.spi.XML;
 
-import com.ctc.wstx.api.WstxInputProperties;
-import com.ctc.wstx.stax.WstxInputFactory;
+final class StAXXML implements XML {
+    private XMLStreamReaderProvider xmlStreamReaderProvider;
+    
+    StAXXML(XMLStreamReaderProvider xmlStreamReaderProvider) {
+        this.xmlStreamReaderProvider = xmlStreamReaderProvider;
+    }
 
-abstract class StAXXML implements XML {
     @Override
     public boolean isReportingElementContentWhitespace() {
         return true;
     }
 
     @Override
-    public final Traverser createTraverser(boolean expandEntityReferences) throws TraverserException
{
-        WstxInputFactory factory = new WstxInputFactory();
-        factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.valueOf(expandEntityReferences));
-        factory.setProperty(WstxInputFactory.P_AUTO_CLOSE_INPUT, Boolean.TRUE);
-        factory.setProperty(WstxInputFactory.P_REPORT_PROLOG_WHITESPACE, Boolean.TRUE);
-        factory.setProperty(WstxInputFactory.P_REPORT_CDATA, Boolean.TRUE);
-        factory.setProperty(WstxInputProperties.P_MIN_TEXT_SEGMENT, Integer.MAX_VALUE);
+    public Traverser createTraverser(boolean expandEntityReferences) throws TraverserException
{
         try {
-            return new StAXTraverser(createXMLStreamReader(factory));
+            return new StAXTraverser(xmlStreamReaderProvider.getXMLStreamReader(expandEntityReferences));
         } catch (XMLStreamException ex) {
             throw new TraverserException(ex);
         }
     }
-    
-    abstract XMLStreamReader createXMLStreamReader(XMLInputFactory factory) throws XMLStreamException;
 }

Added: webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLStreamReaderProvider.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLStreamReaderProvider.java?rev=1739812&view=auto
==============================================================================
--- webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLStreamReaderProvider.java
(added)
+++ webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLStreamReaderProvider.java
Mon Apr 18 19:56:32 2016
@@ -0,0 +1,26 @@
+/*
+ * 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.truth.xml;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+abstract class XMLStreamReaderProvider {
+    abstract XMLStreamReader getXMLStreamReader(boolean expandEntityReferences) throws XMLStreamException;
+}

Propchange: webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLStreamReaderProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLTruth.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLTruth.java?rev=1739812&r1=1739811&r2=1739812&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLTruth.java
(original)
+++ webservices/axiom/trunk/testing/xml-truth/src/main/java/org/apache/axiom/truth/xml/XMLTruth.java
Mon Apr 18 19:56:32 2016
@@ -78,12 +78,12 @@ public final class XMLTruth {
 
             @Override
             public XML createXML(final InputStream in) {
-                return new StAXXML() {
+                return new StAXXML(new Parsable() {
                     @Override
                     XMLStreamReader createXMLStreamReader(XMLInputFactory factory) throws
XMLStreamException {
                         return factory.createXMLStreamReader(in);
                     }
-                };
+                });
             }
         });
         factories.add(new XMLFactory<Reader>() {
@@ -94,12 +94,12 @@ public final class XMLTruth {
 
             @Override
             public XML createXML(final Reader reader) {
-                return new StAXXML() {
+                return new StAXXML(new Parsable() {
                     @Override
                     XMLStreamReader createXMLStreamReader(XMLInputFactory factory) throws
XMLStreamException {
                         return factory.createXMLStreamReader(reader);
                     }
-                };
+                });
             }
         });
         factories.add(new XMLFactory<StreamSource>() {
@@ -110,12 +110,12 @@ public final class XMLTruth {
 
             @Override
             public XML createXML(final StreamSource source) {
-                return new StAXXML() {
+                return new StAXXML(new Parsable() {
                     @Override
                     XMLStreamReader createXMLStreamReader(XMLInputFactory factory) throws
XMLStreamException {
                         return factory.createXMLStreamReader(source);
                     }
-                };
+                });
             }
         });
         factories.add(new XMLFactory<InputSource>() {
@@ -189,6 +189,25 @@ public final class XMLTruth {
                 return new DOMXML(element);
             }
         });
+        factories.add(new XMLFactory<XMLStreamReader>() {
+            @Override
+            public Class<XMLStreamReader> getExpectedType() {
+                return XMLStreamReader.class;
+            }
+
+            @Override
+            public XML createXML(final XMLStreamReader source) {
+                return new StAXXML(new XMLStreamReaderProvider() {
+                    @Override
+                    XMLStreamReader getXMLStreamReader(boolean expandEntityReferences) throws
XMLStreamException {
+                        if (expandEntityReferences) {
+                            throw new UnsupportedOperationException("Can't expand entity
references on a user supplied XMLStreamReader");
+                        }
+                        return source;
+                    }
+                });
+            }
+        });
         for (XMLFactory<?> factory : ServiceLoader.load(
                 XMLFactory.class, XMLTruth.class.getClassLoader())) {
             factories.add(factory);
@@ -235,7 +254,7 @@ public final class XMLTruth {
             }
         }
         if (factory == null) {
-            throw new IllegalArgumentException();
+            throw new IllegalArgumentException("No XMLFactory found for type " + object.getClass().getName());
         } else {
             return createXML0(factory, object);
         }

Modified: webservices/axiom/trunk/testing/xml-truth/src/site/markdown/index.md.vm
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/xml-truth/src/site/markdown/index.md.vm?rev=1739812&r1=1739811&r2=1739812&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/xml-truth/src/site/markdown/index.md.vm (original)
+++ webservices/axiom/trunk/testing/xml-truth/src/site/markdown/index.md.vm Mon Apr 18 19:56:32
2016
@@ -42,6 +42,8 @@ supported:
 
 *   `java.net.URL`
 
+*   `javax.xml.stream.XMLStreamReader`
+
 By default, comparison is strict. E.g. the following assertion would fail:
 
     assertAbout(xml()).that("<p:a xmlns:p='urn:ns'/>").hasSameContentAs("<a xmlns='urn:ns'/>");

Added: webservices/axiom/trunk/testing/xml-truth/src/test/java/org/apache/axiom/truth/xml/StAXTraverserTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/xml-truth/src/test/java/org/apache/axiom/truth/xml/StAXTraverserTest.java?rev=1739812&view=auto
==============================================================================
--- webservices/axiom/trunk/testing/xml-truth/src/test/java/org/apache/axiom/truth/xml/StAXTraverserTest.java
(added)
+++ webservices/axiom/trunk/testing/xml-truth/src/test/java/org/apache/axiom/truth/xml/StAXTraverserTest.java
Mon Apr 18 19:56:32 2016
@@ -0,0 +1,49 @@
+/*
+ * 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.truth.xml;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import java.io.StringReader;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.truth.xml.spi.Event;
+import org.apache.axiom.truth.xml.spi.Traverser;
+import org.junit.Test;
+
+public class StAXTraverserTest {
+    @Test
+    public void testFragment() throws Exception {
+        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(
+                new StringReader("<root><a><b/></a></root>"));
+        reader.next();
+        reader.next();
+        Traverser t = new StAXTraverser(reader);
+        assertThat(t.next()).isEqualTo(Event.START_ELEMENT);
+        assertThat(t.getQName()).isEqualTo(new QName("a"));
+        assertThat(t.next()).isEqualTo(Event.START_ELEMENT);
+        assertThat(t.getQName()).isEqualTo(new QName("b"));
+        assertThat(t.next()).isEqualTo(Event.END_ELEMENT);
+        assertThat(t.next()).isEqualTo(Event.END_ELEMENT);
+        assertThat(t.next()).isNull();
+    }
+}

Propchange: webservices/axiom/trunk/testing/xml-truth/src/test/java/org/apache/axiom/truth/xml/StAXTraverserTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message