1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.test.xml;
17  
18  import java.io.StringWriter;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.commons.jelly.Jelly;
23  import org.apache.commons.jelly.JellyContext;
24  import org.apache.commons.jelly.Script;
25  import org.apache.commons.jelly.XMLOutput;
26  import org.dom4j.io.OutputFormat;
27  import org.dom4j.io.XMLWriter;
28  import org.xml.sax.SAXException;
29  
30  /***
31   * @author mdelagrange
32   *
33   */
34  public class TestCData extends TestCase {
35  
36      public TestCData(String arg) {
37          super(arg);
38      }
39  
40      /***
41       * CDATA sections should be retained in the output.
42       *
43       * @throws Exception
44       */
45      public void testCData() throws Exception {
46          Jelly jelly = new Jelly();
47          jelly.setScript("file:src/test/org/apache/commons/jelly/test/xml/testCData.jelly");
48          Script script = jelly.compileScript();
49          JellyContext context = new JellyContext();
50          script.run(context, XMLOutput.createDummyXMLOutput());
51  
52          String output = (String) context.getVariable("foo");
53          assertTrue("'foo' is not null", output != null);
54  
55          String golden = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
56          golden += "<!DOCTYPE foo [\n";
57          golden += "  <!ELEMENT foo (#PCDATA)>\n";
58          golden += "]><foo></foo>";
59  
60          assertEquals("output should contain the CDATA section", golden, output);
61      }
62  
63      public void testDom4JCData() throws SAXException {
64          StringWriter writer = new StringWriter();
65          OutputFormat format = new OutputFormat();
66          final XMLWriter xmlWriter = new XMLWriter(writer, format);
67          xmlWriter.setEscapeText(false);
68  
69          XMLOutput output = new XMLOutput(xmlWriter, xmlWriter);
70  
71          String decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
72          String golden = "<!DOCTYPE foo [\n";
73          golden += "  <!ELEMENT foo (#PCDATA)>\n";
74          golden += "]><foo></foo>";
75  
76          output.startDocument();
77          output.write(golden);
78          output.endDocument();
79          System.err.println("output was: '" + writer.toString() +"'");
80          System.err.println("golden is : '" + golden +"'");
81          assertEquals("output should contain the CDATA section",
82                  decl + golden, writer.toString());
83      }
84  
85  }