aboutsummaryrefslogtreecommitdiffstats
path: root/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java
blob: 0cc0cbcb9440ce4168f9ab21ab148a61e8501f52 (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
package eu.equalparts.cardbase.cli;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.OutputStream;
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.Test;

import com.fasterxml.jackson.databind.ObjectMapper;

import eu.equalparts.cardbase.cards.Card;
import eu.equalparts.cardbase.testutils.TestFile;
import eu.equalparts.cardbase.testutils.TestUtils;

public class CardbaseCLITest {

	private CardbaseCLI uut;
	private static StringBuilder output = new StringBuilder();
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
//		System.setOut(new PrintStream(new OutputStream() {
//			@Override
//			public void write(int b) throws IOException {
//				output.append((char) b);
//			}
//		}, true));
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		uut = new CardbaseCLI();
	}

	@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(" 	  	this    was    	 a 	 triumph	 	         ");
		
		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);
	}
	
	/***********************************************************************************
	 * Write method tests, happy path
	 ***********************************************************************************/
	@Test
	public void writeCardbaseToSpecifiedFile() throws Exception {
		try (Scanner scanner = new Scanner(getClass().getResourceAsStream("/shivandragon.json"));
				TestFile testFile = TestUtils.createValidTestFile("testsave.cb");
				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);
			
			uut.write(testFile.getAbsolutePath());
			String save = scanner2.useDelimiter("\\Z").next();
			assertTrue(save.contains(cardJSON));
				
		}
	}
}