1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
package eu.equalparts.cardbase.cli;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.equalparts.cardbase.cards.Card;
import eu.equalparts.cardbase.utils.MTGUniverse;
public class CardbaseCLITest {
private CardbaseCLI uut;
private ByteArrayOutputStream testOutput;
private final PrintStream console = System.out;
private final String EOL = System.getProperty("line.separator");
@Rule
public ExpectedException exception = ExpectedException.none();
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
uut = new CardbaseCLI();
testOutput = new ByteArrayOutputStream();
}
@After
public void tearDown() throws Exception {
}
/***********************************************************************************
* User input tests, happy path
***********************************************************************************/
@Test
public void commandAndArgumentsSeparatedBySpaces() throws Exception {
String[] processedInput = uut.sanitiseInput("cOmMand5 argumEnt1 argument2");
assertEquals("Wrong array length.", 3, processedInput.length);
assertEquals("cOmMand5", processedInput[0]);
assertEquals("argumEnt1", processedInput[1]);
assertEquals("argument2", processedInput[2]);
}
@Test
public void commandAndNoArguments() throws Exception {
String[] processedInput = uut.sanitiseInput("Someth1ng");
assertEquals("Wrong array length.", 1, processedInput.length);
assertEquals("Someth1ng", processedInput[0]);
}
/*
* Edge cases
*/
@Test
public void blankInput() throws Exception {
String[] processedInput = uut.sanitiseInput("");
assertEquals("Wrong array length.", 1, processedInput.length);
assertEquals("", processedInput[0]);
}
@Test
public void onlyWhiteSpace() throws Exception {
String[] processedInput = uut.sanitiseInput(" ");
assertEquals("Wrong array length.", 1, processedInput.length);
assertEquals("", processedInput[0]);
}
@Test
public void leadingTrailingAndIntermediaryWhiteSpace() throws Exception {
String[] processedInput = uut.sanitiseInput(" \t this \twas \t \t a triumph \t\t ");
assertEquals("Wrong array length.", 4, processedInput.length);
assertEquals("this", processedInput[0]);
assertEquals("was", processedInput[1]);
assertEquals("a", processedInput[2]);
assertEquals("triumph", processedInput[3]);
}
/***********************************************************************************
* File name sanity tests, happy path
***********************************************************************************/
@Test
public void reasonableFileNameWithoutExtension() throws Exception {
String processedName = uut.sanitiseFileName("f1lename");
assertEquals("f1lename.cb", processedName);
}
@Test
public void reasonableFileNameWithExtension() throws Exception {
String processedName = uut.sanitiseFileName("f1lename.cb");
assertEquals("f1lename.cb", processedName);
}
/*
* Edge cases
*/
@Test
public void nameWithBrokenExtension() throws Exception {
String processedName = uut.sanitiseFileName("f1lename.c b");
assertEquals("f1lename.cb", processedName);
}
@Test
public void nameWithWrongExtension() throws Exception {
String processedName = uut.sanitiseFileName("f1lename.tar");
assertEquals("f1lename.tar.cb", processedName);
}
@Test
public void nameWithIllegalCharacters() throws Exception {
String processedName = uut.sanitiseFileName("f1lEnämẽ\n\t\"--._-//? \t^|#ŧ@fhw9vLL'''");
assertEquals("f1lEnm--._-//fhw9vLL.cb", processedName);
}
/***********************************************************************************
* Constructor tests, happy path
***********************************************************************************/
@Test
public void instantiationWithoutArguments() throws Exception {
uut = new CardbaseCLI();
assertEquals("Cardbase contains the wrong number of card entries.", 0, uut.cardbase.getCards().size());
}
@Test
public void instantiationWithCardbaseFile() throws Exception {
uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile());
assertEquals("Cardbase contains the wrong number of card entries.", 6, uut.cardbase.getCards().size());
class CardInfo {
String setCode, number;
Integer count;
public CardInfo(String setCode, String number, Integer count) {
this.setCode = setCode;
this.number = number;
this.count = count;
}
}
CardInfo[] testCards = new CardInfo[] {
new CardInfo("M12", "34", 2),
new CardInfo("FRF", "129", 8),
new CardInfo("M12", "26", 1),
new CardInfo("FRF", "127", 1),
new CardInfo("FRF", "128", 1),
new CardInfo("M12", "152", 1)};
for (CardInfo ci : testCards) {
Card card = uut.cardbase.getCard(ci.setCode, ci.number);
assertNotNull("Missing card, set " + ci.setCode + ", " + ci.number, card);
assertEquals("Wrong card count, set " + ci.setCode + ", " + ci.number, ci.count, card.count);
}
}
/*
* Edge cases
*/
@Test
public void instantiationWithInvalidArguments() throws Exception {
File notAFile = tempFolder.newFile();
tempFolder.delete();
exception.expect(IllegalArgumentException.class);
uut = new CardbaseCLI(notAFile.getAbsolutePath());
}
@Test
public void instantiationWithEmptyArguments() throws Exception {
uut = new CardbaseCLI("");
assertEquals("Cardbase contains the wrong number of card entries.", 0, uut.cardbase.getCards().size());
}
/***********************************************************************************
* help() tests, happy path
***********************************************************************************/
@Test
public void helpInformationIsPrinted() throws Exception {
try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/help_en"))) {
String help = scanner.useDelimiter("\\Z").next();
try {
System.setOut(new PrintStream(testOutput));
uut.help();
} finally {
System.setOut(console);
}
assertEquals(help + EOL, testOutput.toString());
}
}
/***********************************************************************************
* write() tests, happy path
***********************************************************************************/
@Test
public void writeCardbaseToSpecifiedFile() throws Exception {
File testFile = tempFolder.newFile("saveTest.cb");
try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json"));
Scanner scanner2 = new Scanner(testFile)) {
String cardJSON = scanner.useDelimiter("\\Z").next();
Card testCard = new ObjectMapper().readValue(cardJSON, Card.class);
testCard.count = 1;
uut.cardbase.addCard(testCard);
try {
System.setOut(new PrintStream(testOutput));
uut.write(testFile.getAbsolutePath());
} finally {
System.setOut(console);
}
String save = scanner2.useDelimiter("\\Z").next();
assertTrue(save.contains(cardJSON));
assertEquals("Cardbase was saved to \"" + testFile.getAbsolutePath() + "\". "
+ "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString());
}
}
@Test
public void specifiedFileIsSubsequentlyUsedByDefault() throws Exception {
File testFile = tempFolder.newFile("saveTest.cb");
try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json"));
Scanner scanner2 = new Scanner(testFile)) {
uut.write(testFile.getAbsolutePath());
String cardJSON = scanner.useDelimiter("\\Z").next();
Card testCard = new ObjectMapper().readValue(cardJSON, Card.class);
testCard.count = 1;
uut.cardbase.addCard(testCard);
try {
System.setOut(new PrintStream(testOutput));
uut.write();
} finally {
System.setOut(console);
}
String save = scanner2.useDelimiter("\\Z").next();
assertTrue(save.contains(cardJSON));
assertEquals("Cardbase was saved to \"" + testFile.getAbsolutePath() + "\". "
+ "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString());
}
}
@Test
public void noFileIsProvidedAndNoDefaultIsAvailable() throws Exception {
try {
System.setOut(new PrintStream(testOutput));
uut.write();
} finally {
System.setOut(console);
}
assertEquals("Please provide a file name." + EOL, testOutput.toString());
}
/*
* Edge cases
*/
@Test
public void pathProvidedIsDirectory() throws Exception {
File directory = tempFolder.newFolder("testdirectory.cb");
try {
System.setOut(new PrintStream(testOutput));
uut.write(directory.getAbsolutePath());
} finally {
System.setOut(console);
}
assertEquals("Could not write to \"" + directory.getAbsolutePath() + "\"." + EOL, testOutput.toString());
}
/***********************************************************************************
* version() tests, happy path
***********************************************************************************/
@Test
public void correctVersionIsPrinted() throws Exception {
try {
System.setOut(new PrintStream(testOutput));
uut.version();
} finally {
System.setOut(console);
}
assertEquals("Cardbase v" + CardbaseCLI.VERSION + EOL, testOutput.toString());
}
/***********************************************************************************
* exit() tests, happy path
***********************************************************************************/
@Test
public void exitFlagIsRaised() throws Exception {
uut.exit();
assertEquals("Incorrect state for exit flag.", true, uut.exit);
}
@Test
public void saveReminderIsPrintedIfPromptFlagIsRaised() throws Exception {
uut.savePrompt = true;
try {
System.setOut(new PrintStream(testOutput));
uut.exit();
} finally {
System.setOut(console);
}
assertEquals("Don't forget to save. If you really wish to quit without saving, type \"exit\" again." + EOL, testOutput.toString());
assertEquals("Incorrect state for exit flag.", false, uut.exit);
}
@Test
public void exitFlagIsRaisedAfterSavePromptIsAcknowledged() throws Exception {
uut.savePrompt = true;
uut.exit();
assertEquals("Incorrect state for exit flag.", false, uut.exit);
assertEquals("Incorrect state for save flag.", false, uut.savePrompt);
uut.exit();
assertEquals("Incorrect state for exit flag.", true, uut.exit);
}
/***********************************************************************************
* sets() tests, happy path
***********************************************************************************/
@Test
public void correctSetListIsPrinted() throws Exception {
uut.mtgUniverse = new MTGUniverse(getClass().getResource("").toString());
try {
System.setOut(new PrintStream(testOutput));
uut.sets();
} finally {
System.setOut(console);
}
assertEquals("LEA : Limited Edition Alpha\n"
+ "LEB : Limited Edition Beta\n"
+ "ARN : Arabian Nights\n"
+ "M15 : Magic 2015 Core Set" + EOL, testOutput.toString());
}
/*
* Edge cases
*/
@Test
public void fallbackListIsPrintedIfListCannotBeFound() throws Exception {
File noListLocation = tempFolder.newFolder();
tempFolder.delete();
uut.mtgUniverse = new MTGUniverse("file:" + noListLocation.getCanonicalPath() + "/");
try {
System.setOut(new PrintStream(testOutput));
uut.sets();
} finally {
System.setOut(console);
}
try (Scanner scanner = new Scanner(getClass().getResourceAsStream("expectedFallbackList"))) {
String expectedOutput = scanner.useDelimiter("\\Z").next();
assertEquals(expectedOutput + EOL, testOutput.toString());
}
}
/***********************************************************************************
* set() tests, happy path
***********************************************************************************/
@Test
public void correctSetIsSelected() throws Exception {
uut.mtgUniverse = new MTGUniverse(getClass().getResource("").toString());
try {
System.setOut(new PrintStream(testOutput));
uut.set("M15");
} finally {
System.setOut(console);
}
assertEquals("Selected set: Magic 2015 Core Set." + EOL, testOutput.toString());
assertEquals(uut.selectedSet.code, "M15");
}
/*
* Edge cases
*/
@Test
public void invalidSetIsProvided() throws Exception {
uut.mtgUniverse = new MTGUniverse(getClass().getResource("").toString());
try {
System.setOut(new PrintStream(testOutput));
uut.set("not a set");
} finally {
System.setOut(console);
}
assertEquals("\"not a set\" does not correspond to any set (use \"sets\" to see all valid set codes)."
+ EOL, testOutput.toString());
assertNull(uut.selectedSet);
}
/***********************************************************************************
* glance() tests, happy path
***********************************************************************************/
@Test
public void glanceIsPrintedWithOneCard() throws Exception {
Card testCard = new ObjectMapper().readValue(getClass().getResourceAsStream("/shivandragon.json"), Card.class);
testCard.count = 1;
uut.cardbase.addCard(testCard);
try {
System.setOut(new PrintStream(testOutput));
uut.glance();
} finally {
System.setOut(console);
}
assertEquals("1 Shivan Dragon (M15, 281)\nTotal: 1" + EOL, testOutput.toString());
}
@Test
public void glanceIsPrintedWithZeroCards() throws Exception {
try {
System.setOut(new PrintStream(testOutput));
uut.glance();
} finally {
System.setOut(console);
}
assertEquals("Total: 0" + EOL, testOutput.toString());
}
@Test
public void glanceIsPrintedWithManyCards() throws Exception {
uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile());
try {
System.setOut(new PrintStream(testOutput));
uut.glance();
} finally {
System.setOut(console);
}
assertEquals("1 Reverberate (M12, 152)\n"
+ "1 Mighty Leap (M12, 26)\n"
+ "8 Formless Nurturing (FRF, 129)\n"
+ "1 Feral Krushok (FRF, 128)\n"
+ "1 Destructor Dragon (FRF, 127)\n"
+ "2 Siege Mastodon (M12, 34)\n"
+ "Total: 14" + EOL, testOutput.toString());
}
/***********************************************************************************
* peruse() tests, happy path
***********************************************************************************/
@Test
public void perusalIsPrintedWithOneCard() throws Exception {
Card testCard = new ObjectMapper().readValue(getClass().getResourceAsStream("/shivandragon.json"), Card.class);
testCard.count = 1;
uut.cardbase.addCard(testCard);
try {
System.setOut(new PrintStream(testOutput));
uut.peruse();
} finally {
System.setOut(console);
}
try (Scanner scanner = new Scanner(getClass().getResourceAsStream("singleCardPerusal"))) {
assertEquals(scanner.useDelimiter("\\Z").next() + EOL, testOutput.toString());
}
}
@Test
public void perusalIsPrintedWithZeroCards() throws Exception {
try {
System.setOut(new PrintStream(testOutput));
uut.peruse();
} finally {
System.setOut(console);
}
assertEquals("Total: 0" + EOL, testOutput.toString());
}
@Test
public void perusalIsPrintedWithManyCards() throws Exception {
uut = new CardbaseCLI(getClass().getResource("/testbase.cb").getFile());
try {
System.setOut(new PrintStream(testOutput));
uut.peruse();
} finally {
System.setOut(console);
}
try (Scanner scanner = new Scanner(getClass().getResourceAsStream("multipleCardsPerusal"))) {
assertEquals(scanner.useDelimiter("\\Z").next() + EOL, testOutput.toString());
}
}
/***********************************************************************************
* undo() tests, happy path
***********************************************************************************/
/***********************************************************************************
* remove() tests, happy path
***********************************************************************************/
/***********************************************************************************
* add() tests, happy path
***********************************************************************************/
}
|