polishing .beans tests
This commit is contained in:
parent
6966099d85
commit
3d634f1eb7
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,15 +16,16 @@
|
|||
|
||||
package org.springframework.beans;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public abstract class AbstractPropertyValuesTests extends TestCase {
|
||||
public abstract class AbstractPropertyValuesTests {
|
||||
|
||||
/**
|
||||
* Must contain: forname=Tony surname=Blair age=50
|
||||
|
|
@ -37,7 +38,7 @@ public abstract class AbstractPropertyValuesTests extends TestCase {
|
|||
assertTrue("Doesn't contain tory", !pvs.contains("tory"));
|
||||
|
||||
PropertyValue[] ps = pvs.getPropertyValues();
|
||||
Map m = new HashMap();
|
||||
Map<String, String> m = new HashMap<String, String>();
|
||||
m.put("forname", "Tony");
|
||||
m.put("surname", "Blair");
|
||||
m.put("age", "50");
|
||||
|
|
|
|||
|
|
@ -16,26 +16,31 @@
|
|||
|
||||
package org.springframework.beans;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.propertyeditors.CustomDateEditor;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceEditor;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link BeanUtils}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Rob Harrop
|
||||
* @author Chris Beams
|
||||
* @since 19.05.2003
|
||||
*/
|
||||
public class BeanUtilsTests extends TestCase {
|
||||
public final class BeanUtilsTests {
|
||||
|
||||
@Test
|
||||
public void testInstantiateClass() {
|
||||
// give proper class
|
||||
BeanUtils.instantiateClass(ArrayList.class);
|
||||
|
|
@ -59,6 +64,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPropertyDescriptors() throws Exception {
|
||||
PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors();
|
||||
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class);
|
||||
|
|
@ -66,6 +72,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertEquals("Invalid number of descriptors returned", actual.length, descriptors.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanPropertyIsArray() {
|
||||
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class);
|
||||
for (int i = 0; i < descriptors.length; i++) {
|
||||
|
|
@ -77,10 +84,12 @@ public class BeanUtilsTests extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindEditorByConvention() {
|
||||
assertEquals(ResourceEditor.class, BeanUtils.findEditorByConvention(Resource.class).getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyProperties() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
tb.setName("rod");
|
||||
|
|
@ -96,6 +105,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyPropertiesWithDifferentTypes1() throws Exception {
|
||||
DerivedTestBean tb = new DerivedTestBean();
|
||||
tb.setName("rod");
|
||||
|
|
@ -111,6 +121,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyPropertiesWithDifferentTypes2() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
tb.setName("rod");
|
||||
|
|
@ -126,6 +137,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyPropertiesWithEditable() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
assertTrue("Name empty", tb.getName() == null);
|
||||
|
|
@ -143,6 +155,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertTrue("Touchy still empty", tb2.getTouchy() == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyPropertiesWithIgnore() throws Exception {
|
||||
TestBean tb = new TestBean();
|
||||
assertTrue("Name empty", tb.getName() == null);
|
||||
|
|
@ -160,6 +173,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertTrue("Touchy still empty", tb2.getTouchy() == null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCopyPropertiesWithIgnoredNonExistingProperty() {
|
||||
NameAndSpecialProperty source = new NameAndSpecialProperty();
|
||||
source.setName("name");
|
||||
|
|
@ -168,12 +182,14 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertEquals(target.getName(), "name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveSimpleSignature() throws Exception {
|
||||
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomething");
|
||||
assertSignatureEquals(desiredMethod, "doSomething");
|
||||
assertSignatureEquals(desiredMethod, "doSomething()");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveInvalidSignature() throws Exception {
|
||||
try {
|
||||
BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class);
|
||||
|
|
@ -192,17 +208,20 @@ public class BeanUtilsTests extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveWithAndWithoutArgList() throws Exception {
|
||||
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class});
|
||||
assertSignatureEquals(desiredMethod, "doSomethingElse");
|
||||
assertNull(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveTypedSignature() throws Exception {
|
||||
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class});
|
||||
assertSignatureEquals(desiredMethod, "doSomethingElse(java.lang.String, int)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveOverloadedSignature() throws Exception {
|
||||
// test resolve with no args
|
||||
Method desiredMethod = MethodSignatureBean.class.getMethod("overloaded");
|
||||
|
|
@ -217,6 +236,7 @@ public class BeanUtilsTests extends TestCase {
|
|||
assertSignatureEquals(desiredMethod, "overloaded(java.lang.String, org.springframework.beans.factory.BeanFactory)");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveSignatureWithArray() throws Exception {
|
||||
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAnArray", new Class[]{String[].class});
|
||||
assertSignatureEquals(desiredMethod, "doSomethingWithAnArray(java.lang.String[])");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,33 +16,38 @@
|
|||
|
||||
package org.springframework.beans;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class BeanWrapperEnumTests extends TestCase {
|
||||
public final class BeanWrapperEnumTests {
|
||||
|
||||
@Test
|
||||
public void testCustomEnum() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.setPropertyValue("customEnum", "VALUE_1");
|
||||
Assert.assertEquals(CustomEnum.VALUE_1, gb.getCustomEnum());
|
||||
assertEquals(CustomEnum.VALUE_1, gb.getCustomEnum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomEnumWithNull() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.setPropertyValue("customEnum", null);
|
||||
Assert.assertEquals(null, gb.getCustomEnum());
|
||||
assertEquals(null, gb.getCustomEnum());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomEnumWithEmptyString() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.setPropertyValue("customEnum", "");
|
||||
Assert.assertEquals(null, gb.getCustomEnum());
|
||||
assertEquals(null, gb.getCustomEnum());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
package org.springframework.beans;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
|
@ -29,8 +31,8 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.propertyeditors.CustomNumberEditor;
|
||||
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
|
@ -38,14 +40,16 @@ import org.springframework.core.io.UrlResource;
|
|||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
* @since 18.01.2006
|
||||
*/
|
||||
public class BeanWrapperGenericsTests extends TestCase {
|
||||
public final class BeanWrapperGenericsTests {
|
||||
|
||||
@Test
|
||||
public void testGenericSet() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
Set input = new HashSet();
|
||||
Set<String> input = new HashSet<String>();
|
||||
input.add("4");
|
||||
input.add("5");
|
||||
bw.setPropertyValue("integerSet", input);
|
||||
|
|
@ -53,10 +57,11 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertTrue(gb.getIntegerSet().contains(new Integer(5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericSetWithConversionFailure() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
Set input = new HashSet();
|
||||
Set<TestBean> input = new HashSet<TestBean>();
|
||||
input.add(new TestBean());
|
||||
try {
|
||||
bw.setPropertyValue("integerSet", input);
|
||||
|
|
@ -67,10 +72,11 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericList() throws MalformedURLException {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
List input = new ArrayList();
|
||||
List<String> input = new ArrayList<String>();
|
||||
input.add("http://localhost:8080");
|
||||
input.add("http://localhost:9090");
|
||||
bw.setPropertyValue("resourceList", input);
|
||||
|
|
@ -78,18 +84,20 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericListElement() throws MalformedURLException {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
gb.setResourceList(new ArrayList<Resource>());
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.setPropertyValue("resourceList[0]", "http://localhost:8080");
|
||||
assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMap() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
Map input = new HashMap();
|
||||
Map<String, String> input = new HashMap<String, String>();
|
||||
input.put("4", "5");
|
||||
input.put("6", "7");
|
||||
bw.setPropertyValue("shortMap", input);
|
||||
|
|
@ -97,8 +105,9 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Integer(7), gb.getShortMap().get(new Short("6")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapElement() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
gb.setShortMap(new HashMap<Short, Integer>());
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.setPropertyValue("shortMap[4]", "5");
|
||||
|
|
@ -106,10 +115,11 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Integer(5), gb.getShortMap().get(new Short("4")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapWithKeyType() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
Map input = new HashMap();
|
||||
Map<String, String> input = new HashMap<String, String>();
|
||||
input.put("4", "5");
|
||||
input.put("6", "7");
|
||||
bw.setPropertyValue("longMap", input);
|
||||
|
|
@ -117,8 +127,9 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals("7", gb.getLongMap().get(new Long("6")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapElementWithKeyType() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
gb.setLongMap(new HashMap<Long, Integer>());
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.setPropertyValue("longMap[4]", "5");
|
||||
|
|
@ -126,15 +137,17 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals("5", bw.getPropertyValue("longMap[4]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapWithCollectionValue() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
|
||||
Map input = new HashMap();
|
||||
HashSet value1 = new HashSet();
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Collection> input = new HashMap<String, Collection>();
|
||||
HashSet<Integer> value1 = new HashSet<Integer>();
|
||||
value1.add(new Integer(1));
|
||||
input.put("1", value1);
|
||||
ArrayList value2 = new ArrayList();
|
||||
ArrayList<Boolean> value2 = new ArrayList<Boolean>();
|
||||
value2.add(Boolean.TRUE);
|
||||
input.put("2", value2);
|
||||
bw.setPropertyValue("collectionMap", input);
|
||||
|
|
@ -142,17 +155,19 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapElementWithCollectionValue() {
|
||||
GenericBean gb = new GenericBean();
|
||||
GenericBean<?> gb = new GenericBean<Object>();
|
||||
gb.setCollectionMap(new HashMap<Number, Collection<? extends Object>>());
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
|
||||
HashSet value1 = new HashSet();
|
||||
HashSet<Integer> value1 = new HashSet<Integer>();
|
||||
value1.add(new Integer(1));
|
||||
bw.setPropertyValue("collectionMap[1]", value1);
|
||||
assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericListOfLists() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
List<List<Integer>> list = new LinkedList<List<Integer>>();
|
||||
|
|
@ -164,6 +179,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Integer(5), gb.getListOfLists().get(0).get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericListOfListsWithElementConversion() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
List<List<Integer>> list = new LinkedList<List<Integer>>();
|
||||
|
|
@ -175,6 +191,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Integer(5), gb.getListOfLists().get(0).get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericListOfArrays() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
ArrayList<String[]> list = new ArrayList<String[]>();
|
||||
|
|
@ -186,6 +203,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals("str3 ", gb.getListOfArrays().get(0)[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
ArrayList<String[]> list = new ArrayList<String[]>();
|
||||
|
|
@ -198,6 +216,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals("str3", gb.getListOfArrays().get(0)[1]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericListOfMaps() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
List<Map<Integer, Long>> list = new LinkedList<Map<Integer, Long>>();
|
||||
|
|
@ -209,6 +228,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericListOfMapsWithElementConversion() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
List<Map<Integer, Long>> list = new LinkedList<Map<Integer, Long>>();
|
||||
|
|
@ -220,6 +240,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapOfMaps() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
Map<String, Map<Integer, Long>> map = new HashMap<String, Map<Integer, Long>>();
|
||||
|
|
@ -231,6 +252,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(5), gb.getMapOfMaps().get("mykey").get(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapOfMapsWithElementConversion() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
Map<String, Map<Integer, Long>> map = new HashMap<String, Map<Integer, Long>>();
|
||||
|
|
@ -242,6 +264,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(5), gb.getMapOfMaps().get("mykey").get(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapOfLists() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
|
||||
|
|
@ -253,6 +276,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Integer(5), gb.getMapOfLists().get(new Integer(1)).get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericMapOfListsWithElementConversion() throws MalformedURLException {
|
||||
GenericBean<String> gb = new GenericBean<String>();
|
||||
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
|
||||
|
|
@ -264,6 +288,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Integer(5), gb.getMapOfLists().get(new Integer(1)).get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericTypeNestingMapOfInteger() throws Exception {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("testKey", "100");
|
||||
|
|
@ -276,6 +301,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertTrue(obj instanceof Integer);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericTypeNestingMapOfListOfInteger() throws Exception {
|
||||
Map<String, List<String>> map = new HashMap<String, List<String>>();
|
||||
List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
|
||||
|
|
@ -290,6 +316,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(1, ((Integer) obj).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericTypeNestingListOfMapOfInteger() throws Exception {
|
||||
List<Map<String, String>> list = new LinkedList<Map<String, String>>();
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
|
|
@ -305,6 +332,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(5, ((Integer) obj).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericTypeNestingMapOfListOfListOfInteger() throws Exception {
|
||||
Map<String, List<List<String>>> map = new HashMap<String, List<List<String>>>();
|
||||
List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
|
||||
|
|
@ -319,11 +347,12 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(1, ((Integer) obj).intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexGenericMap() {
|
||||
Map inputMap = new HashMap();
|
||||
List inputKey = new LinkedList();
|
||||
Map<List<String>, List<String>> inputMap = new HashMap<List<String>, List<String>>();
|
||||
List<String> inputKey = new LinkedList<String>();
|
||||
inputKey.add("1");
|
||||
List inputValue = new LinkedList();
|
||||
List<String> inputValue = new LinkedList<String>();
|
||||
inputValue.add("10");
|
||||
inputMap.put(inputKey, inputValue);
|
||||
|
||||
|
|
@ -335,11 +364,12 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexGenericMapWithCollectionConversion() {
|
||||
Map inputMap = new HashMap();
|
||||
Set inputKey = new HashSet();
|
||||
Map<Set<String>, Set<String>> inputMap = new HashMap<Set<String>, Set<String>>();
|
||||
Set<String> inputKey = new HashSet<String>();
|
||||
inputKey.add("1");
|
||||
Set inputValue = new HashSet();
|
||||
Set<String> inputValue = new HashSet<String>();
|
||||
inputValue.add("10");
|
||||
inputMap.put(inputKey, inputValue);
|
||||
|
||||
|
|
@ -351,8 +381,9 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexGenericIndexedMapEntry() {
|
||||
List inputValue = new LinkedList();
|
||||
List<String> inputValue = new LinkedList<String>();
|
||||
inputValue.add("10");
|
||||
|
||||
ComplexMapHolder holder = new ComplexMapHolder();
|
||||
|
|
@ -363,8 +394,9 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexGenericIndexedMapEntryWithCollectionConversion() {
|
||||
Set inputValue = new HashSet();
|
||||
Set<String> inputValue = new HashSet<String>();
|
||||
inputValue.add("10");
|
||||
|
||||
ComplexMapHolder holder = new ComplexMapHolder();
|
||||
|
|
@ -375,8 +407,9 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexDerivedIndexedMapEntry() {
|
||||
List inputValue = new LinkedList();
|
||||
List<String> inputValue = new LinkedList<String>();
|
||||
inputValue.add("10");
|
||||
|
||||
ComplexMapHolder holder = new ComplexMapHolder();
|
||||
|
|
@ -387,8 +420,9 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplexDerivedIndexedMapEntryWithCollectionConversion() {
|
||||
Set inputValue = new HashSet();
|
||||
Set<String> inputValue = new HashSet<String>();
|
||||
inputValue.add("10");
|
||||
|
||||
ComplexMapHolder holder = new ComplexMapHolder();
|
||||
|
|
@ -399,6 +433,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericallyTypedIntegerBean() throws Exception {
|
||||
GenericIntegerBean gb = new GenericIntegerBean();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
|
|
@ -409,6 +444,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
Assert.assertEquals(new Integer(30), gb.getGenericListProperty().get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericallyTypedSetOfIntegerBean() throws Exception {
|
||||
GenericSetOfIntegerBean gb = new GenericSetOfIntegerBean();
|
||||
BeanWrapper bw = new BeanWrapperImpl(gb);
|
||||
|
|
@ -424,7 +460,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
|
||||
public abstract Object getMapOfInteger();
|
||||
|
||||
public abstract Map getMapOfListOfInteger();
|
||||
public abstract Map<String, List<Integer>> getMapOfListOfInteger();
|
||||
|
||||
public abstract void setMapOfListOfInteger(Map<String, List<Integer>> mapOfListOfInteger);
|
||||
}
|
||||
|
|
@ -508,6 +544,7 @@ public class BeanWrapperGenericsTests extends TestCase {
|
|||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class DerivedMap extends HashMap<Integer, List<Long>> {
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2005 the original author or authors.
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -16,15 +16,20 @@
|
|||
|
||||
package org.springframework.beans;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for MutablePropertyValues.
|
||||
* Tests for {@link MutablePropertyValues}.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
|
||||
public final class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
|
||||
|
||||
@Test
|
||||
public void testValid() throws Exception {
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
|
||||
|
|
@ -36,10 +41,11 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
|
|||
doTestTony(deepCopy);
|
||||
deepCopy.setPropertyValueAt(new PropertyValue("name", "Gordon"), 0);
|
||||
doTestTony(pvs);
|
||||
Assert.assertEquals("Gordon", deepCopy.getPropertyValue("name").getValue());
|
||||
assertEquals("Gordon", deepCopy.getPropertyValue("name").getValue());
|
||||
}
|
||||
|
||||
public void addOrOverride() throws Exception {
|
||||
@Test
|
||||
public void testAddOrOverride() throws Exception {
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
|
||||
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
|
||||
|
|
@ -47,12 +53,13 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
|
|||
doTestTony(pvs);
|
||||
PropertyValue addedPv = new PropertyValue("rod", "Rod");
|
||||
pvs.addPropertyValue(addedPv);
|
||||
Assert.assertTrue(pvs.getPropertyValue("rod").equals(addedPv));
|
||||
assertTrue(pvs.getPropertyValue("rod").equals(addedPv));
|
||||
PropertyValue changedPv = new PropertyValue("forname", "Greg");
|
||||
pvs.addPropertyValue(changedPv);
|
||||
Assert.assertTrue(pvs.getPropertyValue("forename").equals(changedPv));
|
||||
assertTrue(pvs.getPropertyValue("forname").equals(changedPv));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangesOnEquals() throws Exception {
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
|
||||
|
|
@ -60,9 +67,10 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
|
|||
pvs.addPropertyValue(new PropertyValue("age", "50"));
|
||||
MutablePropertyValues pvs2 = pvs;
|
||||
PropertyValues changes = pvs2.changesSince(pvs);
|
||||
Assert.assertTrue("changes are empty", changes.getPropertyValues().length == 0);
|
||||
assertTrue("changes are empty", changes.getPropertyValues().length == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeOfOneField() throws Exception {
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
|
||||
|
|
@ -71,29 +79,29 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
|
|||
|
||||
MutablePropertyValues pvs2 = new MutablePropertyValues(pvs);
|
||||
PropertyValues changes = pvs2.changesSince(pvs);
|
||||
Assert.assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
|
||||
assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
|
||||
changes.getPropertyValues().length == 0);
|
||||
|
||||
pvs2.addPropertyValue(new PropertyValue("forname", "Gordon"));
|
||||
changes = pvs2.changesSince(pvs);
|
||||
Assert.assertEquals("1 change", 1, changes.getPropertyValues().length);
|
||||
assertEquals("1 change", 1, changes.getPropertyValues().length);
|
||||
PropertyValue fn = changes.getPropertyValue("forname");
|
||||
Assert.assertTrue("change is forname", fn != null);
|
||||
Assert.assertTrue("new value is gordon", fn.getValue().equals("Gordon"));
|
||||
assertTrue("change is forname", fn != null);
|
||||
assertTrue("new value is gordon", fn.getValue().equals("Gordon"));
|
||||
|
||||
MutablePropertyValues pvs3 = new MutablePropertyValues(pvs);
|
||||
changes = pvs3.changesSince(pvs);
|
||||
Assert.assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
|
||||
assertTrue("changes are empty, not of length " + changes.getPropertyValues().length,
|
||||
changes.getPropertyValues().length == 0);
|
||||
|
||||
// add new
|
||||
pvs3.addPropertyValue(new PropertyValue("foo", "bar"));
|
||||
pvs3.addPropertyValue(new PropertyValue("fi", "fum"));
|
||||
changes = pvs3.changesSince(pvs);
|
||||
Assert.assertTrue("2 change", changes.getPropertyValues().length == 2);
|
||||
assertTrue("2 change", changes.getPropertyValues().length == 2);
|
||||
fn = changes.getPropertyValue("foo");
|
||||
Assert.assertTrue("change in foo", fn != null);
|
||||
Assert.assertTrue("new value is bar", fn.getValue().equals("bar"));
|
||||
assertTrue("change in foo", fn != null);
|
||||
assertTrue("new value is bar", fn.getValue().equals("bar"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue