KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > dom4j > TestXMLResult


1 /*
2  * Copyright 2001 (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.1 2003/07/07 10:30:29 per_nyfelt Exp $
8  */

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 JavaDoc;
21 import javax.xml.transform.Source JavaDoc;
22 import javax.xml.transform.Transformer JavaDoc;
23 import javax.xml.transform.TransformerFactory JavaDoc;
24 import java.io.StringWriter JavaDoc;
25
26 /** Test harness for the XMLResult which acts as a JAXP Result
27   *
28   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
29   * @version $Revision: 1.1 $
30   */

31 public class TestXMLResult extends AbstractTestCase {
32
33     protected static final boolean VERBOSE = false;
34
35
36     public static void main( String JavaDoc[] args ) {
37         TestRunner.run( suite() );
38     }
39
40     public static Test suite() {
41         return new TestSuite( TestXMLResult.class );
42     }
43
44     public TestXMLResult(String JavaDoc name) {
45         super(name);
46     }
47
48     // Test case(s)
49
//-------------------------------------------------------------------------
50
public void testWriter() throws Exception JavaDoc {
51         // load a default transformer
52
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
53         Transformer JavaDoc transformer = factory.newTransformer();
54
55         // use dom4j document as the source
56
Source source = new DocumentSource( document );
57
58         // use pretty print format and a buffer for the result
59
OutputFormat format = OutputFormat.createCompactFormat();
60         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
61         Result result = new XMLResult( buffer, format );
62
63         // now lets transform
64
transformer.transform( source, result );
65
66         String JavaDoc 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 JavaDoc out = new StringWriter JavaDoc();
76
77         XMLWriter writer = new XMLWriter( out, format );
78         writer.write( document );
79
80         String JavaDoc 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 /*
95  * Redistribution and use of this software and associated documentation
96  * ("Software"), with or without modification, are permitted provided
97  * that the following conditions are met:
98  *
99  * 1. Redistributions of source code must retain copyright
100  * statements and notices. Redistributions must also contain a
101  * copy of this document.
102  *
103  * 2. Redistributions in binary form must reproduce the
104  * above copyright notice, this list of conditions and the
105  * following disclaimer in the documentation and/or other
106  * materials provided with the distribution.
107  *
108  * 3. The name "DOM4J" must not be used to endorse or promote
109  * products derived from this Software without prior written
110  * permission of MetaStuff, Ltd. For written permission,
111  * please contact dom4j-info@metastuff.com.
112  *
113  * 4. Products derived from this Software may not be called "DOM4J"
114  * nor may "DOM4J" appear in their names without prior written
115  * permission of MetaStuff, Ltd. DOM4J is a registered
116  * trademark of MetaStuff, Ltd.
117  *
118  * 5. Due credit should be given to the DOM4J Project
119  * (http://dom4j.org/).
120  *
121  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
122  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
123  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
124  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
125  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
126  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
127  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
128  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
129  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
130  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
131  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
132  * OF THE POSSIBILITY OF SUCH DAMAGE.
133  *
134  * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
135  *
136  * $Id: TestXMLResult.java,v 1.1 2003/07/07 10:30:29 per_nyfelt Exp $
137  */

138
Popular Tags