|
@@ -0,0 +1,395 @@
|
|
1
|
+import org.junit.After;
|
|
2
|
+import org.junit.Before;
|
|
3
|
+import org.junit.Ignore;
|
|
4
|
+import org.junit.Test;
|
|
5
|
+
|
|
6
|
+import java.io.IOException;
|
|
7
|
+import java.nio.charset.Charset;
|
|
8
|
+import java.nio.file.Files;
|
|
9
|
+import java.nio.file.Path;
|
|
10
|
+import java.nio.file.Paths;
|
|
11
|
+import java.util.Arrays;
|
|
12
|
+import java.util.Collections;
|
|
13
|
+import java.util.List;
|
|
14
|
+
|
|
15
|
+import static org.junit.Assert.assertEquals;
|
|
16
|
+
|
|
17
|
+public class GrepToolTest {
|
|
18
|
+ private GrepTool grepTool;
|
|
19
|
+
|
|
20
|
+ @Before
|
|
21
|
+ public void setUp() throws IOException {
|
|
22
|
+ List<String> iliadText = Arrays.asList(
|
|
23
|
+ "Achilles sing, O Goddess! Peleus' son;",
|
|
24
|
+ "His wrath pernicious, who ten thousand woes",
|
|
25
|
+ "Caused to Achaia's host, sent many a soul",
|
|
26
|
+ "Illustrious into Ades premature,",
|
|
27
|
+ "And Heroes gave (so stood the will of Jove)",
|
|
28
|
+ "To dogs and to all ravening fowls a prey,",
|
|
29
|
+ "When fierce dispute had separated once",
|
|
30
|
+ "The noble Chief Achilles from the son",
|
|
31
|
+ "Of Atreus, Agamemnon, King of men."
|
|
32
|
+ );
|
|
33
|
+ writeToFile("iliad.txt", iliadText);
|
|
34
|
+
|
|
35
|
+ List<String> midsummerNightText = Arrays.asList(
|
|
36
|
+ "I do entreat your grace to pardon me.",
|
|
37
|
+ "I know not by what power I am made bold,",
|
|
38
|
+ "Nor how it may concern my modesty,",
|
|
39
|
+ "In such a presence here to plead my thoughts;",
|
|
40
|
+ "But I beseech your grace that I may know",
|
|
41
|
+ "The worst that may befall me in this case,",
|
|
42
|
+ "If I refuse to wed Demetrius."
|
|
43
|
+ );
|
|
44
|
+ writeToFile("midsummer-night.txt", midsummerNightText);
|
|
45
|
+
|
|
46
|
+ List<String> paradiseLostText = Arrays.asList(
|
|
47
|
+ "Of Mans First Disobedience, and the Fruit",
|
|
48
|
+ "Of that Forbidden Tree, whose mortal tast",
|
|
49
|
+ "Brought Death into the World, and all our woe,",
|
|
50
|
+ "With loss of Eden, till one greater Man",
|
|
51
|
+ "Restore us, and regain the blissful Seat,",
|
|
52
|
+ "Sing Heav'nly Muse, that on the secret top",
|
|
53
|
+ "Of Oreb, or of Sinai, didst inspire",
|
|
54
|
+ "That Shepherd, who first taught the chosen Seed"
|
|
55
|
+ );
|
|
56
|
+ writeToFile("paradise-lost.txt", paradiseLostText);
|
|
57
|
+
|
|
58
|
+ grepTool = new GrepTool();
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ @After
|
|
62
|
+ public void tearDown() throws IOException {
|
|
63
|
+ deleteFile("iliad.txt");
|
|
64
|
+ deleteFile("midsummer-night.txt");
|
|
65
|
+ deleteFile("paradise-lost.txt");
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ @Test
|
|
69
|
+ public void oneFileOneMatchNoFlags() {
|
|
70
|
+ String expected = "Of Atreus, Agamemnon, King of men.";
|
|
71
|
+
|
|
72
|
+ String actual = grepTool.grep(
|
|
73
|
+ "Agamemnon",
|
|
74
|
+ Collections.emptyList(),
|
|
75
|
+ Collections.singletonList("iliad.txt")
|
|
76
|
+ );
|
|
77
|
+
|
|
78
|
+ assertEquals(expected, actual);
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ @Ignore("Remove to run test")
|
|
82
|
+ @Test
|
|
83
|
+ public void oneFileOneMatchPrintLineNumbersFlag() {
|
|
84
|
+ String expected = "2:Of that Forbidden Tree, whose mortal tast";
|
|
85
|
+
|
|
86
|
+ String actual = grepTool.grep(
|
|
87
|
+ "Forbidden",
|
|
88
|
+ Collections.singletonList("-n"),
|
|
89
|
+ Collections.singletonList("paradise-lost.txt")
|
|
90
|
+ );
|
|
91
|
+
|
|
92
|
+ assertEquals(expected, actual);
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ @Ignore("Remove to run test")
|
|
96
|
+ @Test
|
|
97
|
+ public void oneFileOneMatchCaseInsensitiveFlag() {
|
|
98
|
+ String expected = "Of that Forbidden Tree, whose mortal tast";
|
|
99
|
+
|
|
100
|
+ String actual = grepTool.grep(
|
|
101
|
+ "FORBIDDEN",
|
|
102
|
+ Collections.singletonList("-i"),
|
|
103
|
+ Collections.singletonList("paradise-lost.txt")
|
|
104
|
+ );
|
|
105
|
+
|
|
106
|
+ assertEquals(expected, actual);
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ @Ignore("Remove to run test")
|
|
110
|
+ @Test
|
|
111
|
+ public void oneFileOneMatchPrintFileNamesFlag() {
|
|
112
|
+ String expected = "paradise-lost.txt";
|
|
113
|
+
|
|
114
|
+ String actual = grepTool.grep(
|
|
115
|
+ "Forbidden",
|
|
116
|
+ Collections.singletonList("-l"),
|
|
117
|
+ Collections.singletonList("paradise-lost.txt")
|
|
118
|
+ );
|
|
119
|
+
|
|
120
|
+ assertEquals(expected, actual);
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ @Ignore("Remove to run test")
|
|
124
|
+ @Test
|
|
125
|
+ public void oneFileOneMatchEntireLinesFlag() {
|
|
126
|
+ String expected = "With loss of Eden, till one greater Man";
|
|
127
|
+
|
|
128
|
+ String actual = grepTool.grep(
|
|
129
|
+ "With loss of Eden, till one greater Man",
|
|
130
|
+ Collections.singletonList("-x"),
|
|
131
|
+ Collections.singletonList("paradise-lost.txt")
|
|
132
|
+ );
|
|
133
|
+
|
|
134
|
+ assertEquals(expected, actual);
|
|
135
|
+ }
|
|
136
|
+
|
|
137
|
+ @Ignore("Remove to run test")
|
|
138
|
+ @Test
|
|
139
|
+ public void oneFileOneMatchMultipleFlags() {
|
|
140
|
+ String expected = "9:Of Atreus, Agamemnon, King of men.";
|
|
141
|
+
|
|
142
|
+ String actual = grepTool.grep(
|
|
143
|
+ "OF ATREUS, Agamemnon, KIng of MEN.",
|
|
144
|
+ Arrays.asList("-n", "-i", "-x"),
|
|
145
|
+ Collections.singletonList("iliad.txt")
|
|
146
|
+ );
|
|
147
|
+
|
|
148
|
+ assertEquals(expected, actual);
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ @Ignore("Remove to run test")
|
|
152
|
+ @Test
|
|
153
|
+ public void oneFileSeveralMatchesNoFlags() {
|
|
154
|
+ String expected = "Nor how it may concern my modesty,\n"
|
|
155
|
+ + "But I beseech your grace that I may know\n"
|
|
156
|
+ + "The worst that may befall me in this case,";
|
|
157
|
+
|
|
158
|
+ String actual = grepTool.grep(
|
|
159
|
+ "may",
|
|
160
|
+ Collections.emptyList(),
|
|
161
|
+ Collections.singletonList("midsummer-night.txt")
|
|
162
|
+ );
|
|
163
|
+
|
|
164
|
+ assertEquals(expected, actual);
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ @Ignore("Remove to run test")
|
|
168
|
+ @Test
|
|
169
|
+ public void oneFileSeveralMatchesPrintLineNumbersFlag() {
|
|
170
|
+ String expected = "3:Nor how it may concern my modesty,\n"
|
|
171
|
+ + "5:But I beseech your grace that I may know\n"
|
|
172
|
+ + "6:The worst that may befall me in this case,";
|
|
173
|
+
|
|
174
|
+ String actual = grepTool.grep(
|
|
175
|
+ "may",
|
|
176
|
+ Collections.singletonList("-n"),
|
|
177
|
+ Collections.singletonList("midsummer-night.txt")
|
|
178
|
+ );
|
|
179
|
+
|
|
180
|
+ assertEquals(expected, actual);
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ @Ignore("Remove to run test")
|
|
184
|
+ @Test
|
|
185
|
+ public void oneFileSeveralMatchesMatchEntireLineFlag() {
|
|
186
|
+ String expected = "";
|
|
187
|
+
|
|
188
|
+ String actual = grepTool.grep(
|
|
189
|
+ "may",
|
|
190
|
+ Collections.singletonList("-x"),
|
|
191
|
+ Collections.singletonList("midsummer-night.txt")
|
|
192
|
+ );
|
|
193
|
+
|
|
194
|
+ assertEquals(expected, actual);
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ @Ignore("Remove to run test")
|
|
198
|
+ @Test
|
|
199
|
+ public void oneFileSeveralMatchesCaseInsensitiveFlag() {
|
|
200
|
+ String expected = "Achilles sing, O Goddess! Peleus' son;\n"
|
|
201
|
+ + "The noble Chief Achilles from the son";
|
|
202
|
+
|
|
203
|
+ String actual = grepTool.grep(
|
|
204
|
+ "ACHILLES",
|
|
205
|
+ Collections.singletonList("-i"),
|
|
206
|
+ Collections.singletonList("iliad.txt")
|
|
207
|
+ );
|
|
208
|
+
|
|
209
|
+ assertEquals(expected, actual);
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ @Ignore("Remove to run test")
|
|
213
|
+ @Test
|
|
214
|
+ public void oneFileSeveralMatchesInvertedFlag() {
|
|
215
|
+ String expected = "Brought Death into the World, and all our woe,\n"
|
|
216
|
+ + "With loss of Eden, till one greater Man\n"
|
|
217
|
+ + "Restore us, and regain the blissful Seat,\n"
|
|
218
|
+ + "Sing Heav'nly Muse, that on the secret top\n"
|
|
219
|
+ + "That Shepherd, who first taught the chosen Seed";
|
|
220
|
+
|
|
221
|
+ String actual = grepTool.grep(
|
|
222
|
+ "Of",
|
|
223
|
+ Collections.singletonList("-v"),
|
|
224
|
+ Collections.singletonList("paradise-lost.txt")
|
|
225
|
+ );
|
|
226
|
+
|
|
227
|
+ assertEquals(expected, actual);
|
|
228
|
+ }
|
|
229
|
+
|
|
230
|
+ @Ignore("Remove to run test")
|
|
231
|
+ @Test
|
|
232
|
+ public void oneFileNoMatchesVariousFlags() {
|
|
233
|
+ String expected = "";
|
|
234
|
+
|
|
235
|
+ String actual = grepTool.grep(
|
|
236
|
+ "Gandalf",
|
|
237
|
+ Arrays.asList("-n", "-l", "-x", "-i"),
|
|
238
|
+ Collections.singletonList("iliad.txt")
|
|
239
|
+ );
|
|
240
|
+
|
|
241
|
+ assertEquals(expected, actual);
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ @Ignore("Remove to run test")
|
|
245
|
+ @Test
|
|
246
|
+ public void multipleFilesOneMatchNoFlags() {
|
|
247
|
+ String expected = "iliad.txt:Of Atreus, Agamemnon, King of men.";
|
|
248
|
+
|
|
249
|
+ String actual = grepTool.grep(
|
|
250
|
+ "Agamemnon",
|
|
251
|
+ Collections.emptyList(),
|
|
252
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
253
|
+ );
|
|
254
|
+
|
|
255
|
+ assertEquals(expected, actual);
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ @Ignore("Remove to run test")
|
|
259
|
+ @Test
|
|
260
|
+ public void multipleFilesSeveralMatchesNoFlags() {
|
|
261
|
+ String expected = "midsummer-night.txt:Nor how it may concern my modesty,\n"
|
|
262
|
+ + "midsummer-night.txt:But I beseech your grace that I may know\n"
|
|
263
|
+ + "midsummer-night.txt:The worst that may befall me in this case,";
|
|
264
|
+
|
|
265
|
+ String actual = grepTool.grep(
|
|
266
|
+ "may",
|
|
267
|
+ Collections.emptyList(),
|
|
268
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
269
|
+ );
|
|
270
|
+
|
|
271
|
+ assertEquals(expected, actual);
|
|
272
|
+ }
|
|
273
|
+
|
|
274
|
+ @Ignore("Remove to run test")
|
|
275
|
+ @Test
|
|
276
|
+ public void multipleFilesSeveralMatchesPrintLineNumbersFlag() {
|
|
277
|
+ String expected = "midsummer-night.txt:5:But I beseech your grace that I may know\n"
|
|
278
|
+ + "midsummer-night.txt:6:The worst that may befall me in this case,\n"
|
|
279
|
+ + "paradise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n"
|
|
280
|
+ + "paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top";
|
|
281
|
+
|
|
282
|
+ String actual = grepTool.grep(
|
|
283
|
+ "that",
|
|
284
|
+ Collections.singletonList("-n"),
|
|
285
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
286
|
+ );
|
|
287
|
+
|
|
288
|
+ assertEquals(expected, actual);
|
|
289
|
+ }
|
|
290
|
+
|
|
291
|
+ @Ignore("Remove to run test")
|
|
292
|
+ @Test
|
|
293
|
+ public void multipleFilesOneMatchPrintFileNamesFlag() {
|
|
294
|
+ String expected = "iliad.txt\n"
|
|
295
|
+ + "paradise-lost.txt";
|
|
296
|
+
|
|
297
|
+ String actual = grepTool.grep(
|
|
298
|
+ "who",
|
|
299
|
+ Collections.singletonList("-l"),
|
|
300
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
301
|
+ );
|
|
302
|
+
|
|
303
|
+ assertEquals(expected, actual);
|
|
304
|
+ }
|
|
305
|
+
|
|
306
|
+ @Ignore("Remove to run test")
|
|
307
|
+ @Test
|
|
308
|
+ public void multipleFilesSeveralMatchesCaseInsensitiveFlag() {
|
|
309
|
+ String expected = "iliad.txt:Caused to Achaia's host, sent many a soul\n"
|
|
310
|
+ + "iliad.txt:Illustrious into Ades premature,\n"
|
|
311
|
+ + "iliad.txt:And Heroes gave (so stood the will of Jove)\n"
|
|
312
|
+ + "iliad.txt:To dogs and to all ravening fowls a prey,\n"
|
|
313
|
+ + "midsummer-night.txt:I do entreat your grace to pardon me.\n"
|
|
314
|
+ + "midsummer-night.txt:In such a presence here to plead my thoughts;\n"
|
|
315
|
+ + "midsummer-night.txt:If I refuse to wed Demetrius.\n"
|
|
316
|
+ + "paradise-lost.txt:Brought Death into the World, and all our woe,\n"
|
|
317
|
+ + "paradise-lost.txt:Restore us, and regain the blissful Seat,\n"
|
|
318
|
+ + "paradise-lost.txt:Sing Heav'nly Muse, that on the secret top";
|
|
319
|
+
|
|
320
|
+ String actual = grepTool.grep(
|
|
321
|
+ "TO",
|
|
322
|
+ Collections.singletonList("-i"),
|
|
323
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
324
|
+ );
|
|
325
|
+
|
|
326
|
+ assertEquals(expected, actual);
|
|
327
|
+ }
|
|
328
|
+
|
|
329
|
+ @Ignore("Remove to run test")
|
|
330
|
+ @Test
|
|
331
|
+ public void multipleFilesSeveralMatchesInvertedFlag() {
|
|
332
|
+ String expected = "iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
|
|
333
|
+ + "iliad.txt:The noble Chief Achilles from the son\n"
|
|
334
|
+ + "midsummer-night.txt:If I refuse to wed Demetrius.";
|
|
335
|
+
|
|
336
|
+ String actual = grepTool.grep(
|
|
337
|
+ "a",
|
|
338
|
+ Collections.singletonList("-v"),
|
|
339
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
340
|
+ );
|
|
341
|
+
|
|
342
|
+ assertEquals(expected, actual);
|
|
343
|
+ }
|
|
344
|
+
|
|
345
|
+ @Ignore("Remove to run test")
|
|
346
|
+ @Test
|
|
347
|
+ public void multipleFilesOneMatchEntireLinesFlag() {
|
|
348
|
+ String expected = "midsummer-night.txt:But I beseech your grace that I may know";
|
|
349
|
+
|
|
350
|
+ String actual = grepTool.grep(
|
|
351
|
+ "But I beseech your grace that I may know",
|
|
352
|
+ Collections.singletonList("-x"),
|
|
353
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
354
|
+ );
|
|
355
|
+
|
|
356
|
+ assertEquals(expected, actual);
|
|
357
|
+ }
|
|
358
|
+
|
|
359
|
+ @Ignore("Remove to run test")
|
|
360
|
+ @Test
|
|
361
|
+ public void multipleFilesOneMatchMultipleFlags() {
|
|
362
|
+ String expected = "paradise-lost.txt:4:With loss of Eden, till one greater Man";
|
|
363
|
+
|
|
364
|
+ String actual = grepTool.grep(
|
|
365
|
+ "WITH LOSS OF EDEN, TILL ONE GREATER MAN",
|
|
366
|
+ Arrays.asList("-n", "-i", "-x"),
|
|
367
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
368
|
+ );
|
|
369
|
+
|
|
370
|
+ assertEquals(expected, actual);
|
|
371
|
+ }
|
|
372
|
+
|
|
373
|
+ @Ignore("Remove to run test")
|
|
374
|
+ @Test
|
|
375
|
+ public void multipleFilesNoMatchesVariousFlags() {
|
|
376
|
+ String expected = "";
|
|
377
|
+
|
|
378
|
+ String actual = grepTool.grep(
|
|
379
|
+ "Frodo",
|
|
380
|
+ Arrays.asList("-n", "-l", "-i", "-x"),
|
|
381
|
+ Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
|
|
382
|
+ );
|
|
383
|
+
|
|
384
|
+ assertEquals(expected, actual);
|
|
385
|
+ }
|
|
386
|
+
|
|
387
|
+ private void writeToFile(String filename, List<String> contents) throws IOException {
|
|
388
|
+ Path file = Paths.get(filename);
|
|
389
|
+ Files.write(file, contents, Charset.forName("UTF-8"));
|
|
390
|
+ }
|
|
391
|
+
|
|
392
|
+ private void deleteFile(String filename) throws IOException {
|
|
393
|
+ Files.deleteIfExists(Paths.get(filename));
|
|
394
|
+ }
|
|
395
|
+}
|