01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.tests.constraints;
16:
17: import java.util.Date;
18: import junit.framework.TestCase;
19: import org.araneaframework.mock.MockLifeCycle;
20: import org.araneaframework.tests.constraints.helper.ConstraintTestHelper;
21: import org.araneaframework.tests.mock.MockEnvironment;
22: import org.araneaframework.uilib.form.FormElement;
23: import org.araneaframework.uilib.form.FormWidget;
24: import org.araneaframework.uilib.form.constraint.AfterTodayConstraint;
25: import org.araneaframework.uilib.form.control.DateControl;
26: import org.araneaframework.uilib.form.data.DateData;
27:
28: /**
29: * @author Taimo Peelo (taimo@araneaframework.org)
30: */
31: public class AfterTodayConstraintTest extends TestCase {
32: public static class FakeDateControl extends DateControl {
33: public boolean isRead() {
34: return true;
35: }
36: }
37:
38: public static final long DAY = 1000l * 60 * 60 * 24;
39:
40: private FormWidget form;
41: private FormElement dateElement;
42: private ConstraintTestHelper helper;
43:
44: public void setUp() throws Exception {
45: form = new FormWidget();
46: dateElement = form.createElement("#date",
47: new FakeDateControl(), new DateData(), false);
48: form.addElement("date", dateElement);
49: MockLifeCycle.begin(form, new MockEnvironment());
50:
51: helper = new ConstraintTestHelper(form, dateElement);
52: }
53:
54: public void testFuture() throws Exception {
55: helper.testConstraintValidness(new AfterTodayConstraint(false),
56: new Date(System.currentTimeMillis() + 20
57: * AfterTodayConstraintTest.DAY), true);
58:
59: helper.testConstraintValidness(new AfterTodayConstraint(true),
60: new Date(System.currentTimeMillis() + 20
61: * AfterTodayConstraintTest.DAY), true);
62: }
63:
64: public void testPast() throws Exception {
65: helper.testConstraintValidness(new AfterTodayConstraint(false),
66: new Date(System.currentTimeMillis() - 20
67: * AfterTodayConstraintTest.DAY), false);
68:
69: helper.testConstraintValidness(new AfterTodayConstraint(true),
70: new Date(System.currentTimeMillis() - 20
71: * AfterTodayConstraintTest.DAY), false);
72: }
73:
74: // test that constraint works correctly with today's date
75: public void testPresent() throws Exception {
76: Date now = new Date();
77: // disallow today
78: helper.testConstraintValidness(new AfterTodayConstraint(false),
79: now, false);
80: // allow today
81: helper.testConstraintValidness(new AfterTodayConstraint(true),
82: now, true);
83: }
84:
85: // in case of date being null, after today constraint should invalidate
86: public void testNull() throws Exception {
87: helper.testConstraintValidness(new AfterTodayConstraint(false),
88: null, false);
89: }
90: }
|