📄 propertyutilstestcase.java
字号:
secondMap.put("SECOND-KEY-1", "SECOND-VALUE-1");
secondMap.put("SECOND-KEY-2", "SECOND-VALUE-2");
List mainList = new ArrayList();
mainList.add(firstMap);
mainList.add(secondMap);
TestBean bean = new TestBean(mainList);
try {
assertEquals("listIndexed[0](FIRST-KEY-1)", "FIRST-VALUE-1", PropertyUtils.getProperty(bean, "listIndexed[0](FIRST-KEY-1)"));
assertEquals("listIndexed[0](FIRST-KEY-2)", "FIRST-VALUE-2", PropertyUtils.getProperty(bean, "listIndexed[0](FIRST-KEY-2)"));
assertEquals("listIndexed[1](SECOND-KEY-1)", "SECOND-VALUE-1", PropertyUtils.getProperty(bean, "listIndexed[1](SECOND-KEY-1)"));
assertEquals("listIndexed[1](SECOND-KEY-2)", "SECOND-VALUE-2", PropertyUtils.getProperty(bean, "listIndexed[1](SECOND-KEY-2)"));
} catch (Throwable t) {
fail("Threw " + t + "");
}
}
/**
* Corner cases on getMappedProperty invalid arguments.
*/
public void testGetMappedArguments() {
// Use explicit key argument
try {
PropertyUtils.getMappedProperty(null, "mappedProperty",
"First Key");
fail("Should throw IllegalArgumentException 1");
} catch (IllegalArgumentException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of IllegalArgumentException 1");
}
try {
PropertyUtils.getMappedProperty(bean, null, "First Key");
fail("Should throw IllegalArgumentException 2");
} catch (IllegalArgumentException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of IllegalArgumentException 2");
}
try {
PropertyUtils.getMappedProperty(bean, "mappedProperty", null);
fail("Should throw IllegalArgumentException 3");
} catch (IllegalArgumentException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of IllegalArgumentException 3");
}
// Use key expression
try {
PropertyUtils.getMappedProperty(null,
"mappedProperty(First Key)");
fail("Should throw IllegalArgumentException 4");
} catch (IllegalArgumentException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of IllegalArgumentException 4");
}
try {
PropertyUtils.getMappedProperty(bean, "(Second Key)");
fail("Should throw IllegalArgumentException 5");
} catch (NoSuchMethodException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of NoSuchMethodException 5");
}
try {
PropertyUtils.getMappedProperty(bean, "mappedProperty");
fail("Should throw IllegalArgumentException 6");
} catch (IllegalArgumentException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of IllegalArgumentException 6");
}
}
/**
* Test getting an indexed value out of a mapped array
*/
public void testGetMappedArray() {
TestBean bean = new TestBean();
String[] array = new String[] {"abc", "def", "ghi"};
bean.getMapProperty().put("mappedArray", array);
try {
assertEquals("abc", PropertyUtils.getProperty(bean, "mapProperty(mappedArray)[0]"));
assertEquals("def", PropertyUtils.getProperty(bean, "mapProperty(mappedArray)[1]"));
assertEquals("ghi", PropertyUtils.getProperty(bean, "mapProperty(mappedArray)[2]"));
} catch (Throwable t) {
fail("Threw " + t + "");
}
}
/**
* Test getting an indexed value out of a mapped List
*/
public void testGetMappedList() {
TestBean bean = new TestBean();
List list = new ArrayList();
list.add("klm");
list.add("nop");
list.add("qrs");
bean.getMapProperty().put("mappedList", list);
try {
assertEquals("klm", PropertyUtils.getProperty(bean, "mapProperty(mappedList)[0]"));
assertEquals("nop", PropertyUtils.getProperty(bean, "mapProperty(mappedList)[1]"));
assertEquals("qrs", PropertyUtils.getProperty(bean, "mapProperty(mappedList)[2]"));
} catch (Throwable t) {
fail("Threw " + t + "");
}
}
/**
* Test getting a value out of a mapped Map
*/
public void testGetMappedMap() {
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);
try {
assertEquals("sub-value-1", PropertyUtils.getProperty(bean, "mapProperty(mappedMap)(sub-key-1)"));
assertEquals("sub-value-2", PropertyUtils.getProperty(bean, "mapProperty(mappedMap)(sub-key-2)"));
assertEquals("sub-value-3", PropertyUtils.getProperty(bean, "mapProperty(mappedMap)(sub-key-3)"));
} catch (Throwable t) {
fail("Threw " + t + "");
}
}
/**
* Test getting mapped values with periods in the key.
*/
public void testGetMappedPeriods() {
bean.setMappedProperty("key.with.a.dot", "Special Value");
assertEquals("Can retrieve directly",
"Special Value",
bean.getMappedProperty("key.with.a.dot"));
try {
assertEquals("Can retrieve via getMappedProperty",
"Special Value",
PropertyUtils.getMappedProperty
(bean, "mappedProperty", "key.with.a.dot"));
} catch (Exception e) {
fail("Thew exception: " + e);
}
try {
assertEquals("Can retrieve via getNestedProperty",
"Special Value",
PropertyUtils.getNestedProperty
(bean, "mappedProperty(key.with.a.dot)"));
} catch (Exception e) {
fail("Thew exception: " + e);
}
bean.setMappedObjects("nested.property", new TestBean());
assertNotNull("Can retrieve directly",
bean.getMappedObjects("nested.property"));
try {
assertEquals("Can retrieve nested",
"This is a string",
PropertyUtils.getNestedProperty
(bean,
"mappedObjects(nested.property).stringProperty"));
} catch (Exception e) {
fail("Thew exception: " + e);
}
try
{
assertEquals("Can't retrieved nested with mapped property",
"Mapped Value",
PropertyUtils.getNestedProperty(
bean,"mappedNested.value(Mapped Key)"));
} catch (Exception e)
{
fail("Thew exception: " + e);
}
}
/**
* Test getting mapped values with slashes in the key. This is different
* from periods because slashes are not syntactically significant.
*/
public void testGetMappedSlashes() {
bean.setMappedProperty("key/with/a/slash", "Special Value");
assertEquals("Can retrieve directly",
"Special Value",
bean.getMappedProperty("key/with/a/slash"));
try {
assertEquals("Can retrieve via getMappedProperty",
"Special Value",
PropertyUtils.getMappedProperty
(bean, "mappedProperty", "key/with/a/slash"));
} catch (Exception e) {
fail("Thew exception: " + e);
}
try {
assertEquals("Can retrieve via getNestedProperty",
"Special Value",
PropertyUtils.getNestedProperty
(bean, "mappedProperty(key/with/a/slash)"));
} catch (Exception e) {
fail("Thew exception: " + e);
}
bean.setMappedObjects("nested/property", new TestBean());
assertNotNull("Can retrieve directly",
bean.getMappedObjects("nested/property"));
try {
assertEquals("Can retrieve nested",
"This is a string",
PropertyUtils.getNestedProperty
(bean,
"mappedObjects(nested/property).stringProperty"));
} catch (Exception e) {
fail("Thew exception: " + e);
}
}
/**
* Positive and negative tests on getMappedProperty valid arguments.
*/
public void testGetMappedValues() {
Object value = null;
// Use explicit key argument
try {
value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
"First Key");
assertEquals("Can find first value", "First Value", value);
} catch (Throwable t) {
fail("Finding first value threw " + t);
}
try {
value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
"Second Key");
assertEquals("Can find second value", "Second Value", value);
} catch (Throwable t) {
fail("Finding second value threw " + t);
}
try {
value = PropertyUtils.getMappedProperty(bean, "mappedProperty",
"Third Key");
assertNull("Can not find third value", value);
} catch (Throwable t) {
fail("Finding third value threw " + t);
}
// Use key expression with parentheses
try {
value =
PropertyUtils.getMappedProperty(bean,
"mappedProperty(First Key)");
assertEquals("Can find first value", "First Value", value);
} catch (Throwable t) {
fail("Finding first value threw " + t);
}
try {
value =
PropertyUtils.getMappedProperty(bean,
"mappedProperty(Second Key)");
assertEquals("Can find second value", "Second Value", value);
} catch (Throwable t) {
fail("Finding second value threw " + t);
}
try {
value =
PropertyUtils.getMappedProperty(bean,
"mappedProperty(Third Key)");
assertNull("Can not find third value", value);
} catch (Throwable t) {
fail("Finding third value threw " + t);
}
// Use key expression with dotted syntax
try {
value =
PropertyUtils.getNestedProperty(bean,
"mapProperty.First Key");
assertEquals("Can find first value", "First Value", value);
} catch (Throwable t) {
fail("Finding first value threw " + t);
}
try {
value =
PropertyUtils.getNestedProperty(bean,
"mapProperty.Second Key");
assertEquals("Can find second value", "Second Value", value);
} catch (Throwable t) {
fail("Finding second value threw " + t);
}
try {
value =
PropertyUtils.getNestedProperty(bean,
"mapProperty.Third Key");
assertNull("Can not find third value", value);
} catch (Throwable t) {
fail("Finding third value threw " + t);
}
}
/**
* Corner cases on getNestedProperty invalid arguments.
*/
public void testGetNestedArguments() {
try {
PropertyUtils.getNestedProperty(null, "stringProperty");
fail("Should throw IllegalArgumentException 1");
} catch (IllegalArgumentException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of IllegalArgumentException 1");
}
try {
PropertyUtils.getNestedProperty(bean, null);
fail("Should throw IllegalArgumentException 2");
} catch (IllegalArgumentException e) {
// Expected response
} catch (Throwable t) {
fail("Threw " + t + " instead of IllegalArgumentException 2");
}
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -