📄 beanwrappertests.java
字号:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans;
import java.beans.PropertyEditorSupport;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import junit.framework.TestCase;
import org.hibernate.FlushMode;
import org.springframework.beans.propertyeditors.StringArrayPropertyEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.beans.support.DerivedFromProtectedBaseBean;
import org.springframework.util.StringUtils;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class BeanWrapperTests extends TestCase {
public void testSetWrappedInstanceOfSameClass() throws Exception {
TestBean tb = new TestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
assertTrue(bw.isReadableProperty("age"));
tb.setAge(11);
TestBean tb2 = new TestBean();
bw.setWrappedInstance(tb2);
bw.setPropertyValue("age", new Integer(14));
assertTrue("2nd changed", tb2.getAge() == 14);
assertTrue("1 didn't change", tb.getAge() == 11);
}
public void testIsReadablePropertyNotReadable() {
NoRead nr = new NoRead();
BeanWrapper bw = new BeanWrapperImpl(nr);
assertFalse(bw.isReadableProperty("age"));
}
/**
* Shouldn't throw an exception: should just return false
*/
public void testIsReadablePropertyNoSuchProperty() {
NoRead nr = new NoRead();
BeanWrapper bw = new BeanWrapperImpl(nr);
assertFalse(bw.isReadableProperty("xxxxx"));
}
public void testIsReadablePropertyNull() {
NoRead nr = new NoRead();
BeanWrapper bw = new BeanWrapperImpl(nr);
try {
bw.isReadableProperty(null);
fail("Can't inquire into readability of null property");
}
catch (IllegalArgumentException ex) {
// expected
}
}
public void testIsWritablePropertyNull() {
NoRead nr = new NoRead();
BeanWrapper bw = new BeanWrapperImpl(nr);
try {
bw.isWritableProperty(null);
fail("Can't inquire into writability of null property");
}
catch (IllegalArgumentException ex) {
// expected
}
}
public void testReadableAndWritableForIndexedProperties() {
BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class);
assertTrue(bw.isReadableProperty("array"));
assertTrue(bw.isReadableProperty("list"));
assertTrue(bw.isReadableProperty("set"));
assertTrue(bw.isReadableProperty("map"));
assertFalse(bw.isReadableProperty("xxx"));
assertTrue(bw.isWritableProperty("array"));
assertTrue(bw.isWritableProperty("list"));
assertTrue(bw.isWritableProperty("set"));
assertTrue(bw.isWritableProperty("map"));
assertFalse(bw.isWritableProperty("xxx"));
assertTrue(bw.isReadableProperty("array[0]"));
assertTrue(bw.isReadableProperty("array[0].name"));
assertTrue(bw.isReadableProperty("list[0]"));
assertTrue(bw.isReadableProperty("list[0].name"));
assertTrue(bw.isReadableProperty("set[0]"));
assertTrue(bw.isReadableProperty("set[0].name"));
assertTrue(bw.isReadableProperty("map[key1]"));
assertTrue(bw.isReadableProperty("map[key1].name"));
assertTrue(bw.isReadableProperty("map[key4][0]"));
assertTrue(bw.isReadableProperty("map[key4][0].name"));
assertTrue(bw.isReadableProperty("map[key4][1]"));
assertTrue(bw.isReadableProperty("map[key4][1].name"));
assertFalse(bw.isReadableProperty("array[key1]"));
assertTrue(bw.isWritableProperty("array[0]"));
assertTrue(bw.isWritableProperty("array[0].name"));
assertTrue(bw.isWritableProperty("list[0]"));
assertTrue(bw.isWritableProperty("list[0].name"));
assertTrue(bw.isWritableProperty("set[0]"));
assertTrue(bw.isWritableProperty("set[0].name"));
assertTrue(bw.isWritableProperty("map[key1]"));
assertTrue(bw.isWritableProperty("map[key1].name"));
assertTrue(bw.isWritableProperty("map[key4][0]"));
assertTrue(bw.isWritableProperty("map[key4][0].name"));
assertTrue(bw.isWritableProperty("map[key4][1]"));
assertTrue(bw.isWritableProperty("map[key4][1].name"));
assertFalse(bw.isWritableProperty("array[key1]"));
}
public void testTypeDeterminationForIndexedProperty() {
BeanWrapper bw = new BeanWrapperImpl(IndexedTestBean.class);
assertEquals(null, bw.getPropertyType("map[key0]"));
bw = new BeanWrapperImpl(IndexedTestBean.class);
bw.setPropertyValue("map[key0]", "my String");
assertEquals(String.class, bw.getPropertyType("map[key0]"));
bw = new BeanWrapperImpl(IndexedTestBean.class);
bw.registerCustomEditor(String.class, "map[key0]", new StringTrimmerEditor(false));
assertEquals(String.class, bw.getPropertyType("map[key0]"));
}
public void testSetWrappedInstanceOfDifferentClass() {
ThrowsException tex = new ThrowsException();
BeanWrapper bw = new BeanWrapperImpl(tex);
TestBean tb2 = new TestBean();
bw.setWrappedInstance(tb2);
bw.setPropertyValue("age", new Integer(14));
assertTrue("2nd changed", tb2.getAge() == 14);
}
public void testGetterThrowsException() {
GetterBean gb = new GetterBean();
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setPropertyValue("name", "tom");
assertTrue("Set name to tom", gb.getName().equals("tom"));
}
public void testEmptyPropertyValuesSet() {
TestBean t = new TestBean();
int age = 50;
String name = "Tony";
t.setAge(age);
t.setName(name);
try {
BeanWrapper bw = new BeanWrapperImpl(t);
assertTrue("age is OK", t.getAge() == age);
assertTrue("name is OK", name.equals(t.getName()));
bw.setPropertyValues(new MutablePropertyValues());
// Check its unchanged
assertTrue("age is OK", t.getAge() == age);
assertTrue("name is OK", name.equals(t.getName()));
}
catch (BeansException ex) {
fail("Shouldn't throw exception when everything is valid");
}
}
public void testAllValid() {
TestBean t = new TestBean();
String newName = "tony";
int newAge = 65;
String newTouchy = "valid";
try {
BeanWrapper bw = new BeanWrapperImpl(t);
//System.out.println(bw);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("age", new Integer(newAge)));
pvs.addPropertyValue(new PropertyValue("name", newName));
pvs.addPropertyValue(new PropertyValue("touchy", newTouchy));
bw.setPropertyValues(pvs);
assertTrue("Validly set property must stick", t.getName().equals(newName));
assertTrue("Validly set property must stick", t.getTouchy().equals(newTouchy));
assertTrue("Validly set property must stick", t.getAge() == newAge);
}
catch (BeansException ex) {
fail("Shouldn't throw exception when everything is valid");
}
}
public void testBeanWrapperUpdates() {
TestBean t = new TestBean();
int newAge = 33;
try {
BeanWrapper bw = new BeanWrapperImpl(t);
t.setAge(newAge);
Object bwAge = bw.getPropertyValue("age");
assertTrue("Age is an integer", bwAge instanceof Integer);
int bwi = ((Integer) bwAge).intValue();
assertTrue("Bean wrapper must pick up changes", bwi == newAge);
}
catch (Exception ex) {
fail("Shouldn't throw exception when everything is valid");
}
}
public void testValidNullUpdate() {
TestBean t = new TestBean();
t.setName("Frank"); // we need to change it back
t.setSpouse(t);
BeanWrapper bw = new BeanWrapperImpl(t);
assertTrue("name is not null to start off", t.getName() != null);
bw.setPropertyValue("name", null);
assertTrue("name is now null", t.getName() == null);
// now test with non-string
assertTrue("spouse is not null to start off", t.getSpouse() != null);
bw.setPropertyValue("spouse", null);
assertTrue("spouse is now null", t.getSpouse() == null);
}
public void testIgnoringIndexedProperty() {
MutablePropertyValues values = new MutablePropertyValues();
values.addPropertyValue("toBeIgnored[0]", new Integer(42));
BeanWrapper wrapper = new BeanWrapperImpl(new Object());
wrapper.setPropertyValues(values, true);
}
public void testBooleanObject() {
BooleanTestBean tb = new BooleanTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
try {
bw.setPropertyValue("bool2", "true");
}
catch (BeansException ex) {
fail("Should not throw BeansException: " + ex.getMessage());
}
assertTrue("Correct bool2 value", Boolean.TRUE.equals(bw.getPropertyValue("bool2")));
assertTrue("Correct bool2 value", tb.getBool2().booleanValue());
try {
bw.setPropertyValue("bool2", "false");
}
catch (BeansException ex) {
fail("Should not throw BeansException: " + ex.getMessage());
}
assertTrue("Correct bool2 value", Boolean.FALSE.equals(bw.getPropertyValue("bool2")));
assertTrue("Correct bool2 value", !tb.getBool2().booleanValue());
}
public void testNumberObjects() {
NumberTestBean tb = new NumberTestBean();
BeanWrapper bw = new BeanWrapperImpl(tb);
try {
bw.setPropertyValue("short2", "2");
bw.setPropertyValue("int2", "8");
bw.setPropertyValue("long2", "6");
bw.setPropertyValue("bigInteger", "3");
bw.setPropertyValue("float2", "8.1");
bw.setPropertyValue("double2", "6.1");
bw.setPropertyValue("bigDecimal", "4.0");
}
catch (BeansException ex) {
fail("Should not throw BeansException: " + ex.getMessage());
}
assertTrue("Correct short2 value", new Short("2").equals(bw.getPropertyValue("short2")));
assertTrue("Correct short2 value", new Short("2").equals(tb.getShort2()));
assertTrue("Correct int2 value", new Integer("8").equals(bw.getPropertyValue("int2")));
assertTrue("Correct int2 value", new Integer("8").equals(tb.getInt2()));
assertTrue("Correct long2 value", new Long("6").equals(bw.getPropertyValue("long2")));
assertTrue("Correct long2 value", new Long("6").equals(tb.getLong2()));
assertTrue("Correct bigInteger value", new BigInteger("3").equals(bw.getPropertyValue("bigInteger")));
assertTrue("Correct bigInteger value", new BigInteger("3").equals(tb.getBigInteger()));
assertTrue("Correct float2 value", new Float("8.1").equals(bw.getPropertyValue("float2")));
assertTrue("Correct float2 value", new Float("8.1").equals(tb.getFloat2()));
assertTrue("Correct double2 value", new Double("6.1").equals(bw.getPropertyValue("double2")));
assertTrue("Correct double2 value", new Double("6.1").equals(tb.getDouble2()));
assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(bw.getPropertyValue("bigDecimal")));
assertTrue("Correct bigDecimal value", new BigDecimal("4.0").equals(tb.getBigDecimal()));
}
public void testEnum() {
EnumTest et = new EnumTest();
BeanWrapper bw = new BeanWrapperImpl(et);
bw.setPropertyValue("flushMode", "NEVER");
assertEquals(FlushMode.NEVER, et.getFlushMode());
try {
bw.setPropertyValue("flushMode", "EVER");
fail("Should have thrown TypeMismatchException");
}
catch (TypeMismatchException ex) {
// expected
}
}
public void testPropertiesProperty() throws Exception {
PropsTest pt = new PropsTest();
BeanWrapper bw = new BeanWrapperImpl(pt);
bw.setPropertyValue("name", "ptest");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -