📄 dynapropertyutilstestcase.java
字号:
try { Object value = PropertyUtils.getNestedProperty (bean, "nested.stringProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof String)); TestBean nested = (TestBean) bean.get("nested"); assertEquals("Got correct value", ((String) value), nested.getStringProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Negative test getNestedProperty on an unknown property. */ public void testGetNestedUnknown() { try { PropertyUtils.getNestedProperty(bean, "nested.unknown"); fail("Should have thrown NoSuchMethodException"); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { ; // Correct result for this test } } /** * Corner cases on getSimpleProperty invalid arguments. */ public void testGetSimpleArguments() { try { PropertyUtils.getSimpleProperty(null, "stringProperty"); fail("Should throw IllegalArgumentException 1"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 1"); } try { PropertyUtils.getSimpleProperty(bean, null); fail("Should throw IllegalArgumentException 2"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 2"); } } /** * Test getSimpleProperty on a boolean property. */ public void testGetSimpleBoolean() { try { Object value = PropertyUtils.getSimpleProperty(bean, "booleanProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Boolean)); assertTrue("Got correct value", ((Boolean) value).booleanValue() == true); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getSimpleProperty on a double property. */ public void testGetSimpleDouble() { try { Object value = PropertyUtils.getSimpleProperty(bean, "doubleProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Double)); assertEquals("Got correct value", ((Double) value).doubleValue(), (double) 321.0, (double) 0.005); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getSimpleProperty on a float property. */ public void testGetSimpleFloat() { try { Object value = PropertyUtils.getSimpleProperty(bean, "floatProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Float)); assertEquals("Got correct value", ((Float) value).floatValue(), (float) 123.0, (float) 0.005); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Negative test getSimpleProperty on an indexed property. */ public void testGetSimpleIndexed() { Object value = null; try { value = PropertyUtils.getSimpleProperty(bean, "intIndexed[0]"); fail("Should have thrown IllegalArgumentException"); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { ; // Correct result for this test } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getSimpleProperty on an int property. */ public void testGetSimpleInt() { try { Object value = PropertyUtils.getSimpleProperty(bean, "intProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Integer)); assertEquals("Got correct value", ((Integer) value).intValue(), 123); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getSimpleProperty on a long property. */ public void testGetSimpleLong() { try { Object value = PropertyUtils.getSimpleProperty(bean, "longProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Long)); assertEquals("Got correct value", ((Long) value).longValue(), (long) 321); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Negative test getSimpleProperty on a nested property. */ public void testGetSimpleNested() { Object value = null; try { value = PropertyUtils.getSimpleProperty(bean, "nested.stringProperty"); fail("Should have thrown IllegaArgumentException"); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { ; // Correct result for this test } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getSimpleProperty on a short property. */ public void testGetSimpleShort() { try { Object value = PropertyUtils.getSimpleProperty(bean, "shortProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Short)); assertEquals("Got correct value", ((Short) value).shortValue(), (short) 987); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getSimpleProperty on a String property. */ public void testGetSimpleString() { try { Object value = PropertyUtils.getSimpleProperty(bean, "stringProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof String)); assertEquals("Got correct value", (String) value, "This is a string"); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Negative test getSimpleProperty on an unknown property. */ public void testGetSimpleUnknown() { try { PropertyUtils.getSimpleProperty(bean, "unknown"); fail("Should have thrown NoSuchMethodException"); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { ; // Correct result for this test } } /** * Corner cases on setIndexedProperty invalid arguments. */ public void testSetIndexedArguments() { // Use explicit index argument try { PropertyUtils.setIndexedProperty(null, "intArray", 0, new Integer(1)); fail("Should throw IllegalArgumentException 1"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 1"); } try { PropertyUtils.setIndexedProperty(bean, null, 0, new Integer(1)); fail("Should throw IllegalArgumentException 2"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 2"); } // Use index expression try { PropertyUtils.setIndexedProperty(null, "intArray[0]", new Integer(1)); fail("Should throw IllegalArgumentException 3"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 3"); } try { PropertyUtils.setIndexedProperty(bean, "[0]", new Integer(1)); fail("Should throw NoSuchMethodException 4"); } catch (NoSuchMethodException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of NoSuchMethodException 4");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -