1   /*
2   	StatCvs - CVS statistics generation 
3   	Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4   	http://statcvs.sf.net/
5       
6   	This library is free software; you can redistribute it and/or
7   	modify it under the terms of the GNU Lesser General Public
8   	License as published by the Free Software Foundation; either
9   	version 2.1 of the License, or (at your option) any later version.
10  
11  	This library is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  	Lesser General Public License for more details.
15  
16  	You should have received a copy of the GNU Lesser General Public
17  	License along with this library; if not, write to the Free Software
18  	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19      
20  	$RCSfile: LookaheadReaderTest.java,v $
21  	$Date: 2008/04/02 11:22:15 $
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              // is expected
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              // is expected
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 }