📄 beanutilstestcase.java
字号:
*/ public void testCopyPropertyFloat() throws Exception { BeanUtils.copyProperty(bean, "floatProperty", new Byte((byte) 123)); assertEquals((float) 123, bean.getFloatProperty(), 0.005); BeanUtils.copyProperty(bean, "floatProperty", new Double((double) 123)); assertEquals((float) 123, bean.getFloatProperty(), 0.005); BeanUtils.copyProperty(bean, "floatProperty", new Float((float) 123)); assertEquals((float) 123, bean.getFloatProperty(), 0.005); BeanUtils.copyProperty(bean, "floatProperty", new Integer((int) 123)); assertEquals((float) 123, bean.getFloatProperty(), 0.005); BeanUtils.copyProperty(bean, "floatProperty", new Long((long) 123)); assertEquals((float) 123, bean.getFloatProperty(), 0.005); BeanUtils.copyProperty(bean, "floatProperty", new Short((short) 123)); assertEquals((float) 123, bean.getFloatProperty(), 0.005); } /** * Test narrowing and widening conversions on int. */ public void testCopyPropertyInteger() throws Exception { BeanUtils.copyProperty(bean, "longProperty", new Byte((byte) 123)); assertEquals((int) 123, bean.getIntProperty()); BeanUtils.copyProperty(bean, "longProperty", new Double((double) 123)); assertEquals((int) 123, bean.getIntProperty()); BeanUtils.copyProperty(bean, "longProperty", new Float((float) 123)); assertEquals((int) 123, bean.getIntProperty()); BeanUtils.copyProperty(bean, "longProperty", new Integer((int) 123)); assertEquals((int) 123, bean.getIntProperty()); BeanUtils.copyProperty(bean, "longProperty", new Long((long) 123)); assertEquals((int) 123, bean.getIntProperty()); BeanUtils.copyProperty(bean, "longProperty", new Short((short) 123)); assertEquals((int) 123, bean.getIntProperty()); } /** * Test narrowing and widening conversions on long. */ public void testCopyPropertyLong() throws Exception { BeanUtils.copyProperty(bean, "longProperty", new Byte((byte) 123)); assertEquals((long) 123, bean.getLongProperty()); BeanUtils.copyProperty(bean, "longProperty", new Double((double) 123)); assertEquals((long) 123, bean.getLongProperty()); BeanUtils.copyProperty(bean, "longProperty", new Float((float) 123)); assertEquals((long) 123, bean.getLongProperty()); BeanUtils.copyProperty(bean, "longProperty", new Integer((int) 123)); assertEquals((long) 123, bean.getLongProperty()); BeanUtils.copyProperty(bean, "longProperty", new Long((long) 123)); assertEquals((long) 123, bean.getLongProperty()); BeanUtils.copyProperty(bean, "longProperty", new Short((short) 123)); assertEquals((long) 123, bean.getLongProperty()); } /** * Test narrowing and widening conversions on short. */ public void testCopyPropertyShort() throws Exception { BeanUtils.copyProperty(bean, "shortProperty", new Byte((byte) 123)); assertEquals((short) 123, bean.getShortProperty()); BeanUtils.copyProperty(bean, "shortProperty", new Double((double) 123)); assertEquals((short) 123, bean.getShortProperty()); BeanUtils.copyProperty(bean, "shortProperty", new Float((float) 123)); assertEquals((short) 123, bean.getShortProperty()); BeanUtils.copyProperty(bean, "shortProperty", new Integer((int) 123)); assertEquals((short) 123, bean.getShortProperty()); BeanUtils.copyProperty(bean, "shortProperty", new Long((long) 123)); assertEquals((short) 123, bean.getShortProperty()); BeanUtils.copyProperty(bean, "shortProperty", new Short((short) 123)); assertEquals((short) 123, bean.getShortProperty()); } /** * Test copying a property using a nested indexed array expression, * with and without conversions. */ public void testCopyPropertyNestedIndexedArray() throws Exception { int origArray[] = { 0, 10, 20, 30, 40 }; int intArray[] = { 0, 0, 0 }; bean.getNested().setIntArray(intArray); int intChanged[] = { 0, 0, 0 }; // No conversion required BeanUtils.copyProperty(bean, "nested.intArray[1]", new Integer(1)); checkIntArray(bean.getIntArray(), origArray); intChanged[1] = 1; checkIntArray(bean.getNested().getIntArray(), intChanged); // Widening conversion required BeanUtils.copyProperty(bean, "nested.intArray[1]", new Byte((byte) 2)); checkIntArray(bean.getIntArray(), origArray); intChanged[1] = 2; checkIntArray(bean.getNested().getIntArray(), intChanged); // Narrowing conversion required BeanUtils.copyProperty(bean, "nested.intArray[1]", new Long((long) 3)); checkIntArray(bean.getIntArray(), origArray); intChanged[1] = 3; checkIntArray(bean.getNested().getIntArray(), intChanged); // String conversion required BeanUtils.copyProperty(bean, "nested.intArray[1]", "4"); checkIntArray(bean.getIntArray(), origArray); intChanged[1] = 4; checkIntArray(bean.getNested().getIntArray(), intChanged); } /** * Test copying a property using a nested mapped map property. */ public void testCopyPropertyNestedMappedMap() throws Exception { Map origMap = new HashMap(); origMap.put("First Key", "First Value"); origMap.put("Second Key", "Second Value"); Map changedMap = new HashMap(); changedMap.put("First Key", "First Value"); changedMap.put("Second Key", "Second Value"); // No conversion required BeanUtils.copyProperty(bean, "nested.mapProperty(Second Key)", "New Second Value"); checkMap(bean.getMapProperty(), origMap); changedMap.put("Second Key", "New Second Value"); checkMap(bean.getNested().getMapProperty(), changedMap); } /** * Test copying a property using a nested simple expression, with and * without conversions. */ public void testCopyPropertyNestedSimple() throws Exception { bean.setIntProperty(0); bean.getNested().setIntProperty(0); // No conversion required BeanUtils.copyProperty(bean, "nested.intProperty", new Integer(1)); assertNotNull(bean.getNested()); assertEquals(0, bean.getIntProperty()); assertEquals(1, bean.getNested().getIntProperty()); // Widening conversion required BeanUtils.copyProperty(bean, "nested.intProperty", new Byte((byte) 2)); assertNotNull(bean.getNested()); assertEquals(0, bean.getIntProperty()); assertEquals(2, bean.getNested().getIntProperty()); // Narrowing conversion required BeanUtils.copyProperty(bean, "nested.intProperty", new Long((long) 3)); assertNotNull(bean.getNested()); assertEquals(0, bean.getIntProperty()); assertEquals(3, bean.getNested().getIntProperty()); // String conversion required BeanUtils.copyProperty(bean, "nested.intProperty", "4"); assertNotNull(bean.getNested()); assertEquals(0, bean.getIntProperty()); assertEquals(4, bean.getNested().getIntProperty()); } /** * Test copying a null property value. */ public void testCopyPropertyNull() throws Exception { bean.setNullProperty("non-null value"); BeanUtils.copyProperty(bean, "nullProperty", null); assertNull("nullProperty is null", bean.getNullProperty()); } /** * Test copying a new value to a write-only property, with and without * conversions. */ public void testCopyPropertyWriteOnly() throws Exception { bean.setWriteOnlyProperty("Original value"); // No conversion required BeanUtils.copyProperty(bean, "writeOnlyProperty", "New value"); assertEquals("New value", bean.getWriteOnlyPropertyValue()); // Integer->String conversion required BeanUtils.copyProperty(bean, "writeOnlyProperty", new Integer(123)); assertEquals("123", bean.getWriteOnlyPropertyValue()); } /** * Test setting a new value to a write-only property, with and without * conversions. */ public void testSetPropertyWriteOnly() throws Exception { bean.setWriteOnlyProperty("Original value"); // No conversion required BeanUtils.setProperty(bean, "writeOnlyProperty", "New value"); assertEquals("New value", bean.getWriteOnlyPropertyValue()); // Integer->String conversion required BeanUtils.setProperty(bean, "writeOnlyProperty", new Integer(123)); assertEquals("123", bean.getWriteOnlyPropertyValue()); } /** Tests that separate instances can register separate instances */ public void testSeparateInstances() throws Exception { BeanUtilsBean utilsOne = new BeanUtilsBean( new ConvertUtilsBean(), new PropertyUtilsBean()); BeanUtilsBean utilsTwo = new BeanUtilsBean( new ConvertUtilsBean(), new PropertyUtilsBean()); TestBean bean = new TestBean(); // Make sure what we're testing works bean.setBooleanProperty(false); utilsOne.setProperty(bean, "booleanProperty", "true"); assertEquals("Set property failed (1)", bean.getBooleanProperty(), true); bean.setBooleanProperty(false); utilsTwo.setProperty(bean, "booleanProperty", "true"); assertEquals("Set property failed (2)", bean.getBooleanProperty(), true); // now change the registered conversion utilsOne.getConvertUtils().register(new ThrowExceptionConverter(), Boolean.TYPE); try { bean.setBooleanProperty(false); utilsOne.setProperty(bean, "booleanProperty", "true"); fail("Registered conversion not used."); } catch (PassTestException e) { /* Do nothing */ } // make sure that this conversion has no been registered in the other instance try { bean.setBooleanProperty(false); utilsTwo.setProperty(bean, "booleanProperty", "true"); assertEquals("Set property failed (3)", bean.getBooleanProperty(), true); } catch (PassTestException e) { fail("Registed converter is used by other instances"); } } public void testArrayPropertyConversion() throws Exception { BeanUtilsBean beanUtils = new BeanUtilsBean( new ConvertUtilsBean(), new PropertyUtilsBean()); beanUtils.getConvertUtils().register( new Converter () { public Object convert(Class type, Object value) { return "Spam, spam, spam, spam!"; } }, String.class); TestBean bean = new TestBean(); String [] results = beanUtils.getArrayProperty(bean, "intArray"); int[] values = bean.getIntArray(); assertEquals( "Converted array size not equal to property array size.", results.length, values.length); for (int i=0, size=values.length ; i<size; i++) { assertEquals( "Value " + i + " incorrectly converted ", "Spam, spam, spam, spam!", results[i]); } } // Ensure that the actual int[] matches the expected int[] protected void checkIntArray(int actual[], int expected[]) { assertNotNull("actual array not null", actual); assertEquals("actual array length", expected.length, actual.length); for (int i = 0; i < actual.length; i++) { assertEquals("actual array value[" + i + "]", expected[i], actual[i]); } } // Ensure that the actual Map matches the expected Map protected void checkMap(Map actual, Map expected) { assertNotNull("actual map not null", actual); assertEquals("actual map size", expected.size(), actual.size()); Iterator keys = expected.keySet().iterator(); while (keys.hasNext()) { Object key = keys.next(); assertEquals("actual map value(" + key + ")", expected.get(key), actual.get(key)); } } public void testMappedProperty() throws Exception { MappedPropertyTestBean bean = new MappedPropertyTestBean(); BeanUtils.setProperty(bean, "mapproperty(this.that.the-other)", "some.dotty.value"); assertEquals( "Mapped property set correctly", "some.dotty.value", bean.getMapproperty("this.that.the-other")); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -