1 9 10 package test.dom4j.dom; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.dom.DOMDocument; 16 import org.dom4j.dom.DOMDocumentFactory; 17 import org.dom4j.io.DOMWriter; 18 import org.dom4j.io.SAXReader; 19 import org.w3c.dom.DOMException ; 20 import org.w3c.dom.NamedNodeMap ; 21 import org.w3c.dom.Node ; 22 import org.w3c.dom.NodeList ; 23 24 import java.io.File ; 25 import java.io.InputStream ; 26 27 32 public class TestDOM extends test.dom4j.AbstractTestCase { 33 34 protected static boolean VERBOSE = false; 35 36 37 private long elements; 38 39 40 private long attributes; 41 42 43 private long characters; 44 45 46 47 public static void main( String [] args ) { 48 TestRunner.run( suite() ); 49 } 50 51 public static Test suite() { 52 return new TestSuite( TestDOM.class ); 53 } 54 55 public TestDOM(String name) { 56 super(name); 57 } 58 59 public void testCount() throws Exception { 62 DOMWriter domWriter = new DOMWriter(); 63 64 long start = System.currentTimeMillis(); 65 org.w3c.dom.Document domDocument = domWriter.write( document ); 66 long end = System.currentTimeMillis(); 67 68 System.out.println( "Converting to a W3C Document took: " + (end - start) + " milliseconds" ); 69 70 traverse( domDocument ); 71 72 log( "elements: " + elements 73 + " attributes: " + attributes 74 + " characters: " + characters 75 ); 76 } 77 78 79 public void testClassCastBug() throws Exception { 80 DOMDocument oDocument = new DOMDocument("Root"); 81 org.w3c.dom.Element oParent = oDocument.createElement("Parent"); 82 84 oParent.setAttribute("name", "N01"); 85 oParent.setAttribute("id", "ID01"); 86 87 oDocument.appendChild(oParent); } 89 90 public void testReplaceChild() throws Exception { 91 DOMDocument document = new DOMDocument("Root"); 92 org.w3c.dom.Element parent = document.createElement("Parent"); 93 org.w3c.dom.Element first = document.createElement("FirstChild"); 94 org.w3c.dom.Element second = document.createElement("SecondChild"); 95 org.w3c.dom.Element third = document.createElement("ThirdChild"); 96 97 document.appendChild(parent); 98 parent.appendChild(first); 99 parent.appendChild(second); 100 parent.appendChild(third); 101 102 org.w3c.dom.Element newFirst = document.createElement("NewFirst"); 103 org.w3c.dom.Element oldFirst = (org.w3c.dom.Element ) parent.replaceChild(newFirst, first); 104 105 106 assertEquals(oldFirst, first); 107 108 109 NodeList children = parent.getChildNodes(); 110 Node firstChild = children.item(0); 111 assertEquals(Node.ELEMENT_NODE, firstChild.getNodeType()); 112 assertEquals(newFirst, firstChild); 113 114 115 org.w3c.dom.Element badNode = document.createElement("No Child"); 116 try { 117 parent.replaceChild(newFirst, badNode); 118 fail("DOMException should be throwed when trying to replace non existing child"); 119 } catch (DOMException e) { 120 assertEquals(DOMException.NOT_FOUND_ERR, e.code); 121 } 122 } 123 124 127 protected void setUp() throws Exception { 128 SAXReader reader = new SAXReader( DOMDocumentFactory.getInstance() ); 130 InputStream testDocument = getClass().getResourceAsStream("/xml/contents.xml"); 131 if (testDocument == null) { 132 document = reader.read( new File ( "xml/contents.xml" ) ); 133 } else { 134 document = reader.read( testDocument ); 135 } 136 } 137 138 protected void traverse(Node node) { 139 140 if (node == null) { 142 return; 143 } 144 145 int type = node.getNodeType(); 146 switch (type) { 147 case Node.DOCUMENT_NODE: { 148 elements = 0; 149 attributes = 0; 150 characters = 0; 151 traverse(((org.w3c.dom.Document )node).getDocumentElement()); 152 break; 153 } 154 155 case Node.ELEMENT_NODE: { 156 elements++; 157 NamedNodeMap attrs = node.getAttributes(); 158 if (attrs != null) { 159 attributes += attrs.getLength(); 160 } 161 NodeList children = node.getChildNodes(); 162 if (children != null) { 163 int len = children.getLength(); 164 for (int i = 0; i < len; i++) { 165 traverse(children.item(i)); 166 } 167 } 168 break; 169 } 170 171 case Node.ENTITY_REFERENCE_NODE: { 172 NodeList children = node.getChildNodes(); 173 if (children != null) { 174 int len = children.getLength(); 175 for (int i = 0; i < len; i++) { 176 traverse(children.item(i)); 177 } 178 } 179 break; 180 } 181 182 case Node.CDATA_SECTION_NODE: { 183 characters += node.getNodeValue().length(); 184 break; 185 } 186 187 case Node.TEXT_NODE: { 188 characters += node.getNodeValue().length(); 189 break; 190 } 191 } 192 } 193 } 194 195 196 197 198 242 | Popular Tags |