KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > EntitiesPerformanceTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc stringWithUnicode;
28     private static String JavaDoc 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 JavaDoc name) {
37         super(name);
38     }
39
40     public static void main(String JavaDoc[] 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 JavaDoc buf = new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc {
75         for (int i = 0; i < COUNT; ++i) {
76             hashEntities = build(new Entities.HashEntityMap());
77         }
78     }
79
80
81     public void testBuildTree() throws Exception JavaDoc {
82         for (int i = 0; i < COUNT; ++i) {
83             treeEntities = build(new Entities.TreeEntityMap());
84         }
85     }
86
87     public void testBuildArray() throws Exception JavaDoc {
88         for (int i = 0; i < COUNT; ++i) {
89             arrayEntities = build(new Entities.ArrayEntityMap());
90         }
91     }
92
93     public void testBuildBinary() throws Exception JavaDoc {
94         for (int i = 0; i < COUNT; ++i) {
95             binaryEntities = build(new Entities.BinaryEntityMap());
96         }
97     }
98
99     public void testBuildPrimitive() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
130         lookup(hashEntities);
131     }
132
133     public void testLookupTree() throws Exception JavaDoc {
134         lookup(treeEntities);
135     }
136
137     public void testLookupArray() throws Exception JavaDoc {
138         lookup(arrayEntities);
139     }
140
141     public void testLookupBinary() throws Exception JavaDoc {
142         lookup(binaryEntities);
143     }
144
145     public void testLookupPrimitive() throws Exception JavaDoc {
146         if (primitiveEntities == null) buildPrimitive();
147         lookup(primitiveEntities);
148     }
149
150     public void testLookupLookup() throws Exception JavaDoc {
151         if (lookupEntities == null) buildLookup();
152         lookup(lookupEntities);
153     }
154
155     public void testEscapeHash() throws Exception JavaDoc {
156         escapeIt(hashEntities);
157     }
158
159     public void testEscapeTree() throws Exception JavaDoc {
160         escapeIt(treeEntities);
161     }
162
163     public void testEscapeArray() throws Exception JavaDoc {
164         escapeIt(arrayEntities);
165     }
166
167     public void testEscapeBinary() throws Exception JavaDoc {
168         escapeIt(binaryEntities);
169     }
170
171     public void testEscapePrimitive() throws Exception JavaDoc {
172         escapeIt(primitiveEntities);
173     }
174
175     public void testEscapeLookup() throws Exception JavaDoc {
176         escapeIt(lookupEntities);
177     }
178
179     public void testUnescapeHash() throws Exception JavaDoc {
180         unescapeIt(hashEntities);
181     }
182
183     public void testUnescapeTree() throws Exception JavaDoc {
184         unescapeIt(treeEntities);
185     }
186
187     public void testUnescapeArray() throws Exception JavaDoc {
188         unescapeIt(arrayEntities);
189     }
190
191     public void testUnescapeBinary() throws Exception JavaDoc {
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 JavaDoc escaped = entities.escape(stringWithUnicode);
204             assertEquals("xxxx&nbsp;", escaped.substring(0, 10));
205         }
206     }
207
208     private void unescapeIt(Entities entities) {
209         for (int i = 0; i < COUNT; ++i) {
210             String JavaDoc unescaped = entities.unescape(stringWithEntities);
211             assertEquals("xxxx\u00A0", unescaped.substring(0, 5));
212         }
213     }
214
215
216 }
217
218
Popular Tags