001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.strategy;
019:
020: import java.io.StringWriter;
021:
022: import org.apache.commons.betwixt.AbstractTestCase;
023: import org.apache.commons.betwixt.ElementDescriptor;
024: import org.apache.commons.betwixt.XMLBeanInfo;
025: import org.apache.commons.betwixt.io.BeanWriter;
026:
027: /**
028: * Tests for mixed content encoding.
029: * Mixed content encoding is the process by which body content
030: * is written out (in an escaped form) into a textual output stream.
031: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
032: * @version $Revision: 438373 $
033: */
034: public class TestMixedContentEncoding extends AbstractTestCase {
035:
036: /** Concrete subclass used for testing */
037: static class TestBaseMixedContentEncoding extends
038: BaseMixedContentEncodingStrategy {
039: boolean encode = false;
040: ElementDescriptor element = null;
041:
042: TestBaseMixedContentEncoding(boolean encode) {
043: this .encode = encode;
044: }
045:
046: protected boolean encodeAsCDATA(ElementDescriptor element) {
047: this .element = element;
048: return encode;
049: }
050: }
051:
052: public TestMixedContentEncoding(String testName) {
053: super (testName);
054: }
055:
056: public void testBaseMixedEscapeCharacters() {
057: BaseMixedContentEncodingStrategy mceStrategy = new TestBaseMixedContentEncoding(
058: false);
059: assertEquals("Check basic escaping", "ab<>&ba",
060: mceStrategy.escapeCharacters("ab<>&ba"));
061: }
062:
063: public void testBaseMixedCDATAEncoding() {
064: BaseMixedContentEncodingStrategy mceStrategy = new TestBaseMixedContentEncoding(
065: false);
066: assertEquals("Check basic escaping",
067: "<![CDATA[<greeting>ab]]>ba</greeting>]]>",
068: mceStrategy
069: .encodeInCDATA("<greeting>ab]]>ba</greeting>"));
070: }
071:
072: public void testBaseMixedEncode() {
073: ElementDescriptor descriptor = new ElementDescriptor();
074: TestBaseMixedContentEncoding mceStrategy = new TestBaseMixedContentEncoding(
075: false);
076: assertEquals("Using character escaping",
077: "<exclaim>hello, mum</exclaim>",
078: mceStrategy.encode("<exclaim>hello, mum</exclaim>",
079: descriptor));
080:
081: assertEquals("Descriptor set", descriptor, mceStrategy.element);
082: mceStrategy = new TestBaseMixedContentEncoding(true);
083: assertEquals("Using CDATA encoding",
084: "<![CDATA[<exclaim>hello, mum</exclaim>]]>",
085: mceStrategy.encode("<exclaim>hello, mum</exclaim>",
086: descriptor));
087:
088: assertEquals("Descriptor set", descriptor, mceStrategy.element);
089: }
090:
091: public void testDefaultImplementation() {
092: ElementDescriptor descriptor = new ElementDescriptor();
093: assertEquals(
094: "Default implementation uses character escaping",
095: "<proclaim>The King Is Dead, Long Live The King</proclaim>",
096: MixedContentEncodingStrategy.DEFAULT
097: .encode(
098: "<proclaim>The King Is Dead, Long Live The King</proclaim>",
099: descriptor));
100: }
101:
102: public void testEscapedCharactersImplementation() {
103: ElementDescriptor descriptor = new ElementDescriptor();
104: assertEquals(
105: "Default implementation uses character escaping",
106: "<proclaim>The King Is Dead, Long Live The King</proclaim>",
107: MixedContentEncodingStrategy.ESCAPED_CHARACTERS
108: .encode(
109: "<proclaim>The King Is Dead, Long Live The King</proclaim>",
110: descriptor));
111: }
112:
113: public void testCDATAImplementation() {
114: ElementDescriptor descriptor = new ElementDescriptor();
115: assertEquals(
116: "Default implementation uses character escaping",
117: "<![CDATA[<proclaim>The King Is Dead, Long Live The King</proclaim>]]>",
118: MixedContentEncodingStrategy.CDATA
119: .encode(
120: "<proclaim>The King Is Dead, Long Live The King</proclaim>",
121: descriptor));
122: }
123:
124: public void testDefaultOutput() throws Exception {
125: Element element = new Element();
126: element.setValue("<greeting>What Ho Jeeves!</greeting>");
127:
128: StringWriter out = new StringWriter();
129: out.write("<?xml version='1.0'?>");
130: BeanWriter writer = new BeanWriter(out);
131: writer.getXMLIntrospector().getConfiguration()
132: .setAttributesForPrimitives(false);
133: writer.getBindingConfiguration().setMapIDs(false);
134: writer.setEndOfLine("\n"); //force to \n so expected values match for sure
135: writer.write(element);
136:
137: String expected = "<?xml version='1.0'?><Element>\n<value><greeting>What Ho Jeeves!</greeting></value>\n</Element>\n";
138: String xml = out.getBuffer().toString();
139:
140: assertEquals(expected, xml);
141:
142: }
143:
144: /** Unit test for default output when CDATA option is set */
145: public void testDefaultOutputWithCDATAOption() throws Exception {
146: Element element = new Element();
147: element.setValue("<greeting>What Ho Jeeves!</greeting>");
148:
149: StringWriter out = new StringWriter();
150: out.write("<?xml version='1.0'?>");
151: BeanWriter writer = new BeanWriter(out);
152: writer.getXMLIntrospector().getConfiguration()
153: .setAttributesForPrimitives(false);
154: writer.getBindingConfiguration().setMapIDs(false);
155: XMLBeanInfo elementInfo = writer.getXMLIntrospector()
156: .introspect(Element.class);
157: elementInfo.getElementDescriptor().getElementDescriptors()[0]
158: .getOptions()
159: .addOption(
160: MixedContentEncodingStrategy.ENCODING_OPTION_NAME,
161: "CDATA");
162:
163: writer.setEndOfLine("\n"); //force to \n so expected values match for sure
164: writer.write(element);
165:
166: String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n";
167: String xml = out.getBuffer().toString();
168:
169: assertEquals(expected, xml);
170:
171: }
172:
173: /** Unit test for default output when character escaping option is set */
174: public void testDefaultOutputWithCharacterEscapingOption()
175: throws Exception {
176: Element element = new Element();
177: element.setValue("<greeting>What Ho Jeeves!</greeting>");
178:
179: StringWriter out = new StringWriter();
180: out.write("<?xml version='1.0'?>");
181: BeanWriter writer = new BeanWriter(out);
182: writer.getXMLIntrospector().getConfiguration()
183: .setAttributesForPrimitives(false);
184: writer.getBindingConfiguration().setMapIDs(false);
185: XMLBeanInfo elementInfo = writer.getXMLIntrospector()
186: .introspect(Element.class);
187: elementInfo.getElementDescriptor().getElementDescriptors()[0]
188: .getOptions()
189: .addOption(
190: "org.apache.commons.betwixt.mixed-content-encoding",
191: "escaped");
192: writer.setEndOfLine("\n"); //force to \n so expected values match for sure
193: writer.write(element);
194:
195: String expected = "<?xml version='1.0'?><Element>\n<value><greeting>What Ho Jeeves!</greeting></value>\n</Element>\n";
196: String xml = out.getBuffer().toString();
197:
198: assertEquals(expected, xml);
199: }
200:
201: public void testDefaultOutputWithDotBetwixtOptions()
202: throws Exception {
203: ABCBean bean = new ABCBean();
204: bean.setA("<strong>weak</strong>");
205: bean.setB("<strong>weak</strong>");
206: bean.setC("<strong>weak</strong>");
207:
208: StringWriter out = new StringWriter();
209: out.write("<?xml version='1.0'?>");
210: BeanWriter writer = new BeanWriter(out);
211: writer.getXMLIntrospector().getConfiguration()
212: .setAttributesForPrimitives(false);
213: writer.getBindingConfiguration().setMapIDs(false);
214: writer.setEndOfLine("\n"); //force to \n so expected values match for sure
215: writer.write(bean);
216:
217: String expected = "<?xml version='1.0'?>" + "<greek-abc>\n"
218: + "<alpha><![CDATA[<strong>weak</strong>]]></alpha>\n"
219: + "<beta><strong>weak</strong></beta>\n"
220: + "<gamma><strong>weak</strong></gamma>\n"
221: + "</greek-abc>\n";
222: String xml = out.getBuffer().toString();
223:
224: assertEquals(expected, xml);
225: }
226:
227: public void testEscapedOutput() throws Exception {
228: Element element = new Element();
229: element.setValue("<greeting>What Ho Jeeves!</greeting>");
230:
231: StringWriter out = new StringWriter();
232: out.write("<?xml version='1.0'?>");
233: BeanWriter writer = new BeanWriter(out);
234: writer.getXMLIntrospector().getConfiguration()
235: .setAttributesForPrimitives(false);
236: writer.getBindingConfiguration().setMapIDs(false);
237: writer
238: .setMixedContentEncodingStrategy(new TestBaseMixedContentEncoding(
239: false));
240: writer.setEndOfLine("\n"); //force to \n so expected values match for sure
241: writer.write(element);
242:
243: String expected = "<?xml version='1.0'?><Element>\n<value><greeting>What Ho Jeeves!</greeting></value>\n</Element>\n";
244: String xml = out.getBuffer().toString();
245:
246: assertEquals(expected, xml);
247:
248: }
249:
250: public void testCDATAEncodedOutput() throws Exception {
251: Element element = new Element();
252: element.setValue("<greeting>What Ho Jeeves!</greeting>");
253:
254: StringWriter out = new StringWriter();
255: out.write("<?xml version='1.0'?>");
256: BeanWriter writer = new BeanWriter(out);
257: writer.getXMLIntrospector().getConfiguration()
258: .setAttributesForPrimitives(false);
259: writer.getBindingConfiguration().setMapIDs(false);
260: writer
261: .setMixedContentEncodingStrategy(new TestBaseMixedContentEncoding(
262: true));
263: writer.setEndOfLine("\n"); //force to \n so expected values match for sure
264: writer.write(element);
265:
266: String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n";
267: String xml = out.getBuffer().toString();
268:
269: assertEquals(expected, xml);
270: }
271: }
|