dynabeanutilstestcase.java
来自「这是一个有关common beanutils 的源码」· Java 代码 · 共 1,267 行 · 第 1/4 页
JAVA
1,267 行
* tests getting a nested property
*/
public void testGetNestedProperty() {
try {
String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty");
String comp = nested.getStringProperty();
assertTrue("nested.StringProperty == " + comp,
val.equals(comp));
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
} catch (NoSuchMethodException e) {
fail("NoSuchMethodException");
}
}
/**
* tests getting a 'whatever' property
*/
public void testGetGeneralProperty() {
try {
String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]");
String comp = String.valueOf(bean.get("intIndexed", 2));
assertTrue("nested.intIndexed[2] == " + comp,
val.equals(comp));
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
} catch (NoSuchMethodException e) {
fail("NoSuchMethodException");
}
}
/**
* tests getting a 'whatever' property
*/
public void testGetSimpleProperty() {
try {
String val = BeanUtils.getSimpleProperty(bean, "shortProperty");
String comp = String.valueOf(bean.get("shortProperty"));
assertTrue("shortProperty == " + comp,
val.equals(comp));
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
} catch (NoSuchMethodException e) {
fail("NoSuchMethodException");
}
}
/**
* Test populate() method on individual array elements.
*/
public void testPopulateArrayElements() {
try {
HashMap map = new HashMap();
map.put("intIndexed[0]", "100");
map.put("intIndexed[2]", "120");
map.put("intIndexed[4]", "140");
BeanUtils.populate(bean, map);
Integer intIndexed0 = (Integer) bean.get("intIndexed", 0);
assertEquals("intIndexed[0] is 100",
100, intIndexed0.intValue());
Integer intIndexed1 = (Integer) bean.get("intIndexed", 1);
assertEquals("intIndexed[1] is 10",
10, intIndexed1.intValue());
Integer intIndexed2 = (Integer) bean.get("intIndexed", 2);
assertEquals("intIndexed[2] is 120",
120, intIndexed2.intValue());
Integer intIndexed3 = (Integer) bean.get("intIndexed", 3);
assertEquals("intIndexed[3] is 30",
30, intIndexed3.intValue());
Integer intIndexed4 = (Integer) bean.get("intIndexed", 4);
assertEquals("intIndexed[4] is 140",
140, intIndexed4.intValue());
map.clear();
map.put("stringIndexed[1]", "New String 1");
map.put("stringIndexed[3]", "New String 3");
BeanUtils.populate(bean, map);
assertEquals("stringIndexed[0] is \"String 0\"",
"String 0",
(String) bean.get("stringIndexed", 0));
assertEquals("stringIndexed[1] is \"New String 1\"",
"New String 1",
(String) bean.get("stringIndexed", 1));
assertEquals("stringIndexed[2] is \"String 2\"",
"String 2",
(String) bean.get("stringIndexed", 2));
assertEquals("stringIndexed[3] is \"New String 3\"",
"New String 3",
(String) bean.get("stringIndexed", 3));
assertEquals("stringIndexed[4] is \"String 4\"",
"String 4",
(String) bean.get("stringIndexed", 4));
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
}
}
/**
* Test populate() on mapped properties.
*/
public void testPopulateMapped() {
try {
HashMap map = new HashMap();
map.put("mappedProperty(First Key)", "New First Value");
map.put("mappedProperty(Third Key)", "New Third Value");
BeanUtils.populate(bean, map);
assertEquals("mappedProperty(First Key)",
"New First Value",
(String) bean.get("mappedProperty", "First Key"));
assertEquals("mappedProperty(Second Key)",
"Second Value",
(String) bean.get("mappedProperty", "Second Key"));
assertEquals("mappedProperty(Third Key)",
"New Third Value",
(String) bean.get("mappedProperty", "Third Key"));
assertNull("mappedProperty(Fourth Key",
bean.get("mappedProperty", "Fourth Key"));
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
}
}
/**
* Test populate() method on nested properties.
*/
public void testPopulateNested() {
try {
HashMap map = new HashMap();
map.put("nested.booleanProperty", "false");
// booleanSecond is left at true
map.put("nested.doubleProperty", "432.0");
// floatProperty is left at 123.0
map.put("nested.intProperty", "543");
// longProperty is left at 321
map.put("nested.shortProperty", "654");
// stringProperty is left at "This is a string"
BeanUtils.populate(bean, map);
TestBean nested = (TestBean) bean.get("nested");
assertTrue("booleanProperty is false",
!nested.getBooleanProperty());
assertTrue("booleanSecond is true",
nested.isBooleanSecond());
assertEquals("doubleProperty is 432.0",
432.0,
nested.getDoubleProperty(),
0.005);
assertEquals("floatProperty is 123.0",
(float) 123.0,
nested.getFloatProperty(),
(float) 0.005);
assertEquals("intProperty is 543",
543, nested.getIntProperty());
assertEquals("longProperty is 321",
321, nested.getLongProperty());
assertEquals("shortProperty is 654",
(short) 654, nested.getShortProperty());
assertEquals("stringProperty is \"This is a string\"",
"This is a string",
nested.getStringProperty());
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
}
}
/**
* Test populate() method on scalar properties.
*/
public void testPopulateScalar() {
try {
bean.set("nullProperty", "non-null value");
HashMap map = new HashMap();
map.put("booleanProperty", "false");
// booleanSecond is left at true
map.put("doubleProperty", "432.0");
// floatProperty is left at 123.0
map.put("intProperty", "543");
// longProperty is left at 321
map.put("nullProperty", null);
map.put("shortProperty", "654");
// stringProperty is left at "This is a string"
BeanUtils.populate(bean, map);
Boolean booleanProperty = (Boolean) bean.get("booleanProperty");
assertTrue("booleanProperty is false", !booleanProperty.booleanValue());
Boolean booleanSecond = (Boolean) bean.get("booleanSecond");
assertTrue("booleanSecond is true", booleanSecond.booleanValue());
Double doubleProperty = (Double) bean.get("doubleProperty");
assertEquals("doubleProperty is 432.0",
432.0, doubleProperty.doubleValue(), 0.005);
Float floatProperty = (Float) bean.get("floatProperty");
assertEquals("floatProperty is 123.0",
(float) 123.0, floatProperty.floatValue(),
(float) 0.005);
Integer intProperty = (Integer) bean.get("intProperty");
assertEquals("intProperty is 543",
543, intProperty.intValue());
Long longProperty = (Long) bean.get("longProperty");
assertEquals("longProperty is 321",
321, longProperty.longValue());
assertNull("nullProperty is null", bean.get("nullProperty"));
Short shortProperty = (Short) bean.get("shortProperty");
assertEquals("shortProperty is 654",
(short) 654, shortProperty.shortValue());
assertEquals("stringProperty is \"This is a string\"",
"This is a string",
(String) bean.get("stringProperty"));
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
}
}
/**
* Test calling setProperty() with null property values.
*/
public void testSetPropertyNullValues() throws Exception {
Object oldValue = null;
Object newValue = null;
// Scalar value into array
oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
BeanUtils.setProperty(bean, "stringArray", (String) null);
newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
assertNotNull("stringArray is not null", newValue);
assertTrue("stringArray of correct type",
newValue instanceof String[]);
assertEquals("stringArray length",
1, ((String[]) newValue).length);
PropertyUtils.setProperty(bean, "stringArray", oldValue);
// Indexed value into array
oldValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
BeanUtils.setProperty(bean, "stringArray[2]", (String) null);
newValue = PropertyUtils.getSimpleProperty(bean, "stringArray");
assertNotNull("stringArray is not null", newValue);
assertTrue("stringArray of correct type",
newValue instanceof String[]);
assertEquals("stringArray length",
5, ((String[]) newValue).length);
assertTrue("stringArray[2] is null",
((String[]) newValue)[2] == null);
PropertyUtils.setProperty(bean, "stringArray", oldValue);
// Value into scalar
BeanUtils.setProperty(bean, "stringProperty", null);
assertTrue("stringProperty is now null",
BeanUtils.getProperty(bean, "stringProperty") == null);
}
/**
* Test converting to and from primitive wrapper types.
*/
public void testSetPropertyOnPrimitiveWrappers() throws Exception {
BeanUtils.setProperty(bean,"intProperty", new Integer(1));
assertEquals(1,((Integer) bean.get("intProperty")).intValue());
BeanUtils.setProperty(bean,"stringProperty", new Integer(1));
assertEquals(1, Integer.parseInt((String) bean.get("stringProperty")));
}
/**
* Test setting a null property value.
*/
public void testSetPropertyNull() throws Exception {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?