KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > strategy > TestMixedContentEncoding


1 /*
2  * Copyright 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  
17 package org.apache.commons.betwixt.strategy;
18
19
20 import java.io.StringWriter JavaDoc;
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 /**
29  * Tests for mixed content encoding.
30  * Mixed content encoding is the process by which body content
31  * is written out (in an escaped form) into a textual output stream.
32  * @author <a HREF='http://jakarta.apache.org/'>Jakarta Commons Team</a>
33  * @version $Revision: 1.4 $
34  */

35 public class TestMixedContentEncoding extends AbstractTestCase {
36
37     /** Concrete subclass used for testing */
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 JavaDoc testName) {
53         super(testName);
54     }
55     
56     public void testBaseMixedEscapeCharacters() {
57         BaseMixedContentEncodingStrategy mceStrategy = new TestBaseMixedContentEncoding(false);
58         assertEquals("Check basic escaping", "ab&lt;&gt;&amp;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]]&gt;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                         "&lt;exclaim&gt;hello, mum&lt;/exclaim&gt;",
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             "&lt;proclaim&gt;The King Is Dead, Long Live The King&lt;/proclaim&gt;",
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             "&lt;proclaim&gt;The King Is Dead, Long Live The King&lt;/proclaim&gt;",
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 JavaDoc {
109         Element element = new Element();
110         element.setValue("<greeting>What Ho Jeeves!</greeting>");
111         
112         StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
120         String JavaDoc xml = out.getBuffer().toString();
121          
122         assertEquals(expected,xml);
123                             
124     }
125     
126     /** Unit test for default output when CDATA option is set */
127     public void testDefaultOutputWithCDATAOption() throws Exception JavaDoc {
128         Element element = new Element();
129         element.setValue("<greeting>What Ho Jeeves!</greeting>");
130         
131         StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n";
143         String JavaDoc xml = out.getBuffer().toString();
144          
145         assertEquals(expected,xml);
146                             
147     }
148     
149     /** Unit test for default output when character escaping option is set */
150     public void testDefaultOutputWithCharacterEscapingOption() throws Exception JavaDoc {
151         Element element = new Element();
152         element.setValue("<greeting>What Ho Jeeves!</greeting>");
153         
154         StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
165         String JavaDoc xml = out.getBuffer().toString();
166          
167         assertEquals(expected,xml);
168     }
169     
170     public void testDefaultOutputWithDotBetwixtOptions() throws Exception JavaDoc {
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 JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc expected = "<?xml version='1.0'?>" +
184             "<greek-abc>\n" +
185             "<alpha><![CDATA[<strong>weak</strong>]]></alpha>\n" +
186             "<beta>&lt;strong&gt;weak&lt;/strong&gt;</beta>\n" +
187             "<gamma>&lt;strong&gt;weak&lt;/strong&gt;</gamma>\n" +
188             "</greek-abc>\n";
189         String JavaDoc xml = out.getBuffer().toString();
190          
191         assertEquals(expected,xml);
192     }
193     
194     public void testEscapedOutput() throws Exception JavaDoc {
195         Element element = new Element();
196         element.setValue("<greeting>What Ho Jeeves!</greeting>");
197         
198         StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
207         String JavaDoc xml = out.getBuffer().toString();
208          
209         assertEquals(expected,xml);
210                             
211     }
212     
213     public void testCDATAEncodedOutput() throws Exception JavaDoc {
214         Element element = new Element();
215         element.setValue("<greeting>What Ho Jeeves!</greeting>");
216         
217         StringWriter JavaDoc out = new StringWriter JavaDoc();
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 JavaDoc expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n";
226         String JavaDoc xml = out.getBuffer().toString();
227          
228         assertEquals(expected,xml);
229     }
230 }
231
Popular Tags