1 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 31 public class EntitiesTest extends TestCase 32 { 33 public EntitiesTest(String name) { 34 super(name); 35 } 36 37 public static void main(String [] 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 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 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 75 { 76 assertEquals("&zzzz;", entities.unescape("&zzzz;")); 77 } 78 79 public void testAddEntitiesArray() throws Exception 80 { 81 String [][] 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 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 98 { 99 Entities.ArrayEntityMap map = new Entities.ArrayEntityMap(2); 100 checkSomeEntityMap(map); 101 } 102 103 public void testTreeIntMap() throws Exception 104 { 105 Entities.EntityMap map = new Entities.TreeEntityMap(); 106 checkSomeEntityMap(map); 107 } 108 109 public void testHashIntMap() throws Exception 110 { 111 Entities.EntityMap map = new Entities.HashEntityMap(); 112 checkSomeEntityMap(map); 113 } 114 115 public void testBinaryIntMap() throws Exception 116 { 117 Entities.BinaryEntityMap map = new Entities.BinaryEntityMap(2); 118 checkSomeEntityMap(map); 119 } 120 121 public void testPrimitiveIntMap() throws Exception 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 138 { 139 assertEquals(" ", Entities.HTML40.escape("\u00A0")); 140 Entities e = new Entities(); 141 e.map = new Entities.PrimitiveEntityMap(); 142 Entities.fillWithHtml40Entities(e); 143 assertEquals(" ", e.escape("\u00A0")); 144 } 145 146 } 147 148 | Popular Tags |