1 /* 2 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 3 * 4 * This software is open source. 5 * See the bottom of this file for the licence. 6 * 7 * $Id: TestXMLResult.java,v 1.5 2004/06/25 08:03:47 maartenc Exp $ 8 */ 9 10 package org.dom4j; 11 12 import java.io.StringWriter; 13 14 import javax.xml.transform.Result; 15 import javax.xml.transform.Source; 16 import javax.xml.transform.Transformer; 17 import javax.xml.transform.TransformerFactory; 18 19 import junit.framework.Test; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 import org.dom4j.io.DocumentSource; 24 import org.dom4j.io.OutputFormat; 25 import org.dom4j.io.XMLResult; 26 import org.dom4j.io.XMLWriter; 27 28 /*** Test harness for the XMLResult which acts as a JAXP Result 29 * 30 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 31 * @version $Revision: 1.5 $ 32 */ 33 public class TestXMLResult extends AbstractTestCase { 34 35 protected static final boolean VERBOSE = false; 36 37 38 public static void main( String[] args ) { 39 TestRunner.run( suite() ); 40 } 41 42 public static Test suite() { 43 return new TestSuite( TestXMLResult.class ); 44 } 45 46 public TestXMLResult(String name) { 47 super(name); 48 } 49 50 // Test case(s) 51 //------------------------------------------------------------------------- 52 public void testWriter() throws Exception { 53 // load a default transformer 54 TransformerFactory factory = TransformerFactory.newInstance(); 55 Transformer transformer = factory.newTransformer(); 56 57 // use dom4j document as the source 58 Source source = new DocumentSource( document ); 59 60 // use pretty print format and a buffer for the result 61 OutputFormat format = OutputFormat.createCompactFormat(); 62 StringWriter buffer = new StringWriter(); 63 Result result = new XMLResult( buffer, format ); 64 65 // now lets transform 66 transformer.transform( source, result ); 67 68 String text = buffer.toString(); 69 70 if ( VERBOSE ) { 71 log( "Using JAXP and XMLResult the document is:- " ); 72 log( text ); 73 } 74 75 76 77 StringWriter out = new StringWriter(); 78 79 XMLWriter writer = new XMLWriter( out, format ); 80 writer.write( document ); 81 82 String text2 = out.toString(); 83 84 if ( VERBOSE ) { 85 log( "Using XMLWriter the text is:-" ); 86 log( text2 ); 87 } 88 89 assertEquals( "The text output should be identical", text2 ,text ); 90 } 91 } 92 93 94 95 96 /* 97 * Redistribution and use of this software and associated documentation 98 * ("Software"), with or without modification, are permitted provided 99 * that the following conditions are met: 100 * 101 * 1. Redistributions of source code must retain copyright 102 * statements and notices. Redistributions must also contain a 103 * copy of this document. 104 * 105 * 2. Redistributions in binary form must reproduce the 106 * above copyright notice, this list of conditions and the 107 * following disclaimer in the documentation and/or other 108 * materials provided with the distribution. 109 * 110 * 3. The name "DOM4J" must not be used to endorse or promote 111 * products derived from this Software without prior written 112 * permission of MetaStuff, Ltd. For written permission, 113 * please contact dom4j-info@metastuff.com. 114 * 115 * 4. Products derived from this Software may not be called "DOM4J" 116 * nor may "DOM4J" appear in their names without prior written 117 * permission of MetaStuff, Ltd. DOM4J is a registered 118 * trademark of MetaStuff, Ltd. 119 * 120 * 5. Due credit should be given to the DOM4J Project - 121 * http://www.dom4j.org 122 * 123 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 124 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 125 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 126 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 127 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 128 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 129 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 130 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 131 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 132 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 133 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 134 * OF THE POSSIBILITY OF SUCH DAMAGE. 135 * 136 * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. 137 * 138 * $Id: TestXMLResult.java,v 1.5 2004/06/25 08:03:47 maartenc Exp $ 139 */