KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > TestXMLUtils


1 /*
2  * Copyright 2001-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;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /** Test harness for the XMLUtils
24   *
25   * @author Robert Burrell Donkin
26   * @version $Revision: 1.6 $
27   */

28 public class TestXMLUtils extends AbstractTestCase {
29     
30     public static void main( String JavaDoc[] args ) {
31         TestRunner.run( suite() );
32     }
33     
34     public static Test suite() {
35         return new TestSuite(TestXMLUtils.class);
36     }
37     
38     public TestXMLUtils(String JavaDoc testName) {
39         super(testName);
40     }
41     
42     /**
43      * Test for some common xml naming
44      */

45     public void testXMLNameTest() {
46         // just go through some common cases to make sure code is working
47
assertEquals("Testing name 'Name<'", false, XMLUtils.isWellFormedXMLName("Name<"));
48         assertEquals("Testing name 'Name>'", false, XMLUtils.isWellFormedXMLName("Name>"));
49         assertEquals("Testing name 'Name''", false, XMLUtils.isWellFormedXMLName("Name'"));
50         assertEquals("Testing name 'Name_:-.'", true, XMLUtils.isWellFormedXMLName("Name_:-."));
51         assertEquals("Testing name '.Name'", false, XMLUtils.isWellFormedXMLName(".Name"));
52         assertEquals("Testing name '-Name'", false, XMLUtils.isWellFormedXMLName("-Name"));
53         assertEquals("Testing name ':Name'", true, XMLUtils.isWellFormedXMLName(":Name"));
54         assertEquals("Testing name '_Name'", true, XMLUtils.isWellFormedXMLName("_Name"));
55         assertEquals("Testing name 'A0123456789Name", true, XMLUtils.isWellFormedXMLName("A0123456789Name"));
56     }
57     
58     /** Test attribute escaping */
59     public void testAttributeEscaping() {
60         assertEquals("Escaping: <", "&lt;", XMLUtils.escapeAttributeValue("<"));
61         assertEquals("Escaping: >", "&gt;", XMLUtils.escapeAttributeValue(">"));
62         assertEquals("Escaping: '", "&apos;", XMLUtils.escapeAttributeValue("'"));
63         assertEquals("Escaping: \"", "&quot;", XMLUtils.escapeAttributeValue("\""));
64         assertEquals("Escaping: &", "&amp;", XMLUtils.escapeAttributeValue("&"));
65         assertEquals("Escaping: 1<", "1&lt;", XMLUtils.escapeAttributeValue("1<"));
66         assertEquals("Escaping: 1>", "1&gt;", XMLUtils.escapeAttributeValue("1>"));
67         assertEquals("Escaping: 1'", "1&apos;", XMLUtils.escapeAttributeValue("1'"));
68         assertEquals("Escaping: 1\"", "1&quot;", XMLUtils.escapeAttributeValue("1\""));
69         assertEquals("Escaping: 1&", "1&amp;", XMLUtils.escapeAttributeValue("1&"));
70         assertEquals("Escaping: <2", "&lt;2", XMLUtils.escapeAttributeValue("<2"));
71         assertEquals("Escaping: >2", "&gt;2", XMLUtils.escapeAttributeValue(">2"));
72         assertEquals("Escaping: '2", "&apos;2", XMLUtils.escapeAttributeValue("'2"));
73         assertEquals("Escaping: \"2", "&quot;2", XMLUtils.escapeAttributeValue("\"2"));
74         assertEquals("Escaping: &2", "&amp;2", XMLUtils.escapeAttributeValue("&2"));
75         assertEquals("Escaping: a<b", "a&lt;b", XMLUtils.escapeAttributeValue("a<b"));
76         assertEquals("Escaping: a>b", "a&gt;b", XMLUtils.escapeAttributeValue("a>b"));
77         assertEquals("Escaping: a'b", "a&apos;b", XMLUtils.escapeAttributeValue("a'b"));
78         assertEquals("Escaping: a\"b", "a&quot;b", XMLUtils.escapeAttributeValue("a\"b"));
79         assertEquals("Escaping: a&b", "a&amp;b", XMLUtils.escapeAttributeValue("a&b"));
80         assertEquals("Escaping: <<abba", "&lt;&lt;abba", XMLUtils.escapeAttributeValue("<<abba"));
81         assertEquals("Escaping: >>abba", "&gt;&gt;abba", XMLUtils.escapeAttributeValue(">>abba"));
82         assertEquals("Escaping: ''abba", "&apos;&apos;abba", XMLUtils.escapeAttributeValue("''abba"));
83         assertEquals("Escaping: \"\"abba", "&quot;&quot;abba", XMLUtils.escapeAttributeValue("\"\"abba"));
84         assertEquals("Escaping: &&abba", "&amp;&amp;abba", XMLUtils.escapeAttributeValue("&&abba"));
85         assertEquals(
86             "Escaping: a<>b''c\"e>f'&g",
87             "a&lt;&gt;b&apos;&apos;c&quot;e&gt;f&apos;&amp;g",
88             XMLUtils.escapeAttributeValue("a<>b''c\"e>f'&g"));
89         
90     }
91     
92     /**
93      * Test CDATA escaping
94      * Within a CDATA section, only the CDEnd
95      * string ']]>' is recognized as markup.
96      * Angle brackets and amphersands may occur in their literal form.
97      */

98     public void testCDATAEscaping() {
99         assertEquals("Escaping: <", "<", XMLUtils.escapeCDATAContent("<"));
100         assertEquals("Escaping: >", ">", XMLUtils.escapeCDATAContent(">"));
101         assertEquals("Escaping: '", "'", XMLUtils.escapeCDATAContent("'"));
102         assertEquals("Escaping: \"", "\"", XMLUtils.escapeCDATAContent("\""));
103         assertEquals("Escaping: &", "&", XMLUtils.escapeCDATAContent("&"));
104         assertEquals("Escaping: ]]", "]]", XMLUtils.escapeCDATAContent("]]"));
105         assertEquals("Escaping: ]>", "]>", XMLUtils.escapeCDATAContent("]>"));
106         assertEquals("Escaping: ]]>", "]]&gt;", XMLUtils.escapeCDATAContent("]]>"));
107         assertEquals("Escaping: ]]>]]>", "]]&gt;]]&gt;", XMLUtils.escapeCDATAContent("]]>]]>"));
108         assertEquals("Escaping: ]>]]>", "]>]]&gt;", XMLUtils.escapeCDATAContent("]>]]>"));
109         assertEquals("Escaping: ]]>]]]>", "]]&gt;]]]&gt;", XMLUtils.escapeCDATAContent("]]>]]]>"));
110         assertEquals("Escaping: ", "", XMLUtils.escapeCDATAContent(""));
111     }
112     
113     
114 }
115
116
Popular Tags