1 9 10 package test.dom4j; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Document; 16 import org.dom4j.Element; 17 import org.dom4j.Node; 18 import org.dom4j.ProcessingInstruction; 19 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 20 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 29 public class TestContent extends AbstractTestCase { 30 31 public static void main( String [] args ) { 32 TestRunner.run( suite() ); 33 } 34 35 public static Test suite() { 36 return new TestSuite( TestContent.class ); 37 } 38 39 public TestContent(String name) { 40 super(name); 41 } 42 43 public void testRoot() throws Exception { 46 Element root = document.getRootElement(); 47 assertTrue( "Has root element", root != null ); 48 49 List authors = root.elements( "author" ); 50 assertTrue( "Root has children", authors != null && authors.size() == 2 ); 51 52 Element author1 = (Element) authors.get(0); 53 Element author2 = (Element) authors.get(1); 54 55 assertTrue( "Author1 is James", author1.attributeValue( "name" ).equals( "James" ) ); 56 assertTrue( "Author2 is Bob", author2.attributeValue( "name" ).equals( "Bob" ) ); 57 58 testGetAttributes(author1); 59 testGetAttributes(author2); 60 } 61 62 public void testContent() throws Exception { 63 Element root = document.getRootElement(); 64 assertTrue( "Has root element", root != null ); 65 66 List content = root.content(); 67 assertTrue( "Root has content", content != null && content.size() >= 2 ); 68 69 boolean iterated = false; 70 for ( Iterator iter = content.iterator(); iter.hasNext(); ) { 71 Object object = iter.next(); 72 assertTrue( "Content object is a node", object instanceof Node ); 73 iterated = true; 74 } 75 76 assertTrue( "Iteration completed", iterated ); 77 } 78 79 public void testGetNode() throws Exception { 80 Element root = document.getRootElement(); 81 assertTrue( "Has root element", root != null ); 82 83 int count = root.nodeCount(); 84 assertTrue( "Root has correct node count", count == 2 ); 85 86 boolean iterated = false; 87 for ( int i = 0; i < count; i++ ) { 88 Node node = root.node(i); 89 assertTrue( "Valid node returned from node()", node != null ); 90 iterated = true; 91 } 92 93 assertTrue( "Iteration completed", iterated ); 94 } 95 96 public void testGetXPathNode() throws Exception { 97 Element root = document.getRootElement(); 98 assertTrue( "Has root element", root != null ); 99 100 int count = root.nodeCount(); 101 assertTrue( "Root has correct node count", count == 2 ); 102 103 boolean iterated = false; 104 for ( int i = 0; i < count; i++ ) { 105 Node node = root.getXPathResult(i); 106 assertTrue( "Valid node returned from node()", node != null ); 107 assertTrue( "Node supports the parent relationship", node.supportsParent() ); 108 iterated = true; 109 } 110 111 assertTrue( "Iteration completed", iterated ); 112 } 113 114 public void testOrderOfPI() throws Exception { 115 Document document = nodeFactory.createDocument(); 116 document.addProcessingInstruction( "xml-stylesheet", "type=\"text/xsl\" HREF=\"...\"" ); 117 document.addElement( "root" ); 118 119 List list = document.content(); 120 Object pi = list.get(0); 121 Object root = list.get(1); 122 123 assertTrue( "First element is a PI", pi instanceof ProcessingInstruction ); 124 assertTrue( "Second element is an element", root instanceof Element ); 125 126 document = O3DocumentHelper.parseText( 127 "<?xml version=\"1.0\" ?>\n" 128 + "<?xml-stylesheet type=\"text/xsl\" HREF=\"foo\" ?>\n" 129 + "<root/>" 130 ); 131 132 list = document.content(); 133 pi = list.get(0); 134 root = list.get(1); 135 136 assertTrue( "First element is a PI", pi instanceof ProcessingInstruction ); 137 assertTrue( "Second element is an element", root instanceof Element ); 138 139 } 140 141 public void testAddingInTheMiddle() throws Exception { 142 Document doc = nodeFactory.createDocument(); 143 Element root = doc.addElement( "html" ); 144 Element header = root.addElement( "header" ); 145 Element footer = root.addElement( "footer" ); 146 147 List list = root.content(); 149 Element foo = nodeFactory.createElement( "foo" ); 150 list.add( 1, foo ); 151 152 assertTrue( list.size() == 3 ); 154 assertTrue( list.get(0) == header ); 155 assertTrue( list.get(1) == foo ); 156 assertTrue( list.get(2) == footer ); 157 } 158 159 public void testAddAtIndex() throws Exception { 160 System.out.println("testAddAtIndex"); 161 Document doc = nodeFactory.createDocument(); 162 System.out.println("document is " + doc); 163 Element root = doc.addElement( "html" ); 164 System.out.println("root is " + root); 165 Element header = root.addElement( "header" ); 166 System.out.println("header is " + header); 167 Element body = root.addElement( "body" ); 168 System.out.println("body is " + body); 169 170 Element foo = nodeFactory.createElement( "foo" ); 171 System.out.println("created element " + foo); 172 Element bar = nodeFactory.createElement( "bar" ); 173 System.out.println("created element " + bar); 174 175 List content = header.content(); 176 System.out.println("got content " + content.getClass().getName()); 177 content.add(0, foo); 178 content.add(0, bar); 179 System.out.println("content size is now " + content.size()); 180 181 System.out.println("checking header node 1 " + header.node(1)); 182 assertNotNull("checking header node 1: " + header.node(1)); 183 assertEquals( "foo", header.node(1).getName() ); 184 System.out.println("checking header node 0"); 185 assertNotNull("checking header node 0: " + header.node(0)); 186 assertEquals( "bar", header.node(0).getName() ); 187 188 foo = nodeFactory.createElement( "foo" ); 189 bar = nodeFactory.createElement( "bar" ); 190 191 content = body.content(); 192 System.out.println("Adding foo to body"); 193 content.add(0, foo); 194 System.out.println("Adding bar to body"); 195 content.add(1, bar); 196 197 assertNotNull("checking body node 0: " + body.node(0)); 198 assertEquals( "foo", body.node(0).getName() ); 199 assertNotNull("checking body node 1: " + body.node(1)); 200 assertEquals( "bar", body.node(1).getName() ); 201 } 202 203 204 205 protected void testGetAttributes(Element author) throws Exception { 208 209 String definedName = "name"; 210 String undefinedName = "undefined-attribute-name"; 211 String defaultValue = "** Default Value **"; 212 213 String value = author.attributeValue( definedName, defaultValue ); 214 assertTrue( "Defined value doesn't return specified default value", value != defaultValue ); 215 216 value = author.attributeValue( undefinedName, defaultValue ); 217 assertTrue( "Undefined value returns specified default value", value == defaultValue ); 218 } 219 220 } 221 222 223 224 225 269 | Popular Tags |