You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/11/27 21:28:38 UTC

svn commit: r884987 - in /incubator/pivot/trunk/core/test/org/apache/pivot/xml: ./ test/ test/XMLSerializerTest.java test/sample.xml

Author: gbrown
Date: Fri Nov 27 20:28:38 2009
New Revision: 884987

URL: http://svn.apache.org/viewvc?rev=884987&view=rev
Log:
Add a unit test for XMLSerializer.

Added:
    incubator/pivot/trunk/core/test/org/apache/pivot/xml/
    incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/
    incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java
    incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/sample.xml

Added: incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java?rev=884987&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/XMLSerializerTest.java Fri Nov 27 20:28:38 2009
@@ -0,0 +1,77 @@
+/*
+ * 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.pivot.xml.test;
+
+import java.io.IOException;
+
+import org.apache.pivot.collections.List;
+import org.apache.pivot.serialization.SerializationException;
+import org.apache.pivot.xml.Element;
+import org.apache.pivot.xml.XMLSerializer;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+public class XMLSerializerTest {
+    @Test
+    public void basicTest() throws IOException, SerializationException {
+        XMLSerializer xmlSerializer = new XMLSerializer();
+
+        Element root = xmlSerializer.readObject(getClass().getResourceAsStream("sample.xml"));
+
+        assertEquals(root.getName(), "root");
+
+        Element a = XMLSerializer.getElement(root, "a");
+        assertEquals(a.getName(), "a");
+        assertEquals(a.get("id"), "x");
+
+        Element b = XMLSerializer.getElement(root, "a/b");
+        assertEquals(b.getName(), "b");
+        assertEquals(b.get("id"), "y");
+
+        b = XMLSerializer.getElement(a, "b");
+        assertEquals(b.getName(), "b");
+        assertEquals(b.get("id"), "y");
+
+        List<Element> cs = XMLSerializer.getElements(root, "a/b/c");
+        assertEquals(cs.getLength(), 1);
+
+        List<Element> fs = XMLSerializer.getElements(root, "d/e/f");
+        assertEquals(fs.getLength(), 4);
+
+        Element e = XMLSerializer.getElement(root, "d/e");
+        Element f = XMLSerializer.getElement(e, "f");
+        assertEquals(f.getName(), "f");
+
+        Element g = XMLSerializer.getElement(e, "g");
+        assertEquals(g.getName(), "g");
+
+        String ft = XMLSerializer.getText(root, "d/e/f");
+        assertEquals(ft, "1");
+
+        String gt = XMLSerializer.getText(root, "d/e/g");
+        assertEquals(gt, "4");
+
+        assertNull(XMLSerializer.getElement(root, "a/b/n"));
+        assertNull(XMLSerializer.getText(root, "a/b/n"));
+
+        assertEquals(XMLSerializer.getElements(root, "a/b/n").getLength(), 0);
+
+        assertEquals(XMLSerializer.getText(root, "d/foo:h"), "Hello");
+    }
+}

Added: incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/sample.xml
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/sample.xml?rev=884987&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/sample.xml (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/xml/test/sample.xml Fri Nov 27 20:28:38 2009
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+
+<root xmlns:foo="bar">
+    <a id="x">
+        <b id="y">
+            <c>ABC</c>
+        </b>
+    </a>
+
+    <d>
+        <e>
+            <f>1</f>
+            <f>2</f>
+            <f>3</f>
+            <g>4</g>
+            <f>5</f>
+        </e>
+        <foo:h>Hello</foo:h>
+    </d>
+</root>