001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.util.Calendar;
023: import java.util.Date;
024: import java.util.GregorianCalendar;
025:
026: public class SpinnerDateModelTest extends BasicSwingTestCase {
027: private SpinnerDateModel model;
028:
029: private ChangeController chl;
030:
031: private Date now;
032:
033: private Date past;
034:
035: private Date future;
036:
037: @Override
038: public void setUp() {
039: model = new SpinnerDateModel();
040: chl = new ChangeController(false);
041: model.addChangeListener(chl);
042: Calendar calendar = new GregorianCalendar();
043: calendar.add(Calendar.DAY_OF_MONTH, -1);
044: now = calendar.getTime();
045: calendar.add(Calendar.DAY_OF_MONTH, -2);
046: past = calendar.getTime();
047: calendar.add(Calendar.DAY_OF_MONTH, 3);
048: future = calendar.getTime();
049: }
050:
051: @Override
052: public void tearDown() {
053: model = null;
054: chl = null;
055: now = null;
056: past = null;
057: future = null;
058: }
059:
060: @SuppressWarnings("deprecation")
061: public void testSpinnerDateModel() {
062: assertEquals(model.getDate().getDay(), now.getDay() + 1);
063: assertEquals(Calendar.DAY_OF_MONTH, model.getCalendarField());
064: assertNull(model.getStart());
065: assertNull(model.getEnd());
066: testExceptionalCase(new IllegalArgumentCase() {
067: @Override
068: public void exceptionalAction() throws Exception {
069: model = new SpinnerDateModel(null, null, null,
070: Calendar.DAY_OF_MONTH);
071: }
072: });
073: testExceptionalCase(new IllegalArgumentCase() {
074: @Override
075: public void exceptionalAction() throws Exception {
076: model = new SpinnerDateModel(now, future, null,
077: Calendar.DAY_OF_MONTH);
078: }
079: });
080: testExceptionalCase(new IllegalArgumentCase() {
081: @Override
082: public void exceptionalAction() throws Exception {
083: model = new SpinnerDateModel(now, null, past,
084: Calendar.DAY_OF_MONTH);
085: }
086: });
087: testExceptionalCase(new IllegalArgumentCase() {
088: @Override
089: public void exceptionalAction() throws Exception {
090: model = new SpinnerDateModel(now, null, null, 666);
091: }
092: });
093: }
094:
095: public void testSetGetStart() {
096: model.setStart(past);
097: assertTrue(chl.isChanged());
098: assertSame(past, model.getStart());
099: model.setValue(now);
100: model.setStart(future);
101: model.setStart(now);
102: chl.reset();
103: model.setStart(now);
104: assertFalse(chl.isChanged());
105: }
106:
107: public void testSetGetEnd() {
108: model.setEnd(future);
109: assertTrue(chl.isChanged());
110: assertSame(future, model.getEnd());
111: model.setValue(now);
112: model.setEnd(past);
113: model.setEnd(now);
114: chl.reset();
115: model.setEnd(now);
116: assertFalse(chl.isChanged());
117: }
118:
119: public void testSetGetValue() {
120: model.setValue(now);
121: assertTrue(chl.isChanged());
122: assertNotSame(now, model.getValue());
123: assertEquals(now, model.getValue());
124: assertNotSame(model.getValue(), model.getValue());
125: testExceptionalCase(new IllegalArgumentCase() {
126: @Override
127: public void exceptionalAction() throws Exception {
128: model.setValue(null);
129: }
130: });
131: testExceptionalCase(new IllegalArgumentCase() {
132: @Override
133: public void exceptionalAction() throws Exception {
134: model.setValue("test");
135: }
136: });
137: model.setEnd(past);
138: model.setValue(now);
139: model.setValue(now);
140: chl.reset();
141: model.setValue(now);
142: assertFalse(chl.isChanged());
143: }
144:
145: public void testGetDate() {
146: model.setValue(now);
147: assertNotSame(now, model.getDate());
148: assertEquals(model.getDate(), model.getValue());
149: }
150:
151: public void testSetGetCalendarField() {
152: model.setCalendarField(Calendar.ERA);
153: assertTrue(chl.isChanged());
154: assertEquals(Calendar.ERA, model.getCalendarField());
155: testExceptionalCase(new IllegalArgumentCase() {
156: @Override
157: public void exceptionalAction() throws Exception {
158: model.setCalendarField(666);
159: }
160: });
161: model.setCalendarField(Calendar.DAY_OF_WEEK);
162: chl.reset();
163: model.setCalendarField(Calendar.DAY_OF_WEEK);
164: assertFalse(chl.isChanged());
165: }
166:
167: @SuppressWarnings("deprecation")
168: public void testGetNextValue() {
169: model.setValue(now.clone());
170: model.setEnd(future);
171: now.setDate(now.getDate() + 1);
172: assertEquals(now, model.getNextValue());
173: now.setDate(now.getDate() - 1);
174: model.setCalendarField(Calendar.MINUTE);
175: now.setMinutes(now.getMinutes() + 1);
176: assertEquals(now, model.getNextValue());
177: model.setCalendarField(Calendar.YEAR);
178: assertNull(model.getNextValue());
179: }
180:
181: @SuppressWarnings("deprecation")
182: public void testGetPreviousValue() {
183: model.setValue(now.clone());
184: model.setStart(past);
185: now.setDate(now.getDate() - 1);
186: assertEquals(now, model.getPreviousValue());
187: now.setDate(now.getDate() + 1);
188: model.setCalendarField(Calendar.MINUTE);
189: now.setMinutes(now.getMinutes() - 1);
190: assertEquals(now, model.getPreviousValue());
191: model.setCalendarField(Calendar.YEAR);
192: assertNull(model.getPreviousValue());
193: }
194: }
|