?? beanutilstestcase.java
字號:
}
}
/**
* Test populate() method on array properties as a whole.
*/
public void testPopulateArrayProperties() {
try {
HashMap map = new HashMap();
int intArray[] = new int[] { 123, 456, 789 };
map.put("intArray", intArray);
String stringArray[] = new String[]
{ "New String 0", "New String 1" };
map.put("stringArray", stringArray);
BeanUtils.populate(bean, map);
intArray = bean.getIntArray();
assertNotNull("intArray is present", intArray);
assertEquals("intArray length",
3, intArray.length);
assertEquals("intArray[0]", 123, intArray[0]);
assertEquals("intArray[1]", 456, intArray[1]);
assertEquals("intArray[2]", 789, intArray[2]);
stringArray = bean.getStringArray();
assertNotNull("stringArray is present", stringArray);
assertEquals("stringArray length", 2, stringArray.length);
assertEquals("stringArray[0]", "New String 0", stringArray[0]);
assertEquals("stringArray[1]", "New String 1", stringArray[1]);
} 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",
bean.getMappedProperty("First Key"));
assertEquals("mappedProperty(Second Key)",
"Second Value",
bean.getMappedProperty("Second Key"));
assertEquals("mappedProperty(Third Key)",
"New Third Value",
bean.getMappedProperty("Third Key"));
assertNull("mappedProperty(Fourth Key",
bean.getMappedProperty("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"
map.put("nested.writeOnlyProperty", "New writeOnlyProperty value");
BeanUtils.populate(bean, map);
assertTrue("booleanProperty is false",
!bean.getNested().getBooleanProperty());
assertTrue("booleanSecond is true",
bean.getNested().isBooleanSecond());
assertEquals("doubleProperty is 432.0",
432.0,
bean.getNested().getDoubleProperty(),
0.005);
assertEquals("floatProperty is 123.0",
(float) 123.0,
bean.getNested().getFloatProperty(),
(float) 0.005);
assertEquals("intProperty is 543",
543, bean.getNested().getIntProperty());
assertEquals("longProperty is 321",
321, bean.getNested().getLongProperty());
assertEquals("shortProperty is 654",
(short) 654, bean.getNested().getShortProperty());
assertEquals("stringProperty is \"This is a string\"",
"This is a string",
bean.getNested().getStringProperty());
assertEquals("writeOnlyProperty is \"New writeOnlyProperty value\"",
"New writeOnlyProperty value",
bean.getNested().getWriteOnlyPropertyValue());
} catch (IllegalAccessException e) {
fail("IllegalAccessException");
} catch (InvocationTargetException e) {
fail("InvocationTargetException");
}
}
/**
* Test populate() method on scalar properties.
*/
public void testPopulateScalar() {
try {
bean.setNullProperty("Non-null value");
HashMap map = new HashMap();
map.put("booleanProperty", "false");
// booleanSecond is left at true
map.put("byteProperty", "111");
map.put("doubleProperty", "432.0");
// floatProperty is left at 123.0
map.put("intProperty", "543");
map.put("longProperty", "");
map.put("nullProperty", null);
map.put("shortProperty", "654");
// stringProperty is left at "This is a string"
map.put("writeOnlyProperty", "New writeOnlyProperty value");
map.put("readOnlyProperty", "New readOnlyProperty value");
BeanUtils.populate(bean, map);
assertTrue("booleanProperty is false", !bean.getBooleanProperty());
assertTrue("booleanSecond is true", bean.isBooleanSecond());
assertEquals("byteProperty is 111",
(byte) 111, bean.getByteProperty());
assertEquals("doubleProperty is 432.0",
432.0, bean.getDoubleProperty(),
0.005);
assertEquals("floatProperty is 123.0",
(float) 123.0, bean.getFloatProperty(),
(float) 0.005);
assertEquals("intProperty is 543",
543, bean.getIntProperty());
assertEquals("longProperty is 0",
0, bean.getLongProperty());
assertNull("nullProperty is null",
bean.getNullProperty());
assertEquals("shortProperty is 654",
(short) 654, bean.getShortProperty());
assertEquals("stringProperty is \"This is a string\"",
"This is a string", bean.getStringProperty());
assertEquals("writeOnlyProperty is \"New writeOnlyProperty value\"",
"New writeOnlyProperty value",
bean.getWriteOnlyPropertyValue());
assertEquals("readOnlyProperty is \"Read Only String Property\"",
"Read Only String Property",
bean.getReadOnlyProperty());
} 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,bean.getIntProperty());
BeanUtils.setProperty(bean,"stringProperty", new Integer(1));
assertEquals(1, Integer.parseInt(bean.getStringProperty()));
}
/**
* Test narrowing and widening conversions on byte.
*/
public void testSetPropertyByte() throws Exception {
BeanUtils.setProperty(bean, "byteProperty", new Byte((byte) 123));
assertEquals((byte) 123, bean.getByteProperty());
/*
BeanUtils.setProperty(bean, "byteProperty", new Double((double) 123));
assertEquals((byte) 123, bean.getByteProperty());
BeanUtils.setProperty(bean, "byteProperty", new Float((float) 123));
assertEquals((byte) 123, bean.getByteProperty());
*/
BeanUtils.setProperty(bean, "byteProperty", new Integer(123));
assertEquals((byte) 123, bean.getByteProperty());
BeanUtils.setProperty(bean, "byteProperty", new Long(123));
assertEquals((byte) 123, bean.getByteProperty());
BeanUtils.setProperty(bean, "byteProperty", new Short((short) 123));
assertEquals((byte) 123, bean.getByteProperty());
}
/**
* Test <code>setProperty()</code> conversion.
*/
public void testSetPropertyConvert() {
try {
BeanUtils.setProperty(bean, "dateProperty", testCalendar);
} catch (Throwable t) {
fail("Threw " + t);
}
assertEquals("Calendar --> java.util.Date", testUtilDate, bean.getDateProperty());
}
/**
* Test <code>setProperty()</code> converting from a String.
*/
public void testSetPropertyConvertFromString() {
try {
BeanUtils.setProperty(bean, "dateProperty", testStringDate);
} catch (Throwable t) {
fail("Threw " + t);
}
assertEquals("String --> java.util.Date", testUtilDate, bean.getDateProperty());
}
/**
* Test <code>setProperty()</code> converting to a String.
*/
public void testSetPropertyConvertToString() {
try {
BeanUtils.setProperty(bean, "stringProperty", testUtilDate);
} catch (Throwable t) {
fail("Threw " + t);
}
assertEquals("java.util.Date --> String", testUtilDate.toString(), bean.getStringProperty());
}
/**
* Test <code>setProperty()</code> converting to a String array.
*/
public void testSetPropertyConvertToStringArray() {
try {
bean.setStringArray(null);
BeanUtils.setProperty(bean, "stringArray", new java.util.Date[] {testUtilDate});
} catch (Throwable t) {
fail("Threw " + t);
}
assertEquals("java.util.Date[] --> String[] length", 1, bean.getStringArray().length);
assertEquals("java.util.Date[] --> String[] value ", testUtilDate.toString(), bean.getStringArray()[0]);
}
/**
* Test <code>setProperty()</code> converting to a String on indexed property
*/
public void testSetPropertyConvertToStringIndexed() {
try {
bean.setStringArray(new String[1]);
BeanUtils.setProperty(bean, "stringArray[0]", testUtilDate);
} catch (Throwable t) {
fail("Threw " + t);
}
assertEquals("java.util.Date --> String[]", testUtilDate.toString(), bean.getStringArray()[0]);
}
/**
* Test narrowing and widening conversions on double.
*/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -