001: /**
002: * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle,
003: * Santa Clara, California 95054, U.S.A. All rights reserved.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018: */package org.jdesktop.swingx;
019:
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.SortedSet;
023: import java.util.TreeSet;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for the DefaultDateSelectionModel
029: */
030: public class DefaultDateSelectionModelTest extends TestCase {
031: private DefaultDateSelectionModel model;
032:
033: @Override
034: public void setUp() {
035: model = new DefaultDateSelectionModel();
036: }
037:
038: @Override
039: public void tearDown() {
040:
041: }
042:
043: public void testSingleSelection() {
044: model
045: .setSelectionMode(DateSelectionModel.SelectionMode.SINGLE_SELECTION);
046: Date today = new Date();
047: model.setSelectionInterval(today, today);
048: SortedSet<Date> selection = model.getSelection();
049: assertTrue(!selection.isEmpty());
050: assertTrue(1 == selection.size());
051: assertTrue(today.equals(selection.first()));
052:
053: Calendar cal = Calendar.getInstance();
054: cal.setTime(today);
055: cal.roll(Calendar.DAY_OF_MONTH, 1);
056: Date tomorrow = cal.getTime();
057: model.setSelectionInterval(today, tomorrow);
058: selection = model.getSelection();
059: assertTrue(!selection.isEmpty());
060: assertTrue(1 == selection.size());
061: assertTrue(today.equals(selection.first()));
062:
063: model.addSelectionInterval(tomorrow, tomorrow);
064: selection = model.getSelection();
065: assertTrue(!selection.isEmpty());
066: assertTrue(1 == selection.size());
067: assertTrue(tomorrow.equals(selection.first()));
068: }
069:
070: public void testSingleIntervalSelection() {
071: model
072: .setSelectionMode(DateSelectionModel.SelectionMode.SINGLE_INTERVAL_SELECTION);
073: Date startDate = new Date();
074: Calendar cal = Calendar.getInstance();
075: cal.setTime(startDate);
076: cal.add(Calendar.DAY_OF_MONTH, 5);
077: Date endDate = cal.getTime();
078: model.setSelectionInterval(startDate, endDate);
079: SortedSet<Date> selection = model.getSelection();
080: assertTrue(startDate.equals(selection.first()));
081: assertTrue(endDate.equals(selection.last()));
082:
083: cal.setTime(startDate);
084: cal.roll(Calendar.MONTH, 1);
085: Date startDateNextMonth = cal.getTime();
086: model.addSelectionInterval(startDateNextMonth,
087: startDateNextMonth);
088: selection = model.getSelection();
089: assertTrue(startDateNextMonth.equals(selection.first()));
090: assertTrue(startDateNextMonth.equals(selection.last()));
091: }
092:
093: public void testUnselctableDates() {
094: // Make sure the unselectable dates returns an empty set if it hasn't been
095: // used.
096: SortedSet<Date> unselectableDates = model
097: .getUnselectableDates();
098: assert (unselectableDates.isEmpty());
099:
100: model
101: .setSelectionMode(DateSelectionModel.SelectionMode.MULTIPLE_INTERVAL_SELECTION);
102: Calendar cal = Calendar.getInstance();
103: cal.setTimeInMillis(System.currentTimeMillis());
104:
105: Date today = cal.getTime();
106: cal.add(Calendar.DAY_OF_MONTH, 1);
107: Date tPlus1 = cal.getTime();
108: cal.add(Calendar.DAY_OF_MONTH, 1);
109: Date tPlus2 = cal.getTime();
110: cal.add(Calendar.DAY_OF_MONTH, 1);
111: Date tPlus3 = cal.getTime();
112: cal.add(Calendar.DAY_OF_MONTH, 1);
113: Date tPlus4 = cal.getTime();
114:
115: model.setSelectionInterval(today, tPlus4);
116: SortedSet<Date> selection = model.getSelection();
117: assertTrue(!selection.isEmpty());
118: assertTrue(5 == selection.size());
119: assertTrue(today.equals(selection.first()));
120: assertTrue(tPlus4.equals(selection.last()));
121:
122: unselectableDates = new TreeSet<Date>();
123: unselectableDates.add(tPlus1);
124: unselectableDates.add(tPlus3);
125: model.setUnselectableDates(unselectableDates);
126:
127: // Make sure setting the unselectable dates to include a selected date removes
128: // it from the selected set.
129: selection = model.getSelection();
130: assertTrue(!selection.isEmpty());
131: assertTrue(3 == selection.size());
132: assertTrue(selection.contains(today));
133: assertTrue(selection.contains(tPlus2));
134: assertTrue(selection.contains(tPlus4));
135:
136: // Make sure the unselectable dates is the same as what we set.
137: SortedSet<Date> result = model.getUnselectableDates();
138: assertTrue(unselectableDates.equals(result));
139: }
140: }
|