/* Gregory M. Lobdell, CS& 145, Winter 2010 In class: 04 Feb 2010 AssassinTest.java */ import java.util.*; import java.io.*; /** AssassinTest Class Test the Assassin Manager */ public class AssassinTest { public static final int TEST_COUNT = 39; // change this number if the indention is different for your assignment public static final int INDENT_COUNT = 2; public static String INDENT; public static void main ( String [] args ) { // initialize the number of indent spaces INDENT = ""; for ( int i = 0 ; i < INDENT_COUNT ; i++ ) INDENT += " "; test(); } /** Test all aspects of the Assassin Manager */ public static void test() { System.out.println ( "Testing Assassin Manager: " + TEST_COUNT + " tests..." ); PrintStream origOut = null; PrintStream origError = null; // initialize pass and fail counters, [0] is pass, [1] is fail int[] counts = {0, 0}; try { // Capture the streams so we can listen // @throws java.lang.Exception ByteArrayOutputStream testOutput = new ByteArrayOutputStream(); origOut = System.out; origError = System.err; testOutput = new ByteArrayOutputStream(); PrintStream err = new PrintStream(testOutput); PrintStream out = new PrintStream(testOutput); // set the output streams to the byte array System.setErr(err); System.setOut(out); // get the char(s) produced by "\n" String cr = getCRLF( testOutput, origOut ); AssassinManager theAM; theAM = test_constructor ( "Testing null list to constructor...", origOut, null, false, true, counts ); String[] zeroArray = new String[0]; theAM = test_constructor ( "Testing zero length list to constructor...", origOut, zeroArray, false, true, counts ); String[] oneArray = { "The Winner" }; theAM = test_constructor ( "Testing constructor with one player...", origOut, oneArray, true, false, counts ); String result = INDENT + "The Winner is stalking The Winner" + cr; test_printKillRing ( "Testing printKillRing() with one assassin in the kill ring...", origOut, testOutput, theAM, result, counts ); result = ""; test_printGraveyard ( "Testing printGraveyard() with an empty graveyard...", origOut, testOutput, theAM, result, counts ); test_isGameOver ( "Testing isGameOver() => true...", origOut, testOutput, theAM, true, counts ); test_winner ( "Testing winner() => \"The Winner\"...", origOut, theAM, "The Winner", counts ); String[] multiArray = { "Harry Potter", "Hermione Granger", "Ron Weasley", "Draco Malfoy", "Ginny Weasley", "Luna Lovegood", "Fred Weasley", "George Weasley", "Pansy Parkinson", "Lavender Brown", "Dean Thomas", "Neville Longbottom"}; theAM = test_constructor ( "Testing constructor with many players...", origOut, multiArray, true, false, counts ); result = INDENT + "Harry Potter is stalking Hermione Granger" + cr + INDENT + "Hermione Granger is stalking Ron Weasley" + cr + INDENT + "Ron Weasley is stalking Draco Malfoy" + cr + INDENT + "Draco Malfoy is stalking Ginny Weasley" + cr + INDENT + "Ginny Weasley is stalking Luna Lovegood" + cr + INDENT + "Luna Lovegood is stalking Fred Weasley" + cr + INDENT + "Fred Weasley is stalking George Weasley" + cr + INDENT + "George Weasley is stalking Pansy Parkinson" + cr + INDENT + "Pansy Parkinson is stalking Lavender Brown" + cr + INDENT + "Lavender Brown is stalking Dean Thomas" + cr + INDENT + "Dean Thomas is stalking Neville Longbottom" + cr + INDENT + "Neville Longbottom is stalking Harry Potter" + cr; test_printKillRing ( "Testing printKillRing with many assassins in the kill ring...", origOut, testOutput, theAM, result, counts ); test_isGameOver ( "Testing isGameOver() => false...", origOut, testOutput, theAM, false, counts ); test_winner ( "Testing winner() => null...", origOut, theAM, null, counts ); test_killRingContains ( "Testing killRingContains( \"Luna Lovegood\" ) => true...", origOut, theAM, "Luna Lovegood", true, counts ); test_killRingContains ( "Testing ignore case killRingContains( \"luna LOVEGOOD\" ) => true...", origOut, theAM, "luna LOVEGOOD", true, counts ); test_killRingContains ( "Testing killRingContains( \"Albus Percival Wulfric Brian Dumbledore\" ) => false...", origOut, theAM, "Albus Percival Wulfric Brian Dumbledore", false, counts ); boolean[] resultsNormal = {true, false, false}; test_kill ( "Testing kill( \"Luna Lovegood\" )...", origOut, theAM, "Luna Lovegood", resultsNormal, counts ); test_killRingContains ( "Testing killRingContains( \"Luna Lovegood\" ) => false...", origOut, theAM, "Luna Lovegood", false, counts ); test_killRingContains ( "Testing killRingContains( \"Harry Potter\" ) => true...", origOut, theAM, "Harry Potter", true, counts ); test_killRingContains ( "Testing killRingContains( \"Neville Longbottom\" ) => true...", origOut, theAM, "Neville Longbottom", true, counts ); test_graveyardContains ( "Testing graveyardContains( \"Luna Lovegood\" ) => true...", origOut, theAM, "Luna Lovegood", true, counts ); test_graveyardContains ( "Testing graveyardContains( \"Harry Potter\" ) => false...", origOut, theAM, "Harry Potter", false, counts ); test_kill ( "Testing kill( \"Harry Potter\" ) first in list...", origOut, theAM, "Harry Potter", resultsNormal, counts ); result = INDENT + "Hermione Granger is stalking Ron Weasley" + cr + INDENT + "Ron Weasley is stalking Draco Malfoy" + cr + INDENT + "Draco Malfoy is stalking Ginny Weasley" + cr + INDENT + "Ginny Weasley is stalking Fred Weasley" + cr + INDENT + "Fred Weasley is stalking George Weasley" + cr + INDENT + "George Weasley is stalking Pansy Parkinson" + cr + INDENT + "Pansy Parkinson is stalking Lavender Brown" + cr + INDENT + "Lavender Brown is stalking Dean Thomas" + cr + INDENT + "Dean Thomas is stalking Neville Longbottom" + cr + INDENT + "Neville Longbottom is stalking Hermione Granger" + cr; test_printKillRing ( "Testing printKillRing()...", origOut, testOutput, theAM, result, counts ); test_kill ( "Testing kill( \"Neville Longbottom\" ) last in list...", origOut, theAM, "Neville Longbottom", resultsNormal, counts ); result = INDENT + "Hermione Granger is stalking Ron Weasley" + cr + INDENT + "Ron Weasley is stalking Draco Malfoy" + cr + INDENT + "Draco Malfoy is stalking Ginny Weasley" + cr + INDENT + "Ginny Weasley is stalking Fred Weasley" + cr + INDENT + "Fred Weasley is stalking George Weasley" + cr + INDENT + "George Weasley is stalking Pansy Parkinson" + cr + INDENT + "Pansy Parkinson is stalking Lavender Brown" + cr + INDENT + "Lavender Brown is stalking Dean Thomas" + cr + INDENT + "Dean Thomas is stalking Hermione Granger" + cr; test_printKillRing ( "Testing printKillRing()...", origOut, testOutput, theAM, result, counts ); result = INDENT + "Neville Longbottom was killed by Dean Thomas" + cr + INDENT + "Harry Potter was killed by Neville Longbottom" + cr + INDENT + "Luna Lovegood was killed by Ginny Weasley" + cr; test_printGraveyard( "Testing printGraveyard() should print Neville, Harry & Luna...", origOut, testOutput, theAM, result, counts ); boolean[] resultsIllegalArg = {false, true, false}; test_kill ( "Testing kill(\"Albus Dumbledore\") not in list...", origOut, theAM, "Albus Dumbledore", resultsIllegalArg, counts ); origOut.print ( "Killing all but two players..."); try { theAM.kill ( "Lavender Brown" ); theAM.kill ( "George Weasley" ); theAM.kill ( "Dean Thomas" ); theAM.kill ( "Ginny Weasley" ); theAM.kill ( "Draco Malfoy" ); theAM.kill ( "Fred Weasley" ); theAM.kill ( "Pansy Parkinson" ); origOut.println ( "PASS" ); counts[0]++; } catch ( Exception e ) { origOut.println ( "FAIL" ); counts[1]++; } test_kill ( "Testing kill(\"Dean Thomas\") already killed...", origOut, theAM, "Dean Thomas", resultsIllegalArg, counts ); result = INDENT + "Hermione Granger is stalking Ron Weasley" + cr + INDENT + "Ron Weasley is stalking Hermione Granger" + cr; test_printKillRing ( "Testing printKillRing()...", origOut, testOutput, theAM, result, counts ); result = INDENT + "Pansy Parkinson was killed by Ron Weasley" + cr + INDENT + "Fred Weasley was killed by Ron Weasley" + cr + INDENT + "Draco Malfoy was killed by Ron Weasley" + cr + INDENT + "Ginny Weasley was killed by Draco Malfoy" + cr + INDENT + "Dean Thomas was killed by Pansy Parkinson" + cr + INDENT + "George Weasley was killed by Fred Weasley" + cr + INDENT + "Lavender Brown was killed by Pansy Parkinson" + cr + INDENT + "Neville Longbottom was killed by Dean Thomas" + cr + INDENT + "Harry Potter was killed by Neville Longbottom" + cr + INDENT + "Luna Lovegood was killed by Ginny Weasley" + cr; test_printGraveyard( "Testing printGraveyard()...", origOut, testOutput, theAM, result, counts ); test_kill ( "Testing kill(\"Ron Weasley\"), the final player...", origOut, theAM, "Ron Weasley", resultsNormal, counts ); result = INDENT + "Hermione Granger is stalking Hermione Granger" + cr; test_printKillRing ( "Testing printKillRing()...", origOut, testOutput, theAM, result, counts ); result = INDENT + "Ron Weasley was killed by Hermione Granger" + cr + INDENT + "Pansy Parkinson was killed by Ron Weasley" + cr + INDENT + "Fred Weasley was killed by Ron Weasley" + cr + INDENT + "Draco Malfoy was killed by Ron Weasley" + cr + INDENT + "Ginny Weasley was killed by Draco Malfoy" + cr + INDENT + "Dean Thomas was killed by Pansy Parkinson" + cr + INDENT + "George Weasley was killed by Fred Weasley" + cr + INDENT + "Lavender Brown was killed by Pansy Parkinson" + cr + INDENT + "Neville Longbottom was killed by Dean Thomas" + cr + INDENT + "Harry Potter was killed by Neville Longbottom" + cr + INDENT + "Luna Lovegood was killed by Ginny Weasley" + cr; test_printGraveyard( "Testing printGraveyard()...", origOut, testOutput, theAM, result, counts ); test_isGameOver ( "Testing isGameOver() => true...", origOut, testOutput, theAM, true, counts ); test_winner ( "Testing winner() => \"Hermione Granger\"...", origOut, theAM, "Hermione Granger", counts ); boolean[] resultsBothErr = { false, true, true }; test_kill ( "Testing kill(\"Albus Dumbledore\") not in list...", origOut, theAM, "Ron Weasley", resultsBothErr, counts ); boolean[] resultsIllegalState = { false, false, true }; test_kill ( "Testing kill(\"Hermione Granger\") when isGameOver()==true...", origOut, theAM, "Hermione Granger", resultsIllegalState, counts ); result = "alive: [Hermione Granger] dead: [Ron Weasley, Pansy Parkinson, Fred Weasley, Draco Malfoy, Ginny Weasley, Dean Thomas, George Weasley, Lavender Brown, Neville Longbottom, Harry Potter, Luna Lovegood]"; test_toString ( "Testing optional toString()...", origOut, theAM, result, counts ); theAM = new AssassinManager(Arrays.asList(multiArray)); result = "alive: [Harry Potter, Hermione Granger, Ron Weasley, Draco Malfoy, Ginny Weasley, " + "Luna Lovegood, Fred Weasley, George Weasley, Pansy Parkinson, Lavender Brown, Dean Thomas, " + "Neville Longbottom] dead: []"; test_toString ( "Testing optional toString() with new AssassinManager...", origOut, theAM, result, counts ); printReport ( origOut, counts ); } catch (Exception e) { if ( origOut == null ) origOut = System.out; origOut.println ( "\nUnhandled exception: " + e ); counts[1]++; printReport ( origOut, counts ); } } public static void test_printGraveyard ( String message, PrintStream stdout, ByteArrayOutputStream testout, AssassinManager theAM, String result, int[] counts ) { stdout.print ( message ); try { theAM.printGraveyard(); if ( testout.toString().equals(result) ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } testout.reset(); } catch ( Exception e ) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; } } public static void test_printKillRing ( String message, PrintStream stdout, ByteArrayOutputStream testout, AssassinManager theAM, String result, int[] counts ) { stdout.print ( message ); try { theAM.printKillRing(); if ( testout.toString().equals(result) ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } testout.reset(); } catch ( Exception e ) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; } } public static void test_isGameOver ( String message, PrintStream stdout, ByteArrayOutputStream testout, AssassinManager theAM, boolean result, int[] counts ) { stdout.print ( message ); try { if ( theAM.isGameOver() == result ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } } catch ( Exception e ) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; } } public static void test_kill ( String message, PrintStream stdout, AssassinManager theAM, String arg, boolean[] result, int[] counts ) { stdout.print ( message ); try { theAM.kill ( arg ); if ( result[0] ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } } catch (IllegalArgumentException e) { if ( result[1] ) { stdout.println ( "exception: IllegalArgumentException - PASS" ); counts[0]++; } else { stdout.println ( "FAIL - IllegalArgumentException: " + e ); counts[1]++; } } catch (IllegalStateException e) { if ( result[2] ) { stdout.println ( "exception: IllegalStateException - PASS" ); counts[0]++; } else { stdout.println ( "FAIL - IllegalStateException: " + e ); counts[1]++; } } catch (Exception e) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; } } public static void test_killRingContains ( String message, PrintStream stdout, AssassinManager theAM, String arg, boolean result, int[] counts ) { stdout.print ( message ); try { if ( theAM.killRingContains( arg ) == result ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } } catch ( Exception e ) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; } } public static void test_graveyardContains ( String message, PrintStream stdout, AssassinManager theAM, String arg, boolean result, int[] counts ) { stdout.print ( message ); try { if ( theAM.graveyardContains( arg ) == result ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } } catch ( Exception e ) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; } } public static void test_winner ( String message, PrintStream stdout, AssassinManager theAM, String result, int[] counts ) { stdout.print ( message ); try { String value = theAM.winner(); if ( value == null && result == null ) { stdout.println ( "PASS" ); counts[0]++; } else if ( theAM.winner().equals ( result ) ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } } catch ( Exception e ) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; } } public static AssassinManager test_constructor ( String message, PrintStream stdout, String[] arg, boolean success, boolean exception, int[] counts ) { String[] MESSAGE = { "PASS", "FAIL" }; List newArg = null; if ( arg != null ) newArg = Arrays.asList(arg); AssassinManager theAM; try { stdout.print ( message ); theAM = new AssassinManager(newArg); if ( success ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } } catch ( IllegalArgumentException e ) { if ( exception ) { stdout.println ( "PASS" ); counts[0]++; } else { stdout.println ( "FAIL - IllegalArgumentException: " + e ); counts[1]++; } theAM = null; } catch ( Exception e ) { stdout.println ( "FAIL - exception: " + e ); counts[1]++; theAM = null; } return theAM; } public static void test_toString ( String message, PrintStream stdout, AssassinManager theAM, String result, int[] counts ) { try { stdout.print ( message ); String defaultString = theAM.getClass().getCanonicalName() + "@" + Integer.toHexString(theAM.hashCode()); if ( theAM.toString().equals(result) ) { stdout.println ( "PASS" ); counts[0]++; } else if ( theAM.toString().equals(defaultString) ) { stdout.println ( "default toString() method - PASS" ); counts[0]++; } else { stdout.println ( "FAIL" ); counts[1]++; } } catch ( Exception e ) { stdout.println ( "FAIL = exception: " + e ); counts[1]++; } } public static void printReport ( PrintStream stdout, int[] counts ) { if ( stdout == null ) stdout = System.out; stdout.println (); if ( counts[0] + counts[1] == TEST_COUNT ) { stdout.println ( "Testing Complete - PASS: " + counts[0] + "/" + TEST_COUNT + " FAIL: " + counts[1] ); } else { stdout.println ( "Test Score - PASS: " + counts[0] + "/" + TEST_COUNT + " FAIL: " + counts[1] ); } if ( counts[0] == TEST_COUNT && counts[1] == 0 ) { stdout.println ( "Congratulations! All tests passed." ); } else if ( counts[0] != TEST_COUNT && counts[1] == 0 ) { stdout.println ( "Not all tests complete" ); } else { stdout.println ( "Test FAIL"); } } public static String getCRLF ( ByteArrayOutputStream testOutput, PrintStream stdout) { String cr; try { System.out.println ( ); cr = testOutput.toString(); testOutput.reset(); } catch ( Exception e ) { stdout.println ( " Error getting printed output" ); stdout.println ( " Printing tests will probably fail" ); cr = "\n"; } return cr; } }