Signer { public synchronized String sign(String str) { if (str == null || str.length() == 0) { throw new IllegalArgumentException("NULL or empty string to sign"); } byte[] secret = secretProvider.getCurrentSecret(); String signature = computeSignature(secret, str); return str + SIGNATURE + signature; } Signer(SignerSecretProvider secretProvider); synchronized String sign(String str); String verifyAndExtract(String signedStr);  }
@Test public void testNullAndEmptyString() throws Exception { Signer signer = new Signer(createStringSignerSecretProvider()); try { signer.sign(null); Assert.fail(); } catch (IllegalArgumentException ex) { } catch (Throwable ex) { Assert.fail(); } try { signer.sign(""); Assert.fail(); } catch (IllegalArgumentException ex) { } catch (Throwable ex) { Assert.fail(); } }
@Test public void testSignature() throws Exception { Signer signer = new Signer(createStringSignerSecretProvider()); String s1 = signer.sign("ok"); String s2 = signer.sign("ok"); String s3 = signer.sign("wrong"); Assert.assertEquals(s1, s2); Assert.assertNotEquals(s1, s3); }