1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17   
18  package org.apache.commons.betwixt.strategy;
19  
20  
21  import java.io.StringWriter;
22  
23  import org.apache.commons.betwixt.AbstractTestCase;
24  import org.apache.commons.betwixt.ElementDescriptor;
25  import org.apache.commons.betwixt.XMLBeanInfo;
26  import org.apache.commons.betwixt.io.BeanWriter;
27  
28  
29  /**
30   * Tests for mixed content encoding.
31   * Mixed content encoding is the process by which body content
32   * is written out (in an escaped form) into a textual output stream. 
33   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
34   * @version $Revision: 561314 $
35   */
36  public class TestMixedContentEncoding extends AbstractTestCase {
37  
38      /** Concrete subclass used for testing */
39      static class TestBaseMixedContentEncoding extends BaseMixedContentEncodingStrategy {
40          boolean encode = false;
41          ElementDescriptor element = null;
42          
43          TestBaseMixedContentEncoding(boolean encode) {
44              this.encode = encode;
45          }
46          
47          protected boolean encodeAsCDATA(ElementDescriptor element) {
48              this.element = element;
49              return encode;
50          }
51      }
52      
53      public TestMixedContentEncoding(String testName) {
54          super(testName);
55      }
56      
57      public void testBaseMixedEscapeCharacters() {
58          BaseMixedContentEncodingStrategy mceStrategy = new TestBaseMixedContentEncoding(false);
59          assertEquals("Check basic escaping", "ab&lt;&gt;&amp;ba", mceStrategy.escapeCharacters("ab<>&ba"));
60      }
61      
62      public void testBaseMixedCDATAEncoding() {
63          BaseMixedContentEncodingStrategy mceStrategy = new TestBaseMixedContentEncoding(false);
64          assertEquals("Check basic escaping", "<![CDATA[<greeting>ab]]&gt;ba</greeting>]]>", mceStrategy.encodeInCDATA("<greeting>ab]]>ba</greeting>"));
65      }
66      
67      public void testBaseMixedEncode() {
68          ElementDescriptor descriptor = new ElementDescriptor();
69          TestBaseMixedContentEncoding mceStrategy = new TestBaseMixedContentEncoding(false);
70          assertEquals(
71                          "Using character escaping", 
72                          "&lt;exclaim&gt;hello, mum&lt;/exclaim&gt;", 
73                          mceStrategy.encode("<exclaim>hello, mum</exclaim>", descriptor));
74          
75          assertEquals("Descriptor set", descriptor, mceStrategy.element);
76          mceStrategy = new TestBaseMixedContentEncoding(true);
77          assertEquals(
78                          "Using CDATA encoding", 
79                          "<![CDATA[<exclaim>hello, mum</exclaim>]]>", 
80                          mceStrategy.encode("<exclaim>hello, mum</exclaim>", descriptor));
81          
82          assertEquals("Descriptor set", descriptor, mceStrategy.element);
83      }
84      
85      public void testDefaultImplementation() {
86          ElementDescriptor descriptor = new ElementDescriptor();
87          assertEquals(
88              "Default implementation uses character escaping",
89              "&lt;proclaim&gt;The King Is Dead, Long Live The King&lt;/proclaim&gt;",
90              MixedContentEncodingStrategy.DEFAULT.encode("<proclaim>The King Is Dead, Long Live The King</proclaim>", descriptor));
91      }
92      
93      public void testEscapedCharactersImplementation() {
94          ElementDescriptor descriptor = new ElementDescriptor();
95          assertEquals(
96              "Default implementation uses character escaping",
97              "&lt;proclaim&gt;The King Is Dead, Long Live The King&lt;/proclaim&gt;",
98              MixedContentEncodingStrategy.ESCAPED_CHARACTERS.encode("<proclaim>The King Is Dead, Long Live The King</proclaim>", descriptor));
99      }
100     
101     public void testCDATAImplementation() {
102         ElementDescriptor descriptor = new ElementDescriptor();
103         assertEquals(
104             "Default implementation uses character escaping",
105             "<![CDATA[<proclaim>The King Is Dead, Long Live The King</proclaim>]]>",
106             MixedContentEncodingStrategy.CDATA.encode("<proclaim>The King Is Dead, Long Live The King</proclaim>", descriptor));
107     }
108     
109     public void testDefaultOutput() throws Exception {
110         Element element = new Element();
111         element.setValue("<greeting>What Ho Jeeves!</greeting>");
112         
113         StringWriter out = new StringWriter();
114         out.write("<?xml version='1.0'?>");
115         BeanWriter writer = new BeanWriter(out);
116         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
117         writer.getBindingConfiguration().setMapIDs(false);
118         writer.setEndOfLine("\n"); //force to \n so expected values match for sure
119         writer.write(element);
120         
121         String expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
122         String xml = out.getBuffer().toString();
123          
124         assertEquals(expected,xml); 
125                             
126     }
127     
128     /** Unit test for default output when CDATA option is set */
129     public void testDefaultOutputWithCDATAOption() throws Exception {
130         Element element = new Element();
131         element.setValue("<greeting>What Ho Jeeves!</greeting>");
132         
133         StringWriter out = new StringWriter();
134         out.write("<?xml version='1.0'?>");
135         BeanWriter writer = new BeanWriter(out);
136         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
137         writer.getBindingConfiguration().setMapIDs(false);
138         XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class);
139         elementInfo.getElementDescriptor().getElementDescriptors()[0]
140             .getOptions().addOption(MixedContentEncodingStrategy.ENCODING_OPTION_NAME, "CDATA");  
141          
142         writer.setEndOfLine("\n"); //force to \n so expected values match for sure
143         writer.write(element);
144         
145         String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n";
146         String xml = out.getBuffer().toString();
147          
148         assertEquals(expected,xml); 
149                             
150     }
151     
152     /** Unit test for default output when character escaping option is set */
153     public void testDefaultOutputWithCharacterEscapingOption() throws Exception {
154         Element element = new Element();
155         element.setValue("<greeting>What Ho Jeeves!</greeting>");
156         
157         StringWriter out = new StringWriter();
158         out.write("<?xml version='1.0'?>");
159         BeanWriter writer = new BeanWriter(out);
160         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
161         writer.getBindingConfiguration().setMapIDs(false);
162         XMLBeanInfo elementInfo = writer.getXMLIntrospector().introspect(Element.class);
163         elementInfo.getElementDescriptor().getElementDescriptors()[0]
164             .getOptions().addOption("org.apache.commons.betwixt.mixed-content-encoding", "escaped");
165         writer.setEndOfLine("\n"); //force to \n so expected values match for sure
166         writer.write(element);
167         
168         String expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
169         String xml = out.getBuffer().toString();
170          
171         assertEquals(expected,xml); 
172     }
173     
174     public void testDefaultOutputWithDotBetwixtOptions() throws Exception {
175         ABCBean bean = new ABCBean();
176         bean.setA("<strong>weak</strong>");
177         bean.setB("<strong>weak</strong>");
178         bean.setC("<strong>weak</strong>");
179         
180         StringWriter out = new StringWriter();
181         out.write("<?xml version='1.0'?>");
182         BeanWriter writer = new BeanWriter(out);
183         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
184         writer.getBindingConfiguration().setMapIDs(false);
185         writer.setEndOfLine("\n"); //force to \n so expected values match for sure
186         writer.write(bean);
187         
188         String expected = "<?xml version='1.0'?>" +
189             "<greek-abc>\n" +
190             "<alpha><![CDATA[<strong>weak</strong>]]></alpha>\n" +
191             "<beta>&lt;strong&gt;weak&lt;/strong&gt;</beta>\n" +
192             "<gamma>&lt;strong&gt;weak&lt;/strong&gt;</gamma>\n" +
193             "</greek-abc>\n";
194         String xml = out.getBuffer().toString();
195          
196         assertEquals(expected,xml); 
197     }
198     
199     public void testEscapedOutput() throws Exception {
200         Element element = new Element();
201         element.setValue("<greeting>What Ho Jeeves!</greeting>");
202         
203         StringWriter out = new StringWriter();
204         out.write("<?xml version='1.0'?>");
205         BeanWriter writer = new BeanWriter(out);
206         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
207         writer.getBindingConfiguration().setMapIDs(false);
208         writer.setMixedContentEncodingStrategy(new TestBaseMixedContentEncoding(false));
209         writer.setEndOfLine("\n"); //force to \n so expected values match for sure
210         writer.write(element);
211         
212         String expected = "<?xml version='1.0'?><Element>\n<value>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</value>\n</Element>\n";
213         String xml = out.getBuffer().toString();
214          
215         assertEquals(expected,xml); 
216                             
217     }
218     
219     public void testCDATAEncodedOutput() throws Exception {
220         Element element = new Element();
221         element.setValue("<greeting>What Ho Jeeves!</greeting>");
222         
223         StringWriter out = new StringWriter();
224         out.write("<?xml version='1.0'?>");
225         BeanWriter writer = new BeanWriter(out);
226         writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(false);
227         writer.getBindingConfiguration().setMapIDs(false);
228         writer.setMixedContentEncodingStrategy(new TestBaseMixedContentEncoding(true));
229         writer.setEndOfLine("\n"); //force to \n so expected values match for sure
230         writer.write(element);
231         
232         String expected = "<?xml version='1.0'?><Element>\n<value><![CDATA[<greeting>What Ho Jeeves!</greeting>]]></value>\n</Element>\n";
233         String xml = out.getBuffer().toString();
234          
235         assertEquals(expected,xml);      
236     }
237 }