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.io.OutputFormat; 18 import org.dom4j.io.SAXReader; 19 import org.dom4j.io.XMLWriter; 20 import org.dom4j.tree.BaseElement; 21 import org.dom4j.tree.DefaultDocument; 22 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 23 import org.xml.sax.ContentHandler ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.helpers.AttributesImpl ; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.StringWriter ; 30 31 36 public class TestXMLWriter extends AbstractTestCase { 37 38 protected static final boolean VERBOSE = false; 39 40 41 public static void main( String [] args ) { 42 TestRunner.run( suite() ); 43 } 44 45 public static Test suite() { 46 return new TestSuite( TestXMLWriter.class ); 47 } 48 49 public TestXMLWriter(String name) { 50 super(name); 51 } 52 53 public void testWriter() throws Exception { 56 Object object = document; 57 StringWriter out = new StringWriter (); 58 59 XMLWriter writer = new XMLWriter( out ); 60 writer.write( object ); 61 writer.close(); 62 63 String text = out.toString(); 64 65 if ( VERBOSE ) { 66 log( "Text output is [" ); 67 log( text ); 68 log( "]. Done" ); 69 } 70 71 assertTrue( "Output text is bigger than 10 characters", text.length() > 10 ); 72 } 73 74 public void testEncodingFormats() throws Exception { 75 testEncoding( "UTF-8" ); 76 testEncoding( "UTF-16" ); 77 testEncoding( "ISO-8859-1" ); 78 } 79 80 protected void testEncoding(String encoding) throws Exception { 81 ByteArrayOutputStream out = new ByteArrayOutputStream (); 82 83 OutputFormat format = OutputFormat.createPrettyPrint(); 84 format.setEncoding( encoding ); 85 XMLWriter writer = new XMLWriter( out, format ); 86 writer.write( document ); 87 writer.close(); 88 89 log( "Wrote to encoding: " + encoding ); 90 } 91 92 public void testWriterBug() throws Exception { 93 Element project = new BaseElement("project"); 94 Document doc = new DefaultDocument(project); 95 96 ByteArrayOutputStream out = new ByteArrayOutputStream (); 97 XMLWriter writer = new XMLWriter(out, new OutputFormat("\t", true, "ISO-8859-1")); 98 writer.write(doc); 99 100 ByteArrayInputStream in = new ByteArrayInputStream ( out.toByteArray() ); 101 SAXReader reader = new SAXReader(); 102 Document doc2 = reader.read( in ); 103 104 assertTrue( "Generated document has a root element", doc2.getRootElement() != null ); 105 assertEquals( "Generated document has corrent named root element", doc2.getRootElement().getName(), "project" ); 106 } 107 108 public void testNamespaceBug() throws Exception { 109 Document doc = O3DocumentHelper.createDocument(); 110 111 Element root = doc.addElement("root","ns1"); 112 Element child1 = root.addElement("joe","ns2"); 113 child1.addElement("zot","ns1"); 114 115 StringWriter out = new StringWriter (); 116 XMLWriter writer = new XMLWriter( 117 out, 118 OutputFormat.createPrettyPrint() 119 ); 120 writer.write(doc); 121 String text = out.toString(); 122 123 125 Document doc2 = O3DocumentHelper.parseText( text ); 126 root = doc2.getRootElement(); 127 assertEquals( "root has correct namespace", "ns1", root.getNamespaceURI() ); 128 129 Element joe = (Element) root.elementIterator().next(); 130 assertEquals( "joe has correct namespace", "ns2", joe.getNamespaceURI() ); 131 132 Element zot = (Element) joe.elementIterator().next(); 133 assertEquals( "zot has correct namespace", "ns1", zot.getNamespaceURI() ); 134 } 135 136 137 public void testContentHandler() throws Exception { 138 StringWriter out = new StringWriter (); 139 OutputFormat format = OutputFormat.createPrettyPrint(); 140 format.setEncoding("iso-8859-1"); 141 XMLWriter writer = new XMLWriter(out, format); 142 generateXML(writer); 143 writer.close(); 144 String text = out.toString(); 145 146 if ( VERBOSE ) { 147 log( "Created XML" ); 148 log( text ); 149 } 150 151 Document doc = O3DocumentHelper.parseText( text ); 153 String value = doc.valueOf( "/processes[@name='arvojoo']" ); 154 assertEquals( "Document contains the correct text", "jeejee", value ); 155 } 156 157 158 public void testWhitespaceBug() throws Exception { 159 Document doc = O3DocumentHelper.parseText( 160 "<notes> This is a multiline\n\rentry</notes>" 161 ); 162 163 OutputFormat format = new OutputFormat(); 164 format.setEncoding("UTF-8"); 165 format.setIndentSize(4); 166 format.setNewlines(true); 167 format.setTrimText(true); 168 format.setExpandEmptyElements(true); 169 170 StringWriter buffer = new StringWriter (); 171 XMLWriter writer = new XMLWriter(buffer, format); 172 writer.write( doc ); 173 174 String xml = buffer.toString(); 175 log( xml ); 176 177 Document doc2 = O3DocumentHelper.parseText( xml ); 178 String text = doc2.valueOf( "/notes" ); 179 String expected = "This is a multiline entry"; 180 181 assertEquals( "valueOf() returns the correct text padding", expected, text ); 182 183 assertEquals( "getText() returns the correct text padding", expected, doc2.getRootElement().getText() ); 184 } 185 186 187 public void testWhitespaceBug2() throws Exception { 188 Document doc = O3DocumentHelper.createDocument(); 189 Element root = doc.addElement( "root" ); 190 Element meaning = root.addElement( "meaning" ); 191 meaning.addText( "to li" ); 192 meaning.addText( "ve" ); 193 194 OutputFormat format = new OutputFormat(); 195 format.setEncoding("UTF-8"); 196 format.setIndentSize(4); 197 format.setNewlines(true); 198 format.setTrimText(true); 199 format.setExpandEmptyElements(true); 200 201 StringWriter buffer = new StringWriter (); 202 XMLWriter writer = new XMLWriter(buffer, format); 203 writer.write( doc ); 204 205 String xml = buffer.toString(); 206 log( xml ); 207 208 Document doc2 = O3DocumentHelper.parseText( xml ); 209 String text = doc2.valueOf( "/root/meaning" ); 210 String expected = "to live"; 211 212 assertEquals( "valueOf() returns the correct text padding", expected, text ); 213 214 assertEquals( "getText() returns the correct text padding", expected, doc2.getRootElement().element("meaning").getText() ); 215 } 216 217 220 public void testPrettyPrinting() throws Exception { 221 Document doc = nodeFactory.createDocument(); 222 doc.addElement("summary").addAttribute("date", "6/7/8").addElement("orderline").addText("puffins").addElement("ranjit").addComment("Ranjit is a happy Puffin"); 223 XMLWriter writer = new XMLWriter(System.out, OutputFormat.createPrettyPrint()); 224 writer.write(doc); 225 226 doc = nodeFactory.createDocument(); 227 doc.addElement("summary").addAttribute("date", "6/7/8").addElement("orderline").addText("puffins").addElement("ranjit").addComment("Ranjit is a happy Puffin").addComment("another comment").addElement("anotherElement"); 228 writer.write(doc); 229 } 230 231 232 protected void generateXML(ContentHandler handler) throws SAXException { 233 handler.startDocument(); 234 AttributesImpl attrs = new AttributesImpl (); 235 attrs.clear(); 236 attrs.addAttribute("","","name","CDATA", "arvojoo"); 237 handler.startElement("","","processes",attrs); 238 String text="jeejee"; 239 char textch[] = text.toCharArray(); 240 handler.characters(textch,0,textch.length); 241 handler.endElement("","","processes" ); 242 handler.endDocument(); 243 } 244 } 245 246 247 248 249 293 | Popular Tags |