📄 lazydynamaptestcase.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.beanutils;
import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.lang.reflect.InvocationTargetException;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* <p>Test Case for the <code>LazyDynaMap</code> implementation class.</p>
*
* @author Niall Pemberton
*/
public class LazyDynaMapTestCase extends TestCase {
protected LazyDynaMap dynaMap = null;
protected String testProperty = "myProperty";
protected String testPropertyA = "myProperty-A";
protected String testPropertyB = "myProperty-B";
protected String testString1 = "myStringValue-1";
protected String testString2 = "myStringValue-2";
protected Integer testInteger1 = new Integer(30);
protected Integer testInteger2 = new Integer(40);
protected String testKey = "myKey";
// ---------------------------------------------------------- Constructors
/**
* Construct a new instance of this test case.
*
* @param name Name of the test case
*/
public LazyDynaMapTestCase(String name) {
super(name);
}
// -------------------------------------------------- Overall Test Methods
/**
* Run thus Test
*/
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
/**
* Return the tests included in this test suite.
*/
public static Test suite() {
return (new TestSuite(LazyDynaMapTestCase.class));
}
/**
* Set up instance variables required by this test case.
*/
public void setUp() throws Exception {
dynaMap = new LazyDynaMap();
dynaMap.setReturnNull(true);
}
/**
* Tear down instance variables required by this test case.
*/
public void tearDown() {
dynaMap = null;
}
// ------------------------------------------------ Individual Test Methods
/**
* General Tests
*/
public void testGeneral() {
// LazyDynaMap bean = new LazyDynaMap("TestBean");
assertEquals("Check DynaClass name", "TestBean", new LazyDynaMap("TestBean").getName());
}
/**
* Test Getting/Setting a Simple Property
*/
public void testSimpleProperty() {
// Check the property & value doesn't exist
assertNull("Check Property doesn't exist", dynaMap.getDynaProperty(testProperty));
assertNull("Check Value is null", dynaMap.get(testProperty));
// Set a new property - should add new property and set value
dynaMap.set(testProperty, testInteger1);
assertEquals("Check First Value is correct", testInteger1, dynaMap.get(testProperty));
assertEquals("Check Property type is correct", Integer.class, dynaMap.getDynaProperty(testProperty).getType());
// Set the property again - should set the new value
dynaMap.set(testProperty, testInteger2);
assertEquals("Check Second Value is correct", testInteger2, dynaMap.get(testProperty));
// Set the property again - with a different type, should succeed
dynaMap.set(testProperty, testString1);
assertEquals("Check Third Value is correct", testString1, dynaMap.get(testProperty));
}
/**
* Test Setting a Simple Property when MutableDynaClass is set to restricted
*/
public void testSimplePropertyRestricted() {
// Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added
dynaMap.setRestricted(true);
assertTrue("Check MutableDynaClass is restricted", dynaMap.isRestricted());
// Check the property & value doesn't exist
assertNull("Check Property doesn't exist", dynaMap.getDynaProperty(testProperty));
assertNull("Check Value is null", dynaMap.get(testProperty));
// Set the property - should fail because property doesn't exist and MutableDynaClass is restricted
try {
dynaMap.set(testProperty, testString1);
fail("expected IllegalArgumentException trying to add new property to restricted DynaClass");
} catch (IllegalArgumentException expected) {
// expected result
}
}
/**
* Test Getting/Setting a 'Mapped' Property - default HashMap property
*/
public void testMappedPropertyDefault() {
// Check the property & value doesn't exist
assertNull("Check Mapped Property doesn't exist", dynaMap.getDynaProperty(testProperty));
assertNull("Check Map is null", dynaMap.get(testProperty));
assertNull("Check Mapped Value is null", dynaMap.get(testProperty, testKey));
// Set a new mapped property - should add new HashMap property and set the mapped value
dynaMap.set(testProperty, testKey, testInteger1);
assertEquals("Check Mapped Property exists", HashMap.class, dynaMap.get(testProperty).getClass());
assertEquals("Check First Mapped Value is correct(a)", testInteger1, dynaMap.get(testProperty, testKey));
assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((HashMap)dynaMap.get(testProperty)).get(testKey));
// Set the property again - should set the new value
dynaMap.set(testProperty, testKey, testInteger2);
assertEquals("Check Second Mapped Value is correct(a)", testInteger2, dynaMap.get(testProperty, testKey));
assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((HashMap)dynaMap.get(testProperty)).get(testKey));
}
/**
* Test Getting/Setting a 'Mapped' Property - use TreeMap property
*/
public void testMappedPropertyTreeMap() {
// Check the property & value doesn't exist
assertNull("Check Mapped Property doesn't exist", dynaMap.getDynaProperty(testProperty));
assertNull("Check Map is null", dynaMap.get(testProperty));
// Add a 'TreeMap' property to the DynaClass
dynaMap.add(testProperty, TreeMap.class);
assertTrue("Check Property is mapped", dynaMap.getDynaProperty(testProperty).isMapped());
assertEquals("Check Property is correct type", TreeMap.class, dynaMap.getDynaProperty(testProperty).getType());
assertEquals("Check Mapped Property now exists", TreeMap.class, dynaMap.get(testProperty).getClass());
// Set a new mapped property - should instatiate a new TreeMap property and set the mapped value
dynaMap.set(testProperty, testKey, testInteger1);
assertEquals("Check Mapped Property exists", TreeMap.class, dynaMap.get(testProperty).getClass());
assertEquals("Check First Mapped Value is correct(a)", testInteger1, dynaMap.get(testProperty, testKey));
assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((TreeMap)dynaMap.get(testProperty)).get(testKey));
// Set the property again - should set the new value
dynaMap.set(testProperty, testKey, testInteger2);
assertEquals("Check Second Mapped Value is correct(a)", testInteger2, dynaMap.get(testProperty, testKey));
assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((TreeMap)dynaMap.get(testProperty)).get(testKey));
}
/**
* Test Setting a 'Mapped' Property using PropertyUtils
*/
public void testMappedPropertyUtils() {
dynaMap.setReturnNull(false);
// Check the property & value doesn't exist
assertFalse("Check Mapped Property doesn't exist", dynaMap.isDynaProperty(testProperty));
assertNull("Check Map is null", dynaMap.get(testProperty));
assertNull("Check Mapped Value is null", dynaMap.get(testProperty, testKey));
// Set the mapped property using PropertyUtils
try {
PropertyUtils.setProperty(dynaMap, testProperty+"("+testKey+")", 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 Mapped Bean Value is correct", testString1, dynaMap.get(testProperty, testKey));
}
/**
* Test Setting a Mapped Property when MutableDynaClass is set to restricted
*/
public void testMappedPropertyRestricted() {
// Set the MutableDyanClass to 'restricted' (i.e. no new properties cab be added
dynaMap.setRestricted(true);
assertTrue("Check MutableDynaClass is restricted", dynaMap.isRestricted());
// Check the property & value doesn't exist
assertNull("Check Property doesn't exist", dynaMap.getDynaProperty(testProperty));
assertNull("Check Value is null", dynaMap.get(testProperty));
// Set the property - should fail because property doesn't exist and MutableDynaClass is restricted
try {
dynaMap.set(testProperty, testKey, testInteger1);
fail("expected IllegalArgumentException trying to add new property to restricted MutableDynaClass");
} catch (IllegalArgumentException expected) {
// expected result
}
}
/**
* Test setting mapped property for type which is not Map
*/
public void testMappedInvalidType() {
dynaMap.set(testProperty, new Integer(1));
assertFalse("Check Property is not mapped", dynaMap.getDynaProperty(testProperty).isMapped());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -