//: TestMyDate.java

/** Tests used to verify MyDates class.
  * @author Mark Crocker <mcrocker@micron.net>
  * @author http://www.markcrocker.com/~mcrocker/
  * @version 0.92
  */

package com.markcrocker.thoughtworks;

public class TestMyDate {
    private int debugLevel = 0;  // vestigal 1960s style debugging tool
    
    /** testStandardDates.  Basic tests of MyDate class using several known dates,
      * comparing the number of days between and verifying the answers.
      * @return testPassed.  True if all tests were passed, false otherwise.
      */ 
    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) {      // shouldnt generate any exceptions because input is fixed.
	    e.printStackTrace(System.err);
	    System.exit(1);
	}

	return testPassed;
    }

    /** testDateExceptions.  Tests of MyDate class exceptions.  Checks
      * to make sure that known bad dates are caught by the constructor 
      * checking algorithms.  Does not verify results... yet.
      */
    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);  // actually a good date, shouldnt produce an exception
	} catch(BadDateException e) { e.printStackTrace(System.err); }
	try { MyDate badDay3 = new MyDate(83,2,29);
	} catch(BadDateException e) { e.printStackTrace(System.err); }

    }

    /** syntaxMessage.  Prints out syntax/help message.
      */
    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;  // vestigal 1960s style debugging tool

	if (args.length < 1) {
	    syntaxMessage();
	} else {
	    for (int i = 0; i < args.length; i++) {  // go through all args
		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 { // not a valid input, so spit out a syntax message.
		    syntaxMessage();
		    break;
		}
	    }
	}
    }


} ///:~


/* Todo
-------

1. Add verification to exception testing routine.

 */