⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appointmenttest.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.entities.tests;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.rapla.components.util.DateTools;
import org.rapla.entities.domain.Appointment;
import org.rapla.entities.domain.AppointmentBlockArray;
import org.rapla.entities.domain.Repeating;
import org.rapla.entities.domain.RepeatingType;
import org.rapla.entities.domain.internal.AppointmentImpl;
import org.rapla.framework.RaplaException;

public class AppointmentTest extends TestCase {
    TimeZone zone = DateTools.getTimeZone(); //this is GMT
    Locale locale = Locale.getDefault();

    public AppointmentTest(String name) {
        super(name);
    }

    public static Test suite() {
        return new TestSuite(AppointmentTest.class);
    }

    public void setUp() throws Exception {
    }

    Appointment createAppointment(Day day,Time start,Time end) {
        Calendar cal = Calendar.getInstance(zone,locale);
        cal.set(Calendar.YEAR,day.getYear());
        cal.set(Calendar.MONTH,day.getMonth() - 1);
        cal.set(Calendar.DATE,day.getDate());
        cal.set(Calendar.HOUR_OF_DAY,start.getHour());
        cal.set(Calendar.MINUTE,start.getMinute());
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        Date startTime = cal.getTime();
        cal.set(Calendar.HOUR_OF_DAY,end.getHour());
        cal.set(Calendar.MINUTE,end.getMinute());
        Date endTime = cal.getTime();
        return new AppointmentImpl(startTime,endTime);
    }

    public void testOverlapAndCompareTo() throws RaplaException {
        Appointment a1 = createAppointment(new Day(2002,5,25),new Time(12,15),new Time(14,15));
        Appointment a2 = createAppointment(new Day(2002,5,26),new Time(13,0),new Time(15,0));
        Appointment a3 = createAppointment(new Day(2002,5,25),new Time(13,0),new Time(15,0));

        // First the single Appointments
        assertTrue(a1.compareTo(a2) == -1);
        assertTrue(a2.compareTo(a1) == 1);
        assertTrue(a1.compareTo(a3) == -1);
        assertTrue(a3.compareTo(a1) == 1);
        assertTrue(a1.overlaps(a3));
        assertTrue(a3.overlaps(a1));
        assertTrue(!a2.overlaps(a3));
        assertTrue(!a3.overlaps(a2));
        assertTrue(!a1.overlaps(a2));
        assertTrue(!a2.overlaps(a1));


        // Now we test repeatings
        a1.setRepeatingEnabled(true);
        a2.setRepeatingEnabled(true);
        a3.setRepeatingEnabled(true);

        // Weekly repeating until 2002-6-2
        Repeating repeating1 = a1.getRepeating();
        repeating1.setEnd(new Day(2002,6,2).toDate(zone));

        // daily repeating 8x
        Repeating repeating2 = a2.getRepeating();
        repeating2.setType(Repeating.DAILY);
        repeating2.setNumber(8);

        // weekly repeating 1x
        Repeating repeating3 = a3.getRepeating();
        repeating3.setNumber(1);

        assertTrue(a1.overlaps(
                               new Day(2002,5,26).toDate(zone)
                               ,new Day(2002,6,2).toDate(zone)
                               )
                   );

        assertTrue(a1.overlaps(a2));
        assertTrue("overlap is not symetric",a2.overlaps(a1));

        assertTrue(a1.overlaps(a3));
        assertTrue("overlap is not symetric",a3.overlaps(a1));

        assertTrue(!a2.overlaps(a3));
        assertTrue("overlap is not symetric",!a3.overlaps(a2));

        // No appointment in first week of repeating
        repeating1.addException(new Day(2002,5,25).toDate(zone));

        assertTrue(!a1.overlaps(a3));
        assertTrue("overlap is not symetric",!a3.overlaps(a1));
    }

    public void testOverlap1() throws RaplaException {
        Appointment a1 = createAppointment(new Day(2002,4,15),new Time(10,0),new Time(12,0));
        Appointment a2 = createAppointment(new Day(2002,4,15),new Time(9,0),new Time(11,0));

        a1.setRepeatingEnabled(true);
        Repeating repeating1 = a1.getRepeating();
        repeating1.setEnd(new Day(2002,7,11).toDate(zone));

        a2.setRepeatingEnabled(true);
        Repeating repeating2 = a2.getRepeating();
        repeating2.setType(Repeating.DAILY);
        repeating2.setNumber(5);

        assertTrue(a1.overlaps(a2));
        assertTrue("overlap is not symetric",a2.overlaps(a1));
    }

    public void testOverlap2() throws RaplaException {
        Appointment a1 = createAppointment(new Day(2002,4,12),new Time(12,0),new Time(14,0));
        a1.setRepeatingEnabled(true);
        Repeating repeating1 = a1.getRepeating();
        repeating1.setEnd(new Day(2002,7,11).toDate(zone));
        assertTrue(a1.overlaps(new Day(2002,4,15).toDate(zone)
                                          , new Day(2002,4,22).toDate(zone)));
    }

    public void testBlocks() {
        Appointment a1 = createAppointment(new Day(2002,4,12),new Time(12,0),new Time(14,0));
        a1.setRepeatingEnabled(true);
        Repeating repeating1 = a1.getRepeating();
        repeating1.setEnd(new Day(2002,2,11).toDate(zone));
        AppointmentBlockArray blocks = new AppointmentBlockArray();
        a1.createBlocks( new Day(2002,4,5).toDate(zone)
                                , new Day(2002,5,5).toDate(zone)
                                , blocks );
        assertEquals( "Repeating end is in the past: createBlocks should only return one block", 1, blocks.size() );
    }

    public void testMatchNeverEnding() throws RaplaException {
        Appointment a1 = createAppointment(new Day(2002,5,25),new Time(11,15),new Time(13,15));
        Appointment a2 = createAppointment(new Day(2002,5,25),new Time(11,15),new Time(13,15));

        a1.setRepeatingEnabled(true);
        Repeating repeating1 = a1.getRepeating();
        repeating1.setType(Repeating.WEEKLY);

        a2.setRepeatingEnabled(true);
        Repeating repeating2 = a2.getRepeating();
        repeating2.setType(Repeating.WEEKLY);

        repeating1.addException(new Day(2002,4,10).toDate( zone ));
        assertTrue( !a1.matches( a2 ) );
        assertTrue( !a2.matches( a1 ) );
    }
    
    public void testMonthly()
    {
        Appointment a1 = createAppointment(new Day(2006,8,17),new Time(10,30),new Time(12,0));
        Date start = a1.getStart();
        a1.setRepeatingEnabled(true);
        Repeating repeating1 = a1.getRepeating();
        repeating1.setType( RepeatingType.MONTHLY);
        repeating1.setNumber( 4);
        AppointmentBlockArray blocks = new AppointmentBlockArray();
        a1.createBlocks( new Day(2006,8,17).toGMTDate(), new Day( 2007, 3, 30).toGMTDate(), blocks);
        assertEquals( 4, blocks.size());
        assertEquals( start, new Date(blocks.getStartAt( 0)));
        Calendar cal = DateTools.createGMTCalendar();
        cal.setTime( start );
        int weekday = cal.get( Calendar.DAY_OF_WEEK);
        int dayofweekinmonth = cal.get( Calendar.DAY_OF_WEEK_IN_MONTH);
        assertEquals( Calendar.THURSDAY,weekday );
        assertEquals( 3, dayofweekinmonth );
        assertEquals( Calendar.AUGUST, cal.get( Calendar.MONTH));
        // we expect the second wednesday in april
        cal.add( Calendar.MONTH, 1 );
        cal.set( Calendar.DAY_OF_WEEK , weekday );
        cal.set( Calendar.DAY_OF_WEEK_IN_MONTH, dayofweekinmonth);
        start = cal.getTime();
        assertEquals( start, new Date(blocks.getStartAt( 1)));
        cal.add( Calendar.MONTH, 1 );
        cal.set( Calendar.DAY_OF_WEEK , weekday );
        cal.set( Calendar.DAY_OF_WEEK_IN_MONTH, dayofweekinmonth);
        assertEquals(10, cal.get( Calendar.HOUR_OF_DAY));
        start = cal.getTime();
        assertEquals( start, new Date(blocks.getStartAt( 2)));
        cal.add( Calendar.MONTH, 1 );
        cal.set( Calendar.DAY_OF_WEEK , weekday );
        cal.set( Calendar.DAY_OF_WEEK_IN_MONTH, dayofweekinmonth);
        start = cal.getTime();
        assertEquals( start, new Date(blocks.getStartAt( 3)));

        assertEquals( start, repeating1.getEnd() );
        assertEquals( start, a1.getMaxEnd() );
        
        blocks = new AppointmentBlockArray();
        a1.createBlocks( new Day(2006,1,1).toGMTDate(), new Day( 2007, 10, 20).toGMTDate(), blocks);
        assertEquals( 4, blocks.size());
        
        blocks = new AppointmentBlockArray();
        a1.createBlocks( new Day(2006,10,19).toGMTDate(), new Day( 2006, 10, 20).toGMTDate(), blocks);
        assertEquals( 1, blocks.size());        
    }

    public void testMonthly5ft()
    {
        Appointment a1 = createAppointment(new Day(2006,8,31),new Time(10,30),new Time(12,0));
        Date start = a1.getStart();
        a1.setRepeatingEnabled(true);
        Repeating repeating1 = a1.getRepeating();
        repeating1.setType( RepeatingType.MONTHLY);
        repeating1.setNumber( 4);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -