import  java.awt.*;
import  java.io.*;
import java.util.*;
import cnp.ew.converter.*;

public class TestCpDateToStringConverter extends Frame {

    CpDateToStringConverter converter = new CpDateToStringConverter();

	TestCpDateToStringConverter () {

		super("CpDateToStringConverter Tester");

	    boolean failed=false;
	    if (!runTestSuite1()) {
	        failed = true;
	    }
	    if (!runTestSuite2()) {
	        failed = true;
	    }
	    if (!runTestSuite3()) {
	        failed = true;
	    }
	    if (!runTestSuite4()) {
	        failed = true;
	    }
	    if (!runTestSuite5()) {
	        failed = true;
	    }
        if (failed) {
            System.out.println("Test of CpDateToStringConverter FAILED");
        } else {
            System.out.println("Test of CpDateToStringConverter passed");
        }

		SecMan secMan = new SecMan();
		System.setSecurityManager(secMan);
		reshape(100, 100, 150, 300);
		show();
	}

	/**
	 * Test all date formats against basic data.  Iterate January through
	 * December, making sure all days of the week are covered.
	 */

	boolean runTestSuite1()
	{
	    boolean failed=false;

        if (!runTest("Jan 1 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "1 01 Mon Monday 2 1 1 01 Jan January 1 1 96 1996")) {
                failed = true;
        }
        // Test a Tuesday and Wednesday...
        if (!runTest("Jan 2 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "2 02 Tue Tuesday 3 1 1 01 Jan January 1 2 96 1996")) {
                failed = true;
        }
        if (!runTest("Jan 3 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "3 03 Wed Wednesday 4 1 1 01 Jan January 1 3 96 1996")) {
                failed = true;
        }
        if (!runTest("Feb 2 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "2 02 Fri Friday 6 5 2 02 Feb February 1 33 96 1996")) {
                failed = true;
        }
        // Make sure to test a Saturday in here...
        if (!runTest("Feb 3 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "3 03 Sat Saturday 7 5 2 02 Feb February 1 34 96 1996")) {
                failed = true;
        }
        if (!runTest("Mar 3 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "3 03 Sun Sunday 1 10 3 03 Mar March 1 63 96 1996")) {
                failed = true;
        }
        if (!runTest("April 4 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "4 04 Thu Thursday 5 14 4 04 Apr April 2 95 96 1996")) {
                failed = true;
        }
        if (!runTest("May 5 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "5 05 Sun Sunday 1 19 5 05 May May 2 126 96 1996")) {
                failed = true;
        }
        if (!runTest("June 6 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "6 06 Thu Thursday 5 23 6 06 Jun June 2 158 96 1996")) {
                failed = true;
        }
        if (!runTest("July 7 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "7 07 Sun Sunday 1 28 7 07 Jul July 3 189 96 1996")) {
                failed = true;
        }
        if (!runTest("August 8 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "8 08 Thu Thursday 5 32 8 08 Aug August 3 221 96 1996")) {
                failed = true;
        }
        if (!runTest("September 9 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "9 09 Mon Monday 2 37 9 09 Sep September 3 253 96 1996")) {
                failed = true;
        }
        if (!runTest("October 10 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "10 10 Thu Thursday 5 41 10 10 Oct October 4 284 96 1996")) {
                failed = true;
        }
        if (!runTest("November 11 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "11 11 Mon Monday 2 46 11 11 Nov November 4 316 96 1996")) {
                failed = true;
        }
        if (!runTest("December 12 1996",
            "d dd ddd dddd w ww m mm mmm mmmm q y yy yyyy",
            "12 12 Thu Thursday 5 50 12 12 Dec December 4 347 96 1996")) {
                failed = true;
        }

        return !failed;
    }

    /**
     * Test all time formats against basic data.  Iterate through several hours
     * of the day, making sure preceding zero values are covered.
     */
    boolean runTestSuite2()
    {
        boolean failed= false;

        if (!runTest("1/1/96 0:00:00",
            "h hh n nn s ss",
            "0 00 0 00 0 00")) {
                failed = true;
        }
        if (!runTest("1/1/96 1:01:01",
            "h hh n nn s ss",
            "1 01 1 01 1 01")) {
                failed = true;
        }
        if (!runTest("1/1/96 2:02:02",
            "h hh n nn s ss",
            "2 02 2 02 2 02")) {
                failed = true;
        }
        if (!runTest("1/1/96 3:03:03",
            "h hh n nn s ss",
            "3 03 3 03 3 03")) {
                failed = true;
        }
        if (!runTest("1/1/96 4:04:04",
            "h hh n nn s ss",
            "4 04 4 04 4 04")) {
                failed = true;
        }
        if (!runTest("1/1/96 11:59:59",
            "h hh n nn s ss",
            "11 11 59 59 59 59")) {
                failed = true;
        }
        if (!runTest("1/1/96 12:00:00",
            "h hh n nn s ss",
            "12 12 0 00 0 00")) {
                failed = true;
        }
        if (!runTest("1/1/96 23:59:59",
            "h hh n nn s ss",
            "23 23 59 59 59 59")) {
                failed = true;
        }
        if (!runTest("1/1/96 24:00:00",
            "h hh n nn s ss",
            "0 00 0 00 0 00")) {
                failed = true;
        }
        return !failed;
    }

    /**
     * Test all time formats against basic data.  Iterate through several hours
     * of the day, making sure preceding zero values are covered.
     */
    boolean runTestSuite3()
    {
        boolean failed= false;

        if (!runTest("1/1/96 0:00:00",
            "h hh n nn s ss AM/PM",
            "0 00 0 00 0 00 AM")) {
                failed = true;
        }
        if (!runTest("1/1/96 0:00:00",
            "h hh n nn s ss am/pm",
            "0 00 0 00 0 00 am")) {
                failed = true;
        }
        if (!runTest("1/1/96 0:00:00",
            "h hh n nn s ss A/P",
            "0 00 0 00 0 00 A")) {
                failed = true;
        }
        if (!runTest("1/1/96 0:00:00",
            "h hh n nn s ss a/p",
            "0 00 0 00 0 00 a")) {
                failed = true;
        }


        if (!runTest("1/1/96 11:59:59",
            "h hh n nn s ss AM/PM",
            "11 11 59 59 59 59 AM")) {
                failed = true;
        }
        if (!runTest("1/1/96 11:59:59",
            "h hh n nn s ss am/pm",
            "11 11 59 59 59 59 am")) {
                failed = true;
        }
        if (!runTest("1/1/96 11:59:59",
            "h hh n nn s ss A/P",
            "11 11 59 59 59 59 A")) {
                failed = true;
        }
        if (!runTest("1/1/96 11:59:59",
            "h hh n nn s ss a/p",
            "11 11 59 59 59 59 a")) {
                failed = true;
        }

        if (!runTest("1/1/96 12:00:00",
            "h hh n nn s ss AM/PM",
            "12 12 0 00 0 00 PM")) {
                failed = true;
        }
        if (!runTest("1/1/96 12:00:00",
            "h hh n nn s ss am/pm",
            "12 12 0 00 0 00 pm")) {
                failed = true;
        }
        if (!runTest("1/1/96 12:00:00",
            "h hh n nn s ss A/P",
            "12 12 0 00 0 00 P")) {
                failed = true;
        }
        if (!runTest("1/1/96 12:00:00",
            "h hh n nn s ss a/p",
            "12 12 0 00 0 00 p")) {
                failed = true;
        }


        if (!runTest("1/1/96 13:00:00",
            "h hh n nn s ss AM/PM",
            "1 01 0 00 0 00 PM")) {
                failed = true;
        }
        if (!runTest("1/1/96 13:00:00",
            "h hh n nn s ss am/pm",
            "1 01 0 00 0 00 pm")) {
                failed = true;
        }
        if (!runTest("1/1/96 13:00:00",
            "h hh n nn s ss A/P",
            "1 01 0 00 0 00 P")) {
                failed = true;
        }
        if (!runTest("1/1/96 13:00:00",
            "h hh n nn s ss a/p",
            "1 01 0 00 0 00 p")) {
                failed = true;
        }

        if (!runTest("1/1/96 23:59:59",
            "h hh n nn s ss AM/PM",
            "11 11 59 59 59 59 PM")) {
                failed = true;
        }
        if (!runTest("1/1/96 23:59:59",
            "h hh n nn s ss am/pm",
            "11 11 59 59 59 59 pm")) {
                failed = true;
        }
        if (!runTest("1/1/96 23:59:59",
            "h hh n nn s ss A/P",
            "11 11 59 59 59 59 P")) {
                failed = true;
        }
        if (!runTest("1/1/96 23:59:59",
            "h hh n nn s ss a/p",
            "11 11 59 59 59 59 p")) {
                failed = true;
        }


        return !failed;
    }

    /**
     * Test some common formats
     */
    boolean runTestSuite4()
    {
        boolean failed= false;

        if (!runTest("Feb 4 1996 12:44",
            "dddd, mmmm d, yyyy hh:nn AM/PM",
            "Sunday, February 4, 1996 12:44 PM")) {
                failed = true;
        }
        if (!runTest("Feb 4 1996 12:44",
            "mm/dd/yy hh:nn:ss",
            "02/04/96 12:44:00")) {
                failed = true;
        }
        if (!runTest("Feb 4 1996 12:44",
            "dd-mmm-yy",
            "04-Feb-96")) {
                failed = true;
        }

        return !failed;
    }

    /**
     * Test embedded literals
     */
    boolean runTestSuite5()
    {
        boolean failed= false;

        if (!runTest("Feb 4 1996 12:44",
            "\"dDmMyYwWqQyYhHnNsSaAqQ\"",
            "dDmMyYwWqQyYhHnNsSaAqQ")) {
                failed = true;
        }
        if (!runTest("Feb 4 1996 12:44",
            "\"Howdy, today is \"dddd",
            "Howdy, today is Sunday")) {
                failed = true;
        }

        return !failed;
    }



	/**
	 * runTest - execute one test, printing failure to the transcript, answering
	 * false if the test fails.
	 *
	 */
	boolean runTest(String testDateString, String testFormatString, String expectedResultString)
	{
	    converter.setFormatString(testFormatString);
	    String convertedString = converter.convert(new Date(testDateString));
	    if (expectedResultString.equals(convertedString)) {
	        return true;
	    } else {
	        System.out.println("FAILED:");
	        System.out.println("  Date    : " + testDateString);
	        System.out.println("  Format  : " + testFormatString);
	        System.out.println("  Result  : " + convertedString);
	        System.out.println("  Expected: " + expectedResultString);
	        return false;
	    }
	}


	public static void main (String argv[]) {
		new TestCpDateToStringConverter();
	}

	public boolean handleEvent (Event evt) {

		switch(evt.id) {
		case Event.WINDOW_DESTROY:
			System.exit(0);
		}
		return super.handleEvent(evt);
	}

}

class SecMan extends SecurityManager{
        public void checkRead(String s) {return;}
        public void checkPropertyAccess(String s) {return;}
        public void checkLink(String s) {return;}
        public void checkAccess(Thread t) {return;}
        public void checkAccess(ThreadGroup tg) {return;}
        public void checkExit(int i) {return;}
}
