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: TestWriteUnmergedText.java,v 1.2 2004/06/25 08:03:50 maartenc Exp $
8    */
9   
10  package org.dom4j.io;
11  
12  import java.io.ByteArrayInputStream;
13  import java.io.ByteArrayOutputStream;
14  
15  import java.io.StringWriter;
16  import java.util.Iterator;
17  import java.util.List;
18  
19  import junit.framework.*;
20  import junit.textui.TestRunner;
21  
22  import org.dom4j.*;
23  
24  import java.io.StringWriter;
25  import java.io.StringReader;
26  
27  /*** A simple test harness to check that the XML Writer works
28    *
29    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
30    * @version $Revision: 1.2 $
31    */
32  public class TestWriteUnmergedText extends AbstractTestCase {
33  
34  
35      private String inputText = "<?xml version = \"1.0\"?><TestEscapedEntities><TEXT>Test using &lt; &amp; &gt;</TEXT></TestEscapedEntities>";
36      protected static final boolean VERBOSE = true;
37      
38      
39      public static void main( String[] args ) {
40          TestRunner.run( suite() );
41      }
42      
43      public static Test suite() {
44          return new TestSuite( TestWriteUnmergedText.class );
45      }
46      
47      public TestWriteUnmergedText(String name) {
48          super(name);
49      }
50  
51      // Test case(s)
52      //-------------------------------------------------------------------------                    
53      public String readwriteText(OutputFormat outFormat, boolean mergeAdjacentText) throws Exception {
54          
55          StringWriter out = new StringWriter();
56          StringReader in = new StringReader(inputText);
57          SAXReader reader = new SAXReader();
58   
59          //reader.setValidation(true);
60          reader.setMergeAdjacentText(mergeAdjacentText);
61  
62          Document document = reader.read(in);
63  
64          XMLWriter writer = outFormat == null ? new XMLWriter(out) : new XMLWriter(out, outFormat);
65          writer.write(document);
66          writer.close(); 
67          
68          String outText = out.toString();
69          return outText;
70      }        
71      
72      
73      public void testWithoutFormatNonMerged() throws Exception {
74  
75          String outText = readwriteText(null, false);
76              
77          if ( VERBOSE ) {
78              log( "Text output is ["  );
79              log( outText );
80              log( "]. Done" );
81          }
82          
83          // should contain &amp; and &lt;
84          assertTrue("Output text contains \"&amp;\"", outText.lastIndexOf("&amp;") >= 0);
85          assertTrue("Output text contains \"&lt;\"", outText.lastIndexOf("&lt;") >= 0);
86      } 
87             
88      public void testWithCompactFormatNonMerged() throws Exception {
89  
90          String outText = readwriteText(OutputFormat.createCompactFormat(), false);
91              
92          if ( VERBOSE ) {
93              log( "Text output is ["  );
94              log( outText );
95              log( "]. Done" );
96          }
97          
98          // should contain &amp; and &lt;
99          assertTrue("Output text contains \"&amp;\"", outText.lastIndexOf("&amp;") >= 0);
100         assertTrue("Output text contains \"&lt;\"", outText.lastIndexOf("&lt;") >= 0);
101     }
102             
103     public void testWithPrettyPrintFormatNonMerged() throws Exception {
104 
105         String outText = readwriteText(OutputFormat.createPrettyPrint(), false);
106             
107         if ( VERBOSE ) {
108             log( "Text output is ["  );
109             log( outText );
110             log( "]. Done" );
111         }
112         
113         // should contain &amp; and &lt;
114         assertTrue("Output text contains \"&amp;\"", outText.lastIndexOf("&amp;") >= 0);
115         assertTrue("Output text contains \"&lt;\"", outText.lastIndexOf("&lt;") >= 0);
116     }        
117    
118     public void testWithoutFormatMerged() throws Exception {
119 
120         String outText = readwriteText(null, true);
121             
122         if ( VERBOSE ) {
123             log( "Text output is ["  );
124             log( outText );
125             log( "]. Done" );
126         }
127         
128         // should contain &amp; and &lt;
129         assertTrue("Output text contains \"&amp;\"", outText.lastIndexOf("&amp;") >= 0);
130         assertTrue("Output text contains \"&lt;\"", outText.lastIndexOf("&lt;") >= 0);
131     } 
132            
133     public void testWithCompactFormatMerged() throws Exception {
134 
135         String outText = readwriteText(OutputFormat.createCompactFormat(), true);
136             
137         if ( VERBOSE ) {
138             log( "Text output is ["  );
139             log( outText );
140             log( "]. Done" );
141         }
142         
143         // should contain &amp; and &lt;
144         assertTrue("Output text contains \"&amp;\"", outText.lastIndexOf("&amp;") >= 0);
145         assertTrue("Output text contains \"&lt;\"", outText.lastIndexOf("&lt;") >= 0);
146     }
147             
148     public void testWithPrettyPrintFormatMerged() throws Exception {
149 
150         String outText = readwriteText(OutputFormat.createPrettyPrint(), true);
151             
152         if ( VERBOSE ) {
153             log( "Text output is ["  );
154             log( outText );
155             log( "]. Done" );
156         }
157         
158         // should contain &amp; and &lt;
159         assertTrue("Output text contains \"&amp;\"", outText.lastIndexOf("&amp;") >= 0);
160         assertTrue("Output text contains \"&lt;\"", outText.lastIndexOf("&lt;") >= 0);
161     }        
162 }
163 
164 
165 
166 
167 /*
168  * Redistribution and use of this software and associated documentation
169  * ("Software"), with or without modification, are permitted provided
170  * that the following conditions are met:
171  *
172  * 1. Redistributions of source code must retain copyright
173  *    statements and notices.  Redistributions must also contain a
174  *    copy of this document.
175  *
176  * 2. Redistributions in binary form must reproduce the
177  *    above copyright notice, this list of conditions and the
178  *    following disclaimer in the documentation and/or other
179  *    materials provided with the distribution.
180  *
181  * 3. The name "DOM4J" must not be used to endorse or promote
182  *    products derived from this Software without prior written
183  *    permission of MetaStuff, Ltd.  For written permission,
184  *    please contact dom4j-info@metastuff.com.
185  *
186  * 4. Products derived from this Software may not be called "DOM4J"
187  *    nor may "DOM4J" appear in their names without prior written
188  *    permission of MetaStuff, Ltd. DOM4J is a registered
189  *    trademark of MetaStuff, Ltd.
190  *
191  * 5. Due credit should be given to the DOM4J Project - 
192  *    http://www.dom4j.org
193  *
194  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
195  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
196  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
197  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
198  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
199  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
200  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
201  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
202  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
203  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
204  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
205  * OF THE POSSIBILITY OF SUCH DAMAGE.
206  *
207  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
208  *
209  * $Id: TestWriteUnmergedText.java,v 1.2 2004/06/25 08:03:50 maartenc Exp $
210  */