001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.File;
020: import java.io.FileNotFoundException;
021: import java.io.FileReader;
022: import java.io.IOException;
023: import java.io.Reader;
024: import java.io.UnsupportedEncodingException;
025: import java.util.ArrayList;
026: import java.util.List;
027: import java.util.NoSuchElementException;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031: import junit.textui.TestRunner;
032:
033: import org.apache.commons.io.testtools.FileBasedTestCase;
034:
035: /**
036: * This is used to test LineIterator for correctness.
037: *
038: * @author Niall Pemberton
039: * @author Stephen Colebourne
040: * @version $Id: LineIteratorTestCase.java 437567 2006-08-28 06:39:07Z bayard $
041: */
042: public class LineIteratorTestCase extends FileBasedTestCase {
043:
044: public static void main(String[] args) {
045: TestRunner.run(suite());
046: }
047:
048: public static Test suite() {
049: return new TestSuite(LineIteratorTestCase.class);
050: }
051:
052: public LineIteratorTestCase(String name) throws IOException {
053: super (name);
054: }
055:
056: /** @see junit.framework.TestCase#setUp() */
057: protected void setUp() throws Exception {
058: File dir = getTestDirectory();
059: if (dir.exists()) {
060: FileUtils.deleteDirectory(dir);
061: }
062: dir.mkdirs();
063:
064: }
065:
066: /** @see junit.framework.TestCase#tearDown() */
067: protected void tearDown() throws Exception {
068: FileUtils.deleteDirectory(getTestDirectory());
069: }
070:
071: //-----------------------------------------------------------------------
072: /**
073: * Test constructor.
074: */
075: public void testConstructor() throws Exception {
076: try {
077: new LineIterator((Reader) null);
078: fail();
079: } catch (IllegalArgumentException ex) {
080: // expected
081: }
082: }
083:
084: /**
085: * Test a file with no lines.
086: */
087: public void testZeroLines() throws Exception {
088: doTestFileWithSpecifiedLines(0);
089: }
090:
091: /**
092: * Test a file with 1 line.
093: */
094: public void testOneLines() throws Exception {
095: doTestFileWithSpecifiedLines(1);
096: }
097:
098: /**
099: * Test a file with 2 lines.
100: */
101: public void testTwoLines() throws Exception {
102: doTestFileWithSpecifiedLines(2);
103: }
104:
105: /**
106: * Test a file with 3 lines.
107: */
108: public void testThreeLines() throws Exception {
109: doTestFileWithSpecifiedLines(3);
110: }
111:
112: /**
113: * Test a missing File.
114: */
115: public void testMissingFile() throws Exception {
116: File testFile = new File(getTestDirectory(),
117: "dummy-missing-file.txt");
118:
119: LineIterator iterator = null;
120: try {
121: iterator = FileUtils.lineIterator(testFile, "UTF-8");
122: fail("Expected FileNotFoundException");
123: } catch (FileNotFoundException expected) {
124: // ignore, expected result
125: } finally {
126: LineIterator.closeQuietly(iterator);
127: }
128: }
129:
130: /**
131: * Test a file with a Valid encoding.
132: */
133: public void testValidEncoding() throws Exception {
134: String encoding = "UTF-8";
135:
136: File testFile = new File(getTestDirectory(),
137: "LineIterator-validEncoding.txt");
138: createFile(testFile, encoding, 3);
139:
140: LineIterator iterator = FileUtils.lineIterator(testFile,
141: encoding);
142: try {
143: int count = 0;
144: while (iterator.hasNext()) {
145: assertTrue(iterator.next() instanceof String);
146: count++;
147: }
148: assertEquals(3, count);
149: } finally {
150: LineIterator.closeQuietly(iterator);
151: }
152: }
153:
154: /**
155: * Test a file with an Invalid encoding.
156: */
157: public void testInvalidEncoding() throws Exception {
158: String encoding = "XXXXXXXX";
159:
160: File testFile = new File(getTestDirectory(),
161: "LineIterator-invalidEncoding.txt");
162: createFile(testFile, "UTF-8", 3);
163:
164: LineIterator iterator = null;
165: try {
166: iterator = FileUtils.lineIterator(testFile, encoding);
167: fail("Expected UnsupportedEncodingException");
168: } catch (UnsupportedEncodingException expected) {
169: // ignore, expected result
170: } finally {
171: LineIterator.closeQuietly(iterator);
172: }
173: }
174:
175: /**
176: * Test the iterator using only the next() method.
177: */
178: public void testNextOnly() throws Exception {
179: String encoding = null;
180:
181: File testFile = new File(getTestDirectory(),
182: "LineIterator-nextOnly.txt");
183: List lines = createFile(testFile, encoding, 3);
184:
185: LineIterator iterator = FileUtils.lineIterator(testFile,
186: encoding);
187: try {
188: for (int i = 0; i < lines.size(); i++) {
189: String line = (String) iterator.next();
190: assertEquals("next() line " + i, lines.get(i), line);
191: }
192: assertEquals("No more expected", false, iterator.hasNext());
193: } finally {
194: LineIterator.closeQuietly(iterator);
195: }
196: }
197:
198: /**
199: * Test the iterator using only the nextLine() method.
200: */
201: public void testNextLineOnly() throws Exception {
202: String encoding = null;
203:
204: File testFile = new File(getTestDirectory(),
205: "LineIterator-nextOnly.txt");
206: List lines = createFile(testFile, encoding, 3);
207:
208: LineIterator iterator = FileUtils.lineIterator(testFile,
209: encoding);
210: try {
211: for (int i = 0; i < lines.size(); i++) {
212: String line = iterator.nextLine();
213: assertEquals("nextLine() line " + i, lines.get(i), line);
214: }
215: assertFalse("No more expected", iterator.hasNext());
216: } finally {
217: LineIterator.closeQuietly(iterator);
218: }
219: }
220:
221: /**
222: * Test closing the iterator before all the file has been
223: * processed.
224: */
225: public void testCloseEarly() throws Exception {
226: String encoding = "UTF-8";
227:
228: File testFile = new File(getTestDirectory(),
229: "LineIterator-closeEarly.txt");
230: createFile(testFile, encoding, 3);
231:
232: LineIterator iterator = FileUtils.lineIterator(testFile,
233: encoding);
234: try {
235: // get
236: assertTrue("Line expected",
237: iterator.next() instanceof String);
238: assertTrue("More expected", iterator.hasNext());
239:
240: // close
241: iterator.close();
242: assertFalse("No more expected", iterator.hasNext());
243: try {
244: iterator.next();
245: fail();
246: } catch (NoSuchElementException ex) {
247: // expected
248: }
249: try {
250: iterator.nextLine();
251: fail();
252: } catch (NoSuchElementException ex) {
253: // expected
254: }
255:
256: // try closing again
257: iterator.close();
258: try {
259: iterator.next();
260: fail();
261: } catch (NoSuchElementException ex) {
262: // expected
263: }
264: try {
265: iterator.nextLine();
266: fail();
267: } catch (NoSuchElementException ex) {
268: // expected
269: }
270: } finally {
271: LineIterator.closeQuietly(iterator);
272: }
273: }
274:
275: /**
276: * Utility method to create and test a file with a specified
277: * number of lines.
278: */
279: private void doTestFileWithSpecifiedLines(int lineCount)
280: throws Exception {
281: String encoding = "UTF-8";
282:
283: String fileName = "LineIterator-" + lineCount + "-test.txt";
284: File testFile = new File(getTestDirectory(), fileName);
285: List lines = createFile(testFile, encoding, lineCount);
286:
287: LineIterator iterator = FileUtils.lineIterator(testFile,
288: encoding);
289: try {
290: try {
291: iterator.remove();
292: fail("Remove is unsupported");
293: } catch (UnsupportedOperationException ex) {
294: // expected
295: }
296:
297: int idx = 0;
298: while (iterator.hasNext()) {
299: String line = (String) iterator.next();
300: assertEquals("Comparing line " + idx, lines.get(idx),
301: line);
302: assertTrue("Exceeded expected idx=" + idx + " size="
303: + lines.size(), idx < lines.size());
304: idx++;
305: }
306: assertEquals("Line Count doesn't match", idx, lines.size());
307:
308: // try calling next() after file processed
309: try {
310: iterator.next();
311: fail("Expected NoSuchElementException");
312: } catch (NoSuchElementException expected) {
313: // ignore, expected result
314: }
315: try {
316: iterator.nextLine();
317: fail("Expected NoSuchElementException");
318: } catch (NoSuchElementException expected) {
319: // ignore, expected result
320: }
321: } finally {
322: LineIterator.closeQuietly(iterator);
323: }
324: }
325:
326: /**
327: * Utility method to create a test file with a specified
328: * number of lines.
329: */
330: private List createFile(File file, String encoding, int lineCount)
331: throws Exception {
332: List lines = new ArrayList();
333: for (int i = 0; i < lineCount; i++) {
334: lines.add("LINE " + i);
335: }
336: FileUtils.writeLines(file, encoding, lines);
337: return lines;
338: }
339:
340: //-----------------------------------------------------------------------
341: public void testFiltering() throws Exception {
342: String encoding = "UTF-8";
343:
344: String fileName = "LineIterator-Filter-test.txt";
345: File testFile = new File(getTestDirectory(), fileName);
346: List lines = createFile(testFile, encoding, 9);
347:
348: Reader reader = new FileReader(testFile);
349: LineIterator iterator = new LineIterator(reader) {
350: protected boolean isValidLine(String line) {
351: char c = line.charAt(line.length() - 1);
352: return ((c - 48) % 3 != 1);
353: }
354: };
355: try {
356: try {
357: iterator.remove();
358: fail("Remove is unsupported");
359: } catch (UnsupportedOperationException ex) {
360: // expected
361: }
362:
363: int idx = 0;
364: int actualLines = 0;
365: while (iterator.hasNext()) {
366: String line = (String) iterator.next();
367: actualLines++;
368: assertEquals("Comparing line " + idx, lines.get(idx),
369: line);
370: assertTrue("Exceeded expected idx=" + idx + " size="
371: + lines.size(), idx < lines.size());
372: idx++;
373: if (idx % 3 == 1) {
374: idx++;
375: }
376: }
377: assertEquals("Line Count doesn't match", 9, lines.size());
378: assertEquals("Line Count doesn't match", 9, idx);
379: assertEquals("Line Count doesn't match", 6, actualLines);
380:
381: // try calling next() after file processed
382: try {
383: iterator.next();
384: fail("Expected NoSuchElementException");
385: } catch (NoSuchElementException expected) {
386: // ignore, expected result
387: }
388: try {
389: iterator.nextLine();
390: fail("Expected NoSuchElementException");
391: } catch (NoSuchElementException expected) {
392: // ignore, expected result
393: }
394: } finally {
395: LineIterator.closeQuietly(iterator);
396: }
397: }
398:
399: }
|