1 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 34 public class TestCData extends TestCase { 35 36 public TestCData(String arg) { 37 super(arg); 38 } 39 40 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 } 86 | Popular Tags |