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: TestLineFeed.java,v 1.2 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import java.io.StringWriter;
13  import junit.framework.*;
14  import junit.textui.TestRunner;
15  import org.dom4j.Document;
16  import org.dom4j.DocumentHelper;
17  import org.dom4j.Element;
18  import org.dom4j.io.OutputFormat;
19  import org.dom4j.io.XMLWriter;
20  
21  public class TestLineFeed extends AbstractTestCase {
22      
23      private static final String ATT_TEXT = "Hello
There
<>&";
24      private static final String TEXT = "Hello\nThere\n<>&";
25      private static final String EXPECTED_TEXT = "Hello\nThere\n<>&";
26      private static final String EXPECTED_ATT_TEXT = "Hello There <>&";
27  
28      public TestLineFeed(String name) {
29          super(name);
30      }
31  
32      public static void main( String[] args ) {
33          TestRunner.run( suite() );
34      }
35      
36      public static Test suite() {
37          return new TestSuite( TestLineFeed.class );
38      }
39      
40      public void testElement() throws Exception {
41          Document doc = DocumentHelper.parseText("<elem>" + TEXT + "</elem>");
42          Element elem = doc.getRootElement();
43          assertEquals(EXPECTED_TEXT, elem.getText());
44      }
45  
46  
47      public void testAttribute() throws Exception {
48          Document doc = DocumentHelper.parseText("<elem attr=\"" + TEXT + "\"/>");
49          Element elem = doc.getRootElement();
50          //System.out.println(elem.attributeValue("attr"));
51          assertEquals(EXPECTED_ATT_TEXT, elem.attributeValue("attr"));
52          
53          doc = DocumentHelper.parseText("<elem attr=\"" + ATT_TEXT + "\"/>");
54          elem = doc.getRootElement();
55          //System.out.println(elem.attributeValue("attr"));
56          assertEquals(EXPECTED_TEXT, elem.attributeValue("attr"));
57      }
58  
59  
60      public void testCDATA() throws Exception {
61          Document doc = DocumentHelper.parseText("<elem><![CDATA[" + EXPECTED_TEXT + "]]></elem>");
62          Element elem = doc.getRootElement();
63          assertEquals(EXPECTED_TEXT, elem.getText());
64      }
65      
66      
67      public void testXmlWriter() throws Exception {
68          Element elem = DocumentHelper.createElement("elem");
69          Document doc = DocumentHelper.createDocument(elem);
70          elem.addCDATA(EXPECTED_TEXT);
71          StringWriter sw = new StringWriter();
72          XMLWriter xWriter = new XMLWriter(sw, OutputFormat.createPrettyPrint());
73          xWriter.write(doc);
74          xWriter.close();
75          String xmlString = sw.toString();
76          doc = DocumentHelper.parseText(xmlString);
77          elem = doc.getRootElement();
78          assertEquals(EXPECTED_TEXT, elem.getText());
79      }
80  }
81  
82  
83  
84  
85  /*
86   * Redistribution and use of this software and associated documentation
87   * ("Software"), with or without modification, are permitted provided
88   * that the following conditions are met:
89   *
90   * 1. Redistributions of source code must retain copyright
91   *    statements and notices.  Redistributions must also contain a
92   *    copy of this document.
93   *
94   * 2. Redistributions in binary form must reproduce the
95   *    above copyright notice, this list of conditions and the
96   *    following disclaimer in the documentation and/or other
97   *    materials provided with the distribution.
98   *
99   * 3. The name "DOM4J" must not be used to endorse or promote
100  *    products derived from this Software without prior written
101  *    permission of MetaStuff, Ltd.  For written permission,
102  *    please contact dom4j-info@metastuff.com.
103  *
104  * 4. Products derived from this Software may not be called "DOM4J"
105  *    nor may "DOM4J" appear in their names without prior written
106  *    permission of MetaStuff, Ltd. DOM4J is a registered
107  *    trademark of MetaStuff, Ltd.
108  *
109  * 5. Due credit should be given to the DOM4J Project - 
110  *    http://www.dom4j.org
111  *
112  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
113  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
114  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
115  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
116  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
117  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
118  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
119  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
120  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
121  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
122  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
123  * OF THE POSSIBILITY OF SUCH DAMAGE.
124  *
125  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
126  *
127  * $Id: TestLineFeed.java,v 1.2 2004/06/25 08:03:47 maartenc Exp $
128  */