📄 lazydynabeantestcase.java
字号:
/** * Test setting mapped property for type which is not Map */ public void testMappedInvalidType() { dynaClass.add(testProperty, String.class); assertFalse("Check Property is not mapped", dynaClass.getDynaProperty(testProperty).isMapped()); try { bean.set(testProperty, testKey, testInteger1); fail("set(property, key, value) should have thrown IllegalArgumentException"); } catch (IllegalArgumentException expected) { // expected result } } /** * Test Getting/Setting an 'Indexed' Property - default ArrayList property */ public void testIndexedPropertyDefault() { int index = 3; // Check the property & value doesn't exist assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty)); assertNull("Check Indexed Property is null", bean.get(testProperty)); assertNull("Check Indexed value is null", bean.get(testProperty, index)); // Set the property, should create new ArrayList and set appropriate indexed value bean.set(testProperty, index, testInteger1); assertNotNull("Check Indexed Property is not null", bean.get(testProperty)); assertEquals("Check Indexed Property is correct type", ArrayList.class, bean.get(testProperty).getClass()); assertEquals("Check First Indexed Value is correct", testInteger1, bean.get(testProperty, index)); assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((ArrayList)bean.get(testProperty)).size())); // Set a second indexed value, should automatically grow the ArrayList and set appropriate indexed value index = index + 2; bean.set(testProperty, index, testString1); assertEquals("Check Second Indexed Value is correct", testString1, bean.get(testProperty, index)); assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((ArrayList)bean.get(testProperty)).size())); } /** * Test Getting/Setting a List 'Indexed' Property - use alternative List (LinkedList) */ public void testIndexedLinkedList() { int index = 3; // Check the property & value doesn't exist assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty)); assertNull("Check Indexed Property is null", bean.get(testProperty)); // Add a 'LinkedList' property to the DynaClass dynaClass.add(testProperty, LinkedList.class); assertTrue("Check Property is indexed", dynaClass.getDynaProperty(testProperty).isIndexed()); assertEquals("Check Property is correct type", LinkedList.class, dynaClass.getDynaProperty(testProperty).getType()); assertEquals("Check Property type is correct", LinkedList.class, bean.get(testProperty).getClass()); // Set the property, should instantiate a new LinkedList and set appropriate indexed value bean.set(testProperty, index, testString1); assertEquals("Check Property type is correct", LinkedList.class, bean.get(testProperty).getClass()); assertEquals("Check First Indexed Value is correct", testString1, bean.get(testProperty, index)); assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((LinkedList)bean.get(testProperty)).size())); // Set a second indexed value, should automatically grow the LinkedList and set appropriate indexed value index = index + 2; bean.set(testProperty, index, testInteger1); assertEquals("Check Second Indexed Value is correct", testInteger1, bean.get(testProperty, index)); assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((LinkedList)bean.get(testProperty)).size())); } /** * Test Getting/Setting a primitive array 'Indexed' Property - use int[] */ public void testIndexedPrimitiveArray() { int index = 3; int[] primitiveArray = new int[0]; // Check the property & value doesn't exist assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty)); assertNull("Check Indexed Property is null", bean.get(testProperty)); // Add a DynaProperty of type int[] dynaClass.add(testProperty, primitiveArray.getClass()); assertEquals("Check Indexed Property exists", primitiveArray.getClass(), dynaClass.getDynaProperty(testProperty).getType()); assertEquals("Check Indexed Property is correct type", primitiveArray.getClass(), bean.get(testProperty).getClass()); // Set an indexed value bean.set(testProperty, index, testInteger1); assertNotNull("Check Indexed Property is not null", bean.get(testProperty)); assertEquals("Check Indexed Property is correct type", primitiveArray.getClass(), bean.get(testProperty).getClass()); assertEquals("Check First Indexed Value is correct(a)", testInteger1, bean.get(testProperty, index)); assertEquals("Check First Indexed Value is correct(b)", testInteger1, new Integer(((int[])bean.get(testProperty))[index])); assertEquals("Check Array length is correct", new Integer(index+1), new Integer(((int[])bean.get(testProperty)).length)); // Set a second indexed value, should automatically grow the int[] and set appropriate indexed value index = index + 2; bean.set(testProperty, index, testInteger2); assertEquals("Check Second Indexed Value is correct(a)", testInteger2, bean.get(testProperty, index)); assertEquals("Check Second Indexed Value is correct(b)", testInteger2, new Integer(((int[])bean.get(testProperty))[index])); assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((int[])bean.get(testProperty)).length)); } /** * Test Getting/Setting an Object array 'Indexed' Property - use String[] */ public void testIndexedObjectArray() { int index = 3; Object objectArray = new String[0]; // Check the property & value doesn't exist assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty)); assertNull("Check Indexed Property is null", bean.get(testProperty)); // Add a DynaProperty of type String[] dynaClass.add(testProperty, objectArray.getClass()); assertEquals("Check Indexed Property exists", objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType()); assertEquals("Check Indexed Property is correct type", objectArray.getClass(), bean.get(testProperty).getClass()); // Set an indexed value bean.set(testProperty, index, testString1); assertNotNull("Check Indexed Property is not null", bean.get(testProperty)); assertEquals("Check Indexed Property is correct type", objectArray.getClass(), bean.get(testProperty).getClass()); assertEquals("Check First Indexed Value is correct(a)", testString1, bean.get(testProperty, index)); assertEquals("Check First Indexed Value is correct(b)", testString1, ((String[])bean.get(testProperty))[index]); assertEquals("Check Array length is correct", new Integer(index+1), new Integer(((String[])bean.get(testProperty)).length)); // Set a second indexed value, should automatically grow the String[] and set appropriate indexed value index = index + 2; bean.set(testProperty, index, testString2); assertEquals("Check Second Indexed Value is correct(a)", testString2, bean.get(testProperty, index)); assertEquals("Check Second Indexed Value is correct(b)", testString2, ((String[])bean.get(testProperty))[index]); assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((String[])bean.get(testProperty)).length)); } /** * Test Getting/Setting an DynaBean[] array */ public void testIndexedDynaBeanArray() { int index = 3; Object objectArray = new LazyDynaMap[0]; // Check the property & value doesn't exist assertNull("Check Indexed Property doesn't exist", dynaClass.getDynaProperty(testProperty)); assertNull("Check Indexed Property is null", bean.get(testProperty)); // Add a DynaProperty of type String[] dynaClass.add(testProperty, objectArray.getClass()); assertEquals("Check Indexed Property exists", objectArray.getClass(), dynaClass.getDynaProperty(testProperty).getType()); assertEquals("Check Indexed Property is correct type", objectArray.getClass(), bean.get(testProperty).getClass()); // Retrieving from Array should initialize DynaBean for (int i = index; i >= 0; i--) { assertEquals("Check Array Components initialized", LazyDynaMap.class, bean.get(testProperty, index).getClass()); } dynaClass.add(testPropertyB, objectArray.getClass()); LazyDynaMap newMap = new LazyDynaMap(); newMap.set(testPropertyB, testString2); bean.set(testPropertyA, index, newMap); assertEquals("Check Indexed Value is correct(a)", testString2, ((DynaBean)bean.get(testPropertyA, index)).get(testPropertyB)); } /** * Test Setting an 'Indexed' Property using PropertyUtils */ public void testIndexedPropertyUtils() { int index = 3; dynaClass.setReturnNull(false); // Check the property & value doesn't exist assertFalse("Check Indexed Property doesn't exist", dynaClass.isDynaProperty(testProperty)); assertNull("Check Indexed Property is null", bean.get(testProperty)); assertNull("Check Indexed value is null", bean.get(testProperty, index)); // Use PropertyUtils to set the indexed value try { PropertyUtils.setProperty(bean, testProperty+"["+index+"]", testString1); } catch (NoSuchMethodException ex) { fail("testIndexedPropertyUtils threw "+ex); } catch (InvocationTargetException ex) { fail("testIndexedPropertyUtils threw "+ex); } catch (IllegalAccessException ex) { fail("testIndexedPropertyUtils threw "+ex); } // Check property value correctly set assertEquals("Check Indexed Bean Value is correct", testString1, bean.get(testProperty, index)); } /** * Test Setting an Indexed Property when MutableDynaClass is set to restricted */ public void testIndexedPropertyRestricted() { int index = 3; // Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added dynaClass.setRestricted(true); assertTrue("Check MutableDynaClass is restricted", dynaClass.isRestricted()); // Check the property & value doesn't exist assertNull("Check Property doesn't exist", dynaClass.getDynaProperty(testProperty)); assertNull("Check Value is null", bean.get(testProperty)); // Set the property - should fail because property doesn't exist and MutableDynaClass is restricted try { bean.set(testProperty, index, testInteger1); fail("expected IllegalArgumentException trying to add new property to restricted MutableDynaClass"); } catch (IllegalArgumentException expected) { // expected result } } /** * Test setting indexed property for type which is not List or Array */ public void testIndexedInvalidType() { int index = 3; dynaClass.add(testProperty, String.class); assertFalse("Check Property is not indexed", dynaClass.getDynaProperty(testProperty).isIndexed()); try { bean.set(testProperty, index, testString1); fail("set(property, index, value) should have thrown IllegalArgumentException"); } catch (IllegalArgumentException expected) { // expected result } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -