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;
019:
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /** Test harness for the XMLUtils
025: *
026: * @author Robert Burrell Donkin
027: * @version $Revision: 438373 $
028: */
029: public class TestXMLUtils extends AbstractTestCase {
030:
031: public static void main(String[] args) {
032: TestRunner.run(suite());
033: }
034:
035: public static Test suite() {
036: return new TestSuite(TestXMLUtils.class);
037: }
038:
039: public TestXMLUtils(String testName) {
040: super (testName);
041: }
042:
043: /**
044: * Test for some common xml naming
045: */
046: public void testXMLNameTest() {
047: // just go through some common cases to make sure code is working
048: assertEquals("Testing name 'Name<'", false, XMLUtils
049: .isWellFormedXMLName("Name<"));
050: assertEquals("Testing name 'Name>'", false, XMLUtils
051: .isWellFormedXMLName("Name>"));
052: assertEquals("Testing name 'Name''", false, XMLUtils
053: .isWellFormedXMLName("Name'"));
054: assertEquals("Testing name 'Name_:-.'", true, XMLUtils
055: .isWellFormedXMLName("Name_:-."));
056: assertEquals("Testing name '.Name'", false, XMLUtils
057: .isWellFormedXMLName(".Name"));
058: assertEquals("Testing name '-Name'", false, XMLUtils
059: .isWellFormedXMLName("-Name"));
060: assertEquals("Testing name ':Name'", true, XMLUtils
061: .isWellFormedXMLName(":Name"));
062: assertEquals("Testing name '_Name'", true, XMLUtils
063: .isWellFormedXMLName("_Name"));
064: assertEquals("Testing name 'A0123456789Name", true, XMLUtils
065: .isWellFormedXMLName("A0123456789Name"));
066: }
067:
068: /** Test attribute escaping */
069: public void testAttributeEscaping() {
070: assertEquals("Escaping: <", "<", XMLUtils
071: .escapeAttributeValue("<"));
072: assertEquals("Escaping: >", ">", XMLUtils
073: .escapeAttributeValue(">"));
074: assertEquals("Escaping: '", "'", XMLUtils
075: .escapeAttributeValue("'"));
076: assertEquals("Escaping: \"", """, XMLUtils
077: .escapeAttributeValue("\""));
078: assertEquals("Escaping: &", "&", XMLUtils
079: .escapeAttributeValue("&"));
080: assertEquals("Escaping: 1<", "1<", XMLUtils
081: .escapeAttributeValue("1<"));
082: assertEquals("Escaping: 1>", "1>", XMLUtils
083: .escapeAttributeValue("1>"));
084: assertEquals("Escaping: 1'", "1'", XMLUtils
085: .escapeAttributeValue("1'"));
086: assertEquals("Escaping: 1\"", "1"", XMLUtils
087: .escapeAttributeValue("1\""));
088: assertEquals("Escaping: 1&", "1&", XMLUtils
089: .escapeAttributeValue("1&"));
090: assertEquals("Escaping: <2", "<2", XMLUtils
091: .escapeAttributeValue("<2"));
092: assertEquals("Escaping: >2", ">2", XMLUtils
093: .escapeAttributeValue(">2"));
094: assertEquals("Escaping: '2", "'2", XMLUtils
095: .escapeAttributeValue("'2"));
096: assertEquals("Escaping: \"2", ""2", XMLUtils
097: .escapeAttributeValue("\"2"));
098: assertEquals("Escaping: &2", "&2", XMLUtils
099: .escapeAttributeValue("&2"));
100: assertEquals("Escaping: a<b", "a<b", XMLUtils
101: .escapeAttributeValue("a<b"));
102: assertEquals("Escaping: a>b", "a>b", XMLUtils
103: .escapeAttributeValue("a>b"));
104: assertEquals("Escaping: a'b", "a'b", XMLUtils
105: .escapeAttributeValue("a'b"));
106: assertEquals("Escaping: a\"b", "a"b", XMLUtils
107: .escapeAttributeValue("a\"b"));
108: assertEquals("Escaping: a&b", "a&b", XMLUtils
109: .escapeAttributeValue("a&b"));
110: assertEquals("Escaping: <<abba", "<<abba", XMLUtils
111: .escapeAttributeValue("<<abba"));
112: assertEquals("Escaping: >>abba", ">>abba", XMLUtils
113: .escapeAttributeValue(">>abba"));
114: assertEquals("Escaping: ''abba", "''abba", XMLUtils
115: .escapeAttributeValue("''abba"));
116: assertEquals("Escaping: \"\"abba", """abba", XMLUtils
117: .escapeAttributeValue("\"\"abba"));
118: assertEquals("Escaping: &&abba", "&&abba", XMLUtils
119: .escapeAttributeValue("&&abba"));
120: assertEquals("Escaping: a<>b''c\"e>f'&g",
121: "a<>b''c"e>f'&g",
122: XMLUtils.escapeAttributeValue("a<>b''c\"e>f'&g"));
123:
124: }
125:
126: /**
127: * Test CDATA escaping
128: * Within a CDATA section, only the CDEnd
129: * string ']]>' is recognized as markup.
130: * Angle brackets and amphersands may occur in their literal form.
131: */
132: public void testCDATAEscaping() {
133: assertEquals("Escaping: <", "<", XMLUtils
134: .escapeCDATAContent("<"));
135: assertEquals("Escaping: >", ">", XMLUtils
136: .escapeCDATAContent(">"));
137: assertEquals("Escaping: '", "'", XMLUtils
138: .escapeCDATAContent("'"));
139: assertEquals("Escaping: \"", "\"", XMLUtils
140: .escapeCDATAContent("\""));
141: assertEquals("Escaping: &", "&", XMLUtils
142: .escapeCDATAContent("&"));
143: assertEquals("Escaping: ]]", "]]", XMLUtils
144: .escapeCDATAContent("]]"));
145: assertEquals("Escaping: ]>", "]>", XMLUtils
146: .escapeCDATAContent("]>"));
147: assertEquals("Escaping: ]]>", "]]>", XMLUtils
148: .escapeCDATAContent("]]>"));
149: assertEquals("Escaping: ]]>]]>", "]]>]]>", XMLUtils
150: .escapeCDATAContent("]]>]]>"));
151: assertEquals("Escaping: ]>]]>", "]>]]>", XMLUtils
152: .escapeCDATAContent("]>]]>"));
153: assertEquals("Escaping: ]]>]]]>", "]]>]]]>", XMLUtils
154: .escapeCDATAContent("]]>]]]>"));
155: assertEquals("Escaping: ", "", XMLUtils.escapeCDATAContent(""));
156: }
157:
158: }
|