001 /* 002 * Copyright 2002-2004 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.binding.form.support; 017 018 import org.springframework.binding.support.TestPropertyChangeListener; 019 import org.springframework.binding.value.ValueModel; 020 import org.springframework.binding.value.support.DirtyTrackingValueModel; 021 import org.springframework.binding.value.support.ValueHolder; 022 import org.springframework.richclient.test.SpringRichTestCase; 023 024 /** 025 * Tests for @link FormModelMediatingValueModel 026 * 027 * @author Oliver Hutchison 028 */ 029 public class FormModelMediatingValueModelTests extends SpringRichTestCase { 030 031 private FormModelMediatingValueModel mvm; 032 033 private ValueModel pvm; 034 035 private TestPropertyChangeListener vcl; 036 037 private TestPropertyChangeListener dcl; 038 039 protected void doSetUp() throws Exception { 040 pvm = new ValueHolder(); 041 mvm = new FormModelMediatingValueModel(pvm, true); 042 vcl = new TestPropertyChangeListener(ValueModel.VALUE_PROPERTY); 043 dcl = new TestPropertyChangeListener(DirtyTrackingValueModel.DIRTY_PROPERTY); 044 mvm.addValueChangeListener(vcl); 045 mvm.addPropertyChangeListener(DirtyTrackingValueModel.DIRTY_PROPERTY, dcl); 046 } 047 048 public void testSetGet() { 049 mvm.setValue("1"); 050 vcl.assertLastEvent(1, null, "1"); 051 assertEquals("1", mvm.getValue()); 052 assertEquals("1", pvm.getValue()); 053 054 mvm.setValue("2"); 055 vcl.assertLastEvent(2, "1", "2"); 056 assertEquals("2", mvm.getValue()); 057 assertEquals("2", pvm.getValue()); 058 059 mvm.setValue("2"); 060 vcl.assertEventCount(2); 061 062 mvm.setValue(null); 063 vcl.assertLastEvent(3, "2", null); 064 assertEquals(null, mvm.getValue()); 065 assertEquals(null, pvm.getValue()); 066 067 mvm.setValue(null); 068 vcl.assertEventCount(3); 069 } 070 071 public void testDirtyTracking() { 072 assertTrue(!mvm.isDirty()); 073 dcl.assertEventCount(0); 074 075 mvm.setValue("1"); 076 assertTrue(mvm.isDirty()); 077 dcl.assertLastEvent(1, false, true); 078 079 mvm.setValue(null); 080 assertTrue(!mvm.isDirty()); 081 dcl.assertLastEvent(2, true, false); 082 083 mvm.setValue("2"); 084 assertTrue(mvm.isDirty()); 085 dcl.assertLastEvent(3, false, true); 086 087 mvm.setValue("3"); 088 dcl.assertEventCount(3); 089 090 mvm.clearDirty(); 091 assertTrue(!mvm.isDirty()); 092 dcl.assertLastEvent(4, true, false); 093 } 094 095 public void testValueUpFromPropertyValueModelClearsDirty() { 096 pvm.setValue("1"); 097 assertTrue(!mvm.isDirty()); 098 dcl.assertEventCount(0); 099 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 }