1   /*
2    * Copyright 2002-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.form.binding.swing.date;
17  
18  import java.util.Calendar;
19  import java.util.Date;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.swing.JComponent;
24  
25  import org.springframework.binding.form.FieldMetadata;
26  import org.springframework.richclient.form.binding.swing.BindingAbstractTests;
27  
28  /**
29   * Skeleton test for <code>AbstractDateFieldBinder</code> and
30   * <code>AbstractDateFieldBinding</code> subclasses.
31   * 
32   * @author Geoffrey De Smet
33   * @author Peter De Bruycker
34   */
35  public abstract class AbstractDateFieldBindingTestCase extends BindingAbstractTests {
36  
37  	private AbstractDateFieldBinder binder;
38  
39  	private AbstractDateFieldBinding binding;
40  
41  	private JComponent dateField;
42  
43  	protected String setUpBinding() {
44  		Map context = new HashMap();
45  
46  		binder = createBinder();
47  		binding = (AbstractDateFieldBinding) binder.bind(fm, "dateProperty", context);
48  		dateField = binding.getControl();
49  
50  		return "dateProperty";
51  	}
52  
53  	protected abstract AbstractDateFieldBinder createBinder();
54  
55  	public void testInitialValue() {
56  		Date date = createDate(1981, 10, 16);
57  		vm.setValue(date);
58  		assertNotNull(vm.getValue());
59  	}
60  
61  	private Date createDate(int year, int month, int day) {
62  		Calendar calendar = Calendar.getInstance();
63  		calendar.set(year, month, day, 0, 0, 0);
64  		Date date = calendar.getTime();
65  		return date;
66  	}
67  
68  	public final void testComponentTracksEnabledChanges() {
69  		assertEquals(true, dateField.isEnabled());
70  		fm.setEnabled(false);
71  		assertEquals(false, dateField.isEnabled());
72  		fm.setEnabled(true);
73  		assertEquals(true, dateField.isEnabled());
74  	}
75  
76  	public final void testComponentTracksReadOnlyChanges() {
77  		FieldMetadata state = fm.getFieldMetadata("dateProperty");
78  		assertEquals(true, !isReadOnly(dateField));
79  		state.setReadOnly(true);
80  		assertEquals(false, !isReadOnly(dateField));
81  		state.setReadOnly(false);
82  		assertEquals(true, !isReadOnly(dateField));
83  	}
84  	
85  	protected abstract boolean isReadOnly(JComponent dateField);
86  
87  	protected abstract Date getValue(JComponent dateField);
88  
89  	protected abstract void setValue(JComponent dateField, Date date);
90  
91  	public final void testComponentUpdatesValueModel() {
92  		Date date1 = createDate(1981, 10, 16);
93  		setValue(dateField, date1);
94  		assertEquals(date1, (Date)vm.getValue());
95  		setValue(dateField, null);
96  		assertEquals(null, (Date)vm.getValue());
97  		Date date2 = createDate(1999, 11, 31);
98  		setValue(dateField, date2);
99  		assertEquals(date2, (Date)vm.getValue());
100 	}
101 
102 	public final void testValueModelUpdatesComponent() {
103 		Date date1 = createDate(1981, 10, 16);
104 		vm.setValue(date1);
105 		assertEquals(date1, getValue(dateField));
106 		vm.setValue(null);
107 		assertEquals(null, getValue(dateField));
108 		Date date2 = createDate(1999, 11, 31);
109 		vm.setValue(date2);
110 		assertEquals(date2, getValue(dateField));
111 	}
112 	
113 	/*
114 	 * trim the dates to seconds
115 	 */
116 	protected void assertEquals(Date date1, Date date2) {
117 		if(date1 == null || date2 == null) {
118 			super.assertEquals(date1, date2);
119 			return;
120 		}
121 		
122 		long l1 = date1.getTime();
123 		l1 = ((long)Math.round(l1/1000))*1000;
124 		long l2 = date2.getTime();
125 		l2 = ((long)Math.round(l1/1000))*1000;
126 		
127 		assertEquals(l1, l2);
128 	}
129 }