import java.io.*; // ------------------------------------------------------------------------- /** * This test case class contains all of the tests for the LineCounter. * * @author Stephen Edwards * @version Feb 22, 2005 */ public class LineCounterTest extends junit.framework.TestCase { //~ Instance/static variables ............................................. private LineCounter lineCounter; //~ Methods ............................................................... // ---------------------------------------------------------- /** * Sets up the test fixture. * Called before every test case method. */ protected void setUp() { lineCounter = new LineCounter(); } // ---------------------------------------------------------- /** * Test the line count on an empty line counter. */ public void testEmptyLines() { assertEquals( 0, lineCounter.lines() ); } // ---------------------------------------------------------- /** * Test the character count on an empty line counter. */ public void testEmptyChars() { assertEquals( 0, lineCounter.characters() ); } // ---------------------------------------------------------- /** * This helper method takes a string, creates a buffered reader that * can read from the string, and uses the test fixture's line counter to * count the lines/chars in the string using this buffered reader. * @param input the string to count */ private void countString( String input ) { try { BufferedReader in = new BufferedReader( new StringReader( input ) ); lineCounter.count( in ); in.close(); } catch ( IOException e ) { e.printStackTrace(); } } // ---------------------------------------------------------- /** * Count an empty input source. */ public void testCountEmpty() { countString( "" ); assertEquals( 0, lineCounter.lines() ); assertEquals( 0, lineCounter.characters() ); } // ---------------------------------------------------------- /** * Count an input source containing a single character. */ public void testCountA() { countString( "a" ); assertEquals( 1, lineCounter.lines() ); assertEquals( 1, lineCounter.characters() ); } // ---------------------------------------------------------- /** * Count an input source containing two lines, where the last line * does not end in a newline. */ public void testCountTwoLines() { countString( "abc\nd" ); assertEquals( 2, lineCounter.lines() ); assertEquals( 4, lineCounter.characters() ); } // ---------------------------------------------------------- /** * Count an input source containing two lines, where the last line * does end in a newline. */ public void testCount3() { countString( "abc\nd\n" ); assertEquals( 2, lineCounter.lines() ); assertEquals( 4, lineCounter.characters() ); } // ---------------------------------------------------------- /** * Count multiple input sources, and check for appropriate * accumulation of multiple counts. */ public void testCount4() { countString( "abc\nd\n" ); assertEquals( 2, lineCounter.lines() ); assertEquals( 4, lineCounter.characters() ); countString( "a" ); assertEquals( 1, lineCounter.lines() ); assertEquals( 1, lineCounter.characters() ); countString( "" ); assertEquals( 0, lineCounter.lines() ); assertEquals( 0, lineCounter.characters() ); } }