package cal;

import junit.framework.TestCase;

/** Testing for Appointment.
 * @author Gary T. Leavens
 * @version $Revision: 1.2 $
 */
public class AppointmentTest extends TestCase {

    /**
     * Constructor for AppointmentTest.
     * @param name
     */
    public AppointmentTest(String name) {
        super(name);
    }

    /** Run the tests with the text user interface. */
    public static void main(String[] args) {
        junit.textui.TestRunner.run(AppointmentTest.class);
    }

    /** Test data. */
    private Appointment[] receivers;
    
    /**
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        receivers = new Appointment[] {
            new Appointment(),
            new Appointment(),
            new Appointment(),
            new Appointment(),
        };
        receivers[1].setLocation("229 Atanasoff");
        receivers[2].setLocation("217 Atanasoff");
        receivers[3].setLocation("217 Atanasoff");
        receivers[1].setWho("Abbey R.");
        receivers[2].setWho("Strawberry F.");
        receivers[3].setWho("Carl");
        receivers[1].setStart(new TimeOfDay(9,0));
        receivers[2].setStart(new TimeOfDay(16,10));
        receivers[3].setStart(new TimeOfDay(17,15));
        receivers[1].setLength(50);
        receivers[2].setLength(30);
        receivers[3].setLength(30);
    }

    /**
     * Test for the constructor Appointment(), 
     * and the methods getStart(), getLength(), getLocation(),
     *  and getWho().
     */
    public void testAppointment() {
       Appointment a = new Appointment();
       assertEquals(new TimeOfDay(8,00), a.getStart());
       assertEquals("my office", a.getLocation());
       assertEquals("", a.getWho());
       assertEquals(60, a.getLength());
    }


    /**
     * Test for the methods setLength() and getLength().
     */
    public void testSetLength() {
        Appointment a = new Appointment();
        for (int i = 0; i < 24*60; i = (i == 0 ? 1 : i+5)) {
            a.setLength(i);
            assertEquals(i, a.getLength());
        }
    }

    /**
     * Test for the method setStart()
     */
    public void testSetStart() {
        Appointment a = new Appointment();
        TimeOfDay t = new TimeOfDay(14, 10);
        a.setStart(t);
        assertEquals(t, a.getStart());
        t = new TimeOfDay(11, 3);
        a.setStart(t);
        assertEquals(t, a.getStart());
    }

    /**
     * Test for the method setEnd()
     */
    public void testSetEnd() {
        Appointment a = new Appointment();
        TimeOfDay t = new TimeOfDay(14, 10);
        a.setStart(TimeOfDay.pm(1,0));
        a.setEnd(t);
        assertEquals(t, a.getEnd());
        assertEquals(70, a.getLength());
        t = new TimeOfDay(15,0);
        a.setEnd(t);
        assertEquals(t, a.getEnd());
        assertEquals(120, a.getLength());
    }

    /**
     * Test for the methods getEnd()
     */
    public void testGetEnd() {
        Appointment a = new Appointment();
        TimeOfDay t = new TimeOfDay(14, 10);
        a.setStart(TimeOfDay.pm(1,0));
        a.setLength(70);
        assertEquals(t, a.getEnd());
        assertEquals(70, a.getLength());
    }

    /**
     * Test for the methods getLength()
     */
    public void testGetLength() {
        Appointment a = new Appointment();
        TimeOfDay t = new TimeOfDay(14, 10);
        a.setStart(TimeOfDay.pm(1,0));
        a.setEnd(t);
        assertEquals(t, a.getEnd());
        assertEquals(70, a.getLength());
    }

    /**
     * Test for the method setLocation()
     */
    public void testSetLocation() {
        Appointment a = new Appointment();
        a.setLocation("Campus Town");
        assertEquals("Campus Town", a.getLocation());
        a.setLocation("Beedle's");
        assertEquals("Beedle's", a.getLocation());        
    }

    /**
     * Test for the method setWho()
     */
    public void testSetWho() {
        Appointment a = new Appointment();
        a.setWho("Al");
        assertEquals("Al", a.getWho());
        a.setWho("Betty");
        assertEquals("Betty", a.getWho());        
    }

    /**
     * Test for Object clone()
     */
    public void testClone() {
        for (int i = 0; i < receivers.length; i++) {
            assertEquals(receivers[i].getStart(),
                         ((Appointment)receivers[i].clone()).getStart());
            assertEquals(receivers[i].getLength(),
                         ((Appointment)receivers[i].clone()).getLength());
            assertEquals(receivers[i].getLocation(),
                         ((Appointment)receivers[i].clone()).getLocation());
            assertEquals(receivers[i].getWho(),
                         ((Appointment)receivers[i].clone()).getWho());
            assertFalse(receivers[i] == receivers[i].clone());
        }
    }

    /**
     * Test for equals(), which is object identity for Appointments.
     */
    public void testEquals() {
        for (int i = 0; i < receivers.length; i++) {
            assertFalse(receivers[i].equals(null));
            for (int j = 0; j < receivers.length; j++) {
                assertEquals(i == j, receivers[i].equals(receivers[j]));
            }
        }
    }
    
    /**
     * Test for String toString()
     */
    public void testToString0() {
        assertEquals("At 08:00AM in my office meet  for 60 min",
                     receivers[0].toString());
    }

    /**
     * Test for String toString()
     */
    public void testToString1() {
        assertEquals("At 09:00AM in 229 Atanasoff meet Abbey R. for 50 min",
                     receivers[1].toString());
    }

    /**
     * Test for String toString()
     */
    public void testToString2() {
        assertEquals("At 04:10PM in 217 Atanasoff meet Strawberry F. for 30 min",
                     receivers[2].toString());
    }

    /**
     * Test for String toString()
     */
    public void testToString3() {
        assertEquals("At 05:15PM in 217 Atanasoff meet Carl for 30 min",
                     receivers[3].toString());
    }
}
