View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
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 org.openehealth.ipf.platform.camel.core.process.splitter.support;
17  
18  import static org.junit.Assert.*;
19  
20  import java.io.BufferedWriter;
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.io.FileWriter;
24  
25  import org.junit.After;
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  
30  /**
31   * @author Jens Riemschneider
32   */
33  public class TextFileIteratorTest {
34      private File file; 
35  
36      @Before
37      public void setUp() throws Exception {
38          file = File.createTempFile("TextFileIteratorTest", "txt");
39          
40          BufferedWriter writer = new BufferedWriter(new FileWriter(file));
41          writer.write("a,b,17,4\n");            
42          writer.write("c,d,e,dotter");            
43          writer.close();
44      }
45      
46      @After
47      public void tearDown() throws Exception {
48          assertTrue(file.delete());      // If this fails the last test did not
49                                          // close the FileReader in the 
50                                          // TextFileIterator
51      }
52      
53      @Test
54      public void testSimpleIteration() throws Exception {
55          TextFileIterator iterator = new TextFileIterator(file.getAbsolutePath());
56          assertTrue(iterator.hasNext());
57          assertEquals("a,b,17,4", iterator.next());
58          assertTrue(iterator.hasNext());
59          assertEquals("c,d,e,dotter", iterator.next());
60          assertFalse(iterator.hasNext());
61      }
62  
63      @Test
64      public void testLineSplitLogic() throws Exception {
65          TextFileIterator iterator = new TextFileIterator(
66                  file.getAbsolutePath(),
67                  new SplitStringLineSplitterLogic(","));
68          
69          assertTrue(iterator.hasNext());
70          assertEquals("a", iterator.next());
71          assertTrue(iterator.hasNext());
72          assertEquals("b", iterator.next());
73          assertTrue(iterator.hasNext());
74          assertEquals("17", iterator.next());
75          assertTrue(iterator.hasNext());
76          assertEquals("4", iterator.next());
77          assertTrue(iterator.hasNext());
78          assertEquals("c", iterator.next());
79          assertTrue(iterator.hasNext());
80          assertEquals("d", iterator.next());
81          assertTrue(iterator.hasNext());
82          assertEquals("e", iterator.next());
83          assertTrue(iterator.hasNext());
84          assertEquals("dotter", iterator.next());
85          assertFalse(iterator.hasNext());
86      }
87  
88      @Test
89      public void testHugeFile() throws Exception {
90          final int NUMBER_OF_LINES = 10000;
91          
92          File hugeFile = File.createTempFile("testHugeFile", "txt");
93          try {
94              BufferedWriter writer = new BufferedWriter(new FileWriter(hugeFile));
95              for (int idx = 1; idx <= NUMBER_OF_LINES; ++idx) {
96                  writer.write("Line " + idx + "\n");            
97              }
98              writer.close();
99              
100             System.out.println("Test file written");
101             
102             TextFileIterator iterator = new TextFileIterator(hugeFile.getAbsolutePath());
103             int idx = 1;
104             while (iterator.hasNext()) {
105                 String line = iterator.next();
106                 assertEquals("Line " + idx, line);
107                 ++idx;
108             }
109         }
110         finally {
111             assertTrue(hugeFile.delete());
112         }
113     }
114     
115     @Test(expected=UnsupportedOperationException.class)
116     public void testRemoveNotSupported() throws Exception {
117         TextFileIterator iterator = new TextFileIterator(file.getAbsolutePath());
118         try {
119             iterator.remove();
120         }
121         finally {
122             assertTrue(iterator.isClosed());
123         }
124     }    
125     
126     @Test(expected=FileNotFoundException.class)
127     public void testFileNotFound() throws Exception {
128         new TextFileIterator("isnotthere");
129     }
130     
131     @Test(expected=IllegalStateException.class)
132     public void testSafeAbortOfIteration() throws Exception {
133         TextFileIterator iterator = new TextFileIterator(file.getAbsolutePath());
134         iterator.close();
135         iterator.next();
136     }
137 }
138