From 132aa8a77685ec92bc90c03f987650d275a7b639 Mon Sep 17 00:00:00 2001 From: lresende Date: Mon, 30 Sep 2013 06:59:11 +0000 Subject: 2.0.1 RC1 release tag git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1527464 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/common/xml/dom/DOMHelperTestCase.java | 106 +++++++++++ .../sca/common/xml/stax/StAXHelperTestCase.java | 112 +++++++++++ .../stax/reader/XmlTreeStreamReaderTestCase.java | 207 +++++++++++++++++++++ .../sca/common/xml/xpath/XPathHelperTestCase.java | 147 +++++++++++++++ 4 files changed, 572 insertions(+) create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java create mode 100644 sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelperTestCase.java (limited to 'sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany') diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java new file mode 100644 index 0000000000..1be852ba05 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/dom/DOMHelperTestCase.java @@ -0,0 +1,106 @@ +/* + * 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.tuscany.sca.common.xml.dom; + +import static org.junit.Assert.assertNotNull; + +import javax.xml.parsers.DocumentBuilder; + +import org.apache.tuscany.sca.common.xml.sax.SAXHelper; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.custommonkey.xmlunit.XMLAssert; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.ContentHandler; + +/** + * Test Case for StAXHelper + * + * @version $Rev$ $Date$ + */ +public class DOMHelperTestCase { + private static final String XML = + "" + "" + + ""; + + private static ExtensionPointRegistry registry; + + @BeforeClass + public static void init() { + registry = new DefaultExtensionPointRegistry(); + registry.start(); + } + + @AfterClass + public static void destroy() { + if (registry != null) { + registry.stop(); + } + } + + @Test + public void testHelper() throws Exception { + DOMHelper helper = DOMHelper.getInstance(registry); + Document document = helper.load(XML); + String xml = helper.saveAsString(document); + XMLAssert.assertXMLEqual(XML, xml); + + Document root = helper.newDocument(); + ContentHandler handler = helper.createContentHandler(root); + + DOMHelper helper2 = DOMHelper.getInstance(registry); + Assert.assertSame(helper, helper2); + + SAXHelper saxHelper = new SAXHelper(registry); + saxHelper.parse(XML, handler); + + assertNotNull(root.getFirstChild()); + xml = helper.saveAsString(root); + XMLAssert.assertXMLEqual(XML, xml); + } + + @Test + public void testPool() { + DOMHelper helper = DOMHelper.getInstance(registry); + + DocumentBuilder buidler1 = helper.newDocumentBuilder(); + Assert.assertTrue(helper.builderPool.getObjects().get(buidler1)); + + Assert.assertEquals(1, helper.builderPool.inUse()); + + DocumentBuilder buidler2 = helper.newDocumentBuilder(); + Assert.assertTrue(helper.builderPool.getObjects().get(buidler2)); + Assert.assertEquals(2, helper.builderPool.inUse()); + + helper.returnDocumentBuilder(buidler2); + Assert.assertFalse(helper.builderPool.getObjects().get(buidler2)); + Assert.assertEquals(1, helper.builderPool.inUse()); + + helper.returnDocumentBuilder(buidler1); + Assert.assertFalse(helper.builderPool.getObjects().get(buidler1)); + Assert.assertEquals(0, helper.builderPool.inUse()); + + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java new file mode 100644 index 0000000000..77f060b081 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/StAXHelperTestCase.java @@ -0,0 +1,112 @@ +/* + * 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.tuscany.sca.common.xml.stax; + +import static org.junit.Assert.assertNotNull; + +import java.net.URL; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.common.xml.stax.StAXHelper.Attribute; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.custommonkey.xmlunit.XMLAssert; +import org.junit.Assert; +import org.junit.Test; +import org.w3c.dom.Node; + +/** + * Test Case for StAXHelper + * + * @version $Rev$ $Date$ + */ +public class StAXHelperTestCase { + private static final String XML = + "" + "" + + ""; + public static final QName WSDL11 = new QName("http://schemas.xmlsoap.org/wsdl/", "definitions"); + public static final QName WSDL20 = new QName("http://www.w3.org/ns/wsdl", "description"); + public static final QName XSD = new QName("http://www.w3.org/2001/XMLSchema", "schema"); + + @Test + public void testHelper() throws Exception { + StAXHelper helper = new StAXHelper(new DefaultExtensionPointRegistry()); + XMLStreamReader reader = helper.createXMLStreamReader(XML); + String xml = helper.saveAsString(reader); + XMLAssert.assertXMLEqual(XML, xml); + reader = helper.createXMLStreamReader(xml); + assertNotNull(reader); + + Node node = helper.saveAsNode(reader); + assertNotNull(node.getFirstChild()); + reader = helper.createXMLStreamReader(node); + xml = helper.saveAsString(reader); + XMLAssert.assertXMLEqual(XML, xml); + } + + @Test + public void testNoRepairingNamespaces() throws Exception { + StAXHelper helper = new StAXHelper(new DefaultExtensionPointRegistry()); + helper.getOutputFactory().setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.FALSE); + XMLStreamReader reader = helper.createXMLStreamReader(XML); + String xml = helper.saveAsString(reader); + XMLAssert.assertXMLEqual(XML, xml); + reader = helper.createXMLStreamReader(xml); + assertNotNull(reader); + } + + @Test + public void testIndex() throws Exception { + StAXHelper helper = new StAXHelper(new DefaultExtensionPointRegistry()); + URL xsd = getClass().getResource("test.xsd"); + String tns = helper.readAttribute(xsd, XSD, "targetNamespace"); + Assert.assertEquals("http://www.example.org/test/", tns); + + List tnsList = helper.readAttributes(xsd, XSD, "targetNamespace"); + Assert.assertEquals(1, tnsList.size()); + Assert.assertEquals("http://www.example.org/test/", tnsList.get(0)); + + URL wsdl = getClass().getResource("test.wsdl"); + tns = helper.readAttribute(wsdl, WSDL11, "targetNamespace"); + Assert.assertEquals("http://www.example.org/test/wsdl", tns); + + tns = helper.readAttribute(wsdl, XSD, "targetNamespace"); + Assert.assertNull(tns); + + tnsList = helper.readAttributes(wsdl, XSD, "targetNamespace"); + Assert.assertEquals(2, tnsList.size()); + Assert.assertEquals("http://www.example.org/test/xsd1", tnsList.get(0)); + Assert.assertEquals("http://www.example.org/test/xsd2", tnsList.get(1)); + + Attribute attr1 = new Attribute(WSDL11, "targetNamespace"); + Attribute attr2 = new Attribute(XSD, "targetNamespace"); + Attribute[] attrs = helper.readAttributes(wsdl, attr1, attr2); + + Assert.assertEquals(2, attrs.length); + Assert.assertEquals("http://www.example.org/test/wsdl", attrs[0].getValues().get(0)); + Assert.assertEquals("http://www.example.org/test/xsd1", attrs[1].getValues().get(0)); + Assert.assertEquals("http://www.example.org/test/xsd2", attrs[1].getValues().get(1)); + + } + +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java new file mode 100644 index 0000000000..264c1a0d61 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/stax/reader/XmlTreeStreamReaderTestCase.java @@ -0,0 +1,207 @@ +/* + * 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.tuscany.sca.common.xml.stax.reader; + +import java.io.StringReader; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.tuscany.sca.common.xml.stax.impl.XMLStreamSerializer; +import org.custommonkey.xmlunit.XMLAssert; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @version $Rev$ $Date$ + */ +public class XmlTreeStreamReaderTestCase { + private static final String IPO_XML = + "" + "" + + " " + + " Helen Zoe" + + " 47 Eden Street" + + " Cambridge" + + " CB1 1JR" + + " " + + " " + + " Robert Smith" + + " 8 Oak Avenue" + + " Old Town" + + " PA" + + " 95819" + + " " + + " " + + " " + + " Lapis necklace" + + " 1" + + " 99.95" + + " Want this for the holidays" + + " 1999-12-05" + + " " + + " " + + ""; + + private static final String XML_RESULT = + "" + "" + + "MyText" + + "" + + " " + + " " + + "Helen Zoe 47 Eden Street " + + "Cambridge CB1 1JR " + + " Robert Smith " + + "8 Oak Avenue Old Town PA " + + "95819 " + + "Lapis necklace 1 " + + "99.95 Want this for the holidays " + + "1999-12-05 "; + private XmlNodeImpl root; + + @Before + public void setUp() throws Exception { + root = new XmlNodeImpl(); + root.name = new QName("http://ns", "e1", "p1"); + + XmlNodeImpl e11 = new XmlNodeImpl(); + e11.name = new QName("http://ns1", "e11", "p2"); + + XmlNodeImpl e12 = new XmlNodeImpl(); + e12.name = new QName("http://ns", "e12"); + + root.children.add(e11); + root.children.add(e12); + + XmlNodeImpl e121 = new XmlNodeImpl(); + e121.name = new QName("http://ns", "e121"); + e12.children.add(e121); + + XmlNodeImpl e111 = new XmlNodeImpl(); + e111.value = "MyText"; + e11.children.add(e111); + + XmlNodeImpl e13 = new XmlNodeImpl(); + e13.value = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(IPO_XML)); + root.children.add(e13); + + } + + @Test + public void testIterator() { + List elements = new ArrayList(); + XmlNodeIterator i = new XmlNodeIterator(root); + for (; i.hasNext();) { + XmlNode e = i.next(); + elements.add(e.getName()); + } + // System.out.println(elements); + QName[] names = + {new QName("http://ns", "e1"), new QName("http://ns1", "e11"), null, null, new QName("http://ns1", "e11"), + new QName("http://ns", "e12"), new QName("http://ns", "e121"), new QName("http://ns", "e121"), + new QName("http://ns", "e12"), null, null, new QName("http://ns", "e1")}; + Assert.assertEquals(Arrays.asList(names), elements); + } + + @Test + public void testReader() throws Exception { + XmlTreeStreamReaderImpl reader = new XmlTreeStreamReaderImpl(root); + XMLStreamSerializer serializer = new XMLStreamSerializer(); + StringWriter sw = new StringWriter(); + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE); + XMLStreamWriter writer = outputFactory.createXMLStreamWriter(sw); + serializer.serialize(reader, writer); + String xml = sw.toString(); + XMLAssert.assertXMLEqual(XML_RESULT, xml); + } + + private static class XmlNodeImpl implements XmlNode { + private List children = new ArrayList(); + private List attrs = new ArrayList(); + private Map namespaces = new HashMap(); + private QName name; + private Object value = "123"; + + /** + * @see org.apache.tuscany.sca.common.xml.stax.reader.databinding.xml.XmlNode#attributes() + */ + public List attributes() { + return attrs; + } + + /** + * @see org.apache.tuscany.sca.common.xml.stax.reader.databinding.xml.XmlNode#children() + */ + public Iterator children() { + return children.iterator(); + } + + /** + * @see org.apache.tuscany.sca.common.xml.stax.reader.databinding.xml.XmlNode#getName() + */ + public QName getName() { + return name; + } + + /** + * @see org.apache.tuscany.sca.common.xml.stax.reader.databinding.xml.XmlNode#getValue() + */ + public T getValue() { + return (T)value; + } + + /** + * @see org.apache.tuscany.sca.common.xml.stax.reader.databinding.xml.XmlNode#namespaces() + */ + public Map namespaces() { + return namespaces; + } + + @Override + public String toString() { + return String.valueOf(name); + } + + public Type getType() { + if (value instanceof XMLStreamReader) { + return Type.READER; + } + return name == null ? Type.CHARACTERS : Type.ELEMENT; + } + + } +} diff --git a/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelperTestCase.java b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelperTestCase.java new file mode 100644 index 0000000000..3b1f7b52f0 --- /dev/null +++ b/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/test/java/org/apache/tuscany/sca/common/xml/xpath/XPathHelperTestCase.java @@ -0,0 +1,147 @@ +/* + * 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.tuscany.sca.common.xml.xpath; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.stream.XMLStreamReader; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; + +import org.apache.tuscany.sca.common.xml.dom.DOMHelper; +import org.apache.tuscany.sca.common.xml.stax.StAXHelper; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * + */ +public class XPathHelperTestCase { + private static XPathHelper xpathHelper; + private static StAXHelper staxHelper; + private static DOMHelper domHelper; + + private static String XML = + "" + "" + + "" + + "" + + ""; + + private static String XML2 = ""; + + private static String XPATH = + "" + ""; + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + ExtensionPointRegistry registry = new DefaultExtensionPointRegistry(); + xpathHelper = XPathHelper.getInstance(registry); + domHelper = DOMHelper.getInstance(registry); + staxHelper = StAXHelper.getInstance(registry); + } + + @Test + public void testNewXPath() { + XPath path = xpathHelper.newXPath(); + Assert.assertNotNull(path); + } + + @Test + public void testCompile() throws Exception { + XMLStreamReader reader = staxHelper.createXMLStreamReader(XPATH); + reader.nextTag(); + String xpath = reader.getAttributeValue(null, "attachTo"); + XPathExpression expression = xpathHelper.compile(reader.getNamespaceContext(), xpath); + // Advance the reader so that the namespace context changes its prefix/namespace mapping + reader.nextTag(); + reader.close(); + + Document doc = domHelper.load(XML); + NodeList nodes = (NodeList)expression.evaluate(doc, XPathConstants.NODESET); + Assert.assertEquals(1, nodes.getLength()); + Node node = nodes.item(0); + Assert.assertTrue(node instanceof Element); + Assert.assertEquals(node.getNodeName(), "c:child1"); + } + + @Test + public void testCompile2() throws Exception { + XMLStreamReader reader = staxHelper.createXMLStreamReader(XPATH); + reader.nextTag(); + String xpathExp = reader.getAttributeValue(null, "attachTo"); + XPath xpath = xpathHelper.newXPath(); + // Compile the expression without taking a snapshot of the namespace context + XPathExpression expression = xpathHelper.compile(xpath, reader.getNamespaceContext(), xpathExp); + // Advance the reader so that the namespace context changes its prefix/namespace mapping + reader.nextTag(); + reader.close(); + + Document doc = domHelper.load(XML); + NodeList nodes = (NodeList)expression.evaluate(doc, XPathConstants.NODESET); + Assert.assertEquals(1, nodes.getLength()); + Node node = nodes.item(0); + Assert.assertTrue(node instanceof Element); + Assert.assertEquals(node.getNodeName(), "c:child1"); + } + + @Test + public void testGetPrefixes() throws Exception { + XMLStreamReader reader = staxHelper.createXMLStreamReader(XML2); + reader.nextTag(); + NamespaceContext ctx = xpathHelper.getNamespaceContext("//sca:reference[IntentRefs('test:foo')]", + reader.getNamespaceContext()); + Assert.assertNotNull(ctx.getNamespaceURI("sca")); + Assert.assertNotNull(ctx.getNamespaceURI("test")); + + } + + @Test + public void testGetPrefixes2() throws Exception { + XMLStreamReader reader = staxHelper.createXMLStreamReader(XML2); + reader.nextTag(); + NamespaceContext ctx = xpathHelper.getNamespaceContext("//sca:implementation.java[ IntentRefs( 'test:foo' ) ]", + reader.getNamespaceContext()); + Assert.assertNotNull(ctx.getNamespaceURI("sca")); + Assert.assertNotNull(ctx.getNamespaceURI("test")); + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + xpathHelper = null; + } + +} -- cgit v1.2.3