1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang;
18  
19  import java.io.StringWriter;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import junit.textui.TestRunner;
25  
26  /**
27   * Unit tests for {@link StringEscapeUtils}.
28   *
29   * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
30   * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
31   * @version $Id: EntitiesTest.java 471630 2006-11-06 04:14:45Z bayard $
32   */
33  public class EntitiesTest extends TestCase
34  {
35      public EntitiesTest(String name) {
36          super(name);
37      }
38  
39      public static void main(String[] args) {
40          TestRunner.run(suite());
41      }
42  
43      public static Test suite() {
44          TestSuite suite = new TestSuite(EntitiesTest.class);
45          suite.setName("EntitiesTest Tests");
46          return suite;
47      }
48  
49      Entities entities;
50  
51      public void setUp()
52      {
53          entities = new Entities();
54          entities.addEntity("foo", 161);
55          entities.addEntity("bar", 162);
56      }
57  
58      public void testEscapeNamedEntity() throws Exception
59      {
60          doTestEscapeNamedEntity("&foo;", "\u00A1");
61          doTestEscapeNamedEntity("x&foo;", "x\u00A1");
62          doTestEscapeNamedEntity("&foo;x", "\u00A1x");
63          doTestEscapeNamedEntity("x&foo;x", "x\u00A1x");
64          doTestEscapeNamedEntity("&foo;&bar;", "\u00A1\u00A2");
65      }
66  
67      private void doTestEscapeNamedEntity(final String expected, final String entity) throws Exception
68      {
69          assertEquals(expected, entities.escape(entity));
70          StringWriter writer = new StringWriter();
71          entities.escape(writer, entity);
72          assertEquals(expected, writer.toString());
73      }
74      
75      public void testUnescapeNamedEntity() throws Exception
76      {
77          assertEquals("\u00A1", entities.unescape("&foo;"));
78          assertEquals("x\u00A1", entities.unescape("x&foo;"));
79          assertEquals("\u00A1x", entities.unescape("&foo;x"));
80          assertEquals("x\u00A1x", entities.unescape("x&foo;x"));
81          assertEquals("\u00A1\u00A2", entities.unescape("&foo;&bar;"));
82      }
83  
84      public void testUnescapeUnknownEntity() throws Exception
85      {
86          doTestUnescapeEntity("&zzzz;", "&zzzz;");
87      }
88  
89      public void testUnescapeMiscellaneous() throws Exception
90      {
91        doTestUnescapeEntity("&hello", "&hello");
92        doTestUnescapeEntity("&;", "&;");
93        doTestUnescapeEntity("&#;", "&#;");
94        doTestUnescapeEntity("&#invalid;", "&#invalid;");
95        doTestUnescapeEntity("A", "&#X41;");
96      }
97      
98      private void doTestUnescapeEntity(final String expected, final String entity) throws Exception
99      {
100         assertEquals(expected, entities.unescape(entity));
101         StringWriter writer = new StringWriter();
102         entities.unescape(writer, entity);
103         assertEquals(expected, writer.toString());
104     }
105     
106     public void testAddEntitiesArray() throws Exception
107     {
108         String[][] array = {{"foo", "100"}, {"bar", "101"}};
109         Entities e = new Entities();
110         e.addEntities(array);
111         assertEquals("foo", e.entityName(100));
112         assertEquals("bar", e.entityName(101));
113         assertEquals(100, e.entityValue("foo"));
114         assertEquals(101, e.entityValue("bar"));
115     }
116 
117     public void testEntitiesXmlObject() throws Exception
118     {
119         assertEquals("gt", Entities.XML.entityName('>'));
120         assertEquals('>', Entities.XML.entityValue("gt"));
121         assertEquals(-1, Entities.XML.entityValue("xyzzy"));
122     }
123 
124     public void testArrayIntMap() throws Exception
125     {
126         Entities.ArrayEntityMap map = new Entities.ArrayEntityMap(2);
127         checkSomeEntityMap(map);
128         Entities.ArrayEntityMap map1 = new Entities.ArrayEntityMap();
129         checkSomeEntityMap(map1);
130         assertEquals(-1, map.value("null"));
131         assertNull(map.name(-1));
132     }
133 
134     public void testTreeIntMap() throws Exception
135     {
136         Entities.EntityMap map = new Entities.TreeEntityMap();
137         checkSomeEntityMap(map);
138     }
139 
140     public void testHashIntMap() throws Exception
141     {
142         Entities.EntityMap map = new Entities.HashEntityMap();
143         checkSomeEntityMap(map);
144         assertEquals(-1, map.value("noname"));
145     }
146 
147     public void testBinaryIntMap() throws Exception
148     {
149         Entities.BinaryEntityMap map = new Entities.BinaryEntityMap(2);
150         checkSomeEntityMap(map);
151         Entities.BinaryEntityMap map1 = new Entities.BinaryEntityMap();
152         checkSomeEntityMap(map1);
153         
154         // value cannot be added twice
155         map1.add("baz4a", 4);
156         map1.add("baz4b", 4);
157         assertEquals(-1, map1.value("baz4b"));
158         assertEquals("baz4a", map1.name(4));
159         assertNull(map1.name(99));
160         
161         Entities.BinaryEntityMap map2 = new Entities.BinaryEntityMap();
162         map2.add("val1", 1);
163         map2.add("val2", 2);
164         map2.add("val3", 3);
165         map2.add("val4", 4);
166         map2.add("val5", 5);
167         assertEquals("val5", map2.name(5));
168         assertEquals("val4", map2.name(4));
169         assertEquals("val3", map2.name(3));
170         assertEquals("val2", map2.name(2));
171         assertEquals("val1", map2.name(1));
172     }
173 
174     public void testPrimitiveIntMap() throws Exception
175     {
176         Entities.PrimitiveEntityMap map = new Entities.PrimitiveEntityMap();
177         checkSomeEntityMap(map);
178     }
179 
180     private void checkSomeEntityMap(Entities.EntityMap map) {
181         map.add("foo", 1);
182         assertEquals(1, map.value("foo"));
183         assertEquals("foo", map.name(1));
184         map.add("bar", 2);
185         map.add("baz", 3);
186         assertEquals(3, map.value("baz"));
187         assertEquals("baz", map.name(3));
188     }
189     
190     public void testHtml40Nbsp() throws Exception
191     {
192         assertEquals("&nbsp;", Entities.HTML40.escape("\u00A0"));
193         Entities e = new Entities();
194         e.map = new Entities.PrimitiveEntityMap();
195         Entities.fillWithHtml40Entities(e);
196         assertEquals("&nbsp;", e.escape("\u00A0"));
197     }
198 
199     public void testNumberOverflow() throws Exception {
200         doTestUnescapeEntity("&#12345678;", "&#12345678;");
201         doTestUnescapeEntity("x&#12345678;y", "x&#12345678;y");
202         doTestUnescapeEntity("&#x12345678;", "&#x12345678;");
203         doTestUnescapeEntity("x&#x12345678;y", "x&#x12345678;y");
204     }
205 
206 
207 }
208