📄 propertyutilstestcase.java
字号:
/** * 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"); } } /** * Test getNestedProperty on a boolean property. */ public void testGetNestedBoolean() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.booleanProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Boolean)); assertTrue("Got correct value", ((Boolean) value).booleanValue() == bean.getNested().getBooleanProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getNestedProperty on a double property. */ public void testGetNestedDouble() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.doubleProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Double)); assertEquals("Got correct value", ((Double) value).doubleValue(), bean.getNested().getDoubleProperty(), 0.005); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getNestedProperty on a float property. */ public void testGetNestedFloat() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.floatProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Float)); assertEquals("Got correct value", ((Float) value).floatValue(), bean.getNested().getFloatProperty(), (float) 0.005); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getNestedProperty on an int property. */ public void testGetNestedInt() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.intProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Integer)); assertEquals("Got correct value", ((Integer) value).intValue(), bean.getNested().getIntProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getNestedProperty on a long property. */ public void testGetNestedLong() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.longProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Long)); assertEquals("Got correct value", ((Long) value).longValue(), bean.getNested().getLongProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getNestedProperty on a read-only String property. */ public void testGetNestedReadOnly() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.readOnlyProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof String)); assertEquals("Got correct value", (String) value, bean.getReadOnlyProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getNestedProperty on a short property. */ public void testGetNestedShort() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.shortProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof Short)); assertEquals("Got correct value", ((Short) value).shortValue(), bean.getNested().getShortProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (IllegalArgumentException e) { fail("IllegalArgumentException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test getNestedProperty on a String property. */ public void testGetNestedString() { try { Object value = PropertyUtils.getNestedProperty (bean, "nested.stringProperty"); assertNotNull("Got a value", value); assertTrue("Got correct type", (value instanceof String)); assertEquals("Got correct value", ((String) value), bean.getNested().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 } } /** * When a bean has a null property which is reference by the standard access language, * this should throw a NestedNullException. */ public void testThrowNestedNull() throws Exception { NestedTestBean nestedBean = new NestedTestBean("base"); // don't init! try { NestedTestBean value = (NestedTestBean) PropertyUtils.getProperty( nestedBean, "simpleBeanProperty.indexedProperty[0]"); fail("NestedNullException not thrown"); } catch (NestedNullException e) { // that's what we wanted! } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -