KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20 import java.util.Random JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25
26 /**
27  * Unit tests {@link org.apache.commons.lang.RandomStringUtils}.
28  *
29  * @author <a HREF="mailto:steven@caswell.name">Steven Caswell</a>
30  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
31  * @author Phil Steitz
32  * @version $Id: RandomStringUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
33  */

34 public class RandomStringUtilsTest extends junit.framework.TestCase {
35     /**
36      * Construct a new instance of RandomStringUtilsTest with the specified name
37      */

38     public RandomStringUtilsTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public static Test suite() {
43         TestSuite suite = new TestSuite(RandomStringUtilsTest.class);
44         suite.setName("RandomStringUtils Tests");
45         return suite;
46     }
47     
48     /**
49      * Set up instance variables required by this test case.
50      */

51     public void setUp() {
52     }
53     
54     /**
55      * Tear down instance variables required by this test case.
56      */

57     public void tearDown() {
58     }
59     
60     //-----------------------------------------------------------------------
61
public void testConstructor() {
62         assertNotNull(new RandomStringUtils());
63         Constructor JavaDoc[] cons = RandomStringUtils.class.getDeclaredConstructors();
64         assertEquals(1, cons.length);
65         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
66         assertEquals(true, Modifier.isPublic(RandomStringUtils.class.getModifiers()));
67         assertEquals(false, Modifier.isFinal(RandomStringUtils.class.getModifiers()));
68     }
69     
70     //-----------------------------------------------------------------------
71
/**
72      * Test the implementation
73      */

74     public void testRandomStringUtils() {
75         String JavaDoc r1 = RandomStringUtils.random(50);
76         assertEquals("random(50) length", 50, r1.length());
77         String JavaDoc r2 = RandomStringUtils.random(50);
78         assertEquals("random(50) length", 50, r2.length());
79         assertTrue("!r1.equals(r2)", !r1.equals(r2));
80         
81         r1 = RandomStringUtils.randomAscii(50);
82         assertEquals("randomAscii(50) length", 50, r1.length());
83         for(int i = 0; i < r1.length(); i++) {
84             assertTrue("char between 32 and 127", (int) r1.charAt(i) >= 32 && (int) r1.charAt(i) <= 127);
85         }
86         r2 = RandomStringUtils.randomAscii(50);
87         assertTrue("!r1.equals(r2)", !r1.equals(r2));
88
89         r1 = RandomStringUtils.randomAlphabetic(50);
90         assertEquals("randomAlphabetic(50)", 50, r1.length());
91         for(int i = 0; i < r1.length(); i++) {
92             assertEquals("r1 contains alphabetic", true, Character.isLetter(r1.charAt(i)) && !Character.isDigit(r1.charAt(i)));
93         }
94         r2 = RandomStringUtils.randomAlphabetic(50);
95         assertTrue("!r1.equals(r2)", !r1.equals(r2));
96         
97         r1 = RandomStringUtils.randomAlphanumeric(50);
98         assertEquals("randomAlphanumeric(50)", 50, r1.length());
99         for(int i = 0; i < r1.length(); i++) {
100             assertEquals("r1 contains alphanumeric", true, Character.isLetterOrDigit(r1.charAt(i)));
101         }
102         r2 = RandomStringUtils.randomAlphabetic(50);
103         assertTrue("!r1.equals(r2)", !r1.equals(r2));
104         
105         r1 = RandomStringUtils.randomNumeric(50);
106         assertEquals("randomNumeric(50)", 50, r1.length());
107         for(int i = 0; i < r1.length(); i++) {
108             assertEquals("r1 contains numeric", true, Character.isDigit(r1.charAt(i)) && !Character.isLetter(r1.charAt(i)));
109         }
110         r2 = RandomStringUtils.randomNumeric(50);
111         assertTrue("!r1.equals(r2)", !r1.equals(r2));
112         
113         String JavaDoc set = "abcdefg";
114         r1 = RandomStringUtils.random(50, set);
115         assertEquals("random(50, \"abcdefg\")", 50, r1.length());
116         for(int i = 0; i < r1.length(); i++) {
117             assertTrue("random char in set", set.indexOf(r1.charAt(i)) > -1);
118         }
119         r2 = RandomStringUtils.random(50, set);
120         assertTrue("!r1.equals(r2)", !r1.equals(r2));
121         
122         r1 = RandomStringUtils.random(50, (String JavaDoc) null);
123         assertEquals("random(50) length", 50, r1.length());
124         r2 = RandomStringUtils.random(50, (String JavaDoc) null);
125         assertEquals("random(50) length", 50, r2.length());
126         assertTrue("!r1.equals(r2)", !r1.equals(r2));
127         
128         set = "stuvwxyz";
129         r1 = RandomStringUtils.random(50, set.toCharArray());
130         assertEquals("random(50, \"stuvwxyz\")", 50, r1.length());
131         for(int i = 0; i < r1.length(); i++) {
132             assertTrue("random char in set", set.indexOf(r1.charAt(i)) > -1);
133         }
134         r2 = RandomStringUtils.random(50, set);
135         assertTrue("!r1.equals(r2)", !r1.equals(r2));
136         
137         r1 = RandomStringUtils.random(50, (char[]) null);
138         assertEquals("random(50) length", 50, r1.length());
139         r2 = RandomStringUtils.random(50, (char[]) null);
140         assertEquals("random(50) length", 50, r2.length());
141         assertTrue("!r1.equals(r2)", !r1.equals(r2));
142
143         long seed = System.currentTimeMillis();
144         r1 = RandomStringUtils.random(50,0,0,true,true,null,new Random JavaDoc(seed));
145         r2 = RandomStringUtils.random(50,0,0,true,true,null,new Random JavaDoc(seed));
146         assertEquals("r1.equals(r2)", r1, r2);
147
148         r1 = RandomStringUtils.random(0);
149         assertEquals("random(0).equals(\"\")", "", r1);
150
151     }
152     public void testExceptions() {
153         try {
154             RandomStringUtils.random(-1);
155             fail();
156         } catch (IllegalArgumentException JavaDoc ex) {}
157         try {
158             RandomStringUtils.random(-1, true, true);
159             fail();
160         } catch (IllegalArgumentException JavaDoc ex) {}
161         try {
162             RandomStringUtils.random(-1, new char[0]);
163             fail();
164         } catch (IllegalArgumentException JavaDoc ex) {}
165         try {
166             RandomStringUtils.random(-1, "");
167             fail();
168         } catch (IllegalArgumentException JavaDoc ex) {}
169         try {
170             RandomStringUtils.random(-1, 'a', 'z', false, false);
171             fail();
172         } catch (IllegalArgumentException JavaDoc ex) {}
173         try {
174             RandomStringUtils.random(-1, 'a', 'z', false, false, new char[0]);
175             fail();
176         } catch (IllegalArgumentException JavaDoc ex) {}
177         try {
178             RandomStringUtils.random(-1, 'a', 'z', false, false, new char[0], new Random JavaDoc());
179             fail();
180         } catch (IllegalArgumentException JavaDoc ex) {}
181     }
182     
183     /**
184      * Make sure boundary alphanumeric characters are generated by randomAlphaNumeric
185      * This test will fail randomly with probability = 6 * (61/62)**1000 ~ 5.2E-7
186      */

187     public void testRandomAlphaNumeric() {
188         char[] testChars = {'a', 'z', 'A', 'Z', '0', '9'};
189         boolean[] found = {false, false, false, false, false, false};
190         for (int i = 0; i < 100; i++) {
191             String JavaDoc randString = RandomStringUtils.randomAlphanumeric(10);
192             for (int j = 0; j < testChars.length; j++) {
193                 if (randString.indexOf(testChars[j]) > 0) {
194                     found[j] = true;
195                 }
196             }
197         }
198         for (int i = 0; i < testChars.length; i++) {
199             if (!found[i]) {
200                 fail("alphanumeric character not generated in 1000 attempts: "
201                    + testChars[i] +" -- repeated failures indicate a problem ");
202             }
203         }
204     }
205     
206     /**
207      * Make sure '0' and '9' are generated by randomNumeric
208      * This test will fail randomly with probability = 2 * (9/10)**1000 ~ 3.5E-46
209      */

210     public void testRandomNumeric() {
211         char[] testChars = {'0','9'};
212         boolean[] found = {false, false};
213         for (int i = 0; i < 100; i++) {
214             String JavaDoc randString = RandomStringUtils.randomNumeric(10);
215             for (int j = 0; j < testChars.length; j++) {
216                 if (randString.indexOf(testChars[j]) > 0) {
217                     found[j] = true;
218                 }
219             }
220         }
221         for (int i = 0; i < testChars.length; i++) {
222             if (!found[i]) {
223                 fail("digit not generated in 1000 attempts: "
224                    + testChars[i] +" -- repeated failures indicate a problem ");
225             }
226         }
227     }
228     
229     /**
230      * Make sure boundary alpha characters are generated by randomAlphabetic
231      * This test will fail randomly with probability = 4 * (51/52)**1000 ~ 1.58E-8
232      */

233     public void testRandomAlphabetic() {
234         char[] testChars = {'a', 'z', 'A', 'Z'};
235         boolean[] found = {false, false, false, false};
236         for (int i = 0; i < 100; i++) {
237             String JavaDoc randString = RandomStringUtils.randomAlphabetic(10);
238             for (int j = 0; j < testChars.length; j++) {
239                 if (randString.indexOf(testChars[j]) > 0) {
240                     found[j] = true;
241                 }
242             }
243         }
244         for (int i = 0; i < testChars.length; i++) {
245             if (!found[i]) {
246                 fail("alphanumeric character not generated in 1000 attempts: "
247                    + testChars[i] +" -- repeated failures indicate a problem ");
248             }
249         }
250     }
251     
252     /**
253      * Make sure 32 and 127 are generated by randomNumeric
254      * This test will fail randomly with probability = 2*(95/96)**1000 ~ 5.7E-5
255      */

256     public void testRandomAscii() {
257         char[] testChars = {(char) 32, (char) 126};
258         boolean[] found = {false, false};
259         for (int i = 0; i < 100; i++) {
260             String JavaDoc randString = RandomStringUtils.randomAscii(10);
261             for (int j = 0; j < testChars.length; j++) {
262                 if (randString.indexOf(testChars[j]) > 0) {
263                     found[j] = true;
264                 }
265             }
266         }
267         for (int i = 0; i < testChars.length; i++) {
268             if (!found[i]) {
269                 fail("ascii character not generated in 1000 attempts: "
270                 + (int) testChars[i] +
271                  " -- repeated failures indicate a problem");
272             }
273         }
274     }
275     
276     /**
277      * Test homogeneity of random strings generated --
278      * i.e., test that characters show up with expected frequencies
279      * in generated strings. Will fail randomly about 1 in 1000 times.
280      * Repeated failures indicate a problem.
281      */

282     public void testRandomStringUtilsHomog() {
283         String JavaDoc set = "abc";
284         char[] chars = set.toCharArray();
285         String JavaDoc gen = "";
286         int[] counts = {0,0,0};
287         int[] expected = {200,200,200};
288         for (int i = 0; i< 100; i++) {
289            gen = RandomStringUtils.random(6,chars);
290            for (int j = 0; j < 6; j++) {
291                switch (gen.charAt(j)) {
292                    case 'a': {counts[0]++; break;}
293                    case 'b': {counts[1]++; break;}
294                    case 'c': {counts[2]++; break;}
295                    default: {fail("generated character not in set");}
296                }
297            }
298         }
299         // Perform chi-square test with df = 3-1 = 2, testing at .001 level
300
assertTrue("test homogeneity -- will fail about 1 in 1000 times",
301             chiSquare(expected,counts) < 13.82);
302     }
303     
304     /**
305      * Computes Chi-Square statistic given observed and expected counts
306      * @param observed array of observed frequency counts
307      * @param expected array of expected frequency counts
308      */

309     private double chiSquare(int[] expected, int[] observed) {
310         double sumSq = 0.0d;
311         double dev = 0.0d;
312         for (int i = 0; i< observed.length; i++) {
313             dev = (double)(observed[i] - expected[i]);
314             sumSq += dev*dev/(double)expected[i];
315         }
316         return sumSq;
317     }
318         
319
320     public static void main(String JavaDoc args[]) {
321         TestRunner.run(suite());
322     }
323 }
324
325
Popular Tags