📄 beanutilstestcase.java
字号:
assertEquals("stringArray length", 2, stringArray.length); assertEquals("stringArray[0]", "New 0", stringArray[0]); assertEquals("stringArray[1]", "New 1", stringArray[1]); } /** * Test the describe() method. */ public void testDescribe() { Map map = null; try { map = BeanUtils.describe(bean); } catch (Exception e) { fail("Threw exception " + e); } // Verify existence of all the properties that should be present for (int i = 0; i < describes.length; i++) { assertTrue("Property '" + describes[i] + "' is present", map.containsKey(describes[i])); } assertTrue("Property 'writeOnlyProperty' is not present", !map.containsKey("writeOnlyProperty")); // Verify the values of scalar properties assertEquals("Value of 'booleanProperty'", "true", (String) map.get("booleanProperty")); assertEquals("Value of 'byteProperty'", "121", (String) map.get("byteProperty")); assertEquals("Value of 'doubleProperty'", "321.0", (String) map.get("doubleProperty")); assertEquals("Value of 'floatProperty'", "123.0", (String) map.get("floatProperty")); assertEquals("Value of 'intProperty'", "123", (String) map.get("intProperty")); assertEquals("Value of 'longProperty'", "321", (String) map.get("longProperty")); assertEquals("Value of 'shortProperty'", "987", (String) map.get("shortProperty")); assertEquals("Value of 'stringProperty'", "This is a string", (String) map.get("stringProperty")); } /** * tests the string and int arrays of TestBean */ public void testGetArrayProperty() { try { String arr[] = BeanUtils.getArrayProperty(bean, "stringArray"); String comp[] = bean.getStringArray(); assertTrue("String array length = " + comp.length, (comp.length == arr.length)); arr = BeanUtils.getArrayProperty(bean, "intArray"); int iarr[] = bean.getIntArray(); assertTrue("String array length = " + iarr.length, (iarr.length == arr.length)); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * tests getting an indexed property */ public void testGetIndexedProperty1() { try { String val = BeanUtils.getIndexedProperty(bean, "intIndexed[3]"); String comp = String.valueOf(bean.getIntIndexed(3)); assertTrue("intIndexed[3] == " + comp, val.equals(comp)); val = BeanUtils.getIndexedProperty(bean, "stringIndexed[3]"); comp = bean.getStringIndexed(3); assertTrue("stringIndexed[3] == " + comp, val.equals(comp)); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * tests getting an indexed property */ public void testGetIndexedProperty2() { try { String val = BeanUtils.getIndexedProperty(bean, "intIndexed", 3); String comp = String.valueOf(bean.getIntIndexed(3)); assertTrue("intIndexed,3 == " + comp, val.equals(comp)); val = BeanUtils.getIndexedProperty(bean, "stringIndexed", 3); comp = bean.getStringIndexed(3); assertTrue("stringIndexed,3 == " + comp, val.equals(comp)); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * tests getting a nested property */ public void testGetNestedProperty() { try { String val = BeanUtils.getNestedProperty(bean, "nested.stringProperty"); String comp = bean.getNested().getStringProperty(); assertTrue("nested.StringProperty == " + comp, val.equals(comp)); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * tests getting a 'whatever' property */ public void testGetGeneralProperty() { try { String val = BeanUtils.getProperty(bean, "nested.intIndexed[2]"); String comp = String.valueOf(bean.getIntIndexed(2)); assertTrue("nested.intIndexed[2] == " + comp, val.equals(comp)); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * tests getting a 'whatever' property */ public void testGetSimpleProperty() { try { String val = BeanUtils.getSimpleProperty(bean, "shortProperty"); String comp = String.valueOf(bean.getShortProperty()); assertTrue("shortProperty == " + comp, val.equals(comp)); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } catch (NoSuchMethodException e) { fail("NoSuchMethodException"); } } /** * Test populate() method on individual array elements. */ public void testPopulateArrayElements() { try { HashMap map = new HashMap(); map.put("intIndexed[0]", "100"); map.put("intIndexed[2]", "120"); map.put("intIndexed[4]", "140"); BeanUtils.populate(bean, map); assertEquals("intIndexed[0] is 100", 100, bean.getIntIndexed(0)); assertEquals("intIndexed[1] is 10", 10, bean.getIntIndexed(1)); assertEquals("intIndexed[2] is 120", 120, bean.getIntIndexed(2)); assertEquals("intIndexed[3] is 30", 30, bean.getIntIndexed(3)); assertEquals("intIndexed[4] is 140", 140, bean.getIntIndexed(4)); map.clear(); map.put("stringIndexed[1]", "New String 1"); map.put("stringIndexed[3]", "New String 3"); BeanUtils.populate(bean, map); assertEquals("stringIndexed[0] is \"String 0\"", "String 0", bean.getStringIndexed(0)); assertEquals("stringIndexed[1] is \"New String 1\"", "New String 1", bean.getStringIndexed(1)); assertEquals("stringIndexed[2] is \"String 2\"", "String 2", bean.getStringIndexed(2)); assertEquals("stringIndexed[3] is \"New String 3\"", "New String 3", bean.getStringIndexed(3)); assertEquals("stringIndexed[4] is \"String 4\"", "String 4", bean.getStringIndexed(4)); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } } /** * Test populate() method on array properties as a whole. */ public void testPopulateArrayProperties() { try { HashMap map = new HashMap(); int intArray[] = new int[] { 123, 456, 789 }; map.put("intArray", intArray); String stringArray[] = new String[] { "New String 0", "New String 1" }; map.put("stringArray", stringArray); BeanUtils.populate(bean, map); intArray = bean.getIntArray(); assertNotNull("intArray is present", intArray); assertEquals("intArray length", 3, intArray.length); assertEquals("intArray[0]", 123, intArray[0]); assertEquals("intArray[1]", 456, intArray[1]); assertEquals("intArray[2]", 789, intArray[2]); stringArray = bean.getStringArray(); assertNotNull("stringArray is present", stringArray); assertEquals("stringArray length", 2, stringArray.length); assertEquals("stringArray[0]", "New String 0", stringArray[0]); assertEquals("stringArray[1]", "New String 1", stringArray[1]); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } } /** * Test populate() on mapped properties. */ public void testPopulateMapped() { try { HashMap map = new HashMap(); map.put("mappedProperty(First Key)", "New First Value"); map.put("mappedProperty(Third Key)", "New Third Value"); BeanUtils.populate(bean, map); assertEquals("mappedProperty(First Key)", "New First Value", bean.getMappedProperty("First Key")); assertEquals("mappedProperty(Second Key)", "Second Value", bean.getMappedProperty("Second Key")); assertEquals("mappedProperty(Third Key)", "New Third Value", bean.getMappedProperty("Third Key")); assertNull("mappedProperty(Fourth Key", bean.getMappedProperty("Fourth Key")); } catch (IllegalAccessException e) { fail("IllegalAccessException"); } catch (InvocationTargetException e) { fail("InvocationTargetException"); } } /** * Test populate() method on nested properties. */ public void testPopulateNested() { try { HashMap map = new HashMap(); map.put("nested.booleanProperty", "false"); // booleanSecond is left at true map.put("nested.doubleProperty", "432.0"); // floatProperty is left at 123.0 map.put("nested.intProperty", "543"); // longProperty is left at 321 map.put("nested.shortProperty", "654"); // stringProperty is left at "This is a string" map.put("nested.writeOnlyProperty", "New writeOnlyProperty value"); BeanUtils.populate(bean, map); assertTrue("booleanProperty is false", !bean.getNested().getBooleanProperty()); assertTrue("booleanSecond is true", bean.getNested().isBooleanSecond());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -