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