WriteUnmergedTextTest xref

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