|
@@ -0,0 +1,142 @@
|
|
1
|
+import org.junit.jupiter.api.Test;
|
|
2
|
+
|
|
3
|
+import java.io.IOException;
|
|
4
|
+
|
|
5
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
6
|
+
|
|
7
|
+public class TestCaesar {
|
|
8
|
+
|
|
9
|
+ @Test
|
|
10
|
+ public void encryptTest1() {
|
|
11
|
+ // Given
|
|
12
|
+ Caesar caesar = new Caesar('A', 'X');
|
|
13
|
+
|
|
14
|
+ String Q1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZj";
|
|
15
|
+ String A1 = "XYZABCDEFGHIJKLMNOPQRSTUVWg";
|
|
16
|
+
|
|
17
|
+ // When
|
|
18
|
+ String actual = caesar.encrypt(Q1);
|
|
19
|
+
|
|
20
|
+ // Then
|
|
21
|
+ assertEquals(A1, actual);
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ @Test
|
|
25
|
+ public void encryptTest2() {
|
|
26
|
+ // Given
|
|
27
|
+ Caesar caesar = new Caesar('a', 'b');
|
|
28
|
+
|
|
29
|
+ String Q1 = "!defend the east wall of the castle?";
|
|
30
|
+ String A1 = "!efgfoe uif fbtu xbmm pg uif dbtumf?";
|
|
31
|
+
|
|
32
|
+ // When
|
|
33
|
+ String actual = caesar.encrypt(Q1);
|
|
34
|
+
|
|
35
|
+ // Then
|
|
36
|
+ assertEquals(A1, actual);
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+ @Test
|
|
40
|
+ public void encryptTest3() {
|
|
41
|
+ // Given
|
|
42
|
+ Caesar caesar = new Caesar('a', 'x');
|
|
43
|
+
|
|
44
|
+ String Q1 = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD";
|
|
45
|
+ String A1 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
|
|
46
|
+
|
|
47
|
+ // When
|
|
48
|
+ String actual = caesar.encrypt(A1);
|
|
49
|
+
|
|
50
|
+ // Then
|
|
51
|
+ assertEquals(Q1, actual);
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ @Test
|
|
55
|
+ public void decryptTest1() {
|
|
56
|
+ // Given
|
|
57
|
+ Caesar caesar = new Caesar('A', 'X');
|
|
58
|
+
|
|
59
|
+ String Q1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZj";
|
|
60
|
+ String A1 = "XYZABCDEFGHIJKLMNOPQRSTUVWg";
|
|
61
|
+
|
|
62
|
+ // When
|
|
63
|
+ String actual = caesar.decrypt(A1);
|
|
64
|
+
|
|
65
|
+ // Then
|
|
66
|
+ assertEquals(Q1, actual);
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ @Test
|
|
70
|
+ public void decryptTest2() {
|
|
71
|
+ // Given
|
|
72
|
+ Caesar caesar = new Caesar('a', 'x');
|
|
73
|
+
|
|
74
|
+ String Q1 = "QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD";
|
|
75
|
+ String A1 = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
|
|
76
|
+
|
|
77
|
+ // When
|
|
78
|
+ String actual = caesar.decrypt(Q1);
|
|
79
|
+
|
|
80
|
+ // Then
|
|
81
|
+ assertEquals(A1, actual);
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ @Test
|
|
85
|
+ public void cryptTest2() {
|
|
86
|
+ // Given
|
|
87
|
+ Caesar cipher = new Caesar('h', 'm');
|
|
88
|
+ String Q1 = "Why did the chicken cross the road?";
|
|
89
|
+ // When
|
|
90
|
+ String actual = cipher.decrypt(cipher.encrypt(cipher.decrypt(cipher.encrypt(Q1))));
|
|
91
|
+ // Then
|
|
92
|
+ assertTrue(actual.equals(Q1));
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ @Test
|
|
96
|
+ public void testEncryptFile() throws IOException {
|
|
97
|
+ // Given
|
|
98
|
+ Caesar cipher = new Caesar('a', 'f');
|
|
99
|
+ String Q1 = "Xmfqq N htrufwj ymjj yt f xzrrjw’x ifd?\n" +
|
|
100
|
+ "Ymtz fwy rtwj qtajqd fsi rtwj yjrujwfyj:\n" +
|
|
101
|
+ "Wtzlm bnsix it xmfpj ymj ifwqnsl gzix tk Rfd,\n" +
|
|
102
|
+ "Fsi xzrrjw’x qjfxj mfym fqq ytt xmtwy f ifyj;\n" +
|
|
103
|
+ "Xtrjynrj ytt mty ymj jdj tk mjfajs xmnsjx,\n" +
|
|
104
|
+ "Fsi tkyjs nx mnx ltqi htruqjcnts inrr'i;\n" +
|
|
105
|
+ "Fsi jajwd kfnw kwtr kfnw xtrjynrj ijhqnsjx,\n" +
|
|
106
|
+ "Gd hmfshj tw sfyzwj’x hmfslnsl htzwxj zsywnrr'i;\n" +
|
|
107
|
+ "Gzy ymd jyjwsfq xzrrjw xmfqq sty kfij,\n" +
|
|
108
|
+ "Stw qtxj utxxjxxnts tk ymfy kfnw ymtz tb’xy;\n" +
|
|
109
|
+ "Stw xmfqq ijfym gwfl ymtz bfsijw’xy ns mnx xmfij,\n" +
|
|
110
|
+ "Bmjs ns jyjwsfq qnsjx yt ynrj ymtz lwtb’xy:\n" +
|
|
111
|
+ " Xt qtsl fx rjs hfs gwjfymj tw jdjx hfs xjj,\n" +
|
|
112
|
+ " Xt qtsl qnajx ymnx, fsi ymnx lnajx qnkj yt ymjj.\n";
|
|
113
|
+ // When
|
|
114
|
+ String actual = cipher.encryptFile();
|
|
115
|
+ // Then
|
|
116
|
+ assertTrue(actual.equals(Q1));
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ @Test
|
|
120
|
+ public void testDecryptFile() throws IOException {
|
|
121
|
+ // Given
|
|
122
|
+ Caesar cipher = new Caesar('b', 'g');
|
|
123
|
+ String Q1 = "Shall I compare thee to a summer’s day?\n" +
|
|
124
|
+ "Thou art more lovely and more temperate:\n" +
|
|
125
|
+ "Rough winds do shake the darling buds of May,\n" +
|
|
126
|
+ "And summer’s lease hath all too short a date;\n" +
|
|
127
|
+ "Sometime too hot the eye of heaven shines,\n" +
|
|
128
|
+ "And often is his gold complexion dimm'd;\n" +
|
|
129
|
+ "And every fair from fair sometime declines,\n" +
|
|
130
|
+ "By chance or nature’s changing course untrimm'd;\n" +
|
|
131
|
+ "But thy eternal summer shall not fade,\n" +
|
|
132
|
+ "Nor lose possession of that fair thou ow’st;\n" +
|
|
133
|
+ "Nor shall death brag thou wander’st in his shade,\n" +
|
|
134
|
+ "When in eternal lines to time thou grow’st:\n" +
|
|
135
|
+ " So long as men can breathe or eyes can see,\n" +
|
|
136
|
+ " So long lives this, and this gives life to thee.\n";
|
|
137
|
+ // When
|
|
138
|
+ String actual = cipher.decryptFile();
|
|
139
|
+ // Then
|
|
140
|
+ assertEquals(Q1, actual);
|
|
141
|
+ }
|
|
142
|
+}
|