From 7ad16a07b4288227c531c485939318768129fb54 Mon Sep 17 00:00:00 2001 From: antelder Date: Wed, 3 Aug 2011 09:20:48 +0000 Subject: Tag debug mod for TUSCANY-3909 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1153403 13f79535-47bb-0310-9956-ffa450edef68 --- .../xml/XmlTreeStreamReaderTestCase.java | 199 +++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 sca-java-1.x/tags/1.6.1-TUSCANY-3909/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java (limited to 'sca-java-1.x/tags/1.6.1-TUSCANY-3909/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java') diff --git a/sca-java-1.x/tags/1.6.1-TUSCANY-3909/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java new file mode 100644 index 0000000000..0463e7d9f5 --- /dev/null +++ b/sca-java-1.x/tags/1.6.1-TUSCANY-3909/databinding/src/test/java/org/apache/tuscany/sca/databinding/xml/XmlTreeStreamReaderTestCase.java @@ -0,0 +1,199 @@ +/* + * 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.databinding.xml; + +import java.io.StringReader; +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.XMLStreamReader; + +import junit.framework.Assert; + +import org.custommonkey.xmlunit.XMLAssert; +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); + XMLStreamReader2String t = new XMLStreamReader2String(); + String xml = t.transform(reader, null); + 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.databinding.xml.XmlNode#attributes() + */ + public List attributes() { + return attrs; + } + + /** + * @see org.apache.tuscany.sca.databinding.xml.XmlNode#children() + */ + public Iterator children() { + return children.iterator(); + } + + /** + * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getName() + */ + public QName getName() { + return name; + } + + /** + * @see org.apache.tuscany.sca.databinding.xml.XmlNode#getValue() + */ + public T getValue() { + return (T)value; + } + + /** + * @see org.apache.tuscany.sca.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; + } + + } +} -- cgit v1.2.3