### Question:
RandomValueGenerator { public static String generate(final String generator) { if (generator == null) { throw new IllegalArgumentException("Generator cannot be null"); } final int split = generator.indexOf(':'); if (split < 0) { return generate(generator, null); } return generate(generator.substring(0, split), generator.substring(split + 1)); } private  RandomValueGenerator(); static String generate(final String generator);  }### Answer:
@Test public void testNegativeValue() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> RandomValueGenerator.generate("alphanum:-1")) .withMessage("Cannot generate a string of negative length"); }
@Test public void testPatternRespectedWithDefault() { for (int i = 0; i < 20; i++) { final String value = RandomValueGenerator.generate("alphanum"); assertThat(value).matches("[A-Za-z0-9]{40}"); } }
@Test public void testPatternRespectedWithLength() { for (int i = 0; i < 20; i++) { final String value = RandomValueGenerator.generate("alphanum:12"); assertThat(value).matches("[A-Za-z0-9]{12}"); } }
@Test public void testTrueRandom() { final Set<String> gen = new HashSet<>(); for (int i = 0; i < 20; i++) { final String value = RandomValueGenerator.generate("alphanum:80"); assertThat(value).matches("[A-Za-z0-9]{80}"); gen.add(value); } assertThat(gen.size()).isGreaterThan(10); }
@Test public void testUnknownGenerator() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> RandomValueGenerator.generate("alphanumx")) .withMessage("Unsupported generator scheme: alphanumx"); }
@Test public void testWrongValueType() { assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> RandomValueGenerator.generate("alphanum:aa")) .withMessage("Unexpected string after the alphanum scheme: expected length"); }
@Test public void testZeroValue() { final String value = RandomValueGenerator.generate("alphanum:0"); assertThat(value).isEqualTo(""); }