TokenStringGenerator { public String generate(final String seed) { if (seed == null) { throw new IllegalArgumentException("seed must not be null"); } long time = System.currentTimeMillis(); StringBuilder sb = new StringBuilder(1000); sb.append(internalSeed).append(seed).append(time).append(counter .getAndIncrement()); MessageDigest md = createMessageDigest(); md.update(sb.toString().getBytes()); byte[] bytes = md.digest(); return toHexString(bytes); } TokenStringGenerator();  TokenStringGenerator(final String algorithm); String generate(final String seed); static final String DEFAULT_ALGORITHM; }
@Test public void testGenerate_defaultMD5() { TokenStringGenerator generator = new TokenStringGenerator(); String value = generator.generate("hoge"); assertThat(value, is(notNullValue())); assertThat(value.length(), is(32)); }
@Test public void testGenerate_defaultMD5_empty() { TokenStringGenerator generator = new TokenStringGenerator(); String value = generator.generate(""); assertThat(value, is(notNullValue())); assertThat(value.length(), is(32)); }
@Test public void testGenerate_SHA256() { TokenStringGenerator generator = new TokenStringGenerator("SHA-256"); String value = generator.generate("hoge"); assertThat(value, is(notNullValue())); assertThat(value.length(), is(64)); }
@Test(expected = IllegalArgumentException.class) public void testGenerate_nullValue() throws Exception { TokenStringGenerator generator = new TokenStringGenerator(); try { generator.generate(null); } catch (Exception e) { assertThat(e.getMessage(), is("seed must not be null")); throw e; } }