Download TextKitTestSuite.java
1: /* TextKitTestSuite - A JUnit set of tests for the utils.TextKit class 2: * methods of "lineOfStars" and "pad". While this may seem like a lot 3: * of work for little gain, many studies have proved that bugs are more 4: * expensive to fix later. More that ten times the cost! So doubling 5: * your effort up front by creating test cases really saves big time 6: * down the road for real (not toy like this) projects. 7: * 8: * There is a popular school of thought that says to write the test cases 9: * first (from the design), then write the project code. Also, whenever a 10: * bug is found, write a test case that reproduces it before attemting to 11: * fix the bug. 12: * 13: * Not every method should have test cases, only when it would be useful. 14: * Getter and Setter methods generally don't need test cases. 15: * 16: * It is never possible to completely test any non-trivial program! Still a 17: * little testing goes a long way - most students give up creating test cases 18: * long before the point of deminishing returns is reached. For each method 19: * you should try to create test cases for: 20: * 21: * Normal, expected inputs: a couple of test cases of these is enough 22: * 23: * Unusual inputs: zero, negative, wierd text, zero length strings and arrays 24: * 25: * Illegal inputs: null for object references, illegal values (numbers for 26: * strings, objects for numbers, values outside of expected ranges such 27: * as negative coordinates), and illegal combinations of values 28: * 29: * Stress tests: huge number of inputs (long lists, large files, large 30: * number of files), huge numer of network connections, multiple 31: * instances running at once. 32: * 33: * As you write the code, you can add test cases to make sure all code is 34: * "exercised" by some test case: every branch of if and switch blocks, ... 35: * 36: * (C) 2005-2006 Wayne Pollock, Tampa FL USA. All Rights Reserved. 37: */ 38: 39: import org.junit.*; // For the annotations (Test, Ignore, ...) 40: import org.junit.runner.JUnitCore; // The test runner 41: import static org.junit.Assert.*; // For the various AssertXXX methods 42: 43: import java.util.Arrays; 44: import static utils.TextKit.*; 45: 46: /* To use JUnit, make sure junit.jar is listed on CLASSPATH. 47: * JUnit tests work by examining the class for specially named methods 48: * and these in turn look to the file system for the .class files used. 49: * For some reason this scheme won't work if junit.jar is found 50: * by using the extensions directory so you really must set CLASSPATH. 51: * (Don't forget to add "." to CLASSPATH too!) 52: * 53: * All test cases are public void methods that take no arguments, 54: * and are marked with @Test annotation. See the junit.framework.Assert 55: * JavaDoc for the various assertion tests you can use. 56: */ 57: 58: // Create a set of tests (a test "suite"): 59: public class TextKitTestSuite 60: { 61: // This main method is just for convenience, so we can run the 62: // test suite easily with: java TextKitTestSuite 63: 64: public static void main ( String [] args ) 65: { 66: JUnitCore.main( "TextKitTestSuite" ); 67: 68: // run from cmd line in JUnit4: 69: // java -ea org.junit.runner.JUnitCore TextKitTestSuite 70: } 71: 72: /***************** Test cases go here ****************/ 73: 74: @Test 75: public void test_LineOfStars_NormalInput () 76: { final int num = 5; 77: final String stars = lineOfStars( num ); 78: 79: // A for-loop and StringBuilder would be more efficient but not as nifty: 80: char [] starArray = new char[ num ]; 81: Arrays.fill( starArray, '*' ); 82: final String expectedResult = new String( starArray ); 83: 84: // The message is displayed if the boolean expression is true: 85: 86: assertFalse( "Result is null string with input of " + num 87: + ".", stars == null ); 88: 89: assertFalse( "Result length should be " + num + " but was " 90: + stars.length() + ".", stars.length() != num ); 91: 92: assertEquals( "Result should be \"" + expectedResult + "\", but was \"" 93: + stars + "\".", expectedResult, stars ); 94: } 95: 96: @Test 97: public void test_LineOfStars_ZeroInput () 98: { final String stars = lineOfStars( 0 ); 99: 100: assertFalse( "Result is null string with input of \"0\".", 101: stars == null ); 102: 103: assertEquals( "Result should be zero-length String, but was \"" 104: + stars + "\".", "", stars ); 105: } 106: 107: @Test(expected = IllegalArgumentException.class) 108: public void testLineOfStarsNegativeInput () 109: { final String stars = lineOfStars( -5 ); 110: // If no exception is thrown, the test will fail. 111: } 112: 113: @Test 114: public void test_LineOfStars_LargeInput () 115: { final int num = 1000; 116: final String stars = lineOfStars( num ); 117: 118: // A for-loop and StringBuilder would be more efficient but not as nifty: 119: char [] starArray = new char[ num ]; 120: Arrays.fill( starArray, '*' ); 121: final String expectedResult = new String( starArray ); 122: 123: // The message is displayed if the boolean expression is true: 124: 125: assertFalse( "Result is null string with input of " + num 126: + ".", stars == null ); 127: 128: assertFalse( "Result length should be " + num + " but was " 129: + stars.length() + ".", stars.length() != num ); 130: 131: assertEquals( "Result should be \"" + expectedResult + "\", but was \"" 132: + stars + "\".", expectedResult, stars ); 133: } 134: 135: @Ignore("not ready yet") // List in output but won't run( v4 feature). 136: @Test 137: public void test_Pad_LegalInputs () 138: { final int num = 512, minWidth = 6; 139: final String result = pad( num, minWidth ); 140: 141: assertFalse( "null String returned from: pad(" + num + ", " 142: + minWidth + ").", result == null ); 143: 144: assertFalse( "String returned too short: pad(" + num + ", " 145: + minWidth + ").", result.length() < minWidth ); 146: 147: assertEquals( "Result not padded corectly from: pad(" + num + ", " 148: + minWidth + ").", result.trim(), Integer.toString(num) ); 149: } 150: 151: // Other test cases are left as an exercise to the reader! 152: }