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.io.DocumentSource; 16 import org.dom4j.io.OutputFormat; 17 import org.dom4j.io.XMLResult; 18 import org.dom4j.io.XMLWriter; 19 20 import javax.xml.transform.Result ; 21 import javax.xml.transform.Source ; 22 import javax.xml.transform.Transformer ; 23 import javax.xml.transform.TransformerFactory ; 24 import java.io.StringWriter ; 25 26 31 public class TestXMLResult extends AbstractTestCase { 32 33 protected static final boolean VERBOSE = false; 34 35 36 public static void main( String [] args ) { 37 TestRunner.run( suite() ); 38 } 39 40 public static Test suite() { 41 return new TestSuite( TestXMLResult.class ); 42 } 43 44 public TestXMLResult(String name) { 45 super(name); 46 } 47 48 public void testWriter() throws Exception { 51 TransformerFactory factory = TransformerFactory.newInstance(); 53 Transformer transformer = factory.newTransformer(); 54 55 Source source = new DocumentSource( document ); 57 58 OutputFormat format = OutputFormat.createCompactFormat(); 60 StringWriter buffer = new StringWriter (); 61 Result result = new XMLResult( buffer, format ); 62 63 transformer.transform( source, result ); 65 66 String text = buffer.toString(); 67 68 if ( VERBOSE ) { 69 log( "Using JAXP and XMLResult the document is:- " ); 70 log( text ); 71 } 72 73 74 75 StringWriter out = new StringWriter (); 76 77 XMLWriter writer = new XMLWriter( out, format ); 78 writer.write( document ); 79 80 String text2 = out.toString(); 81 82 if ( VERBOSE ) { 83 log( "Using XMLWriter the text is:-" ); 84 log( text2 ); 85 } 86 87 assertEquals( "The text output should be identical", text2 ,text ); 88 } 89 } 90 91 92 93 94 138 | Popular Tags |