1 package com.thoughtworks.xstream.io.xml; 2 3 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 4 import junit.framework.TestCase; 5 6 public abstract class AbstractXMLReaderTest extends TestCase { 7 8 protected abstract HierarchicalStreamReader createReader(String xml) throws Exception ; 10 11 public void testStartsAtRootTag() throws Exception { 12 HierarchicalStreamReader xmlReader = createReader("<hello/>"); 13 assertEquals("hello", xmlReader.getNodeName()); 14 } 15 16 public void testCanNavigateDownChildTagsByIndex() throws Exception { 17 HierarchicalStreamReader xmlReader = createReader("<a><b><ooh/></b><b><aah/></b></a>"); 18 19 assertEquals("a", xmlReader.getNodeName()); 20 21 assertTrue(xmlReader.hasMoreChildren()); 22 23 xmlReader.moveDown(); 25 assertEquals("b", xmlReader.getNodeName()); 26 27 assertTrue(xmlReader.hasMoreChildren()); 28 29 xmlReader.moveDown(); assertEquals("ooh", xmlReader.getNodeName()); 31 assertFalse(xmlReader.hasMoreChildren()); 32 xmlReader.moveUp(); 34 assertFalse(xmlReader.hasMoreChildren()); 35 36 xmlReader.moveUp(); 38 assertTrue(xmlReader.hasMoreChildren()); 39 40 xmlReader.moveDown(); 42 assertEquals("b", xmlReader.getNodeName()); 43 44 assertTrue(xmlReader.hasMoreChildren()); 45 46 xmlReader.moveDown(); 48 assertEquals("aah", xmlReader.getNodeName()); 49 assertFalse(xmlReader.hasMoreChildren()); 50 51 xmlReader.moveUp(); 53 assertFalse(xmlReader.hasMoreChildren()); 54 55 xmlReader.moveUp(); 57 assertFalse(xmlReader.hasMoreChildren()); 58 } 59 60 public void testChildTagsCanBeMixedWithOtherNodes() throws Exception { 61 HierarchicalStreamReader xmlReader = createReader("<!-- xx --><a> <hello/> <!-- x --> getValue <world/></a>"); 62 63 assertTrue(xmlReader.hasMoreChildren()); 64 xmlReader.moveDown(); 65 assertEquals("hello", xmlReader.getNodeName()); 66 xmlReader.moveUp(); 67 68 assertTrue(xmlReader.hasMoreChildren()); 69 xmlReader.moveDown(); 70 assertEquals("world", xmlReader.getNodeName()); 71 xmlReader.moveUp(); 72 73 assertFalse(xmlReader.hasMoreChildren()); 74 } 75 76 public void testAttributesCanBeFetchedFromTags() throws Exception { 77 HierarchicalStreamReader xmlReader = createReader("" + 78 "<hello one=\"1\" two=\"2\">" + 79 " <child three=\"3\"/>" + 80 "</hello>"); 82 assertEquals("1", xmlReader.getAttribute("one")); 83 assertEquals("2", xmlReader.getAttribute("two")); 84 assertNull(xmlReader.getAttribute("three")); 85 86 xmlReader.moveDown(); assertNull(xmlReader.getAttribute("one")); 88 assertNull(xmlReader.getAttribute("two")); 89 assertEquals("3", xmlReader.getAttribute("three")); 90 91 } 92 93 public void testTextCanBeExtractedFromTag() throws Exception { 94 HierarchicalStreamReader xmlReader = createReader("<root><a>some<!-- ignore me --> getValue!</a><b>more</b></root>"); 95 96 xmlReader.moveDown(); 97 assertEquals("some getValue!", xmlReader.getValue()); 98 xmlReader.moveUp(); 99 100 xmlReader.moveDown(); 101 assertEquals("more", xmlReader.getValue()); 102 xmlReader.moveUp(); 103 } 104 105 public void testDoesNotIgnoreWhitespaceAroundText() throws Exception { 106 HierarchicalStreamReader xmlReader = createReader("<root> hello world </root>"); 107 108 assertEquals(" hello world ", xmlReader.getValue()); 109 } 110 111 public void testReturnsEmptyStringForEmptyTags() throws Exception { 112 HierarchicalStreamReader xmlReader = createReader("<root></root>"); 113 114 String text = xmlReader.getValue(); 115 assertNotNull(text); 116 assertEquals("", text); 117 } 118 119 } 120 | Popular Tags |