01: /*
02: * Copyright 2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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
48: .setScript("file:src/test/org/apache/commons/jelly/test/xml/testCData.jelly");
49: Script script = jelly.compileScript();
50: JellyContext context = new JellyContext();
51: script.run(context, XMLOutput.createDummyXMLOutput());
52:
53: String output = (String) context.getVariable("foo");
54: assertTrue("'foo' is not null", output != null);
55:
56: String golden = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
57: golden += "<!DOCTYPE foo [\n";
58: golden += " <!ELEMENT foo (#PCDATA)>\n";
59: golden += "]><foo></foo>";
60:
61: assertEquals("output should contain the CDATA section", golden,
62: output);
63: }
64:
65: public void testDom4JCData() throws SAXException {
66: StringWriter writer = new StringWriter();
67: OutputFormat format = new OutputFormat();
68: final XMLWriter xmlWriter = new XMLWriter(writer, format);
69: xmlWriter.setEscapeText(false);
70:
71: XMLOutput output = new XMLOutput(xmlWriter, xmlWriter);
72:
73: String decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
74: String golden = "<!DOCTYPE foo [\n";
75: golden += " <!ELEMENT foo (#PCDATA)>\n";
76: golden += "]><foo></foo>";
77:
78: output.startDocument();
79: output.write(golden);
80: output.endDocument();
81: System.err.println("output was: '" + writer.toString() + "'");
82: System.err.println("golden is : '" + golden + "'");
83: assertEquals("output should contain the CDATA section", decl
84: + golden, writer.toString());
85: }
86:
87: }
|