/* * Test class for Project 2. * * Created on July 8, 2003, 5:44 PM */ package gamekeeper2a; /** Test cases for two types of game keepers. */ public class MyTestClass { final static boolean printStackTrace = true; /** Tests proper detection of comma as a delimiter * (i.e. there should be exactly one comma) */ static void testCase1() { try { IRecordkeeper bbkeeper = new BaseballGamekeeper(); IRecordkeeper golfkeeper = new GolfGamekeeper(); String case1 = "Singh 77, Sorenstam 71, Woods 68"; String case2 = "Singh 77 Sorenstam 71"; if( bbkeeper.acceptGameResults(case1) || golfkeeper.acceptGameResults(case1) || bbkeeper.acceptGameResults(case2) || golfkeeper.acceptGameResults(case2) ) { throw new Exception(); } else { System.out.println("SUCCESS: there should be exactly one comma in the score"); } } catch (Exception e) { System.out.println("FAILURE: Accepted game result(s) where # of commas != 1:"); if(printStackTrace) { e.printStackTrace(); } } } /** Tests further proper detection of comma as a delimiter * (The comma should immediately follow the score) */ static void testCase2() { try { IRecordkeeper bbkeeper = new BaseballGamekeeper(); IRecordkeeper golfkeeper = new GolfGamekeeper(); String case1 = "Mariners 23 , Yankees 20"; if ( bbkeeper.acceptGameResults(case1) || golfkeeper.acceptGameResults(case1) ) { throw new Exception(); } else { System.out.println("SUCCESS: the comma immediately follows the first score"); } } catch (Exception e) { System.out.println("FAILURE: Accepted game result where comma didn't immediately follow score"); if(printStackTrace) { e.printStackTrace(); } } } /** Tests to ensure that contestant names are not the same, * that contestant's names are trimmed properly, * that contestant's names can be more than 1 word */ static void testCase3() { try { IRecordkeeper bbkeeper = new BaseballGamekeeper(); IRecordkeeper golfkeeper = new GolfGamekeeper(); String case1 = " Seattle Mariners 23, Seattle Mariners 7"; if(bbkeeper.acceptGameResults(case1) || golfkeeper.acceptGameResults(case1) ) { throw new Exception(); } else { System.out.println("SUCCESS: contestant names can't be the same"); } } catch (Exception e) { System.out.println("FAILURE: acceptance of results where contestant names (after trimming) were the same"); if(printStackTrace) { e.printStackTrace(); } } } /** Tests various invalid game results to ensure that they aren't accepted */ static void testCase4() { IRecordkeeper bbkeeper = new BaseballGamekeeper(); IRecordkeeper golfkeeper = new GolfGamekeeper(); String case1 = null; String case2 = ","; String case3 = "Mariners 23, 0"; try { if(bbkeeper.acceptGameResults(case1) || golfkeeper.acceptGameResults(case1) || bbkeeper.acceptGameResults("Mariners", 23, null, 20) || golfkeeper.acceptGameResults("Mariners", 23, null, 20) ) { throw new Exception(); } else { System.out.println("SUCCESS: null game results detected"); } } catch (Exception e) { System.out.println("FAILURE: null games results undetected"); if(printStackTrace) { e.printStackTrace(); } } try { if(bbkeeper.acceptGameResults(case2) || golfkeeper.acceptGameResults(case2) ) { throw new Exception(); } else { System.out.println("SUCCESS: game results lacking team names and scores rejected"); } } catch (Exception e) { System.out.println("FAILURE: game results lacking team names and scores not rejected"); if(printStackTrace) { e.printStackTrace(); } } try { if(bbkeeper.acceptGameResults(case3) || golfkeeper.acceptGameResults(case3) ) { throw new Exception(); } else { System.out.println("SUCCESS: game results partially lacking team names/scores rejected"); } } catch (Exception e) { System.out.println("FAILURE: game results partially lacking team names/scores not rejected"); if(printStackTrace) { e.printStackTrace(); } } } /** Tests GolfGamekeeper * (low score wins, no matter what the order) */ static void testCase5() { IRecordkeeper golfkeeper = new GolfGamekeeper(); golfkeeper.acceptGameResults("Woods 63, Ellis 70"); golfkeeper.acceptGameResults("Weir 65, Els 64"); golfkeeper.acceptGameResults("Woods", 63, "Ellis", 70); golfkeeper.acceptGameResults("Weir", 65, "Els", 64); try { if (golfkeeper.listContestants().size() != 4) { throw new Exception(); } String[] golfers = golfkeeper.listContestantResults(); if(isARecordIn("Woods 2-0", golfers) && isARecordIn("Weir 0-2", golfers)) { System.out.println("SUCCESS: low score wins in golf"); } else { throw new Exception(); } } catch (Exception e) { System.out.println("FAILURE: golfers records aren't correct when low scores should win"); if(printStackTrace) { e.printStackTrace(); } } } /** Tests GolfGamekeeper * (score must be at least 18) */ static void testCase6() { boolean testSuccess1 = false; boolean testSuccess2 = false; IRecordkeeper golfkeeper = new GolfGamekeeper(); try { if ( !golfkeeper.acceptGameResults("Woods 18, Els 20") ) { throw new Exception(); } testSuccess1 = true; } catch (Exception e) { System.out.println("FAILURE: scores >= 18 not accepted"); if(printStackTrace) { e.printStackTrace(); } } golfkeeper = new GolfGamekeeper(); golfkeeper.acceptGameResults("Weir 20, Els 15"); golfkeeper.acceptGameResults("Woods 7, Ellis 8"); golfkeeper.acceptGameResults("Weir 18, Els 7"); try { if (golfkeeper.listContestants().size() != 0) { throw new Exception(); } testSuccess2 = true; } catch (Exception e) { System.out.println("FAILURE: scores < 18 accepted"); if(printStackTrace) { e.printStackTrace(); } } if(testSuccess1 && testSuccess2) { System.out.println("SUCCESS: scores 18+ accepted in golf"); } } /** Further Tests GolfGamekeeper * (ties are possible) */ static void testCase7() { IRecordkeeper golfkeeper = new GolfGamekeeper(); try { if ( !golfkeeper.acceptGameResults("Woods 23, Els 23") && isARecordIn("Woods 0-0", golfkeeper.listContestantResults()) && isARecordIn("Els 0-0", golfkeeper.listContestantResults()) ) { throw new Exception(); } else { System.out.println("SUCCESS: ties accepted in golf"); } } catch (Exception e) { System.out.println("FAILURE: ties not accepted in golf"); if(printStackTrace) { e.printStackTrace(); } } } /** Tests BaseballGamekeeper * (high score wins, no matter what the order or how great the number) */ static void testCase8() { IRecordkeeper bbkeeper = new BaseballGamekeeper(); bbkeeper.acceptGameResults("Mariners 1, Yankees 0"); bbkeeper.acceptGameResults("Mariners 10, Yankees 0"); bbkeeper.acceptGameResults("Mariners 100, Yankees 0"); bbkeeper.acceptGameResults("Mariners 1000, Yankees 0"); try { if ( !isARecordIn("Mariners 4-0", bbkeeper.listContestantResults()) || !isARecordIn("Yankees 0-4", bbkeeper.listContestantResults()) ){ throw new Exception(); } else { System.out.println("SUCCESS: high scores wins in baseball"); } } catch (Exception e) { System.out.println("FAILURE: games not accepted where high score wins in baseball"); if(printStackTrace) { e.printStackTrace(); } } } /** Further Tests BaseballGamekeeper * (ties not possible) */ static void testCase9() { IRecordkeeper bbkeeper = new BaseballGamekeeper(); try { if ( bbkeeper.acceptGameResults("NL 6, AL 6") ){ throw new Exception(); } else { System.out.println("SUCCESS: ties not accepted in baseball after All-Star game fiasco"); } } catch (Exception e) { System.out.println("FAILURE: tied games in baseball are accepted"); if(printStackTrace) { e.printStackTrace(); } } } private static boolean isARecordIn(String record, String[] recordList) { for (int i = 0; i < recordList.length; i++) { if (recordList[i].equals(record)){ return true; } } return false; } /** Run the test cases contained in this file. */ public static void main(String[] args) { testCase1(); testCase2(); testCase3(); testCase4(); testCase5(); testCase6(); testCase7(); testCase8(); testCase9(); } }