KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > MutablePropertyValuesTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans;
18
19 /**
20  * Tests for MutablePropertyValues.
21  *
22  * @author Rod Johnson
23  */

24 public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
25
26     public void testValid() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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         // add new
88
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