1   /*
2    * Copyright 2002-2004 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.binding.form.support;
17  
18  import org.springframework.binding.support.TestPropertyChangeListener;
19  import org.springframework.binding.value.ValueModel;
20  import org.springframework.binding.value.support.DirtyTrackingValueModel;
21  import org.springframework.binding.value.support.ValueHolder;
22  import org.springframework.richclient.test.SpringRichTestCase;
23  
24  /**
25   * Tests for @link FormModelMediatingValueModel
26   * 
27   * @author  Oliver Hutchison
28   */
29  public class FormModelMediatingValueModelTests extends SpringRichTestCase {
30      
31      private FormModelMediatingValueModel mvm;
32      
33      private ValueModel pvm;
34      
35      private TestPropertyChangeListener vcl;
36      
37      private TestPropertyChangeListener dcl;
38      
39      protected void doSetUp() throws Exception {
40          pvm = new ValueHolder();
41          mvm = new FormModelMediatingValueModel(pvm, true);
42          vcl = new TestPropertyChangeListener(ValueModel.VALUE_PROPERTY);
43          dcl = new TestPropertyChangeListener(DirtyTrackingValueModel.DIRTY_PROPERTY);
44          mvm.addValueChangeListener(vcl);
45          mvm.addPropertyChangeListener(DirtyTrackingValueModel.DIRTY_PROPERTY, dcl);
46      }
47      
48      public void testSetGet() {
49          mvm.setValue("1");
50          vcl.assertLastEvent(1, null, "1");
51          assertEquals("1", mvm.getValue());
52          assertEquals("1", pvm.getValue());
53          
54          mvm.setValue("2");
55          vcl.assertLastEvent(2, "1", "2");
56          assertEquals("2", mvm.getValue());
57          assertEquals("2", pvm.getValue());
58          
59          mvm.setValue("2");
60          vcl.assertEventCount(2);
61          
62          mvm.setValue(null);
63          vcl.assertLastEvent(3, "2", null);
64          assertEquals(null, mvm.getValue());
65          assertEquals(null, pvm.getValue());
66          
67          mvm.setValue(null);
68          vcl.assertEventCount(3);
69      }
70      
71      public void testDirtyTracking() {
72          assertTrue(!mvm.isDirty());
73          dcl.assertEventCount(0);
74          
75          mvm.setValue("1");
76          assertTrue(mvm.isDirty());
77          dcl.assertLastEvent(1, false, true);
78          
79          mvm.setValue(null);
80          assertTrue(!mvm.isDirty());
81          dcl.assertLastEvent(2, true, false);
82                  
83          mvm.setValue("2");
84          assertTrue(mvm.isDirty());
85          dcl.assertLastEvent(3, false, true);
86          
87          mvm.setValue("3");
88          dcl.assertEventCount(3);
89          
90          mvm.clearDirty();
91          assertTrue(!mvm.isDirty());
92          dcl.assertLastEvent(4, true, false);
93      }
94      
95      public void testValueUpFromPropertyValueModelClearsDirty() {        
96          pvm.setValue("1");
97          assertTrue(!mvm.isDirty());
98          dcl.assertEventCount(0);
99          
100         mvm.setValue("1");
101         assertTrue(!mvm.isDirty());
102         dcl.assertEventCount(0);
103         
104         mvm.setValue("2");
105         assertTrue(mvm.isDirty());
106         dcl.assertLastEvent(1, false, true);
107         
108         // XXX Is this failing test important? Fixing this behavior would be fairly complex.     
109         // pvm.setValue("2");
110         // assertTrue(!mvm.isDirty());
111         // dcl.assertLastEvent(2, true, false);        
112     }
113     
114     public void testTurningOnAndOffDeliverValueChangeEvents() {
115         mvm.setDeliverValueChangeEvents(false);
116         mvm.setValue("1");
117         vcl.assertEventCount(0);
118         dcl.assertEventCount(0);        
119         assertEquals("1", mvm.getValue());
120         assertEquals("1", pvm.getValue());
121         assertEquals(true, mvm.isDirty());
122         
123         mvm.setValue("2");
124         
125         mvm.setDeliverValueChangeEvents(true);
126         vcl.assertLastEvent(1, null, "2");
127         dcl.assertLastEvent(1, false, true);        
128         
129         mvm.setDeliverValueChangeEvents(false);
130         pvm.setValue("1");
131         vcl.assertEventCount(1);
132         dcl.assertEventCount(1);        
133         assertEquals("1", mvm.getValue());
134         assertEquals("1", pvm.getValue());
135         assertEquals(false, mvm.isDirty());
136         
137         mvm.setDeliverValueChangeEvents(true);
138         vcl.assertLastEvent(2, "2", "1");
139         dcl.assertLastEvent(2, true, false);         
140     }
141 }