MutablePropertyValuesTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 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. package org.springframework.beans;
  17. /**
  18.  * Test for MutablePropertyValues.
  19.  * 
  20.  * @author Rod Johnson
  21.  */
  22. public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
  23. public void testValid() throws Exception {
  24. MutablePropertyValues pvs = new MutablePropertyValues();
  25. pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
  26. pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
  27. pvs.addPropertyValue(new PropertyValue("age", "50"));
  28. testTony(pvs);
  29. MutablePropertyValues deepCopy = new MutablePropertyValues(pvs);
  30. testTony(deepCopy);
  31. deepCopy.setPropertyValueAt(new PropertyValue("name", "Gordon"), 0);
  32. testTony(pvs);
  33. assertEquals("Gordon", deepCopy.getPropertyValue("name").getValue());
  34. }
  35. public void addOrOverride() throws Exception {
  36. MutablePropertyValues pvs = new MutablePropertyValues();
  37. pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
  38. pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
  39. pvs.addPropertyValue(new PropertyValue("age", "50"));
  40. testTony(pvs);
  41. PropertyValue addedPv = new PropertyValue("rod", "Rod");
  42. pvs.addPropertyValue(addedPv);
  43. assertTrue(pvs.getPropertyValue("rod").equals(addedPv));
  44. PropertyValue changedPv = new PropertyValue("forname", "Greg");
  45. pvs.addPropertyValue(changedPv);
  46. assertTrue(pvs.getPropertyValue("forename").equals(changedPv));
  47. }
  48. public void testChangesOnEquals() throws Exception {
  49. MutablePropertyValues pvs = new MutablePropertyValues();
  50. pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
  51. pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
  52. pvs.addPropertyValue(new PropertyValue("age", "50"));
  53. MutablePropertyValues pvs2 = pvs;
  54. PropertyValues changes = pvs2.changesSince(pvs);
  55. assertTrue("changes are empty", changes.getPropertyValues().length == 0);
  56. }
  57. public void testChangeOfOneField() throws Exception {
  58. MutablePropertyValues pvs = new MutablePropertyValues();
  59. pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
  60. pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
  61. pvs.addPropertyValue(new PropertyValue("age", "50"));
  62. MutablePropertyValues pvs2 = new MutablePropertyValues(pvs);
  63. PropertyValues changes = pvs2.changesSince(pvs);
  64. assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
  65.            changes.getPropertyValues().length == 0);
  66. pvs2.addPropertyValue(new PropertyValue("forname", "Gordon"));
  67. changes = pvs2.changesSince(pvs);
  68. assertTrue("1 change", changes.getPropertyValues().length == 1);
  69. PropertyValue fn = changes.getPropertyValue("forname");
  70. assertTrue("change is forname", fn != null);
  71. assertTrue("new value is gordon", fn.getValue().equals("Gordon"));
  72. MutablePropertyValues pvs3 = new MutablePropertyValues(pvs);
  73. changes = pvs3.changesSince(pvs);
  74. assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
  75.            changes.getPropertyValues().length == 0);
  76. // add new
  77. pvs3.addPropertyValue(new PropertyValue("foo", "bar"));
  78. pvs3.addPropertyValue(new PropertyValue("fi", "fum"));
  79. changes = pvs3.changesSince(pvs);
  80. assertTrue("2 change", changes.getPropertyValues().length == 2);
  81. fn = changes.getPropertyValue("foo");
  82. assertTrue("change in foo", fn != null);
  83. assertTrue("new value is bar", fn.getValue().equals("bar"));
  84. }
  85. }