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