1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package broadwick.io;
17
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.nio.charset.Charset;
26 import lombok.extern.slf4j.Slf4j;
27 import org.junit.After;
28 import org.junit.AfterClass;
29 import static org.junit.Assert.*;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.ClassRule;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.rules.TestRule;
36 import org.junit.rules.TestWatcher;
37 import org.junit.runner.Description;
38 import org.junit.runners.model.Statement;
39 import org.slf4j.MarkerFactory;
40
41
42
43
44 @Slf4j
45 public class FileOutputTest {
46
47 private static String testsFileName;
48 public FileOutputTest() {
49 }
50
51 @BeforeClass
52 public static void setUpClass() throws IOException {
53 File temp = File.createTempFile("temp-output-test", ".csv");
54 testsFileName = temp.getName();
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 File temp = new File(testsFileName);
68 boolean success = temp.delete();
69 }
70
71 @ClassRule
72 public static TestRule classWatchman = new TestWatcher() {
73 @Override
74 protected void starting(Description description) {
75 log.info(MarkerFactory.getMarker("TEST"), " Running tests from {} ...", description.getClassName());
76 }
77
78 };
79 @Rule
80 public TestRule watchman = new TestWatcher() {
81 @Override
82 public Statement apply(Statement base, Description description) {
83 log.info(MarkerFactory.getMarker("TEST"), " Running {} ...", description.getMethodName());
84 return base;
85 }
86
87 };
88 @Test
89 public void testWrite_String() {
90 InputStream fis = null;
91 BufferedReader br;
92 String line1 = "token1,token2,token3";
93 FileOutput instance = new FileOutput(testsFileName);
94 try {
95 instance.write(line1);
96 } catch (BroadwickIOException ex) {
97 fail("Caught BroadwickIOException in testWrite_String()");
98 }
99 try {
100
101 fis = new FileInputStream(testsFileName);
102 } catch (FileNotFoundException ex) {
103 fail("Caught FileNotFoundException in testWrite_String()");
104 }
105 br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
106 try {
107 if (!br.readLine().equals(line1)) {
108 fail("Fail in testWrite_String - line written to file unsuccessfully");
109 }
110 } catch (IOException ex) {
111 fail("Caught IOException in testWrite_String()");
112 }
113 }
114
115 @Test
116 public void testWrite_String_ObjectArr() {
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 @Test
148 public void testClose() {
149 try {
150 String line1 = "token1,token2,token3";
151 String line2 = "apples, oranges, pears";
152 String line3 = "one , two , three";
153 String line4 = "a,b,c,d,e,f";
154 String line5 = "last, complete, line,";
155 FileOutput instance = new FileOutput(testsFileName);
156 instance.write(line1);
157 instance.write(line2);
158 instance.close();
159 try {
160 instance.write(line5);
161 fail("Expected BroadwickIOException to be thrown after using a method on a closed FileOutput object.");
162 } catch (BroadwickIOException ex) {
163
164
165 }
166 } catch (BroadwickIOException ex) {
167 fail("Caught BroadwickIOException in testClose()");
168 }
169
170 }
171
172 @Test
173 public void testFlush() {
174
175
176
177
178
179 }
180
181 @Test
182 public void testSaveToFile_3args() {
183 InputStream fis = null;
184 BufferedReader br;
185 String data = "token1,token2,token3";
186 boolean addVersion = false;
187 FileOutput.saveToFile(testsFileName, data, addVersion);
188 try {
189 fis = new FileInputStream(testsFileName);
190 } catch (FileNotFoundException ex) {
191 fail("Fail in testSaveToFile_3args - saved file not found");
192 }
193 try {
194 br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
195 String output = br.readLine();
196 if (br == null || !output.equals(data)) {
197 fail("Fail in testSaveToFile_3args - save to file unsuccessful");
198 }
199 } catch (IOException ex) {
200 fail("Fail in testSaveToFile_3args - IO exception");
201 }
202 }
203
204 @Test
205 public void testSaveToFile_4args() {
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 }