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 public class EntitiesPerformanceTest extends TestCase { 24 private int COUNT = 10000; 25 private int STRING_LENGTH = 1000; 26 27 private static String stringWithUnicode; 28 private static String stringWithEntities; 29 private static Entities treeEntities; 30 private static Entities hashEntities; 31 private static Entities arrayEntities; 32 private static Entities binaryEntities; 33 private static Entities primitiveEntities; 34 private static Entities lookupEntities; 35 36 public EntitiesPerformanceTest(String name) { 37 super(name); 38 } 39 40 public static void main(String [] args) { 41 TestRunner.run(suite()); 42 } 43 44 public static Test suite() { 45 TestSuite suite = new TestSuite(EntitiesPerformanceTest.class); 46 return suite; 47 } 48 49 public void setUp() { 50 if (stringWithUnicode == null) { 51 StringBuffer buf = new StringBuffer (STRING_LENGTH); 52 for (int i = 0; i < STRING_LENGTH / 5; ++i) { 53 buf.append("xxxx"); 54 char ch = isovalue(i); 55 buf.append(ch); 56 } 57 stringWithUnicode = buf.toString(); 58 stringWithEntities = Entities.HTML40.unescape(stringWithUnicode); 59 } 60 } 61 62 private char html40value(int i) { 63 String entityValue = Entities.HTML40_ARRAY[i % Entities.HTML40_ARRAY.length][1]; 64 char ch = (char) Integer.parseInt(entityValue); 65 return ch; 66 } 67 68 private char isovalue(int i) { 69 String entityValue = Entities.ISO8859_1_ARRAY[i % Entities.ISO8859_1_ARRAY.length][1]; 70 char ch = (char) Integer.parseInt(entityValue); 71 return ch; 72 } 73 74 public void testBuildHash() throws Exception { 75 for (int i = 0; i < COUNT; ++i) { 76 hashEntities = build(new Entities.HashEntityMap()); 77 } 78 } 79 80 81 public void testBuildTree() throws Exception { 82 for (int i = 0; i < COUNT; ++i) { 83 treeEntities = build(new Entities.TreeEntityMap()); 84 } 85 } 86 87 public void testBuildArray() throws Exception { 88 for (int i = 0; i < COUNT; ++i) { 89 arrayEntities = build(new Entities.ArrayEntityMap()); 90 } 91 } 92 93 public void testBuildBinary() throws Exception { 94 for (int i = 0; i < COUNT; ++i) { 95 binaryEntities = build(new Entities.BinaryEntityMap()); 96 } 97 } 98 99 public void testBuildPrimitive() throws Exception { 100 for (int i = 0; i < COUNT; ++i) { 101 buildPrimitive(); 102 } 103 } 104 105 private void buildPrimitive() 106 { 107 primitiveEntities = build(new Entities.PrimitiveEntityMap()); 108 } 109 110 public void testBuildLookup() throws Exception { 111 for (int i = 0; i < COUNT; ++i) { 112 buildLookup(); 113 } 114 } 115 116 private void buildLookup() 117 { 118 lookupEntities = build(new Entities.LookupEntityMap()); 119 } 120 121 private Entities build(Entities.EntityMap intMap) { 122 Entities entities; 123 entities = new Entities(); 124 entities.map = intMap; 125 Entities.fillWithHtml40Entities(entities); 126 return entities; 127 } 128 129 public void testLookupHash() throws Exception { 130 lookup(hashEntities); 131 } 132 133 public void testLookupTree() throws Exception { 134 lookup(treeEntities); 135 } 136 137 public void testLookupArray() throws Exception { 138 lookup(arrayEntities); 139 } 140 141 public void testLookupBinary() throws Exception { 142 lookup(binaryEntities); 143 } 144 145 public void testLookupPrimitive() throws Exception { 146 if (primitiveEntities == null) buildPrimitive(); 147 lookup(primitiveEntities); 148 } 149 150 public void testLookupLookup() throws Exception { 151 if (lookupEntities == null) buildLookup(); 152 lookup(lookupEntities); 153 } 154 155 public void testEscapeHash() throws Exception { 156 escapeIt(hashEntities); 157 } 158 159 public void testEscapeTree() throws Exception { 160 escapeIt(treeEntities); 161 } 162 163 public void testEscapeArray() throws Exception { 164 escapeIt(arrayEntities); 165 } 166 167 public void testEscapeBinary() throws Exception { 168 escapeIt(binaryEntities); 169 } 170 171 public void testEscapePrimitive() throws Exception { 172 escapeIt(primitiveEntities); 173 } 174 175 public void testEscapeLookup() throws Exception { 176 escapeIt(lookupEntities); 177 } 178 179 public void testUnescapeHash() throws Exception { 180 unescapeIt(hashEntities); 181 } 182 183 public void testUnescapeTree() throws Exception { 184 unescapeIt(treeEntities); 185 } 186 187 public void testUnescapeArray() throws Exception { 188 unescapeIt(arrayEntities); 189 } 190 191 public void testUnescapeBinary() throws Exception { 192 unescapeIt(binaryEntities); 193 } 194 195 private void lookup(Entities entities) { 196 for (int i = 0; i < COUNT * 1000; ++i) { 197 entities.entityName(isovalue(i)); 198 } 199 } 200 201 private void escapeIt(Entities entities) { 202 for (int i = 0; i < COUNT; ++i) { 203 String escaped = entities.escape(stringWithUnicode); 204 assertEquals("xxxx ", escaped.substring(0, 10)); 205 } 206 } 207 208 private void unescapeIt(Entities entities) { 209 for (int i = 0; i < COUNT; ++i) { 210 String unescaped = entities.unescape(stringWithEntities); 211 assertEquals("xxxx\u00A0", unescaped.substring(0, 5)); 212 } 213 } 214 215 216 } 217 218 | Popular Tags |