📄 propertyutilstestcase.java
字号:
public void testGetDescriptorDouble() { testGetDescriptorBase("doubleProperty", "getDoubleProperty", "setDoubleProperty"); } /** * Positive getPropertyDescriptor on property <code>floatProperty</code>. */ public void testGetDescriptorFloat() { testGetDescriptorBase("floatProperty", "getFloatProperty", "setFloatProperty"); } /** * Positive getPropertyDescriptor on property <code>intProperty</code>. */ public void testGetDescriptorInt() { testGetDescriptorBase("intProperty", "getIntProperty", "setIntProperty"); } /** * <p>Negative tests on an invalid property with two different boolean * getters (which is fine, according to the JavaBeans spec) but a * String setter instead of a boolean setter.</p> * * <p>Although one could logically argue that this combination of method * signatures should not identify a property at all, there is a sentence * in Section 8.3.1 making it clear that the behavior tested for here * is correct: "If we find only one of these methods, then we regard * it as defining either a read-only or write-only property called * <em><property-name></em>.</p> */ public void testGetDescriptorInvalidBoolean() throws Exception { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(bean, "invalidBoolean"); assertNotNull("invalidBoolean is a property", pd); assertNotNull("invalidBoolean has a getter method", pd.getReadMethod()); assertNull("invalidBoolean has no write method", pd.getWriteMethod()); assertTrue("invalidBoolean getter method is isInvalidBoolean", "isInvalidBoolean".equals(pd.getReadMethod().getName())); } /** * Positive getPropertyDescriptor on property <code>longProperty</code>. */ public void testGetDescriptorLong() { testGetDescriptorBase("longProperty", "getLongProperty", "setLongProperty"); } /** * Positive getPropertyDescriptor on property * <code>readOnlyProperty</code>. */ public void testGetDescriptorReadOnly() { testGetDescriptorBase("readOnlyProperty", "getReadOnlyProperty", null); } /** * Positive getPropertyDescriptor on property <code>booleanSecond</code> * that uses an "is" method as the getter. */ public void testGetDescriptorSecond() { testGetDescriptorBase("booleanSecond", "isBooleanSecond", "setBooleanSecond"); } /** * Positive getPropertyDescriptor on property <code>shortProperty</code>. */ public void testGetDescriptorShort() { testGetDescriptorBase("shortProperty", "getShortProperty", "setShortProperty"); } /** * Positive getPropertyDescriptor on property <code>stringProperty</code>. */ public void testGetDescriptorString() { testGetDescriptorBase("stringProperty", "getStringProperty", "setStringProperty"); } /** * Negative getPropertyDescriptor on property <code>unknown</code>. */ public void testGetDescriptorUnknown() { testGetDescriptorBase("unknown", null, null); } /** * Positive getPropertyDescriptor on property * <code>writeOnlyProperty</code>. */ public void testGetDescriptorWriteOnly() { testGetDescriptorBase("writeOnlyProperty", null, "setWriteOnlyProperty"); } /** * Positive test for getPropertyDescriptors(). Each property name * listed in <code>properties</code> should be returned exactly once. */ public void testGetDescriptors() { PropertyDescriptor pd[] = PropertyUtils.getPropertyDescriptors(bean); assertNotNull("Got descriptors", pd); int count[] = new int[properties.length]; for (int i = 0; i < pd.length; i++) { String name = pd[i].getName(); for (int j = 0; j < properties.length; j++) { if (name.equals(properties[j])) count[j]++; } } for (int j = 0; j < properties.length; j++) { if (count[j] < 0) fail("Missing property " + properties[j]); else if (count[j] > 1) fail("Duplicate property " + properties[j]); } } /** * Corner cases on getPropertyDescriptors invalid arguments. */ public void testGetDescriptorsArguments() { try { PropertyUtils.getPropertyDescriptors(null); fail("Should throw IllegalArgumentException"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException"); } } /** * Corner cases on getIndexedProperty invalid arguments. */ public void testGetIndexedArguments() { // Use explicit index argument try { PropertyUtils.getIndexedProperty(null, "intArray", 0); fail("Should throw IllegalArgumentException 1"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 1"); } try { PropertyUtils.getIndexedProperty(bean, null, 0); 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.getIndexedProperty(null, "intArray[0]"); fail("Should throw IllegalArgumentException 3"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 3"); } try { PropertyUtils.getIndexedProperty(bean, "[0]"); fail("Should throw NoSuchMethodException 4"); } catch (NoSuchMethodException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of NoSuchMethodException 4"); } try { PropertyUtils.getIndexedProperty(bean, "intArray"); fail("Should throw IllegalArgumentException 5"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 5"); } // Use explicit index argument try { PropertyUtils.getIndexedProperty(null, "intIndexed", 0); fail("Should throw IllegalArgumentException 1"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 1"); } try { PropertyUtils.getIndexedProperty(bean, null, 0); 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.getIndexedProperty(null, "intIndexed[0]"); fail("Should throw IllegalArgumentException 3"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 3"); } try { PropertyUtils.getIndexedProperty(bean, "[0]"); fail("Should throw NoSuchMethodException 4"); } catch (NoSuchMethodException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of NoSuchMethodException 4"); } try { PropertyUtils.getIndexedProperty(bean, "intIndexed"); fail("Should throw IllegalArgumentException 5"); } catch (IllegalArgumentException e) { ; // Expected response } catch (Throwable t) { fail("Threw " + t + " instead of IllegalArgumentException 5"); } } /** * Positive and negative tests on getIndexedProperty valid arguments. */ public void testGetIndexedValues() { Object value = null; // Use explicit key argument for (int i = 0; i < 5; i++) { try { value = PropertyUtils.getIndexedProperty (bean, "dupProperty", i); assertNotNull("dupProperty returned value " + i, value); assertTrue("dupProperty returned String " + i, value instanceof String); assertEquals("dupProperty returned correct " + i, "Dup " + i, (String) value); } catch (Throwable t) { fail("dupProperty " + i + " threw " + t); } try { value = PropertyUtils.getIndexedProperty(bean, "intArray", i); assertNotNull("intArray returned value " + i, value); assertTrue("intArray returned Integer " + i, value instanceof Integer); assertEquals("intArray returned correct " + i, i * 10, ((Integer) value).intValue()); } catch (Throwable t) { fail("intArray " + i + " threw " + t); } try { value = PropertyUtils.getIndexedProperty(bean, "intIndexed", i); assertNotNull("intIndexed returned value " + i, value); assertTrue("intIndexed returned Integer " + i, value instanceof Integer); assertEquals("intIndexed returned correct " + i, i * 10, ((Integer) value).intValue()); } catch (Throwable t) { fail("intIndexed " + i + " threw " + t); } try { value = PropertyUtils.getIndexedProperty(bean, "listIndexed", i); assertNotNull("listIndexed returned value " + i, value); assertTrue("list returned String " + i, value instanceof String); assertEquals("listIndexed returned correct " + i, "String " + i, (String) value); } catch (Throwable t) { fail("listIndexed " + i + " threw " + t); } try { value = PropertyUtils.getIndexedProperty(bean, "stringArray", i); assertNotNull("stringArray returned value " + i, value); assertTrue("stringArray returned String " + i, value instanceof String); assertEquals("stringArray returned correct " + i, "String " + i, (String) value); } catch (Throwable t) { fail("stringArray " + i + " threw " + t); } try { value = PropertyUtils.getIndexedProperty(bean, "stringIndexed", i); assertNotNull("stringIndexed returned value " + i, value); assertTrue("stringIndexed returned String " + i, value instanceof String); assertEquals("stringIndexed returned correct " + i, "String " + i, (String) value); } catch (Throwable t) { fail("stringIndexed " + i + " threw " + t); } } // Use key expression for (int i = 0; i < 5; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -