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 2021/11/19 23:36:03 UTC

[ws-axiom] branch master updated: Build some better test infrastructure for StAXPivot

This is an automated email from the ASF dual-hosted git repository.

veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git


The following commit(s) were added to refs/heads/master by this push:
     new a7c7639  Build some better test infrastructure for StAXPivot
a7c7639 is described below

commit a7c7639f732c171e574b40a1812095c2922fff63
Author: Andreas Veithen <ve...@apache.org>
AuthorDate: Fri Nov 19 23:35:49 2021 +0000

    Build some better test infrastructure for StAXPivot
---
 .../axiom/core/stream/stax/pull/output/Action.java | 28 +++++++++++
 .../core/stream/stax/pull/output/FakeReader.java   | 54 ++++++++++++++++++++++
 .../stream/stax/pull/output/StAXPivotTest.java     | 41 +++++++++-------
 3 files changed, 107 insertions(+), 16 deletions(-)

diff --git a/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/Action.java b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/Action.java
new file mode 100644
index 0000000..67e8b68
--- /dev/null
+++ b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/Action.java
@@ -0,0 +1,28 @@
+/*
+ * 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.core.stream.stax.pull.output;
+
+import org.apache.axiom.core.stream.StreamException;
+import org.apache.axiom.core.stream.XmlHandler;
+
+public interface Action {
+    public static final Action DEFAULT_START_DOCUMENT = h -> h.startDocument("utf-8", "1.0", "utf-8", true);
+
+    void run(XmlHandler handler) throws StreamException;
+}
diff --git a/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/FakeReader.java b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/FakeReader.java
new file mode 100644
index 0000000..d328a4e
--- /dev/null
+++ b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/FakeReader.java
@@ -0,0 +1,54 @@
+/*
+ * 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.core.stream.stax.pull.output;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.Queue;
+
+import org.apache.axiom.core.stream.StreamException;
+import org.apache.axiom.core.stream.XmlHandler;
+import org.apache.axiom.core.stream.XmlReader;
+
+public final class FakeReader implements XmlReader {
+    private final XmlHandler handler;
+    private final Queue<Action> actions;
+
+    public FakeReader(XmlHandler handler, Action[] actions) {
+        this.handler = handler;
+        this.actions = new LinkedList<>(Arrays.asList(actions));
+    }
+
+    public void addAction(Action action) {
+        actions.add(action);
+    }
+
+    @Override
+    public boolean proceed() throws StreamException {
+        if (actions.isEmpty()) {
+            return true;
+        }
+        actions.remove().run(handler);
+        return false;
+    }
+
+    @Override
+    public void dispose() {
+    }
+}
diff --git a/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java
index 2239edc..416f107 100644
--- a/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java
+++ b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java
@@ -21,28 +21,26 @@ package org.apache.axiom.core.stream.stax.pull.output;
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 
-import org.apache.axiom.core.stream.dom.input.DOMInput;
-import org.apache.axiom.ts.jaxp.dom.DOMImplementation;
-import org.junit.Before;
+import org.apache.axiom.core.stream.StreamException;
+import org.apache.axiom.core.stream.XmlHandler;
 import org.junit.Test;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
 
 public class StAXPivotTest {
-    private StAXPivot pivot;
-
-    @Before
-    public void setUp() throws Exception {
-        Document document = DOMImplementation.XERCES.newDocument();
-        Element element = document.createElementNS("urn:test", "p:test");
-        document.appendChild(element);
-        element.appendChild(document.createEntityReference("ent"));
-        pivot = new StAXPivot(null);
-        pivot.setReader(new DOMInput(document, false).createReader(pivot));
+    private static StAXPivot createStAXPivot(Action... actions) throws StreamException {
+        StAXPivot pivot = new StAXPivot(null);
+        pivot.setReader(new FakeReader(pivot, actions));
+        return pivot;
     }
-    
+
     @Test
     public void testSuccess() throws Exception {
+        StAXPivot pivot = createStAXPivot(
+                Action.DEFAULT_START_DOCUMENT,
+                h -> h.startElement("urn:test", "test", "p"),
+                XmlHandler::attributesCompleted,
+                h -> h.processEntityReference("ent", "foobar"),
+                XmlHandler::endElement,
+                XmlHandler::completed);
         pivot.require(XMLStreamConstants.START_DOCUMENT, null, null);
         pivot.next();
         pivot.require(XMLStreamConstants.START_ELEMENT, "urn:test", "test");
@@ -56,27 +54,38 @@ public class StAXPivotTest {
     
     @Test(expected=XMLStreamException.class)
     public void testEventTypeMismatch() throws Exception {
+        StAXPivot pivot = createStAXPivot(Action.DEFAULT_START_DOCUMENT);
         pivot.require(XMLStreamConstants.CHARACTERS, null, null);
     }
     
     @Test(expected=XMLStreamException.class)
     public void testLocalNameOnStartDocument() throws Exception {
+        StAXPivot pivot = createStAXPivot(Action.DEFAULT_START_DOCUMENT);
         pivot.require(XMLStreamConstants.START_DOCUMENT, null, "test");
     }
     
     @Test(expected=XMLStreamException.class)
     public void testLocalNameMismatchOnStartElement() throws Exception {
+        StAXPivot pivot = createStAXPivot(
+                Action.DEFAULT_START_DOCUMENT,
+                h -> h.startElement("urn:test", "test", "p"),
+                XmlHandler::attributesCompleted);
         pivot.next();
         pivot.require(XMLStreamConstants.START_ELEMENT, "urn:test", "wrong_name");
     }
     
     @Test(expected=XMLStreamException.class)
     public void testNamespaceURIOnStartDocument() throws Exception {
+        StAXPivot pivot = createStAXPivot(Action.DEFAULT_START_DOCUMENT);
         pivot.require(XMLStreamConstants.START_DOCUMENT, "http://example.org", null);
     }
     
     @Test(expected=XMLStreamException.class)
     public void testNamespaceURIMismatchOnStartElement() throws Exception {
+        StAXPivot pivot = createStAXPivot(
+                Action.DEFAULT_START_DOCUMENT,
+                h -> h.startElement("urn:test", "test", "p"),
+                XmlHandler::attributesCompleted);
         pivot.next();
         pivot.require(XMLStreamConstants.START_ELEMENT, "urn:wrong_uri", "test");
     }