📄 propertyutilstestcase.java
字号:
/** * Test getNestedProperty on a write-only String property. */ public void testGetNestedWriteOnly() { try { PropertyUtils.getNestedProperty(bean, "writeOnlyProperty"); 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 } } /** * Test getPropertyType() on all kinds of properties. */ public void testGetPropertyType() { Class clazz = null; int intArray[] = new int[0]; String stringArray[] = new String[0]; try { // Scalar and Indexed Properties clazz = PropertyUtils.getPropertyType(bean, "booleanProperty"); assertEquals("booleanProperty type", Boolean.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "booleanSecond"); assertEquals("booleanSecond type", Boolean.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "doubleProperty"); assertEquals("doubleProperty type", Double.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "dupProperty"); assertEquals("dupProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "floatProperty"); assertEquals("floatProperty type", Float.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "intArray"); assertEquals("intArray type", intArray.getClass(), clazz); clazz = PropertyUtils.getPropertyType(bean, "intIndexed"); assertEquals("intIndexed type", Integer.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "intProperty"); assertEquals("intProperty type", Integer.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "listIndexed"); assertEquals("listIndexed type", List.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "longProperty"); assertEquals("longProperty type", Long.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "mappedProperty"); assertEquals("mappedProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "mappedIntProperty"); assertEquals("mappedIntProperty type", Integer.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "readOnlyProperty"); assertEquals("readOnlyProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "shortProperty"); assertEquals("shortProperty type", Short.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "stringArray"); assertEquals("stringArray type", stringArray.getClass(), clazz); clazz = PropertyUtils.getPropertyType(bean, "stringIndexed"); assertEquals("stringIndexed type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "stringProperty"); assertEquals("stringProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "writeOnlyProperty"); assertEquals("writeOnlyProperty type", String.class, clazz); // Nested Properties clazz = PropertyUtils.getPropertyType(bean, "nested.booleanProperty"); assertEquals("booleanProperty type", Boolean.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.booleanSecond"); assertEquals("booleanSecond type", Boolean.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.doubleProperty"); assertEquals("doubleProperty type", Double.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.dupProperty"); assertEquals("dupProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.floatProperty"); assertEquals("floatProperty type", Float.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.intArray"); assertEquals("intArray type", intArray.getClass(), clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.intIndexed"); assertEquals("intIndexed type", Integer.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.intProperty"); assertEquals("intProperty type", Integer.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.listIndexed"); assertEquals("listIndexed type", List.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.longProperty"); assertEquals("longProperty type", Long.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.mappedProperty"); assertEquals("mappedProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.mappedIntProperty"); assertEquals("mappedIntProperty type", Integer.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.readOnlyProperty"); assertEquals("readOnlyProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.shortProperty"); assertEquals("shortProperty type", Short.TYPE, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.stringArray"); assertEquals("stringArray type", stringArray.getClass(), clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.stringIndexed"); assertEquals("stringIndexed type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.stringProperty"); assertEquals("stringProperty type", String.class, clazz); clazz = PropertyUtils.getPropertyType(bean, "nested.writeOnlyProperty"); assertEquals("writeOnlyProperty type", String.class, clazz); } catch (Exception e) { fail("Exception: " + e.getMessage()); } } /** * Test getting accessible property reader methods for a specified * list of properties of our standard test bean. */ public void testGetReadMethodBasic() { testGetReadMethod(bean, properties, TEST_BEAN_CLASS); } /** * Test getting accessible property reader methods for a specified * list of properties of a package private subclass of our standard * test bean. */ public void testGetReadMethodPackageSubclass() { testGetReadMethod(beanPackageSubclass, properties, TEST_BEAN_CLASS); } /** * Test getting accessible property reader methods for a specified * list of properties that are declared either directly or via * implemented interfaces. */ public void testGetReadMethodPublicInterface() { // Properties "bar" and "baz" are visible via implemented interfaces // (one direct and one indirect) testGetReadMethod(beanPrivate, new String[]{ "bar" }, PRIVATE_DIRECT_CLASS); testGetReadMethod(beanPrivate, new String[]{ "baz" }, PRIVATE_INDIRECT_CLASS); // Properties "bar" and "baz" are visible via implemented interfaces // (one direct and one indirect). The interface is implemented in // a superclass testGetReadMethod(beanPrivateSubclass, new String[]{ "bar" }, PRIVATE_DIRECT_CLASS); testGetReadMethod(beanPrivateSubclass, new String[]{ "baz" }, PRIVATE_INDIRECT_CLASS); // Property "foo" is not accessible because the underlying // class has package scope PropertyDescriptor pd[] = PropertyUtils.getPropertyDescriptors(beanPrivate); int n = -1; for (int i = 0; i < pd.length; i++) { if ("foo".equals(pd[i].getName())) { n = i; break; } } assertTrue("Found foo descriptor", n >= 0); Method reader = pd[n].getReadMethod(); assertNotNull("Found foo read method", reader); Object value = null; try { value = reader.invoke(beanPrivate, new Class[0]); fail("Foo reader did throw IllegalAccessException"); } catch (IllegalAccessException e) { ; // Expected result for this test } catch (Throwable t) { fail("Invoke foo reader: " + t); } } /** * Test getting accessible property reader methods for a specified * list of properties of a public subclass of our standard test bean. */ public void testGetReadMethodPublicSubclass() { testGetReadMethod(beanPublicSubclass, properties, TEST_BEAN_CLASS); } /** * 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() == bean.getBooleanProperty()); } 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(), bean.getDoubleProperty(), (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(), bean.getFloatProperty(), (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(), bean.getIntProperty()); } 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(), bean.getLongProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catc
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -