authorauthorversion
package com.markcrocker.thoughtworks;
public class TestMyDate {
private int debugLevel = 0;
return
static boolean testStandardDates() {
boolean testPassed = true;
try {
MyDate start1 = new MyDate(83,6,2);
MyDate end1 = new MyDate(83,6,22);
int daysBetween = end1.between(start1);
System.out.println("Line #1: There are " + daysBetween + " Days between 6, 2, 1983 and 6, 22, 1983.");
if (daysBetween != 19) {
testPassed = false;
System.out.println("Test failed: result should have been 19 days");
}
MyDate start2 = new MyDate(84,7,4);
MyDate end2 = new MyDate(84,12,25);
daysBetween = end2.between(start2);
System.out.println("Line #2: There are " + daysBetween + " Days between 7, 4, 1984 and 12, 25, 1984.");
if (daysBetween != 173) {
testPassed = false;
System.out.println("Test failed: result should have been 173 days");
}
MyDate start3a = new MyDate(89,1,3);
MyDate end3a = new MyDate(83,8,3);
daysBetween = end3a.between(start3a);
System.out.println("Line #3a: There are " + daysBetween + " Days between 1, 3, 1989 and 8, 3, 1983.");
if (daysBetween != 1979) {
testPassed = false;
System.out.println("Test failed: result should have been 1979 days");
}
MyDate start3b = new MyDate(89,1,3);
MyDate end3b = new MyDate(83,3,8);
daysBetween = end3b.between(start3b);
System.out.println("Line #3b: There are " + daysBetween + " Days between 1, 3, 1989 and 3, 8, 1983.");
if (daysBetween != 2127) {
testPassed = false;
System.out.println("Test failed: result should have been 2127 days");
}
MyDate start4 = new MyDate(100,9,3);
MyDate end4 = new MyDate(100,9,5);
daysBetween = end4.between(start4);
System.out.println("Test #4: There are " + daysBetween + " Days between 9, 3, 2000 and 9, 5, 2000.");
if (daysBetween != 1) {
testPassed = false;
System.out.println("Test failed: result should have been 1 days");
}
MyDate start5 = new MyDate(100,9,3);
MyDate end5 = new MyDate(100,9,4);
daysBetween = end5.between(start5);
System.out.println("Test #5: There are " + daysBetween + " Days between 9, 3, 2000 and 9, 4, 2000.");
if (daysBetween != 0) {
testPassed = false;
System.out.println("Test failed: result should have been 0 days");
}
MyDate start6 = new MyDate(100,9,3);
MyDate end6 = new MyDate(100,9,3);
daysBetween = end6.between(start6);
System.out.println("Test #6: There are " + daysBetween + " Days between 9, 3, 2000 and 9, 3, 2000.");
if (daysBetween != 0) {
testPassed = false;
System.out.println(" Test failed: result should have been 0 days");
}
MyDate start7 = new MyDate(00,1,1);
MyDate end7 = new MyDate(199,12,31);
daysBetween = end7.between(start7);
System.out.println("Test #7: There are " + daysBetween + " Days between 1, 1, 1900 and 12, 31, 2099.");
if (daysBetween != 73047) {
testPassed = false;
System.out.println("Test failed: result should have been 73047 days");
}
} catch(BadDateException e) { e.printStackTrace(System.err);
System.exit(1);
}
return testPassed;
}
static void testDateExceptions() {
try { MyDate badYear1 = new MyDate(-83,6,2);
} catch(BadDateException e) { e.printStackTrace(System.err); }
try { MyDate badYear2 = new MyDate(103,6,2);
} catch(BadDateException e) { e.printStackTrace(System.err); }
try { MyDate badMonth1 = new MyDate(83,0,2);
} catch(BadDateException e) { e.printStackTrace(System.err); }
try { MyDate badMonth2 = new MyDate(83,13,2);
} catch(BadDateException e) { e.printStackTrace(System.err); }
try { MyDate badDay1 = new MyDate(83,6,-4);
} catch(BadDateException e) { e.printStackTrace(System.err); }
try { MyDate badDay2 = new MyDate(83,6,34);
} catch(BadDateException e) { e.printStackTrace(System.err); }
try { MyDate badDay3 = new MyDate(84,2,29); } catch(BadDateException e) { e.printStackTrace(System.err); }
try { MyDate badDay3 = new MyDate(83,2,29);
} catch(BadDateException e) { e.printStackTrace(System.err); }
}
private static void syntaxMessage() {
System.out.println("Syntax:\n" +
" TestMyDate [-t|--test|-e|--exceptions|-h|--help]\n" +
" Produces test output, exception test output or this message.\n" +
" These switches may be used in combination with each other and multiple times.\n");
}
public static void main(String[] args) throws BadDateException {
int debugLevel = 0;
if (args.length < 1) {
syntaxMessage();
} else {
for (int i = 0; i < args.length; i++) { if ( args[i].equals("-t") ||
args[i].equals("--test")) {
if (debugLevel > 1) { System.out.println("Test mode flag detected"); }
System.out.println("Tests passed = " + testStandardDates());
} else if ( args[i].equals("-e") ||
args[i].equals("--exceptions")) {
if (debugLevel > 1) { System.out.println("Exception test mode flag detected"); }
testDateExceptions();
} else if ( args[i].equals("-h") ||
args[i].equals("--help")) {
if (debugLevel > 1) { System.out.println("Help mode flag detected"); }
syntaxMessage();
} else { syntaxMessage();
break;
}
}
}
}
}