1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs.util;
24
25 import java.io.StringReader;
26 import java.util.NoSuchElementException;
27
28 import junit.framework.TestCase;
29
30 /**
31 * Tests for {@link LookaheadReader}
32 *
33 * @author Richard Cyganiak <rcyg@gmx.de>
34 * @version $Id: LookaheadReaderTest.java,v 1.3 2008/04/02 11:22:15 benoitx Exp $
35 */
36 public class LookaheadReaderTest extends TestCase {
37
38 private LookaheadReader l;
39
40 /**
41 * Constructor
42 * @param arg arg
43 */
44 public LookaheadReaderTest(final String arg) {
45 super(arg);
46 }
47
48 /**
49 * @see junit.framework.TestCase#setUp()
50 */
51 protected void setUp() {
52 this.l = new LookaheadReader(new StringReader("1\n2\n3"));
53 }
54
55 /**
56 * Tests creation of a new LookaheadReader and reading of the first line
57 * @throws Exception on error
58 */
59 public void testCreation() throws Exception {
60 assertNotNull(this.l);
61 assertEquals(0, this.l.getLineNumber());
62 assertTrue(this.l.hasNextLine());
63 try {
64 this.l.getCurrentLine();
65 fail();
66 } catch (final NoSuchElementException ex) {
67
68 }
69 }
70
71 /**
72 * Tests {@link LookaheadReader#getCurrentLine} and
73 * {@link LookaheadReader#nextLine} and
74 * {@link LookaheadReader#hasNextLine}.
75 * @throws Exception on error
76 */
77 public void testCurrentLine() throws Exception {
78 assertTrue(this.l.hasNextLine());
79 assertEquals("1", this.l.nextLine());
80 assertTrue(this.l.hasNextLine());
81 assertEquals("1", this.l.getCurrentLine());
82 assertTrue(this.l.hasNextLine());
83 assertEquals("1", this.l.getCurrentLine());
84 assertEquals("2", this.l.nextLine());
85 assertEquals("2", this.l.getCurrentLine());
86 assertEquals("2", this.l.getCurrentLine());
87 assertEquals("3", this.l.nextLine());
88 assertEquals("3", this.l.getCurrentLine());
89 assertEquals("3", this.l.getCurrentLine());
90 assertFalse(this.l.hasNextLine());
91 try {
92 this.l.nextLine();
93 fail();
94 } catch (final NoSuchElementException ex) {
95
96 }
97 }
98
99 /**
100 * Tests {@link LookaheadReader#getLineNumber}
101 * @throws Exception on error
102 */
103 public void testLineNumbers() throws Exception {
104 assertEquals(0, this.l.getLineNumber());
105 this.l.nextLine();
106 assertEquals(1, this.l.getLineNumber());
107 this.l.nextLine();
108 assertEquals(2, this.l.getLineNumber());
109 this.l.getCurrentLine();
110 assertEquals(2, this.l.getLineNumber());
111 this.l.hasNextLine();
112 assertEquals(2, this.l.getLineNumber());
113 this.l.nextLine();
114 assertEquals(3, this.l.getLineNumber());
115 this.l.getCurrentLine();
116 assertEquals(3, this.l.getLineNumber());
117 }
118 }