1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package broadwick.io;
17
18 import java.io.IOException;
19 import java.util.Arrays;
20 import java.util.List;
21 import lombok.extern.slf4j.Slf4j;
22 import org.junit.After;
23 import org.junit.AfterClass;
24 import static org.junit.Assert.*;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.ClassRule;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.TestRule;
31 import org.junit.rules.TestWatcher;
32 import org.junit.runner.Description;
33 import org.junit.runners.model.Statement;
34 import org.slf4j.MarkerFactory;
35
36
37
38
39 @Slf4j
40 public class FileInputTest {
41
42 public FileInputTest() {
43 }
44
45 @BeforeClass
46 public static void setUpClass() {
47 testsFileName = FileInputTest.class.getClass().getResource("/FileInputDummy.csv").getFile();
48
49
50
51 if (System.getProperty("os.name").startsWith("Windows")
52 && testsFileName.startsWith("/")) {
53 testsFileName = testsFileName.substring(1);
54 }
55 }
56
57 @AfterClass
58 public static void tearDownClass() {
59 }
60
61 @Before
62 public void setUp() {
63 }
64
65 @After
66 public void tearDown() {
67 }
68
69 @ClassRule
70 public static TestRule classWatchman = new TestWatcher() {
71 @Override
72 protected void starting(Description description) {
73 log.info(MarkerFactory.getMarker("TEST"), " Running tests from {} ...", description.getClassName());
74 }
75
76 };
77 @Rule
78 public TestRule watchman = new TestWatcher() {
79 @Override
80 public Statement apply(Statement base, Description description) {
81 log.info(MarkerFactory.getMarker("TEST"), " Running {} ...", description.getMethodName());
82 return base;
83 }
84
85 };
86 @Test
87 public void testclose() {
88 try (FileInput instance = new FileInput(testsFileName)) {
89 instance.readLine();
90 instance.readLine();
91 instance.close();
92 try {
93 instance.readLine();
94 fail("Expected IOException to be thrown after using a method on a closed FileInput object.");
95 } catch (IOException ex) {
96
97
98 }
99 } catch (IOException ex) {
100 fail("Caught IOException in testClose()");
101 }
102 }
103
104 @Test
105 public void testReadNextLineAsTokensUsingCommaSeparator() {
106 try (FileInput instance = new FileInput(testsFileName, ",")) {
107
108 List<String> line = instance.readLine();
109 assertTrue(Arrays.asList("token1", "token2", "token3").equals(line));
110
111 line = instance.readLine();
112 assertTrue(Arrays.asList("apples", "oranges", "pears").equals(line));
113
114 line = instance.readLine();
115 assertTrue(Arrays.asList("one", "two", "three").equals(line));
116
117 line = instance.readLine();
118 assertTrue(Arrays.asList("a", "b", "c", "d", "e", "f").equals(line));
119
120 line = instance.readLine();
121 assertTrue(Arrays.asList("unos", "dos", "tres", "", "quatorze").equals(line));
122
123 line = instance.readLine();
124 assertTrue(Arrays.asList("last", "complete", "line", "").equals(line));
125
126 line = instance.readLine();
127 assertTrue(line.isEmpty());
128 } catch (IOException ex) {
129 fail("Caught IOException in testReadNextLine()");
130 }
131 }
132
133 @Test
134 public void testReadNextLineAsTokensUsingDefaultSeparator() {
135 try (FileInput instance = new FileInput(testsFileName)) {
136 List<String> line = instance.readLine();
137 assertTrue(Arrays.asList("token1", "token2", "token3").equals(line));
138
139 line = instance.readLine();
140 assertTrue(Arrays.asList("apples", "", "oranges", "", "pears").equals(line));
141
142 line = instance.readLine();
143 assertTrue(Arrays.asList("one", "", "", "two", "", "", "three").equals(line));
144
145 line = instance.readLine();
146 assertTrue(Arrays.asList("a", "b", "c", "d", "e", "f").equals(line));
147
148 line = instance.readLine();
149 assertTrue(Arrays.asList("unos", "", "dos", "tres", "", "quatorze").equals(line));
150
151 line = instance.readLine();
152 assertTrue(Arrays.asList("last", "", "complete", "", "line", "").equals(line));
153
154 line = instance.readLine();
155 assertTrue(line.isEmpty());
156 } catch (IOException ex) {
157 fail("Caught IOException in testReadNextLine()");
158 }
159 }
160
161 @Test
162 public void testReadNextLine() {
163 try (FileInput instance = new FileInput(testsFileName)) {
164
165 String line = instance.readNextLine();
166 assertTrue("token1,token2,token3".equals(line));
167
168 line = instance.readNextLine();
169 assertTrue("apples, oranges, pears".equals(line));
170
171 line = instance.readNextLine();
172 assertTrue("one , two , three".equals(line));
173
174 line = instance.readNextLine();
175 assertTrue("a,b,c,d,e,f".equals(line));
176
177 line = instance.readNextLine();
178 assertTrue("unos, dos,tres,,quatorze".equals(line));
179
180 line = instance.readNextLine();
181 assertTrue("last, complete, line,".equals(line));
182
183 line = instance.readNextLine();
184 assertNull(line);
185
186 } catch (IOException ex) {
187 fail("Caught IOException in testReadNextLine()");
188 }
189 }
190
191 @Test
192 public void testRead() {
193 try (FileInput instance = new FileInput(testsFileName)) {
194
195 String str = "token1,token2,token3\n\napples, oranges, pears\none , two , three\na,b,c,d,e,f\nunos, dos,tres,,quatorze\n\nlast, complete, line,\n# comment1, comment2, comment3\n\n";
196 String contents = instance.read();
197 assertTrue(str.equals(contents));
198
199 } catch (IOException ex) {
200 fail("Caught IOException in testReadNextLine()");
201 }
202 }
203
204 private static String testsFileName;
205 }