    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.IOException;
    13
    14  /***
    15   * A test harness to test the xml:space attribute for preserve. If it is
    16   * preserve, then keep whitespace.
    17   *
    18   * @author <a href="mailto:ddlucas@lse.com">David Lucas </a>
    19   * @version $Revision: 1.4 $
    20   */
    21  public class XMLSpaceAttributeTest extends AbstractTestCase {
    22      public static void main(String[] args) {
    23          TestRunner.run(XMLSpaceAttributeTest.class);
    24      }
    25
    26      // Test case(s)
    27      // -------------------------------------------------------------------------
    28      public void testWithTextTrimOn() throws Exception {
    29          String xmlString = "<top >"
    30                  + "<row><col>   This is a test!</col></row>"
    31                  + "<row><col xml:space=\'preserve\' >   This is a test!</col>"
    32                  + "</row><row><col>   This is a test!</col></row>" + "</top>";
    33          Document doc1 = DocumentHelper.parseText(xmlString);
    34          Element c2 = (Element) doc1.selectSingleNode("/top/row[2]/col");
    35          String expected = "   New Text TrimOn! ";
    36          c2.setText(expected);
    37
    38          String xml = rewriteToXmlString(doc1, true);
    39
    40          Document doc2 = DocumentHelper.parseText(xml);
    41          Element c4 = (Element) doc2.selectSingleNode("/top/row[2]/col");
    42          String actual = c4.getText();
    43
    44          assertEquals("compared element text expecting whitespace", expected,
    45                  actual);
    46
    47          expected = expected.trim();
    48          actual = c4.getTextTrim();
    49          assertEquals("compared element getTextTrim", expected, actual);
    50
    51          expected = "This is a test!";
    52
    53          Element c5 = (Element) doc2.selectSingleNode("/top/row[3]/col");
    54          actual = c5.getText();
    55          assertEquals("compared element text expecting trimmed whitespace",
    56                  expected, actual);
    57      }
    58
    59      // -------------------------------------------------------------------------
    60      public void testWithTextTrimOff() throws Exception {
    61          String xmlString = "<top >"
    62                  + "<row><col>   This is a test!</col></row>"
    63                  + "<row><col xml:space=\'preserve\' >   This is a test!</col>"
    64                  + "</row><row><col>   This is a test!</col></row>" + "</top>";
    65          Document doc1 = DocumentHelper.parseText(xmlString);
    66          Element c2 = (Element) doc1.selectSingleNode("/top/row[2]/col");
    67          String expected = "   New Text TrimOff! ";
    68          c2.setText(expected);
    69
    70          String xml = rewriteToXmlString(doc1, false);
    71
    72          Document doc2 = DocumentHelper.parseText(xml);
    73          Element c4 = (Element) doc2.selectSingleNode("/top/row[2]/col");
    74          String actual = c4.getText();
    75
    76          assertEquals("compared element text expecting whitespace", expected,
    77                  actual);
    78      }
    79
    80      // -------------------------------------------------------------------------
    81      public void testWithTextTrimOnFollow() throws Exception {
    82          String xmlString = "<top >"
    83                  + "<row><col>   This is a test!</col></row>" + "<row>"
    84                  + "<col xml:space=\'preserve\' >"
    85                  + "<a><b>   This is embedded!</b></a>"
    86                  + "<a><b>   This is space=preserve too!</b></a>" + "</col>"
    87                  + "</row>" + "<row><col>   This is a test!</col></row>"
    88                  + "</top>";
    89          Document doc1 = DocumentHelper.parseText(xmlString);
    90          Element c2 = (Element) doc1.selectSingleNode("/top/row[2]/col/a[1]/b");
    91          String expected = "   New Text TrimOnFollow! ";
    92          c2.setText(expected);
    93
    94          String xml = rewriteToXmlString(doc1, true);
    95
    96          Document doc2 = DocumentHelper.parseText(xml);
    97
    98          Element c4 = (Element) doc2.selectSingleNode("/top/row[2]/col/a[1]/b");
    99          String actual = c4.getText();
    100
    101         assertEquals("compared element text expecting whitespace", expected,
    102                 actual);
    103
    104         Element c8 = (Element) doc2.selectSingleNode("/top/row[2]/col/a[2]/b");
    105
    106         expected = "   This is space=preserve too!";
    107         actual = c8.getText();
    108         assertEquals("compared element text follow trimmed whitespace",
    109                 expected, actual);
    110
    111         expected = expected.trim();
    112         actual = c8.getTextTrim();
    113         assertEquals("compared element getTextTrim", expected, actual);
    114
    115         Element c12 = (Element) doc2.selectSingleNode("/top/row[3]/col");
    116
    117         expected = "This is a test!";
    118         actual = c12.getText();
    119         assertEquals("compared element text follow trimmed whitespace",
    120                 expected, actual);
    121     }
    122
    123     // -------------------------------------------------------------------------
    124     public void testWithTextTrimOnNested() throws Exception {
    125         String xmlString = "<top >"
    126                 + "<row><col>   This is a test!</col></row>" + "<row>"
    127                 + "<col xml:space='preserve' >" + "<a>"
    128                 + "<b>   This is embedded! </b>"
    129                 + "<b xml:space='default' >   This should do global default! "
    130                 + "</b><b>   This is embedded! </b>" + "</a>" + "</col>"
    131                 + "</row>" + "<row><col>   This is a test!</col></row>"
    132                 + "</top>";
    133         Document doc1 = DocumentHelper.parseText(xmlString);
    134         Element c2 = (Element) doc1.selectSingleNode("/top/row[2]/col/a[1]/b");
    135         String expected = "   New Text TrimOnNested! ";
    136         c2.setText(expected);
    137
    138         String xml = rewriteToXmlString(doc1, true);
    139
    140         Document doc2 = DocumentHelper.parseText(xml);
    141
    142         Element c4 = (Element) doc2
    143                 .selectSingleNode("/top/row[2]/col/a[1]/b[1]");
    144         String actual = c4.getText();
    145         assertEquals("compared element text expecting whitespace", expected,
    146                 actual);
    147
    148         Element c8 = (Element) doc2
    149                 .selectSingleNode("/top/row[2]/col/a[1]/b[2]");
    150         expected = "This should do global default!";
    151         actual = c8.getText();
    152         assertEquals("compared element text nested trimmed whitespace",
    153                 expected, actual);
    154
    155         Element c12 = (Element) doc2
    156                 .selectSingleNode("/top/row[2]/col/a[1]/b[3]");
    157         expected = "   This is embedded! ";
    158         actual = c12.getText();
    159         assertEquals("compared element text nested preserved whitespace",
    160                 expected, actual);
    161     }
    162
    163     // Implementation methods
    164     // -------------------------------------------------------------------------
    165     private String rewriteToXmlString(Document doc, boolean trimOn)
    166             throws IOException {
    167         org.dom4j.io.OutputFormat of = org.dom4j.io.OutputFormat
    168                 .createCompactFormat();
    169         of.setIndent(true);
    170         of.setNewlines(true);
    171         of.setExpandEmptyElements(false);
    172         of.setSuppressDeclaration(false);
    173         of.setOmitEncoding(false);
    174         of.setEncoding("UTF-8");
    175         of.setTrimText(trimOn);
    176
    177         java.io.ByteArrayOutputStream os = new java.io.ByteArrayOutputStream();
    178         java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(os);
    179         org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(of);
    180
    181         xmlWriter.setOutputStream(bos);
    182         xmlWriter.write(doc);
    183         xmlWriter.close();
    184
    185         String xml = os.toString();
    186
    187         // System.out.println("***** xml out *****\n"+xml);
    188         return xml;
    189     }
    190
    191     // -------------------------------------------------------------------------
    192     public void testWithEscapeTextTrimOn() throws Exception {
    193         String xmlString = "<top >"
    194                 + "<row><col>   This is a test!</col></row>"
    195                 + "<row><col xml:space=\'preserve\' >   This is a test!\r\n"
    196                 + "With a new line, special character like &amp; , and\t tab."
    197                 + "</col></row><row><col>   This is a test!\r\nWith a new line,"
    198                 + " special character like &amp; , and\t tab.</col></row>"
    199                 + "</top>";
    200         Document doc1 = DocumentHelper.parseText(xmlString);
    201         String xml = rewriteToXmlString(doc1, true);
    202         Document doc2 = DocumentHelper.parseText(xml);
    203
    204         Element c2 = (Element) doc2.selectSingleNode("/top/row[2]/col");
    205         String expected = "   This is a test!\nWith a new line, special "
    206                 + "character like & , and\t tab.";
    207         String actual = c2.getText();
    208         assertEquals("compared element text expecting whitespace", expected,
    209                 actual);
    210
    211         Element c4 = (Element) doc2.selectSingleNode("/top/row[3]/col");
    212         expected = "This is a test! With a new line, special character "
    213                 + "like & , and tab.";
    214         actual = c4.getText();
    215         assertEquals("compared element text expecting whitespace", expected,
    216                 actual);
    217     }
    218 }
    219
    220 /*
    221  * Redistribution and use of this software and associated documentation
    222  * ("Software"), with or without modification, are permitted provided that the
    223  * following conditions are met:
    224  *
    225  * 1. Redistributions of source code must retain copyright statements and
    226  * notices. Redistributions must also contain a copy of this document.
    227  *
    228  * 2. Redistributions in binary form must reproduce the above copyright notice,
    229  * this list of conditions and the following disclaimer in the documentation
    230  * and/or other materials provided with the distribution.
    231  *
    232  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    233  * from this Software without prior written permission of MetaStuff, Ltd. For
    234  * written permission, please contact dom4j-info@metastuff.com.
    235  *
    236  * 4. Products derived from this Software may not be called "DOM4J" nor may
    237  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    238  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    239  *
    240  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    241  *
    242  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    243  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    244  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    245  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    246  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    247  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    248  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    249  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    250  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    251  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    252  * POSSIBILITY OF SUCH DAMAGE.
    253  *
    254  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    255  */