KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > EntitiesTest


1 /*
2  * Copyright 2002-2005 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 package org.apache.commons.lang;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /**
24  * Unit tests for {@link StringEscapeUtils}.
25  *
26  * @author of original StringUtilsTest.testEscape = ?
27  * @author <a HREF="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
28  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
29  * @version $Id: EntitiesTest.java 161244 2005-04-14 06:16:36Z ggregory $
30  */

31 public class EntitiesTest extends TestCase
32 {
33     public EntitiesTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public static void main(String JavaDoc[] args) {
38         TestRunner.run(suite());
39     }
40
41     public static Test suite() {
42         TestSuite suite = new TestSuite(EntitiesTest.class);
43         suite.setName("EntitiesTest Tests");
44         return suite;
45     }
46
47     Entities entities;
48
49     public void setUp()
50     {
51         entities = new Entities();
52         entities.addEntity("foo", 161);
53         entities.addEntity("bar", 162);
54     }
55
56     public void testEscapeNamedEntity() throws Exception JavaDoc
57     {
58         assertEquals("&foo;", entities.escape("\u00A1"));
59         assertEquals("x&foo;", entities.escape("x\u00A1"));
60         assertEquals("&foo;x", entities.escape("\u00A1x"));
61         assertEquals("x&foo;x", entities.escape("x\u00A1x"));
62         assertEquals("&foo;&bar;", entities.escape("\u00A1\u00A2"));
63     }
64
65     public void testUnescapeNamedEntity() throws Exception JavaDoc
66     {
67         assertEquals("\u00A1", entities.unescape("&foo;"));
68         assertEquals("x\u00A1", entities.unescape("x&foo;"));
69         assertEquals("\u00A1x", entities.unescape("&foo;x"));
70         assertEquals("x\u00A1x", entities.unescape("x&foo;x"));
71         assertEquals("\u00A1\u00A2", entities.unescape("&foo;&bar;"));
72     }
73
74     public void testUnescapeUnknownEntity() throws Exception JavaDoc
75     {
76         assertEquals("&zzzz;", entities.unescape("&zzzz;"));
77     }
78
79     public void testAddEntitiesArray() throws Exception JavaDoc
80     {
81         String JavaDoc[][] array = {{"foo", "100"}, {"bar", "101"}};
82         Entities e = new Entities();
83         e.addEntities(array);
84         assertEquals("foo", e.entityName(100));
85         assertEquals("bar", e.entityName(101));
86         assertEquals(100, e.entityValue("foo"));
87         assertEquals(101, e.entityValue("bar"));
88     }
89
90     public void testEntitiesXmlObject() throws Exception JavaDoc
91     {
92         assertEquals("gt", Entities.XML.entityName('>'));
93         assertEquals('>', Entities.XML.entityValue("gt"));
94         assertEquals(-1, Entities.XML.entityValue("xyzzy"));
95     }
96
97     public void testArrayIntMap() throws Exception JavaDoc
98     {
99         Entities.ArrayEntityMap map = new Entities.ArrayEntityMap(2);
100         checkSomeEntityMap(map);
101     }
102
103     public void testTreeIntMap() throws Exception JavaDoc
104     {
105         Entities.EntityMap map = new Entities.TreeEntityMap();
106         checkSomeEntityMap(map);
107     }
108
109     public void testHashIntMap() throws Exception JavaDoc
110     {
111         Entities.EntityMap map = new Entities.HashEntityMap();
112         checkSomeEntityMap(map);
113     }
114
115     public void testBinaryIntMap() throws Exception JavaDoc
116     {
117         Entities.BinaryEntityMap map = new Entities.BinaryEntityMap(2);
118         checkSomeEntityMap(map);
119     }
120
121     public void testPrimitiveIntMap() throws Exception JavaDoc
122     {
123         Entities.PrimitiveEntityMap map = new Entities.PrimitiveEntityMap();
124         checkSomeEntityMap(map);
125     }
126
127     private void checkSomeEntityMap(Entities.EntityMap map) {
128         map.add("foo", 1);
129         assertEquals(1, map.value("foo"));
130         assertEquals("foo", map.name(1));
131         map.add("bar", 2);
132         map.add("baz", 3);
133         assertEquals(3, map.value("baz"));
134         assertEquals("baz", map.name(3));
135     }
136
137     public void testHtml40Nbsp() throws Exception JavaDoc
138     {
139         assertEquals("&nbsp;", Entities.HTML40.escape("\u00A0"));
140         Entities e = new Entities();
141         e.map = new Entities.PrimitiveEntityMap();
142         Entities.fillWithHtml40Entities(e);
143         assertEquals("&nbsp;", e.escape("\u00A0"));
144     }
145
146 }
147
148
Popular Tags