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: package org.apache.commons.lang;
018:
019: import java.io.StringWriter;
020:
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024: import junit.textui.TestRunner;
025:
026: /**
027: * Unit tests for {@link StringEscapeUtils}.
028: *
029: * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
030: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
031: * @version $Id: EntitiesTest.java 471630 2006-11-06 04:14:45Z bayard $
032: */
033: public class EntitiesTest extends TestCase {
034: public EntitiesTest(String name) {
035: super (name);
036: }
037:
038: public static void main(String[] args) {
039: TestRunner.run(suite());
040: }
041:
042: public static Test suite() {
043: TestSuite suite = new TestSuite(EntitiesTest.class);
044: suite.setName("EntitiesTest Tests");
045: return suite;
046: }
047:
048: Entities entities;
049:
050: public void setUp() {
051: entities = new Entities();
052: entities.addEntity("foo", 161);
053: entities.addEntity("bar", 162);
054: }
055:
056: public void testEscapeNamedEntity() throws Exception {
057: doTestEscapeNamedEntity("&foo;", "\u00A1");
058: doTestEscapeNamedEntity("x&foo;", "x\u00A1");
059: doTestEscapeNamedEntity("&foo;x", "\u00A1x");
060: doTestEscapeNamedEntity("x&foo;x", "x\u00A1x");
061: doTestEscapeNamedEntity("&foo;&bar;", "\u00A1\u00A2");
062: }
063:
064: private void doTestEscapeNamedEntity(final String expected,
065: final String entity) throws Exception {
066: assertEquals(expected, entities.escape(entity));
067: StringWriter writer = new StringWriter();
068: entities.escape(writer, entity);
069: assertEquals(expected, writer.toString());
070: }
071:
072: public void testUnescapeNamedEntity() throws Exception {
073: assertEquals("\u00A1", entities.unescape("&foo;"));
074: assertEquals("x\u00A1", entities.unescape("x&foo;"));
075: assertEquals("\u00A1x", entities.unescape("&foo;x"));
076: assertEquals("x\u00A1x", entities.unescape("x&foo;x"));
077: assertEquals("\u00A1\u00A2", entities.unescape("&foo;&bar;"));
078: }
079:
080: public void testUnescapeUnknownEntity() throws Exception {
081: doTestUnescapeEntity("&zzzz;", "&zzzz;");
082: }
083:
084: public void testUnescapeMiscellaneous() throws Exception {
085: doTestUnescapeEntity("&hello", "&hello");
086: doTestUnescapeEntity("&;", "&;");
087: doTestUnescapeEntity("&#;", "&#;");
088: doTestUnescapeEntity("&#invalid;", "&#invalid;");
089: doTestUnescapeEntity("A", "A");
090: }
091:
092: private void doTestUnescapeEntity(final String expected,
093: final String entity) throws Exception {
094: assertEquals(expected, entities.unescape(entity));
095: StringWriter writer = new StringWriter();
096: entities.unescape(writer, entity);
097: assertEquals(expected, writer.toString());
098: }
099:
100: public void testAddEntitiesArray() throws Exception {
101: String[][] array = { { "foo", "100" }, { "bar", "101" } };
102: Entities e = new Entities();
103: e.addEntities(array);
104: assertEquals("foo", e.entityName(100));
105: assertEquals("bar", e.entityName(101));
106: assertEquals(100, e.entityValue("foo"));
107: assertEquals(101, e.entityValue("bar"));
108: }
109:
110: public void testEntitiesXmlObject() throws Exception {
111: assertEquals("gt", Entities.XML.entityName('>'));
112: assertEquals('>', Entities.XML.entityValue("gt"));
113: assertEquals(-1, Entities.XML.entityValue("xyzzy"));
114: }
115:
116: public void testArrayIntMap() throws Exception {
117: Entities.ArrayEntityMap map = new Entities.ArrayEntityMap(2);
118: checkSomeEntityMap(map);
119: Entities.ArrayEntityMap map1 = new Entities.ArrayEntityMap();
120: checkSomeEntityMap(map1);
121: assertEquals(-1, map.value("null"));
122: assertNull(map.name(-1));
123: }
124:
125: public void testTreeIntMap() throws Exception {
126: Entities.EntityMap map = new Entities.TreeEntityMap();
127: checkSomeEntityMap(map);
128: }
129:
130: public void testHashIntMap() throws Exception {
131: Entities.EntityMap map = new Entities.HashEntityMap();
132: checkSomeEntityMap(map);
133: assertEquals(-1, map.value("noname"));
134: }
135:
136: public void testBinaryIntMap() throws Exception {
137: Entities.BinaryEntityMap map = new Entities.BinaryEntityMap(2);
138: checkSomeEntityMap(map);
139: Entities.BinaryEntityMap map1 = new Entities.BinaryEntityMap();
140: checkSomeEntityMap(map1);
141:
142: // value cannot be added twice
143: map1.add("baz4a", 4);
144: map1.add("baz4b", 4);
145: assertEquals(-1, map1.value("baz4b"));
146: assertEquals("baz4a", map1.name(4));
147: assertNull(map1.name(99));
148:
149: Entities.BinaryEntityMap map2 = new Entities.BinaryEntityMap();
150: map2.add("val1", 1);
151: map2.add("val2", 2);
152: map2.add("val3", 3);
153: map2.add("val4", 4);
154: map2.add("val5", 5);
155: assertEquals("val5", map2.name(5));
156: assertEquals("val4", map2.name(4));
157: assertEquals("val3", map2.name(3));
158: assertEquals("val2", map2.name(2));
159: assertEquals("val1", map2.name(1));
160: }
161:
162: public void testPrimitiveIntMap() throws Exception {
163: Entities.PrimitiveEntityMap map = new Entities.PrimitiveEntityMap();
164: checkSomeEntityMap(map);
165: }
166:
167: private void checkSomeEntityMap(Entities.EntityMap map) {
168: map.add("foo", 1);
169: assertEquals(1, map.value("foo"));
170: assertEquals("foo", map.name(1));
171: map.add("bar", 2);
172: map.add("baz", 3);
173: assertEquals(3, map.value("baz"));
174: assertEquals("baz", map.name(3));
175: }
176:
177: public void testHtml40Nbsp() throws Exception {
178: assertEquals(" ", Entities.HTML40.escape("\u00A0"));
179: Entities e = new Entities();
180: e.map = new Entities.PrimitiveEntityMap();
181: Entities.fillWithHtml40Entities(e);
182: assertEquals(" ", e.escape("\u00A0"));
183: }
184:
185: public void testNumberOverflow() throws Exception {
186: doTestUnescapeEntity("�", "�");
187: doTestUnescapeEntity("x�y", "x�y");
188: doTestUnescapeEntity("�", "�");
189: doTestUnescapeEntity("x�y", "x�y");
190: }
191:
192: }
|