1 16 17 package org.apache.commons.betwixt.strategy; 18 19 20 import java.io.StringWriter ; 21 22 import org.apache.commons.betwixt.AbstractTestCase; 23 import org.apache.commons.betwixt.ElementDescriptor; 24 import org.apache.commons.betwixt.XMLBeanInfo; 25 import org.apache.commons.betwixt.io.BeanWriter; 26 27 28 35 public class TestMixedContentEncoding extends AbstractTestCase { 36 37 38 static class TestBaseMixedContentEncoding extends BaseMixedContentEncodingStrategy { 39 boolean encode = false; 40 ElementDescriptor element = null; 41 42 TestBaseMixedContentEncoding(boolean encode) { 43 this.encode = encode; 44 } 45 46 protected boolean encodeAsCDATA(ElementDescriptor element) { 47 this.element = element; 48 return encode; 49 } 50 } 51 52 public TestMixedContentEncoding(String testName) { 53 super(testName); 54 } 55 56 public void testBaseMixedEscapeCharacters() { 57 BaseMixedContentEncodingStrategy mceStrategy = new TestBaseMixedContentEncoding(false); 58 assertEquals("Check basic escaping", "ab<>&ba", mceStrategy.escapeCharacters("ab<>&ba")); 59 } 60 61 public void testBaseMixedCDATAEncoding() { 62 BaseMixedContentEncodingStrategy mceStrategy = new TestBaseMixedContentEncoding(false); 63 assertEquals("Check basic escaping", "<![CDATA[<greeting>ab]]>ba</greeting>]]>", mceStrategy.encodeInCDATA("<greeting>ab]]>ba</greeting>")); 64 } 65 66 public void testBaseMixedEncode() { 67 ElementDescriptor descriptor = new ElementDescriptor(); 68 TestBaseMixedContentEncoding mceStrategy = new TestBaseMixedContentEncoding(false); 69 assertEquals( 70 "Using character escaping", 71 "<exclaim>hello, mum</exclaim>", 72 mceStrategy.encode("<exclaim>hello, mum</exclaim>", descriptor)); 73 74 assertEquals("Descriptor set", descriptor, mceStrategy.element); 75 mceStrategy = new TestBaseMixedContentEncoding(true); 76 assertEquals( 77 "Using CDATA encoding", 78 "<![CDATA[<exclaim>hello, mum</exclaim>]]>", 79 mceStrategy.encode("<exclaim>hello, mum</exclaim>", descriptor)); 80 81 assertEquals("Descriptor set", descriptor, mceStrategy.element); 82 } 83 84 public void testDefaultImplementation() { 85 ElementDescriptor descriptor = new ElementDescriptor(); 86 assertEquals( 87 "Default implementation uses character escaping", 88 "<proclaim>The King Is Dead, Long Live The King</proclaim>", 89 MixedContentEncodingStrategy.DEFAULT.encode("<proclaim>The King Is Dead, Long Live The King</proclaim>", descriptor)); 90 } 91 92 public void testEscapedCharactersImplementation() { 93 ElementDescriptor descriptor = new ElementDescriptor(); 94 assertEquals( 95 "Default implementation uses character escaping", 96 "<proclaim>The King Is Dead, Long Live The King</proclaim>", 97 MixedContentEncodingStrategy.ESCAPED_CHARACTERS.encode("<proclaim>The King Is Dead, Long Live The King</proclaim>", descriptor)); 98 } 99 100 public void testCDATAImplementation() { 101 ElementDescriptor descriptor = new ElementDescriptor(); 102 assertEquals( 103 "Default implementation uses character escaping", 104 "<![CDATA[<proclaim>The King Is Dead, Long Live The King</proclaim>]]>", 105 MixedContentEncodingStrategy.CDATA.encode("<proclaim>The King Is Dead, Long Live The King</proclaim>", descriptor)); 106 } 107 108 public void testDefaultOutput() throws Exception { 109 Element element = new Element(); 110 element.setValue("<greeting>What Ho Jeeves!</greeting>"); 111 112 StringWriter out = new StringWriter (); 113 out.write("<?xml version='1.0'?>"); 114 BeanWriter writer = new BeanWriter(out); 115 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); 116 writer.getBindingConfiguration().setMapIDs(false); 117 writer.write(element); 118 119 String expected = "<?xml version='1.0'?><Element>\n<value><greeting>What Ho Jeeves!</greeting></value>\n</Element>\n"; 120 String xml = out.getBuffer().toString(); 121 122 assertEquals(expected,xml); 123 124 } 125 126 127 public void testDefaultOutputWithCDATAOption() throws Exception { 128 Element element = new Element(); 129 element.setValue("<greeting>What Ho Jeeves!</greeting>"); 130 131 StringWriter out = new StringWriter (); 132 out.write("<?xml version='1.0'?>"); 133 BeanWriter writer = new BeanWriter(out); 134 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); 135 writer.getBindingConfiguration().setMapIDs(false); 136 XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class); 137 elementInfo.getElementDescriptor().getElementDescriptors()[0] 138 .getOptions().addOption(MixedContentEncodingStrategy.ENCODING_OPTION_NAME, "CDATA"); 139 140 writer.write(element); 141 142 String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n"; 143 String xml = out.getBuffer().toString(); 144 145 assertEquals(expected,xml); 146 147 } 148 149 150 public void testDefaultOutputWithCharacterEscapingOption() throws Exception { 151 Element element = new Element(); 152 element.setValue("<greeting>What Ho Jeeves!</greeting>"); 153 154 StringWriter out = new StringWriter (); 155 out.write("<?xml version='1.0'?>"); 156 BeanWriter writer = new BeanWriter(out); 157 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); 158 writer.getBindingConfiguration().setMapIDs(false); 159 XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class); 160 elementInfo.getElementDescriptor().getElementDescriptors()[0] 161 .getOptions().addOption("org.apache.commons.betwixt.mixed-content-encoding", "escaped"); 162 writer.write(element); 163 164 String expected = "<?xml version='1.0'?><Element>\n<value><greeting>What Ho Jeeves!</greeting></value>\n</Element>\n"; 165 String xml = out.getBuffer().toString(); 166 167 assertEquals(expected,xml); 168 } 169 170 public void testDefaultOutputWithDotBetwixtOptions() throws Exception { 171 ABCBean bean = new ABCBean(); 172 bean.setA("<strong>weak</strong>"); 173 bean.setB("<strong>weak</strong>"); 174 bean.setC("<strong>weak</strong>"); 175 176 StringWriter out = new StringWriter (); 177 out.write("<?xml version='1.0'?>"); 178 BeanWriter writer = new BeanWriter(out); 179 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); 180 writer.getBindingConfiguration().setMapIDs(false); 181 writer.write(bean); 182 183 String expected = "<?xml version='1.0'?>" + 184 "<greek-abc>\n" + 185 "<alpha><![CDATA[<strong>weak</strong>]]></alpha>\n" + 186 "<beta><strong>weak</strong></beta>\n" + 187 "<gamma><strong>weak</strong></gamma>\n" + 188 "</greek-abc>\n"; 189 String xml = out.getBuffer().toString(); 190 191 assertEquals(expected,xml); 192 } 193 194 public void testEscapedOutput() throws Exception { 195 Element element = new Element(); 196 element.setValue("<greeting>What Ho Jeeves!</greeting>"); 197 198 StringWriter out = new StringWriter (); 199 out.write("<?xml version='1.0'?>"); 200 BeanWriter writer = new BeanWriter(out); 201 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); 202 writer.getBindingConfiguration().setMapIDs(false); 203 writer.setMixedContentEncodingStrategy(new TestBaseMixedContentEncoding(false)); 204 writer.write(element); 205 206 String expected = "<?xml version='1.0'?><Element>\n<value><greeting>What Ho Jeeves!</greeting></value>\n</Element>\n"; 207 String xml = out.getBuffer().toString(); 208 209 assertEquals(expected,xml); 210 211 } 212 213 public void testCDATAEncodedOutput() throws Exception { 214 Element element = new Element(); 215 element.setValue("<greeting>What Ho Jeeves!</greeting>"); 216 217 StringWriter out = new StringWriter (); 218 out.write("<?xml version='1.0'?>"); 219 BeanWriter writer = new BeanWriter(out); 220 writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false); 221 writer.getBindingConfiguration().setMapIDs(false); 222 writer.setMixedContentEncodingStrategy(new TestBaseMixedContentEncoding(true)); 223 writer.write(element); 224 225 String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n"; 226 String xml = out.getBuffer().toString(); 227 228 assertEquals(expected,xml); 229 } 230 } 231 | Popular Tags |