lots of exercises in java... from https://github.com/exercism/java

RandomKeyCipherTest.java 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import org.junit.Before;
  2. import org.junit.Test;
  3. import static org.junit.Assert.assertEquals;
  4. import static org.junit.Assert.assertTrue;
  5. public class RandomKeyCipherTest {
  6. private Cipher cipher;
  7. @Before
  8. public void setup() {
  9. this.cipher = new Cipher();
  10. }
  11. @Test
  12. public void cipherKeyIsMadeOfLetters() {
  13. assertTrue(cipher.getKey().matches("[a-z]+"));
  14. }
  15. @Test
  16. public void defaultCipherKeyIs100Characters() {
  17. assertEquals(100, cipher.getKey().length());
  18. }
  19. @Test
  20. public void cipherKeysAreRandomlyGenerated() {
  21. assertTrue(!(new Cipher().getKey().equals(cipher.getKey())));
  22. }
  23. /**
  24. * Here we take advantage of the fact that plaintext of "aaa..." doesn't output the key. This is a critical problem
  25. * with shift ciphers, some characters will always output the key verbatim.
  26. */
  27. @Test
  28. public void cipherCanEncode() {
  29. String expectedOutput = cipher.getKey().substring(0, 10);
  30. assertEquals(expectedOutput, cipher.encode("aaaaaaaaaa"));
  31. }
  32. @Test
  33. public void cipherCanDecode() {
  34. String expectedOutput = "aaaaaaaaaa";
  35. assertEquals(expectedOutput, cipher.decode(cipher.getKey().substring(0, 10)));
  36. }
  37. @Test
  38. public void cipherIsReversible() {
  39. String plainText = "abcdefghij";
  40. assertEquals(plainText, cipher.decode(cipher.encode(plainText)));
  41. }
  42. }