Source Code Cross Referenced for TestMixedContentEncoding.java in  » Library » Apache-commons-betwixt-0.8-src » org » apache » commons » betwixt » strategy » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons betwixt 0.8 src » org.apache.commons.betwixt.strategy 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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&lt;&gt;&amp;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]]&gt;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:                        "&lt;exclaim&gt;hello, mum&lt;/exclaim&gt;",
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:                        "&lt;proclaim&gt;The King Is Dead, Long Live The King&lt;/proclaim&gt;",
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:                        "&lt;proclaim&gt;The King Is Dead, Long Live The King&lt;/proclaim&gt;",
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>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</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>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</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>&lt;strong&gt;weak&lt;/strong&gt;</beta>\n"
220:                        + "<gamma>&lt;strong&gt;weak&lt;/strong&gt;</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>&lt;greeting&gt;What Ho Jeeves!&lt;/greeting&gt;</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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.