1 /* ====================================================================
2 * License:
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. The end-user documentation included with the redistribution,
17 * if any, must include the following acknowledgment:
18 * "This product includes software developed by
19 * Robert Half International (http://www.rhi.com/)."
20 * Alternately, this acknowledgment may appear in the software itself,
21 * if and wherever such third-party acknowledgments normally appear.
22 *
23 * 4. The names "Parc", "RHI", and "Robert Half International" must
24 * not be used to endorse or promote products derived from this
25 * software without prior written permission. For written
26 * permission, please contact pete.mckinstry@rhi.com.
27 *
28 * 5. Products derived from this software may not be called "PARC",
29 * nor may "PARC" appear in their name, without prior written
30 * permission of Robert Half International.
31 *
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 * ====================================================================
45 *
46 */
47 package com.rhi.architecture.lang;
48
49 import com.rhi.architecture.logging.*;
50
51 import java.util.*;
52
53 import junit.framework.*;
54
55
56 /***
57 * StringUtilsTest
58 *
59 * @author Pete McKinstry
60 * @version 1.0
61 */
62 public class StringUtilsTest extends TestCase {
63
64 private static Logger log = new DefaultLogger();
65
66 private static String longEvaluationString =
67 "Short Strings Perform Similarly To The JDK Version, " +
68 "While Longer Strings Are Much Better With The Optimized " +
69 "Method.";
70 private static String shortString = "Pete McKinstry";
71 private static String shortUpper = "PETE MCKINSTRY";
72 private static String shortLower = "pete mckinstry";
73
74 /***
75 * Constructor for StringUtilsTest.
76 * @param name
77 */
78 public StringUtilsTest(String name) {
79 super(name);
80 }
81
82 /***
83 * @throws Exception
84 */
85 protected void setUp() throws Exception {
86 super.setUp();
87 }
88
89 /***
90 * @throws Exception
91 */
92 protected void tearDown() throws Exception {
93 super.tearDown();
94 }
95
96 /***
97 * test mixed case conversion.
98 * @throws Exception
99 */
100 public void testMixedCase() throws Exception {
101 String eval = shortString;
102 String upper = StringUtils.toUpperCase(eval);
103 String lower = StringUtils.toLowerCase(eval);
104 assertTrue("toUpperCase() doesn't work.",
105 upper.equals(shortUpper));
106 assertTrue("toLowerCase() doesn't work.",
107 lower.equals(shortLower));
108 }
109
110 /***
111 * test toLower function
112 * @throws Exception
113 */
114 public void testLower() throws Exception {
115 String eval = shortLower;
116 String upper = StringUtils.toUpperCase(eval);
117 String lower = StringUtils.toLowerCase(eval);
118 assertTrue("toUpperCase() doesn't work.",
119 upper.equals(shortUpper));
120 assertTrue("toLowerCase() doesn't work.",
121 lower.equals(shortLower));
122 }
123
124 /***
125 * test toUpper function.
126 * @throws Exception
127 */
128 public void testUpper() throws Exception {
129 String eval = shortUpper;
130 String upper = StringUtils.toUpperCase(eval);
131 String lower = StringUtils.toLowerCase(eval);
132 assertTrue("toUpperCase() doesn't work.",
133 upper.equals(shortUpper));
134 assertTrue("toLowerCase() doesn't work.",
135 lower.equals(shortLower));
136 }
137
138 /***
139 * test toUpper performance
140 * @throws Exception
141 */
142 public void testToUpperPerformance() throws Exception {
143 String eval = longEvaluationString;
144 ArrayList list = new ArrayList();
145 long startTime = System.currentTimeMillis();
146 // force loop to be unoptimized.
147 for (int i=0;i< 10000; ++i) {
148 list.add(StringUtils.toUpperCase(eval));
149 }
150 long endTime = System.currentTimeMillis();
151 long fastTime = endTime-startTime;
152
153 eval = longEvaluationString;
154 ArrayList list2 = new ArrayList();
155 startTime = System.currentTimeMillis();
156 // force loop to be unoptimized.
157 for (int i=0;i< 10000; ++i) {
158 list2.add(eval.toUpperCase());
159 }
160 endTime = System.currentTimeMillis();
161 long slowTime = endTime-startTime;
162
163 log.debug("fastTime = " + fastTime);
164 log.debug("slowTime = " + slowTime);
165 assertTrue("Performance is no better than JDK version",
166 fastTime < slowTime );
167 }
168
169 /***
170 * test toLower performance.
171 * @throws Exception
172 */
173 public void testToLowerPerformance() throws Exception {
174 String eval = longEvaluationString;
175 ArrayList list = new ArrayList();
176 long startTime = System.currentTimeMillis();
177 // force loop to be unoptimized.
178 for (int i=0;i< 10000; ++i) {
179 list.add(StringUtils.toLowerCase(eval));
180 }
181 long endTime = System.currentTimeMillis();
182 long fastTime = endTime-startTime;
183
184 eval = longEvaluationString;
185 ArrayList list2 = new ArrayList();
186 startTime = System.currentTimeMillis();
187 // force loop to be unoptimized.
188 for (int i=0;i< 10000; ++i) {
189 list2.add(eval.toLowerCase());
190 }
191 endTime = System.currentTimeMillis();
192 long slowTime = endTime-startTime;
193
194 log.debug("fastTime = " + fastTime);
195 log.debug("slowTime = " + slowTime);
196 assertTrue("Performance is no better than JDK version",
197 fastTime < slowTime );
198 }
199
200 /***
201 * Suite method.
202 * @return Test suite of all tests.
203 */
204 public static Test suite() {
205 return new TestSuite(StringUtilsTest.class);
206 }
207
208 /***
209 * Convenience method
210 * @param args[] - cmdline arguments.
211 */
212 public static void main(String args[]) {
213 junit.textui.TestRunner.run(suite());
214 }
215 }
This page was automatically generated by Maven