KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > TestCmsXmlSaxWriter


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/util/TestCmsXmlSaxWriter.java,v $
3  * Date : $Date: 2006/03/27 14:52:42 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import org.opencms.i18n.CmsEncoder;
35 import org.opencms.test.OpenCmsTestCase;
36 import org.opencms.xml.CmsXmlException;
37 import org.opencms.xml.CmsXmlUtils;
38
39 import java.io.StringWriter JavaDoc;
40
41 import org.dom4j.Document;
42 import org.dom4j.DocumentHelper;
43 import org.dom4j.Element;
44 import org.dom4j.io.SAXWriter;
45
46 /**
47  * Test cases for the class <code>{@link org.opencms.util.CmsXmlSaxWriter}</code>.<p>
48  *
49  * @author Alexander Kandzior
50  *
51  * @version $Revision: 1.2 $
52  *
53  * @since 6.0.0
54  */

55 public class TestCmsXmlSaxWriter extends OpenCmsTestCase {
56
57     private static final String JavaDoc TEXT_1 = " This is a simple text ";
58     private static final String JavaDoc TEXT_2 = " This is a text with XML entities like <, > and & ";
59     private static final String JavaDoc TEXT_3 = " This is a text containing international chars like \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u20ac ";
60     private static final String JavaDoc TEXT_3_ESC = " This is a text containing international chars like &#228;&#246;&#252;&#196;&#214;&#220;&#223;&#8364; ";
61     private static final String JavaDoc TEXT_4 = " This is a text with \u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df\u20ac as well as <> and & ";
62     private static final String JavaDoc TEXT_4_ESC = " This is a text with &#228;&#246;&#252;&#196;&#214;&#220;&#223;&#8364; as well as &lt;&gt; and &amp; ";
63
64     /**
65      * Default JUnit constructor.<p>
66      *
67      * @param arg0 JUnit parameters
68      */

69     public TestCmsXmlSaxWriter(String JavaDoc arg0) {
70
71         super(arg0);
72     }
73
74     /**
75      * Test HTML escaping in XML using the <code>{@link org.opencms.util.CmsXmlSaxWriter}</code>.<p>
76      *
77      * @throws Exception if the test fails
78      */

79     public void testEntityExcapeInXml() throws Exception JavaDoc {
80
81         // generate the SAX XML writer
82
CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(new StringWriter JavaDoc(4096), "US-ASCII");
83         saxHandler.setEscapeXml(true);
84         saxHandler.setEscapeUnknownChars(true);
85         SAXWriter writer = new SAXWriter(saxHandler, saxHandler);
86
87         // the XML document to write the XMl to
88
Document doc = docTestGenerate();
89
90         writer.write(doc);
91
92         String JavaDoc result1 = saxHandler.getWriter().toString();
93         System.out.println(result1);
94
95         // ensure international chars have been replaced by entities
96
assertTrue(result1.indexOf(TEXT_3_ESC) >= 0);
97         assertTrue(result1.indexOf(TEXT_4_ESC) >= 0);
98
99         // now unmarshal the document from the String
100
Document doc1 = CmsXmlUtils.unmarshalHelper(result1, null);
101
102         // the XML entities must have been replaced with their String representations
103
docTestCheck(doc1);
104     }
105
106     /**
107      * Test if disabling escaping in <code>{@link org.opencms.util.CmsXmlSaxWriter}</code> causes expected issues.<p>
108      *
109      * @throws Exception if the test fails
110      */

111     public void testWithoutEntityEscaping() throws Exception JavaDoc {
112
113         // generate the SAX XML writer
114
CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(new StringWriter JavaDoc(4096), "US-ASCII");
115         saxHandler.setEscapeXml(false);
116         saxHandler.setEscapeUnknownChars(false);
117         SAXWriter writer = new SAXWriter(saxHandler, saxHandler);
118
119         // the XML document to write the XMl to
120
Document doc = docTestGenerate();
121         writer.write(doc);
122         String JavaDoc result1 = saxHandler.getWriter().toString();
123         System.out.println(result1);
124
125         // now unmarshal the document from the String
126
CmsXmlException error = null;
127         try {
128             // this must generate an error since the content was not escaped
129
CmsXmlUtils.unmarshalHelper(result1, null);
130         } catch (CmsXmlException e) {
131             error = e;
132         }
133         assertNotNull("Expected Exception was not thrown", error);
134         assertSame(org.opencms.xml.Messages.ERR_UNMARSHALLING_XML_DOC_0, error.getMessageContainer().getKey());
135     }
136
137     /**
138      * Test round-trip generation of an XML using the <code>{@link org.opencms.util.CmsXmlSaxWriter}</code>.<p>
139      *
140      * @throws Exception if the test fails
141      */

142     public void testXmlRoundtrip() throws Exception JavaDoc {
143
144         // generate the SAX XML writer
145
CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(new StringWriter JavaDoc(4096), CmsEncoder.ENCODING_ISO_8859_1);
146         saxHandler.setEscapeXml(true);
147         saxHandler.setEscapeUnknownChars(false);
148         SAXWriter writer = new SAXWriter(saxHandler, saxHandler);
149
150         // the XML document to write the XMl to
151
Document doc = docTestGenerate();
152
153         // write the document to a String using the SAX writer
154
writer.write(doc);
155
156         String JavaDoc result1 = saxHandler.getWriter().toString();
157         System.out.println(result1);
158         // generate document from String
159
Document doc1 = CmsXmlUtils.unmarshalHelper(result1, null);
160         // check doc1 values
161
docTestCheck(doc1);
162
163         // generate another document
164
CmsXmlSaxWriter saxHandler2 = new CmsXmlSaxWriter(new StringWriter JavaDoc(4096), CmsEncoder.ENCODING_ISO_8859_1);
165         saxHandler.setEscapeXml(true);
166         saxHandler.setEscapeUnknownChars(false);
167         SAXWriter writer2 = new SAXWriter(saxHandler2, saxHandler2);
168         // use the document generated from the String as input
169
writer2.write(doc1);
170         String JavaDoc result2 = saxHandler2.getWriter().toString();
171         System.out.println(result2);
172         Document doc2 = CmsXmlUtils.unmarshalHelper(result2, null);
173
174         // both docs must be equal, String and XML
175
assertEquals(result1, result2);
176         assertEquals(doc1, doc2);
177         // check content of doc2
178
docTestCheck(doc2);
179     }
180
181     /**
182      * Asserts all values in the given focument match.<p>
183      *
184      * @param doc the document to check
185      */

186     private void docTestCheck(Document doc) {
187
188         assertEquals(TEXT_1, doc.getRootElement().element("texts").element("sub1").getText());
189         assertEquals(TEXT_2, doc.getRootElement().element("texts").element("sub2").getText());
190         assertEquals(TEXT_3, doc.getRootElement().element("texts").element("sub3").getText());
191         assertEquals(TEXT_4, doc.getRootElement().element("texts").element("sub4").getText());
192         assertEquals(TEXT_1, doc.getRootElement().element("cdatas").element("sub1").getText());
193         assertEquals(TEXT_2, doc.getRootElement().element("cdatas").element("sub2").getText());
194         assertEquals(TEXT_3, doc.getRootElement().element("cdatas").element("sub3").getText());
195         assertEquals(TEXT_4, doc.getRootElement().element("cdatas").element("sub4").getText());
196     }
197
198     /**
199      * Returns a test XML document.<p>
200      *
201      * @return a test XML document
202      */

203     private Document docTestGenerate() {
204
205         Document doc = DocumentHelper.createDocument();
206
207         Element root = doc.addElement("testroot");
208         Element texts = root.addElement("texts");
209         texts.addElement("sub1").setText(TEXT_1);
210         texts.addElement("sub2").setText(TEXT_2);
211         texts.addElement("sub3").setText(TEXT_3);
212         texts.addElement("sub4").setText(TEXT_4);
213         Element cdatas = root.addElement("cdatas");
214         cdatas.addElement("sub1").addCDATA(TEXT_1);
215         cdatas.addElement("sub2").addCDATA(TEXT_2);
216         cdatas.addElement("sub3").addCDATA(TEXT_3);
217         cdatas.addElement("sub4").addCDATA(TEXT_4);
218
219         return doc;
220     }
221 }
Popular Tags