aboutsummaryrefslogtreecommitdiffstats
path: root/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java
blob: b01f0482254f99df67b8124685dd5f6e84b8de50 (plain)
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
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;

/**
 * These tests do some unusual things, but I think they are reasonable.
 * 
 * @author Eduardo Pedroni
 *
 */
public class CardbaseCLITest {

	private CardbaseCLI uut;
	
	private ByteArrayOutputStream testOutput = new ByteArrayOutputStream();
	private final PrintStream console = System.out;
	private final String EOL = System.getProperty("line.separator");
	
	/**
	 * The test remote URL to "query" from, this is actually the same directory
	 * as the test class. Test files need to be placed accordingly.
	 */
	private final String TEST_REMOTE = getClass().getResource("").toString();
	
	@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(TEST_REMOTE);
		testOutput.reset();
	}

	@After
	public void tearDown() throws Exception {
	}
	
	/***********************************************************************************
	 * Start up tests, happy path
	 ***********************************************************************************/
	@Test
	public void welcomeMessageWithoutCardbaseFile() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut = new CardbaseCLI(TEST_REMOTE);
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Welcome to Cardbase CLI!\nNo cardbase file was provided, creating a clean cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void glanceWithoutCardbaseFile() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE);
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("glance");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Total: 0" + EOL, testOutput.toString());
	}
	
	@Test
	public void welcomeMessageWithCardbaseFile() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Welcome to Cardbase CLI!\nLoading cardbase from \"" + getClass().getResource("/testbase.cb").getFile() + "\"." + EOL, testOutput.toString());
	}
	
	@Test
	public void glanceWithCardbaseFile() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("glance");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("multipleCardsGlance") + EOL, testOutput.toString());
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void welcomeMessageWithEmptyCardbaseFileName() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut = new CardbaseCLI(TEST_REMOTE, "");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Welcome to Cardbase CLI!\nNo cardbase file was provided, creating a clean cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void startWithInvalidArguments() throws Exception {
		File notAFile = tempFolder.newFile();
		tempFolder.delete();

		exception.expect(IllegalArgumentException.class);
		uut = new CardbaseCLI(TEST_REMOTE, notAFile.getAbsolutePath());
	}
	
	// TODO test more invalid file scenarios
	
	/***********************************************************************************
	 * help() tests, happy path
	 ***********************************************************************************/
	@Test
	public void helpInformationIsPrinted() throws Exception {
			String help = getFileContents("/help_en");
			
			try {
				System.setOut(new PrintStream(testOutput));
				uut.interpretInput("help");
			} finally {
				System.setOut(console);
			}
			
			assertEquals(help + EOL, testOutput.toString());
	}
	
	/***********************************************************************************
	 * write() tests, happy path
	 ***********************************************************************************/
	@Test
	public void reloadedCardbaseMatchesOriginal() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		File testFile = tempFolder.newFile("saveTest.cb");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("write " + testFile.getAbsolutePath());
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Cardbase was saved to \"" + testFile.getAbsolutePath() + "\". "
				+ "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString());
		
		testOutput.reset();
		uut = new CardbaseCLI(TEST_REMOTE, testFile.getAbsolutePath());
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("multipleCardsPerusal") + EOL, testOutput.toString());
	}
	
	@Test
	public void specifiedFileIsSubsequentlyUsedByDefault() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		File testFile = tempFolder.newFile("saveTest.cb");
		
		uut.interpretInput("write " + testFile.getAbsolutePath());
		
		uut.interpretInput("set FRF");
		uut.interpretInput("remove 128");
		uut.interpretInput("100");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		String expectedPerusal = testOutput.toString();
		
		testOutput.reset();
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("write");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Cardbase was saved to \"" + testFile.getAbsolutePath() + "\". "
				+ "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString());
		
		uut = new CardbaseCLI(TEST_REMOTE, testFile.getAbsolutePath());
		testOutput.reset();
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(expectedPerusal, testOutput.toString());
		assertNotEquals(getFileContents("multipleCardsPerusal") + EOL, testOutput.toString());
	}

	@Test
	public void userIsPromptedForFileNameIfNoDefaultIsPresent() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("write");
		} finally {
			System.setOut(console);
		}
		assertEquals("Please provide a file name." + EOL, testOutput.toString());
	}
	
	@Test
	public void fileExtensionIsAddedIfMissing() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());

		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("write " + tempFolder.getRoot().getAbsolutePath() + "/testSave");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Cardbase was saved to \"" + tempFolder.getRoot().getAbsolutePath() + "/testSave.cb" + "\". "
				+ "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString());
		assertFalse("File without extension exists.", new File(tempFolder.getRoot().getAbsolutePath() + "/testSave").exists());
		assertTrue("File with extension does not exist.", new File(tempFolder.getRoot().getAbsolutePath() + "/testSave.cb").exists());
		
		testOutput.reset();
		uut = new CardbaseCLI(TEST_REMOTE, tempFolder.getRoot().getAbsolutePath() + "/testSave.cb");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("multipleCardsPerusal") + EOL, testOutput.toString());
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void writeFailsIfProvidedPathIsDirectory() throws Exception {
		File directory = tempFolder.newFolder("testdirectory.cb");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("write " + directory.getAbsolutePath());
		} finally {
			System.setOut(console);
		}
		assertEquals("Could not write to \"" + directory.getAbsolutePath() + "\"." + EOL, testOutput.toString());
	}
	
	@Test
	public void extensionIsAddedEvenIfFileNameAlreadyHasOne() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());

		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("write " + tempFolder.getRoot().getAbsolutePath() + "/testSave.tar");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Cardbase was saved to \"" + tempFolder.getRoot().getAbsolutePath() + "/testSave.tar.cb" + "\". "
				+ "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString());
		assertFalse("File with incorrect extension exists.", new File(tempFolder.getRoot().getAbsolutePath() + "/testSave.tar").exists());
		assertTrue("File with correct extension does not exist.", new File(tempFolder.getRoot().getAbsolutePath() + "/testSave.tar.cb").exists());
		
		testOutput.reset();
		uut = new CardbaseCLI(TEST_REMOTE, tempFolder.getRoot().getAbsolutePath() + "/testSave.tar.cb");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("multipleCardsPerusal") + EOL, testOutput.toString());
	}
	
	@Test
	public void illegalCharactersAreRemovedFromFileName() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());

		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("write " + tempFolder.getRoot().getAbsolutePath() + "/f1lEnämẽ\"--._-");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Cardbase was saved to \"" + tempFolder.getRoot().getAbsolutePath() + "/f1lEnm--._-.cb" + "\". "
				+ "Subsequent writes will be done to this same file unless otherwise requested." + EOL, testOutput.toString());
		assertTrue("File with correct name does not exist.", new File(tempFolder.getRoot().getAbsolutePath() + "/f1lEnm--._-.cb").exists());
		
		testOutput.reset();
		uut = new CardbaseCLI(TEST_REMOTE, tempFolder.getRoot().getAbsolutePath() + "/f1lEnm--._-.cb");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("multipleCardsPerusal") + EOL, testOutput.toString());
	}
	
	/***********************************************************************************
	 * version() tests, happy path
	 ***********************************************************************************/
	@Test
	public void correctVersionIsPrinted() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("version");
		} finally {
			System.setOut(console);
		}
		assertTrue("Incorrect version information was printed.", testOutput.toString().matches("CardbaseCLI v[0-9]+\\.[0-9]+" + EOL));
	}
	
	/***********************************************************************************
	 * exit() tests, happy path
	 ***********************************************************************************/
	// try to exit without changing anything, expect no warning
	@Test
	public void exitWithoutChanges() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("exit");
		} finally {
			System.setOut(console);
		}
		assertEquals("", testOutput.toString());
	}
	
	// try to exit after adding something, expect a warning, try to exit again, expect no warning
	@Test
	public void exitAfterAddingSomething() throws Exception {
		uut.interpretInput("set FRF");
		uut.interpretInput("100");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("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());
		
		testOutput.reset();
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("exit");
		} finally {
			System.setOut(console);
		}
		assertEquals("", testOutput.toString());
	}
	
	// try to exit after removing something, expect a warning, try to exit again, expect no warning
	@Test
	public void exitAfterRemovingSomething() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		uut.interpretInput("remove 128");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("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());
		
		testOutput.reset();
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("exit");
		} finally {
			System.setOut(console);
		}
		assertEquals("", testOutput.toString());
	}
	
	// try to exit after adding something, expect a warning, write, try to exit again, expect no warning
	@Test
	public void exitAfterAddingSomethingAndBeforeWriting() throws Exception {
		uut.interpretInput("set FRF");
		uut.interpretInput("100");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("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());
		
		testOutput.reset();
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("exit");
		} finally {
			System.setOut(console);
		}
		assertEquals("", testOutput.toString());
	}
	
	// try to exit after removing something, expect a warning, write, try to exit again, expect no warning
	@Test
	public void exitAfterRemovingSomethingAndBeforeWriting() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		uut.interpretInput("remove 128");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("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());
		
		testOutput.reset();
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("exit");
		} finally {
			System.setOut(console);
		}
		assertEquals("", testOutput.toString());
	}
	
	// add something, write, try to exit and expect no warning
	@Test
	public void exitAfterAddingSomethingAndWriting() throws Exception {
		uut.interpretInput("set FRF");
		uut.interpretInput("100");
		uut.interpretInput("write " + tempFolder.newFile().getAbsolutePath());
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("exit");
		} finally {
			System.setOut(console);
		}
		assertEquals("", testOutput.toString());
	}
	
	// remove something, write, try to exit and expect no warning
	@Test
	public void exitAfterRemovingSomethingAndWriting() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		uut.interpretInput("remove 128");
		uut.interpretInput("write " + tempFolder.newFile().getAbsolutePath());
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("exit");
		} finally {
			System.setOut(console);
		}
		assertEquals("", testOutput.toString());
	}
	
	/***********************************************************************************
	 * sets() tests, happy path
	 ***********************************************************************************/
	@Test
	public void correctSetListIsPrinted() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("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\n"
				+ "FRF          : Fate Reforged" + EOL, testOutput.toString());
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void fallbackListIsPrintedIfListCannotBeFound() throws Exception {
		File noListLocation = tempFolder.newFolder();
		tempFolder.delete();
		uut = new CardbaseCLI(noListLocation.getAbsolutePath());
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("sets");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("expectedFallbackList") + EOL, testOutput.toString());
	}
	
	/***********************************************************************************
	 * set() tests, happy path
	 ***********************************************************************************/
	@Test
	public void correctSetIsSelected() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("set M15");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Selected set: Magic 2015 Core Set." + EOL, testOutput.toString());
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void invalidSetIsProvided() throws Exception {		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("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());
	}
	
	/***********************************************************************************
	 * glance() tests, happy path
	 ***********************************************************************************/
	@Test
	public void glanceIsPrintedWithOneCard() throws Exception {
		uut.interpretInput("set M15");
		uut.interpretInput("281");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("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.interpretInput("glance");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Total: 0" + EOL, testOutput.toString());
	}
	
	@Test
	public void glanceIsPrintedWithManyCards() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("glance");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("multipleCardsGlance") + EOL, testOutput.toString());
	}
	
	/***********************************************************************************
	 * peruse() tests, happy path
	 ***********************************************************************************/
	@Test
	public void perusalIsPrintedWithOneCard() throws Exception {
		uut.interpretInput("set M15");
		uut.interpretInput("281");
				
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("singleCardPerusal") + EOL, testOutput.toString());
	}
	
	@Test
	public void perusalIsPrintedWithZeroCards() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Total: 0" + EOL, testOutput.toString());
	}
	
	@Test
	public void perusalIsPrintedWithManyCards() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("multipleCardsPerusal") + EOL, testOutput.toString());
	}
	
	@Test
	public void specificPerusalWithValidArgumentIsPrinted() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 129");
		} finally {
			System.setOut(console);
		}
		
		assertEquals(getFileContents("specificCardPerusal") + EOL, testOutput.toString());
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void specificPerusalWithInvalidArgument() throws Exception {
		uut.interpretInput("set FRF");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 100");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Card not in cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void specificPerusalWithNoSelectedSet() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 100");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Please select a set before perusing a specific card." + EOL, testOutput.toString());
	}
	
	/***********************************************************************************
	 * remove() tests, happy path
	 ***********************************************************************************/
	@Test
	public void removeValidAmountOfExistingCard() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		uut.interpretInput("remove 129 3");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 129");
		} finally {
			System.setOut(console);
		}
		
		assertTrue("Perusal indicates wrong number of cards: " + testOutput.toString(), testOutput.toString().startsWith("5    Formless Nurturing (FRF, 129)\n"));
	}
	
	@Test
	public void removeExceedingAmountOfExistingCard() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		uut.interpretInput("remove 128 3");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 128");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Card not in cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void removeExactAmountOfExistingCard() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		uut.interpretInput("remove 128 1");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 128");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Card not in cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void removeSingleExistingCardWithoutAmount() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		uut.interpretInput("remove 128");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 128");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Card not in cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void removeMultipleExistingCardsWithoutAmount() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		uut.interpretInput("remove 129");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 129");
		} finally {
			System.setOut(console);
		}
		
		assertTrue("Perusal indicates wrong number of cards: " + testOutput.toString(), testOutput.toString().startsWith("7    Formless Nurturing (FRF, 129)\n"));
	}
	
	/*
	 * Edge cases
	 */
	@Test
	public void removeNonExistentCardWithoutAmount() throws Exception {
		uut.interpretInput("set FRF");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("remove 128");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Card FRF 128 is not in the cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void removeNonExistentCardWithAmount() throws Exception {
		uut.interpretInput("set FRF");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("remove 128 2");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Card FRF 128 is not in the cardbase." + EOL, testOutput.toString());
	}
	
	@Test
	public void removeZeroOfExistingCard() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		uut.interpretInput("remove 129 0");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 129");
		} finally {
			System.setOut(console);
		}
		
		assertTrue("Card amount should not have changed: " + testOutput.toString(), testOutput.toString().startsWith("8    Formless Nurturing (FRF, 129)\n"));
	}
	
	@Test
	public void removeNegativeAmountOfExistingCard() throws Exception {
		uut = new CardbaseCLI(TEST_REMOTE, getClass().getResource("/testbase.cb").getFile());
		uut.interpretInput("set FRF");
		
		uut.interpretInput("remove 129 -10");
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("peruse 129");
		} finally {
			System.setOut(console);
		}
		
		assertTrue("Card amount should not have changed: " + testOutput.toString(), testOutput.toString().startsWith("8    Formless Nurturing (FRF, 129)\n"));
	}
	
	@Test
	public void removeWithNoSetSelected() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("remove 100");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Please select a set before removing cards." + EOL, testOutput.toString());
	}
	
	/***********************************************************************************
	 * add() tests, happy path
	 ***********************************************************************************/
	// add
	
	
	/*
	 * Edge cases
	 */
	@Test
	public void invalidCommandWithNoSelectedSet() throws Exception {
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("cOmMand5 argumEnt1 argument2");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("Please select a set before adding cards." + EOL, testOutput.toString());
	}
	
	@Test
	public void invalidCommandWithSelectedSet() throws Exception {
		// dummy set just so the uut knows the set to remove from
		uut.interpretInput("set FRF");
		
		try {
			System.setOut(new PrintStream(testOutput));
			uut.interpretInput("cOmMand5 argumEnt1 argument2");
		} finally {
			System.setOut(console);
		}
		
		assertEquals("cOmMand5 does not correspond to a card in Fate Reforged." + EOL, testOutput.toString());
	}
	
//	@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]);
//	}
	
	/***********************************************************************************
	 * undo() tests, happy path
	 ***********************************************************************************/
	
	private String getFileContents(String fileName) {
		try (Scanner scanner = new Scanner(getClass().getResourceAsStream(fileName))) {
			return scanner.useDelimiter("\\Z").next();
		}
	}
}