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: TestHTMLWriter.java,v 1.10 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import java.io.StringWriter;
13  import java.net.URL;
14  
15  import junit.framework.Test;
16  import junit.framework.TestSuite;
17  import junit.textui.TestRunner;
18  
19  import org.dom4j.io.HTMLWriter;
20  import org.dom4j.io.OutputFormat;
21  import org.dom4j.io.SAXReader;
22  
23  /*** Test harness for the HTMLWriter
24    *
25    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
26    * @version $Revision: 1.10 $
27    */
28  public class TestHTMLWriter extends AbstractTestCase {
29  
30      protected static final boolean VERBOSE = false;
31      
32      
33      public static void main( String[] args ) {
34          TestRunner.run( suite() );
35      }
36      
37      public static Test suite() {
38          return new TestSuite( TestHTMLWriter.class );
39      }
40      
41      public TestHTMLWriter(String name) {
42          super(name);
43      }
44  
45      // Test case(s)
46      //-------------------------------------------------------------------------                    
47      public void testWriter() throws Exception {
48          Document document = DocumentHelper.parseText( 
49              "<html> <body><![CDATA[First&nbsp;test]]></body> </html>"
50          );
51          StringWriter buffer = new StringWriter();
52          HTMLWriter writer = new HTMLWriter( buffer );
53          writer.write( document );
54          
55          String output = buffer.toString();
56          
57          String expects = "\n<html>\n  <body>First&nbsp;test</body>\n</html>\n";
58          
59          System.out.println("expects: " + expects);
60          System.out.println("output: " + output);
61          
62          assertEquals( "Output is correct", expects, output );
63      }
64      
65      public void testBug923882() throws Exception {
66          Document doc = DocumentFactory.getInstance().createDocument();
67          Element root = doc.addElement("root");
68          root.addText("this is ");
69          root.addText(" sim");
70          root.addText("ple text ");
71          root.addElement("child");
72          root.addText(" contai");
73          root.addText("ning spaces and");
74          root.addText(" multiple textnodes");
75          OutputFormat format = new OutputFormat();
76          format.setEncoding("UTF-8");
77          format.setIndentSize(4);
78          format.setNewlines(true);
79          format.setTrimText(true);
80          format.setExpandEmptyElements(true);
81          StringWriter buffer = new StringWriter();
82          HTMLWriter writer = new HTMLWriter(buffer, format);
83          writer.write( doc );
84          String xml = buffer.toString();
85          log( xml );
86          int start = xml.indexOf("<root"),
87                  end = xml.indexOf("/root>")+6;
88          String eol = "\n"; //System.getProperty("line.separator");
89          String expected = 
90                  "<root>this is simple text" + eol +
91                  "    <child></child>containing spaces and multiple textnodes" + eol +
92                  "</root>";
93          System.out.println("Expected:"); System.out.println(expected);
94          System.out.println("Obtained:"); System.out.println(xml.substring(start, end));
95          assertEquals(expected, xml.substring(start, end));
96      }
97      
98      public void testBug923882asWriter() throws Exception {
99          // use an the HTMLWriter sax-methods.
100         //
101         StringWriter buffer = new StringWriter();
102         HTMLWriter writer = new HTMLWriter(buffer, OutputFormat.createPrettyPrint());
103         writer.characters("wor".toCharArray(), 0, 3);
104         writer.characters("d-being-cut".toCharArray(), 0, 11);
105                 
106         String expected = "word-being-cut";
107         assertEquals(expected, buffer.toString());
108         
109         buffer = new StringWriter();
110         writer = new HTMLWriter(buffer, OutputFormat.createPrettyPrint());
111         writer.characters("    wor".toCharArray(), 0, 7);
112         writer.characters("d being    ".toCharArray(), 0, 11);
113         writer.characters("  cut".toCharArray(), 0, 5);
114                 
115         expected = "word being cut";
116         assertEquals(expected, buffer.toString());
117     }
118     
119     public void testBug923882asWriterWithEmptyCharArray() throws Exception {
120         // use an the HTMLWriter sax-methods.
121         //
122         StringWriter buffer = new StringWriter();
123         HTMLWriter writer = new HTMLWriter(buffer, OutputFormat.createPrettyPrint());
124         writer.characters("wor".toCharArray(), 0, 3);
125         writer.characters(new char[0], 0, 0);
126         writer.characters("d-being-cut".toCharArray(), 0, 11);
127                 
128         String expected = "word-being-cut";
129         assertEquals(expected, buffer.toString());
130     }
131     
132     public void testBug619415() throws Exception {
133         URL url = getClass().getResource("/xml/test/dosLineFeeds.xml");
134         SAXReader reader = new SAXReader();
135         Document doc = reader.read(url);
136         
137         StringWriter wr = new StringWriter();
138         HTMLWriter writer = new HTMLWriter(wr, new OutputFormat("", false));
139         writer.write(doc);
140         
141         String result = wr.toString();
142         System.out.println(result);
143         
144         assertTrue(result.indexOf("Mary had a little lamb.") > -1);
145         assertTrue(result.indexOf("Hello, this is a test.") > -1);
146     }
147     
148 }
149 
150 
151 
152 
153 /*
154  * Redistribution and use of this software and associated documentation
155  * ("Software"), with or without modification, are permitted provided
156  * that the following conditions are met:
157  *
158  * 1. Redistributions of source code must retain copyright
159  *    statements and notices.  Redistributions must also contain a
160  *    copy of this document.
161  *
162  * 2. Redistributions in binary form must reproduce the
163  *    above copyright notice, this list of conditions and the
164  *    following disclaimer in the documentation and/or other
165  *    materials provided with the distribution.
166  *
167  * 3. The name "DOM4J" must not be used to endorse or promote
168  *    products derived from this Software without prior written
169  *    permission of MetaStuff, Ltd.  For written permission,
170  *    please contact dom4j-info@metastuff.com.
171  *
172  * 4. Products derived from this Software may not be called "DOM4J"
173  *    nor may "DOM4J" appear in their names without prior written
174  *    permission of MetaStuff, Ltd. DOM4J is a registered
175  *    trademark of MetaStuff, Ltd.
176  *
177  * 5. Due credit should be given to the DOM4J Project - 
178  *    http://www.dom4j.org
179  *
180  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
181  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
182  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
183  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
184  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
185  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
186  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
187  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
188  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
189  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
190  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
191  * OF THE POSSIBILITY OF SUCH DAMAGE.
192  *
193  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
194  *
195  * $Id: TestHTMLWriter.java,v 1.10 2004/06/25 08:03:47 maartenc Exp $
196  */