001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: public class EntitiesPerformanceTest extends TestCase {
025: private int COUNT = 10000;
026: private int STRING_LENGTH = 1000;
027:
028: private static String stringWithUnicode;
029: private static String stringWithEntities;
030: private static Entities treeEntities;
031: private static Entities hashEntities;
032: private static Entities arrayEntities;
033: private static Entities binaryEntities;
034: private static Entities primitiveEntities;
035: private static Entities lookupEntities;
036:
037: public EntitiesPerformanceTest(String name) {
038: super (name);
039: }
040:
041: public static void main(String[] args) {
042: TestRunner.run(suite());
043: }
044:
045: public static Test suite() {
046: TestSuite suite = new TestSuite(EntitiesPerformanceTest.class);
047: return suite;
048: }
049:
050: public void setUp() {
051: if (stringWithUnicode == null) {
052: StringBuffer buf = new StringBuffer(STRING_LENGTH);
053: for (int i = 0; i < STRING_LENGTH / 5; ++i) {
054: buf.append("xxxx");
055: char ch = isovalue(i);
056: buf.append(ch);
057: }
058: stringWithUnicode = buf.toString();
059: stringWithEntities = Entities.HTML40
060: .unescape(stringWithUnicode);
061: }
062: }
063:
064: private char html40value(int i) {
065: String entityValue = Entities.HTML40_ARRAY[i
066: % Entities.HTML40_ARRAY.length][1];
067: char ch = (char) Integer.parseInt(entityValue);
068: return ch;
069: }
070:
071: private char isovalue(int i) {
072: String entityValue = Entities.ISO8859_1_ARRAY[i
073: % Entities.ISO8859_1_ARRAY.length][1];
074: char ch = (char) Integer.parseInt(entityValue);
075: return ch;
076: }
077:
078: public void testBuildHash() throws Exception {
079: for (int i = 0; i < COUNT; ++i) {
080: hashEntities = build(new Entities.HashEntityMap());
081: }
082: }
083:
084: public void testBuildTree() throws Exception {
085: for (int i = 0; i < COUNT; ++i) {
086: treeEntities = build(new Entities.TreeEntityMap());
087: }
088: }
089:
090: public void testBuildArray() throws Exception {
091: for (int i = 0; i < COUNT; ++i) {
092: arrayEntities = build(new Entities.ArrayEntityMap());
093: }
094: }
095:
096: public void testBuildBinary() throws Exception {
097: for (int i = 0; i < COUNT; ++i) {
098: binaryEntities = build(new Entities.BinaryEntityMap());
099: }
100: }
101:
102: public void testBuildPrimitive() throws Exception {
103: for (int i = 0; i < COUNT; ++i) {
104: buildPrimitive();
105: }
106: }
107:
108: private void buildPrimitive() {
109: primitiveEntities = build(new Entities.PrimitiveEntityMap());
110: }
111:
112: public void testBuildLookup() throws Exception {
113: for (int i = 0; i < COUNT; ++i) {
114: buildLookup();
115: }
116: }
117:
118: private void buildLookup() {
119: lookupEntities = build(new Entities.LookupEntityMap());
120: }
121:
122: private Entities build(Entities.EntityMap intMap) {
123: Entities entities;
124: entities = new Entities();
125: entities.map = intMap;
126: Entities.fillWithHtml40Entities(entities);
127: return entities;
128: }
129:
130: public void testLookupHash() throws Exception {
131: lookup(hashEntities);
132: }
133:
134: public void testLookupTree() throws Exception {
135: lookup(treeEntities);
136: }
137:
138: public void testLookupArray() throws Exception {
139: lookup(arrayEntities);
140: }
141:
142: public void testLookupBinary() throws Exception {
143: lookup(binaryEntities);
144: }
145:
146: public void testLookupPrimitive() throws Exception {
147: if (primitiveEntities == null)
148: buildPrimitive();
149: lookup(primitiveEntities);
150: }
151:
152: public void testLookupLookup() throws Exception {
153: if (lookupEntities == null)
154: buildLookup();
155: lookup(lookupEntities);
156: }
157:
158: public void testEscapeHash() throws Exception {
159: escapeIt(hashEntities);
160: }
161:
162: public void testEscapeTree() throws Exception {
163: escapeIt(treeEntities);
164: }
165:
166: public void testEscapeArray() throws Exception {
167: escapeIt(arrayEntities);
168: }
169:
170: public void testEscapeBinary() throws Exception {
171: escapeIt(binaryEntities);
172: }
173:
174: public void testEscapePrimitive() throws Exception {
175: escapeIt(primitiveEntities);
176: }
177:
178: public void testEscapeLookup() throws Exception {
179: escapeIt(lookupEntities);
180: }
181:
182: public void testUnescapeHash() throws Exception {
183: unescapeIt(hashEntities);
184: }
185:
186: public void testUnescapeTree() throws Exception {
187: unescapeIt(treeEntities);
188: }
189:
190: public void testUnescapeArray() throws Exception {
191: unescapeIt(arrayEntities);
192: }
193:
194: public void testUnescapeBinary() throws Exception {
195: unescapeIt(binaryEntities);
196: }
197:
198: private void lookup(Entities entities) {
199: for (int i = 0; i < COUNT * 1000; ++i) {
200: entities.entityName(isovalue(i));
201: }
202: }
203:
204: private void escapeIt(Entities entities) {
205: for (int i = 0; i < COUNT; ++i) {
206: String escaped = entities.escape(stringWithUnicode);
207: assertEquals("xxxx ", escaped.substring(0, 10));
208: }
209: }
210:
211: private void unescapeIt(Entities entities) {
212: for (int i = 0; i < COUNT; ++i) {
213: String unescaped = entities.unescape(stringWithEntities);
214: assertEquals("xxxx\u00A0", unescaped.substring(0, 5));
215: }
216: }
217:
218: }
|