|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
5 |
| -import org.junit.jupiter.api.BeforeEach; |
6 |
| -import org.junit.jupiter.api.Test; |
| 5 | +import java.util.stream.Stream; |
7 | 6 |
|
8 |
| -public class MonoAlphabeticTest { |
9 |
| - private MonoAlphabetic monoAlphabetic; |
10 |
| - |
11 |
| - @BeforeEach |
12 |
| - public void setUp() { |
13 |
| - // Initialize the MonoAlphabetic cipher with a sample key |
14 |
| - String key = "QWERTYUIOPASDFGHJKLZXCVBNM"; // Example key |
15 |
| - monoAlphabetic = new MonoAlphabetic(key); |
16 |
| - } |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
17 | 10 |
|
18 |
| - @Test |
19 |
| - public void testEncrypt() { |
20 |
| - String plaintext = "HELLO"; |
21 |
| - String expectedCiphertext = "ITSSG"; // Expected result based on the key |
22 |
| - String actualCiphertext = monoAlphabetic.encrypt(plaintext); |
23 |
| - |
24 |
| - assertEquals(expectedCiphertext, actualCiphertext, "Encryption should match the expected ciphertext."); |
25 |
| - } |
| 11 | +public class MonoAlphabeticTest { |
26 | 12 |
|
27 |
| - @Test |
28 |
| - public void testDecrypt() { |
29 |
| - String ciphertext = "ITSSG"; |
30 |
| - String expectedPlaintext = "HELLO"; // Expected result based on the key |
31 |
| - String actualPlaintext = monoAlphabetic.decrypt(ciphertext); |
| 13 | + private static final String key = "MNBVCXZLKJHGFDSAPOIUYTREWQ"; |
32 | 14 |
|
33 |
| - assertEquals(expectedPlaintext, actualPlaintext, "Decryption should match the expected plaintext."); |
| 15 | + // Test for encryption |
| 16 | + @ParameterizedTest |
| 17 | + @MethodSource("provideEncryptionData") |
| 18 | + public void testEncrypt(String data, String key, String expected) { |
| 19 | + assertEquals(expected, MonoAlphabetic.encrypt(data, key)); |
34 | 20 | }
|
35 | 21 |
|
36 |
| - @Test |
37 |
| - public void testEncryptAndDecrypt() { |
38 |
| - String plaintext = "HELLO"; |
39 |
| - String ciphertext = monoAlphabetic.encrypt(plaintext); |
40 |
| - String decryptedText = monoAlphabetic.decrypt(ciphertext); |
41 |
| - |
42 |
| - assertEquals(plaintext, decryptedText, "Decrypting the ciphertext should return the original plaintext."); |
| 22 | + // Test for decryption |
| 23 | + @ParameterizedTest |
| 24 | + @MethodSource("provideDecryptionData") |
| 25 | + public void testDecrypt(String data, String key, String expected) { |
| 26 | + assertEquals(expected, MonoAlphabetic.decrypt(data, key)); |
43 | 27 | }
|
44 | 28 |
|
45 |
| - @Test |
46 |
| - public void testEncryptWithSpecialCharacters() { |
47 |
| - String plaintext = "HELLO, WORLD!"; |
48 |
| - String expectedCiphertext = "ITSSG, GQHSG!"; |
49 |
| - String actualCiphertext = monoAlphabetic.encrypt(plaintext); |
50 |
| - |
51 |
| - assertEquals(expectedCiphertext, actualCiphertext, "Encryption should correctly handle special characters."); |
| 29 | + // Provide test data for encryption |
| 30 | + private static Stream<Arguments> provideEncryptionData() { |
| 31 | + return Stream.of( |
| 32 | + Arguments.of("HELLO", key, "GFSSD"), |
| 33 | + Arguments.of("JAVA", key, "MZSM") |
| 34 | + ); |
52 | 35 | }
|
53 | 36 |
|
54 |
| - @Test |
55 |
| - public void testDecryptWithSpecialCharacters() { |
56 |
| - String ciphertext = "ITSSG, GQHSG!"; |
57 |
| - String expectedPlaintext = "HELLO, WORLD!"; |
58 |
| - String actualPlaintext = monoAlphabetic.decrypt(ciphertext); |
59 |
| - |
60 |
| - assertEquals(expectedPlaintext, actualPlaintext, "Decryption should correctly handle special characters."); |
| 37 | + // Provide test data for decryption |
| 38 | + private static Stream<Arguments> provideDecryptionData() { |
| 39 | + return Stream.of( |
| 40 | + Arguments.of("GFSSD", key, "HELLO"), |
| 41 | + Arguments.of("MZSM", key, "JAVA") |
| 42 | + ); |
61 | 43 | }
|
62 | 44 | }
|
0 commit comments