View Javadoc

1   /*
2    * Copyright 2013 University of Glasgow.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Test cases for broadwick.io.FileOutput class.
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 // the magic is done here
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             // TODO review the generated test code and remove the default call to fail.
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         /* String format = "";
118          String result = "";
119          Object[] args = {""};
120          FileOutput instance = new FileOutput(testsFileName);
121          String expResult = "%d";
122          InputStream fis = null;
123          BufferedReader br;
124          try {
125          result = instance.write(format, args);
126          //assertEquals(expResult, result);
127          } catch (BroadwickIOException ex) {
128          fail("Caught BroadwickIOException in testWrite_String_ObjectArr()");
129          }
130          try {
131          fis = new FileInputStream(testsFileName);
132          } catch (FileNotFoundException ex) {
133          fail("Caught FileNotFoundException in testWrite_String_ObjectArr()");
134          }
135          br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
136          try {
137          if (br.readLine().equals(result)) {
138          // Do nothing as we expect it to be equal
139          } else {
140          fail("Fail in testWrite_String_ObjectArr - line written to file unsuccessfully");
141          }
142          } catch (IOException ex) {
143          fail("Caught IOException in testWrite_String_ObjectArr()");
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                 // we expect an BroadwickIOException to be caught here because the FileOutput object is closed when the write 
164                 // method is called.
165             }
166         } catch (BroadwickIOException ex) {
167             fail("Caught BroadwickIOException in testClose()");
168         }
169 
170     }
171 
172     @Test
173     public void testFlush() {
174         /*   System.out.println("flush");
175          FileOutput instance = null;
176          instance.flush();
177          // TODO review the generated test code and remove the default call to fail.
178          fail("The test case is a prototype.");*/
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         /*    String format = "";
207          boolean addVersion = false;
208          Object[] args = null;
209          InputStream fis = null;
210          BufferedReader br;
211          String dataFileName = "test-saved-file.csv";
212          String data = "token1,token2,token3";
213          FileOutput instance = new FileOutput(testsFileName);
214          FileOutput.saveToFile(dataFileName, format, addVersion, args);
215          try {
216          fis = new FileInputStream(testsFileName);
217          } catch (FileNotFoundException ex) {
218          fail("Fail in testSaveToFile_4args - saved file not found");
219          }
220          br = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
221          try {
222          if (br.readLine().equals(data)) {
223          // Do nothing as we expect it to be equal
224          } else {
225          fail("Fail in testSaveToFile_4args - save to file unsuccessful");
226          }
227          } catch (IOException ex) {
228          fail("Fail in testSaveToFile_3args - IO exception");
229          } */
230     }
231 
232 }