1 16 17 package org.springframework.beans; 18 19 24 public class MutablePropertyValuesTests extends AbstractPropertyValuesTests { 25 26 public void testValid() throws Exception { 27 MutablePropertyValues pvs = new MutablePropertyValues(); 28 pvs.addPropertyValue(new PropertyValue("forname", "Tony")); 29 pvs.addPropertyValue(new PropertyValue("surname", "Blair")); 30 pvs.addPropertyValue(new PropertyValue("age", "50")); 31 doTestTony(pvs); 32 33 MutablePropertyValues deepCopy = new MutablePropertyValues(pvs); 34 doTestTony(deepCopy); 35 deepCopy.setPropertyValueAt(new PropertyValue("name", "Gordon"), 0); 36 doTestTony(pvs); 37 assertEquals("Gordon", deepCopy.getPropertyValue("name").getValue()); 38 } 39 40 public void addOrOverride() throws Exception { 41 MutablePropertyValues pvs = new MutablePropertyValues(); 42 pvs.addPropertyValue(new PropertyValue("forname", "Tony")); 43 pvs.addPropertyValue(new PropertyValue("surname", "Blair")); 44 pvs.addPropertyValue(new PropertyValue("age", "50")); 45 doTestTony(pvs); 46 PropertyValue addedPv = new PropertyValue("rod", "Rod"); 47 pvs.addPropertyValue(addedPv); 48 assertTrue(pvs.getPropertyValue("rod").equals(addedPv)); 49 PropertyValue changedPv = new PropertyValue("forname", "Greg"); 50 pvs.addPropertyValue(changedPv); 51 assertTrue(pvs.getPropertyValue("forename").equals(changedPv)); 52 } 53 54 public void testChangesOnEquals() throws Exception { 55 MutablePropertyValues pvs = new MutablePropertyValues(); 56 pvs.addPropertyValue(new PropertyValue("forname", "Tony")); 57 pvs.addPropertyValue(new PropertyValue("surname", "Blair")); 58 pvs.addPropertyValue(new PropertyValue("age", "50")); 59 MutablePropertyValues pvs2 = pvs; 60 PropertyValues changes = pvs2.changesSince(pvs); 61 assertTrue("changes are empty", changes.getPropertyValues().length == 0); 62 } 63 64 public void testChangeOfOneField() throws Exception { 65 MutablePropertyValues pvs = new MutablePropertyValues(); 66 pvs.addPropertyValue(new PropertyValue("forname", "Tony")); 67 pvs.addPropertyValue(new PropertyValue("surname", "Blair")); 68 pvs.addPropertyValue(new PropertyValue("age", "50")); 69 70 MutablePropertyValues pvs2 = new MutablePropertyValues(pvs); 71 PropertyValues changes = pvs2.changesSince(pvs); 72 assertTrue("changes are empty, not of length " + changes.getPropertyValues().length, 73 changes.getPropertyValues().length == 0); 74 75 pvs2.addPropertyValue(new PropertyValue("forname", "Gordon")); 76 changes = pvs2.changesSince(pvs); 77 assertEquals("1 change", 1, changes.getPropertyValues().length); 78 PropertyValue fn = changes.getPropertyValue("forname"); 79 assertTrue("change is forname", fn != null); 80 assertTrue("new value is gordon", fn.getValue().equals("Gordon")); 81 82 MutablePropertyValues pvs3 = new MutablePropertyValues(pvs); 83 changes = pvs3.changesSince(pvs); 84 assertTrue("changes are empty, not of length " + changes.getPropertyValues().length, 85 changes.getPropertyValues().length == 0); 86 87 pvs3.addPropertyValue(new PropertyValue("foo", "bar")); 89 pvs3.addPropertyValue(new PropertyValue("fi", "fum")); 90 changes = pvs3.changesSince(pvs); 91 assertTrue("2 change", changes.getPropertyValues().length == 2); 92 fn = changes.getPropertyValue("foo"); 93 assertTrue("change in foo", fn != null); 94 assertTrue("new value is bar", fn.getValue().equals("bar")); 95 } 96 97 } 98 | Popular Tags |