?? beanutilstestcase.java
字號:
public void testCopyPropertyNestedIndexedArray() throws Exception {
int origArray[] = { 0, 10, 20, 30, 40 };
int intArray[] = { 0, 0, 0 };
bean.getNested().setIntArray(intArray);
int intChanged[] = { 0, 0, 0 };
// No conversion required
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer(1));
checkIntArray(bean.getIntArray(), origArray);
intChanged[1] = 1;
checkIntArray(bean.getNested().getIntArray(), intChanged);
// Widening conversion required
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte((byte) 2));
checkIntArray(bean.getIntArray(), origArray);
intChanged[1] = 2;
checkIntArray(bean.getNested().getIntArray(), intChanged);
// Narrowing conversion required
BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long(3));
checkIntArray(bean.getIntArray(), origArray);
intChanged[1] = 3;
checkIntArray(bean.getNested().getIntArray(), intChanged);
// String conversion required
BeanUtils.copyProperty(bean, "nested.intArray[1]", "4");
checkIntArray(bean.getIntArray(), origArray);
intChanged[1] = 4;
checkIntArray(bean.getNested().getIntArray(), intChanged);
}
/**
* Test copying a property using a nested mapped map property.
*/
public void testCopyPropertyNestedMappedMap() throws Exception {
Map origMap = new HashMap();
origMap.put("First Key", "First Value");
origMap.put("Second Key", "Second Value");
Map changedMap = new HashMap();
changedMap.put("First Key", "First Value");
changedMap.put("Second Key", "Second Value");
// No conversion required
BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)",
"New Second Value");
checkMap(bean.getMapProperty(), origMap);
changedMap.put("Second Key", "New Second Value");
checkMap(bean.getNested().getMapProperty(), changedMap);
}
/**
* Test copying a property using a nested simple expression, with and
* without conversions.
*/
public void testCopyPropertyNestedSimple() throws Exception {
bean.setIntProperty(0);
bean.getNested().setIntProperty(0);
// No conversion required
BeanUtils.copyProperty(bean, "nested.intProperty", new Integer(1));
assertNotNull(bean.getNested());
assertEquals(0, bean.getIntProperty());
assertEquals(1, bean.getNested().getIntProperty());
// Widening conversion required
BeanUtils.copyProperty(bean, "nested.intProperty", new Byte((byte) 2));
assertNotNull(bean.getNested());
assertEquals(0, bean.getIntProperty());
assertEquals(2, bean.getNested().getIntProperty());
// Narrowing conversion required
BeanUtils.copyProperty(bean, "nested.intProperty", new Long(3));
assertNotNull(bean.getNested());
assertEquals(0, bean.getIntProperty());
assertEquals(3, bean.getNested().getIntProperty());
// String conversion required
BeanUtils.copyProperty(bean, "nested.intProperty", "4");
assertNotNull(bean.getNested());
assertEquals(0, bean.getIntProperty());
assertEquals(4, bean.getNested().getIntProperty());
}
/**
* Test copying a null property value.
*/
public void testCopyPropertyNull() throws Exception {
bean.setNullProperty("non-null value");
BeanUtils.copyProperty(bean, "nullProperty", null);
assertNull("nullProperty is null", bean.getNullProperty());
}
/**
* Test copying a new value to a write-only property, with and without
* conversions.
*/
public void testCopyPropertyWriteOnly() throws Exception {
bean.setWriteOnlyProperty("Original value");
// No conversion required
BeanUtils.copyProperty(bean, "writeOnlyProperty", "New value");
assertEquals("New value", bean.getWriteOnlyPropertyValue());
// Integer->String conversion required
BeanUtils.copyProperty(bean, "writeOnlyProperty", new Integer(123));
assertEquals("123", bean.getWriteOnlyPropertyValue());
}
/**
* Test setting a new value to a write-only property, with and without
* conversions.
*/
public void testSetPropertyWriteOnly() throws Exception {
bean.setWriteOnlyProperty("Original value");
// No conversion required
BeanUtils.setProperty(bean, "writeOnlyProperty", "New value");
assertEquals("New value", bean.getWriteOnlyPropertyValue());
// Integer->String conversion required
BeanUtils.setProperty(bean, "writeOnlyProperty", new Integer(123));
assertEquals("123", bean.getWriteOnlyPropertyValue());
}
/**
* Test setting a value out of a mapped Map
*/
public void testSetMappedMap() {
TestBean bean = new TestBean();
Map map = new HashMap();
map.put("sub-key-1", "sub-value-1");
map.put("sub-key-2", "sub-value-2");
map.put("sub-key-3", "sub-value-3");
bean.getMapProperty().put("mappedMap", map);
assertEquals("BEFORE", "sub-value-3", ((Map)bean.getMapProperty().get("mappedMap")).get("sub-key-3"));
try {
BeanUtils.setProperty(bean, "mapProperty(mappedMap)(sub-key-3)", "SUB-KEY-3-UPDATED");
} catch (Throwable t) {
fail("Threw " + t + "");
}
assertEquals("AFTER", "SUB-KEY-3-UPDATED", ((Map)bean.getMapProperty().get("mappedMap")).get("sub-key-3"));
}
/** Tests that separate instances can register separate instances */
public void testSeparateInstances() throws Exception {
BeanUtilsBean utilsOne = new BeanUtilsBean(
new ConvertUtilsBean(),
new PropertyUtilsBean());
BeanUtilsBean utilsTwo = new BeanUtilsBean(
new ConvertUtilsBean(),
new PropertyUtilsBean());
TestBean bean = new TestBean();
// Make sure what we're testing works
bean.setBooleanProperty(false);
utilsOne.setProperty(bean, "booleanProperty", "true");
assertEquals("Set property failed (1)", bean.getBooleanProperty(), true);
bean.setBooleanProperty(false);
utilsTwo.setProperty(bean, "booleanProperty", "true");
assertEquals("Set property failed (2)", bean.getBooleanProperty(), true);
// now change the registered conversion
utilsOne.getConvertUtils().register(new ThrowExceptionConverter(), Boolean.TYPE);
try {
bean.setBooleanProperty(false);
utilsOne.setProperty(bean, "booleanProperty", "true");
fail("Registered conversion not used.");
} catch (PassTestException e) { /* Do nothing */ }
// make sure that this conversion has no been registered in the other instance
try {
bean.setBooleanProperty(false);
utilsTwo.setProperty(bean, "booleanProperty", "true");
assertEquals("Set property failed (3)", bean.getBooleanProperty(), true);
} catch (PassTestException e) {
fail("Registed converter is used by other instances");
}
}
public void testArrayPropertyConversion() throws Exception {
BeanUtilsBean beanUtils = new BeanUtilsBean(
new ConvertUtilsBean(),
new PropertyUtilsBean());
TestBean bean = new TestBean();
String [] results = beanUtils.getArrayProperty(bean, "intArray");
int[] values = bean.getIntArray();
assertEquals(
"Converted array size not equal to property array size.",
results.length,
values.length);
for (int i=0, size=values.length ; i<size; i++) {
assertEquals(
"Value " + i + " incorrectly converted ",
values[i] + "",
results[i]);
}
}
// Ensure that the actual int[] matches the expected int[]
protected void checkIntArray(int actual[], int expected[]) {
assertNotNull("actual array not null", actual);
assertEquals("actual array length", expected.length, actual.length);
for (int i = 0; i < actual.length; i++) {
assertEquals("actual array value[" + i + "]",
expected[i], actual[i]);
}
}
// Ensure that the actual Map matches the expected Map
protected void checkMap(Map actual, Map expected) {
assertNotNull("actual map not null", actual);
assertEquals("actual map size", expected.size(), actual.size());
Iterator keys = expected.keySet().iterator();
while (keys.hasNext()) {
Object key = keys.next();
assertEquals("actual map value(" + key + ")",
expected.get(key), actual.get(key));
}
}
public void testMappedProperty() throws Exception {
MappedPropertyTestBean bean = new MappedPropertyTestBean();
BeanUtils.setProperty(bean, "mapproperty(this.that.the-other)", "some.dotty.value");
assertEquals(
"Mapped property set correctly",
"some.dotty.value",
bean.getMapproperty("this.that.the-other"));
}
/**
* Test for {@link BeanUtilsBean#initCause(Throwable, Throwable)} method.
*/
public void testInitCause() {
if (isPre14JVM()) {
return;
}
String parentMsg = "PARENT-THROWABLE";
String causeMsg = "THROWABLE-CAUSE";
try {
initCauseAndThrowException(parentMsg, causeMsg);
} catch (Throwable thrownParent) {
assertEquals("Parent", parentMsg, thrownParent.getMessage());
try {
assertEquals("Parent", parentMsg, thrownParent.getMessage());
Throwable thrownCause = getCause(thrownParent);
assertNotNull("Cause Null", thrownCause);
assertEquals("Cause", causeMsg, thrownCause.getMessage());
} catch (Throwable testError) {
fail("If you're running JDK 1.3 then don't worry this should fail," +
" if not then needs checking out: " + testError);
}
}
}
/**
* Use reflection to get the cause
*/
private Throwable getCause(Throwable t) throws Throwable {
return (Throwable)PropertyUtils.getProperty(t, "cause");
}
/**
* Catch a cause, initialize using BeanUtils.initCause() and throw new exception
*/
private void initCauseAndThrowException(String parent, String cause) throws Throwable {
try {
throwException(cause);
} catch (Throwable e) {
Throwable t = new Exception(parent);
BeanUtils.initCause(t, e);
throw t;
}
}
/**
* Throw an exception with the specified message.
*/
private void throwException(String msg) throws Throwable {
throw new Exception(msg);
}
/**
* Test for JDK 1.4
*/
private boolean isPre14JVM() {
String version = System.getProperty("java.specification.version");
StringTokenizer tokenizer = new StringTokenizer(version,".");
if (tokenizer.nextToken().equals("1")) {
String minorVersion = tokenizer.nextToken();
if (minorVersion.equals("0")) return true;
if (minorVersion.equals("1")) return true;
if (minorVersion.equals("2")) return true;
if (minorVersion.equals("3")) return true;
}
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -