CommonsLang3RandomStringUtils { public static String random(final int count) { return random(count, false, false); } CommonsLang3RandomStringUtils(); static String random(final int count); static String randomAscii(final int count); static String randomAlphabetic(final int count); static String randomAlphanumeric(final int count); static String randomNumeric(final int count); static String random(final int count, final boolean letters, final boolean numbers); static String random(final int count, final int start, final int end, final boolean letters, final boolean numbers); static String random(final int count, final int start, final int end, final boolean letters, final boolean numbers, final char... chars); static String random(int count, int start, int end, final boolean letters, final boolean numbers,
                                final char[] chars, final Random random); static String random(final int count, final String chars); static String random(final int count, final char... chars);  }
@Test public void testCommonsLang3RandomStringUtilsHomog() { final String set = "abc"; final char[] chars = set.toCharArray(); String gen; final int[] counts = {0,0,0}; final int[] expected = {200,200,200}; for (int i = 0; i< 100; i++) { gen = CommonsLang3RandomStringUtils.random(6,chars); for (int j = 0; j < 6; j++) { switch (gen.charAt(j)) { case 'a': {counts[0]++; break;} case 'b': {counts[1]++; break;} case 'c': {counts[2]++; break;} default: {fail("generated character not in set");} } } } assertTrue(chiSquare(expected,counts) < 13.82, "test homogeneity -- will fail about 1 in 1000 times"); }
@Test public void testLang100() { final int size = 5000; final Charset charset = Charset.forName("UTF-8"); final String orig = CommonsLang3RandomStringUtils.random(size); final byte[] bytes = orig.getBytes(charset); final String copy = new String(bytes, charset); for (int i=0; i < orig.length() && i < copy.length(); i++) { final char o = orig.charAt(i); final char c = copy.charAt(i); assertEquals(o, c, "differs at " + i + "(" + Integer.toHexString(new Character(o).hashCode()) + "," + Integer.toHexString(new Character(c).hashCode()) + ")"); } assertEquals(orig.length(), copy.length()); assertEquals(orig, copy); }
@Test public void testLANG805() { final long seed = System.currentTimeMillis(); assertEquals("aaa", CommonsLang3RandomStringUtils.random(3,0,0,false,false,new char[]{'a'},new Random(seed))); }
@Test public void testLANG807() { try { CommonsLang3RandomStringUtils.random(3,5,5,false,false); fail("Expected IllegalArgumentException"); } catch (final IllegalArgumentException ex) { final String msg = ex.getMessage(); assertTrue(msg.contains("start"), "Message (" + msg + ") must contain 'start'"); assertTrue(msg.contains("end"), "Message (" + msg + ") must contain 'end'"); } }
@Test public void testExceptions() { final char[] DUMMY = new char[]{'a'}; try { CommonsLang3RandomStringUtils.random(-1); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(-1, true, true); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(-1, DUMMY); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(1, new char[0]); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(-1, ""); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(-1, (String)null); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(-1, 'a', 'z', false, false); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY); fail(""); } catch (final IllegalArgumentException ex) {} try { CommonsLang3RandomStringUtils.random(-1, 'a', 'z', false, false, DUMMY, new Random()); fail(""); } catch (final IllegalArgumentException ex) {} }