001    /*
002     * Copyright 2002-2007 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.form.binding.swing.date;
017    
018    import java.util.Calendar;
019    import java.util.Date;
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    import javax.swing.JComponent;
024    
025    import org.springframework.binding.form.FieldMetadata;
026    import org.springframework.richclient.form.binding.swing.BindingAbstractTests;
027    
028    /**
029     * Skeleton test for <code>AbstractDateFieldBinder</code> and
030     * <code>AbstractDateFieldBinding</code> subclasses.
031     * 
032     * @author Geoffrey De Smet
033     * @author Peter De Bruycker
034     */
035    public abstract class AbstractDateFieldBindingTestCase extends BindingAbstractTests {
036    
037            private AbstractDateFieldBinder binder;
038    
039            private AbstractDateFieldBinding binding;
040    
041            private JComponent dateField;
042    
043            protected String setUpBinding() {
044                    Map context = new HashMap();
045    
046                    binder = createBinder();
047                    binding = (AbstractDateFieldBinding) binder.bind(fm, "dateProperty", context);
048                    dateField = binding.getControl();
049    
050                    return "dateProperty";
051            }
052    
053            protected abstract AbstractDateFieldBinder createBinder();
054    
055            public void testInitialValue() {
056                    Date date = createDate(1981, 10, 16);
057                    vm.setValue(date);
058                    assertNotNull(vm.getValue());
059            }
060    
061            private Date createDate(int year, int month, int day) {
062                    Calendar calendar = Calendar.getInstance();
063                    calendar.set(year, month, day, 0, 0, 0);
064                    Date date = calendar.getTime();
065                    return date;
066            }
067    
068            public final void testComponentTracksEnabledChanges() {
069                    assertEquals(true, dateField.isEnabled());
070                    fm.setEnabled(false);
071                    assertEquals(false, dateField.isEnabled());
072                    fm.setEnabled(true);
073                    assertEquals(true, dateField.isEnabled());
074            }
075    
076            public final void testComponentTracksReadOnlyChanges() {
077                    FieldMetadata state = fm.getFieldMetadata("dateProperty");
078                    assertEquals(true, !isReadOnly(dateField));
079                    state.setReadOnly(true);
080                    assertEquals(false, !isReadOnly(dateField));
081                    state.setReadOnly(false);
082                    assertEquals(true, !isReadOnly(dateField));
083            }
084            
085            protected abstract boolean isReadOnly(JComponent dateField);
086    
087            protected abstract Date getValue(JComponent dateField);
088    
089            protected abstract void setValue(JComponent dateField, Date date);
090    
091            public final void testComponentUpdatesValueModel() {
092                    Date date1 = createDate(1981, 10, 16);
093                    setValue(dateField, date1);
094                    assertEquals(date1, (Date)vm.getValue());
095                    setValue(dateField, null);
096                    assertEquals(null, (Date)vm.getValue());
097                    Date date2 = createDate(1999, 11, 31);
098                    setValue(dateField, date2);
099                    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    }