1   /*
2    * Copyright 2002-2005 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 junit.framework.TestCase;
19  
20  import org.springframework.binding.support.BeanPropertyAccessStrategy;
21  import org.springframework.binding.support.TestBean;
22  import org.springframework.binding.support.TestPropertyChangeListener;
23  import org.springframework.binding.value.ValueModel;
24  import org.springframework.binding.value.support.ValueHolder;
25  import org.springframework.binding.value.support.ValueModelWrapper;
26  
27  public abstract class AbstractFormModelCornerCaseTests extends TestCase {
28  
29      protected AbstractFormModel getFormModel(BeanPropertyAccessStrategy pas, boolean buffering) {
30          return new TestAbstractFormModel(pas, buffering);
31      }
32  
33      protected AbstractFormModel getFormModel(ValueModel formObjectHolder, boolean buffering) {
34          return new TestAbstractFormModel(formObjectHolder, buffering);
35      }
36  
37      /**
38       * For a given hierarchy of form models or each possible property there should 
39       * only be one (Buffered)ValueModel that is shared between all members.
40       */
41      public void testTwoFormModelsInHierarchyShareSameFormObjectHolderBuffered() {
42          // XXX: fails
43  //        testTwoFormModelsInHierarchyShareSameFormObjectHolder(true);
44      }
45      
46      public void testTwoFormModelsInHierarchyShareSameFormObjectHolderUnbuffered() {
47          // XXX: fails
48  //        testTwoFormModelsInHierarchyShareSameFormObjectHolder(false);
49      }
50      
51      public void testTwoFormModelsInHierarchyShareSameFormObjectHolder(boolean buffered) {
52          ValueHolder vm = new ValueHolder(new TestBean());
53          AbstractFormModel fm1 = getFormModel(vm, buffered);
54          AbstractFormModel fm2 = getFormModel(vm, buffered);
55          TestPropertyChangeListener pcl = new TestPropertyChangeListener(ValueModel.VALUE_PROPERTY);
56          fm2.getValueModel("simpleProperty").addValueChangeListener(pcl);
57          fm1.addChild(fm2);
58  
59          fm1.getValueModel("simpleProperty").setValue("1");
60  
61          assertEquals("update to simpleProperty in fm1 should have propagated to fm2", "1", fm2.getValueModel(
62                  "simpleProperty").getValue());
63          assertEquals(1, pcl.eventCount());
64          assertEquals(((ValueModelWrapper)fm1.getValueModel("simpleProperty")).getInnerMostWrappedValueModel(),
65                  ((ValueModelWrapper)fm2.getValueModel("simpleProperty")).getInnerMostWrappedValueModel());
66      }
67  
68      /** 
69       * When a form model commits it should make sure that it commits buffered values
70       * in breadth first order from the root value model down the property/subproperty 
71       * chain. If there's a hierarchy this ordering would need to be done across for all 
72       * buffered value model in all form models in one go not just on a form model by form
73       * model basis.
74       * If this is not done then changes committed to child properties will be overwritten 
75       * by changes to parent properties that commit after the child.
76       */
77      public void testBufferingMustCommitParentPropertiesBeforeChildProperties() {
78          // XXX: fails
79  //        TestBean t = new TestBean();
80  //        t.setNestedProperty(new TestBean());
81  //        ValueHolder vm = new ValueHolder(t);
82  //        AbstractFormModel fm = getFormModel(vm, true);
83  //
84  //        TestBean t2 = new TestBean();
85  //        t2.setSimpleProperty("*");
86  //        fm.getValueModel("nestedProperty.simpleProperty").setValue("1");
87  //        fm.getValueModel("nestedProperty").setValue(t2);
88  //        fm.getValueModel("nestedProperty.simpleProperty").setValue("2");
89  //
90  //        fm.commit();
91  //
92  //        assertEquals("change to nestedProperty was not committed before change to nestedProperty.simpleProperty", "2",
93  //                t.getNestedProperty().getSimpleProperty());
94      }
95  }