1 16 package org.apache.commons.lang; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.Modifier ; 20 import java.util.Random ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 34 public class RandomStringUtilsTest extends junit.framework.TestCase { 35 38 public RandomStringUtilsTest(String 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 51 public void setUp() { 52 } 53 54 57 public void tearDown() { 58 } 59 60 public void testConstructor() { 62 assertNotNull(new RandomStringUtils()); 63 Constructor [] 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 74 public void testRandomStringUtils() { 75 String r1 = RandomStringUtils.random(50); 76 assertEquals("random(50) length", 50, r1.length()); 77 String 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 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 ) null); 123 assertEquals("random(50) length", 50, r1.length()); 124 r2 = RandomStringUtils.random(50, (String ) 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 (seed)); 145 r2 = RandomStringUtils.random(50,0,0,true,true,null,new Random (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 ex) {} 157 try { 158 RandomStringUtils.random(-1, true, true); 159 fail(); 160 } catch (IllegalArgumentException ex) {} 161 try { 162 RandomStringUtils.random(-1, new char[0]); 163 fail(); 164 } catch (IllegalArgumentException ex) {} 165 try { 166 RandomStringUtils.random(-1, ""); 167 fail(); 168 } catch (IllegalArgumentException ex) {} 169 try { 170 RandomStringUtils.random(-1, 'a', 'z', false, false); 171 fail(); 172 } catch (IllegalArgumentException ex) {} 173 try { 174 RandomStringUtils.random(-1, 'a', 'z', false, false, new char[0]); 175 fail(); 176 } catch (IllegalArgumentException ex) {} 177 try { 178 RandomStringUtils.random(-1, 'a', 'z', false, false, new char[0], new Random ()); 179 fail(); 180 } catch (IllegalArgumentException ex) {} 181 } 182 183 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 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 210 public void testRandomNumeric() { 211 char[] testChars = {'0','9'}; 212 boolean[] found = {false, false}; 213 for (int i = 0; i < 100; i++) { 214 String 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 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 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 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 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 282 public void testRandomStringUtilsHomog() { 283 String set = "abc"; 284 char[] chars = set.toCharArray(); 285 String 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 assertTrue("test homogeneity -- will fail about 1 in 1000 times", 301 chiSquare(expected,counts) < 13.82); 302 } 303 304 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 args[]) { 321 TestRunner.run(suite()); 322 } 323 } 324 325 | Popular Tags |