TextKitTestSuite.java

 

Download TextKitTestSuite.java source file

View/Download TextKit.java source file

/* TextKitTestSuite - A JUnit set of tests for the utils.TextKit class
 * methods of "lineOfStars" and "pad".  While this may seem like a lot
 * of work for little gain, many studies have proved that bugs are more
 * expensive to fix later.  More that ten times the cost!  So doubling
 * your effort up front by creating test cases really saves big time
 * down the road for real (not toy like this) projects.
 *
 * There is a popular school of thought that says to write the test cases
 * first (from the design), then write the project code.  Also, whenever a
 * bug is found, write a test case that reproduces it before attemting to
 * fix the bug.
 *
 * Not every method should have test cases, only when it would be useful.
 * Getter and Setter methods generally don't need test cases.
 *
 * It is never possible to completely test any non-trivial program!  Still a
 * little testing goes a long way - most students give up creating test cases
 * long before the point of deminishing returns is reached.  For each method
 * you should try to create test cases for:
 *
 *   Normal, expected inputs: a couple of test cases of these is enough
 *
 *   Unusual inputs: zero, negative, wierd text, zero length strings and arrays
 *
 *   Illegal inputs: null for object references, illegal values (numbers for
 *       strings, objects for numbers, values outside of expected ranges such
 *       as negative coordinates), and illegal combinations of values
 *
 *   Stress tests:  huge number of inputs (long lists, large files, large
 *       number of files), huge numer of network connections, multiple
 *       instances running at once.
 *
 * As you write the code, you can add test cases to make sure all code is
 * "exercised" by some test case: every branch of if and switch blocks, ...
 *
 * (C) 2005-2006 Wayne Pollock, Tampa FL USA.  All Rights Reserved.
 */

import org.junit.*;                // For the annotations (Test, Ignore, ...)
import org.junit.runner.JUnitCore; // The test runner
import static org.junit.Assert.*;  // For the various AssertXXX methods

import java.util.Arrays;
import static utils.TextKit.*;

/* To use JUnit, make sure junit.jar is listed on CLASSPATH.
 * JUnit tests work by examining the class for specially named methods
 * and these in turn look to the file system for the .class files used.
 * For some reason this scheme won't work if junit.jar is found
 * by using the extensions directory so you really must set CLASSPATH.
 * (Don't forget to add "." to CLASSPATH too!)
 *
 * All test cases are public void methods that take no arguments,
 * and are marked with @Test annotation.  See the junit.framework.Assert
 * JavaDoc for the various assertion tests you can use.
 */

// Create a set of tests (a test "suite"):
public class TextKitTestSuite
{
    // This main method is just for convenience, so we can run the
    // test suite easily with:  java TextKitTestSuite

    public static void main ( String [] args )
    {
        JUnitCore.main( "TextKitTestSuite" );

        // run from cmd line in JUnit4:
        //    java -ea org.junit.runner.JUnitCore TextKitTestSuite
    }

    /***************** Test cases go here ****************/

    @Test
    public void test_LineOfStars_NormalInput ()
    {   final int num = 5;
        final String stars = lineOfStars( num );

        // A for-loop and StringBuilder would be more efficient but not as nifty:
        char [] starArray = new char[ num ];
        Arrays.fill( starArray, '*' );
        final String expectedResult = new String( starArray );

        // The message is displayed if the boolean expression is true:

        assertFalse( "Result is null string with input of " + num
            + ".", stars == null );

        assertFalse( "Result length should be " + num + " but was "
            + stars.length() + ".", stars.length() != num );

        assertEquals( "Result should be \"" + expectedResult + "\", but was \""
            + stars + "\".", stars, expectedResult );
    }

    @Test
    public void test_LineOfStars_ZeroInput ()
    {   final String stars = lineOfStars( 0 );

        assertFalse( "Result is null string with input of \"0\".",
            stars == null );

        assertEquals( "Result should be zero-length String, but was \""
                + stars + "\".", stars, "" );
    }

    @Test(expected = IllegalArgumentException.class)
    public void testLineOfStarsNegativeInput ()
    {   final String stars = lineOfStars( -5 );
        // If no exception is thrown, the test will fail.
    }

    @Test
    public void test_LineOfStars_LargeInput ()
    {   final int num = 1000;
        final String stars = lineOfStars( num );

        // A for-loop and StringBuilder would be more efficient but not as nifty:
        char [] starArray = new char[ num ];
        Arrays.fill( starArray, '*' );
        final String expectedResult = new String( starArray );

        // The message is displayed if the boolean expression is true:

        assertFalse( "Result is null string with input of " + num
            + ".", stars == null );

        assertFalse( "Result length should be " + num + " but was "
            + stars.length() + ".", stars.length() != num );

        assertEquals( "Result should be \"" + expectedResult + "\", but was \""
            + stars + "\".", stars, expectedResult );
    }

    @Ignore("not ready yet")  // List in output but won't run( v4 feature).
    @Test
    public void test_Pad_LegalInputs ()
    {   final int num = 512, minWidth = 6;
        final String result = pad( num, minWidth );

        assertFalse( "null String returned from: pad(" + num + ", "
                    + minWidth + ").", result == null );

        assertFalse( "String returned too short: pad(" + num + ", "
            + minWidth + ").", result.length() < minWidth );

        assertEquals( "Result not padded corectly from: pad(" + num + ", "
            + minWidth + ").", result.trim(), Integer.toString(num) );
    }

    //  Other test cases are left as an exercise to the reader!
}