polishing .beans tests

This commit is contained in:
Chris Beams 2008-12-24 18:16:53 +00:00
parent 6966099d85
commit 3d634f1eb7
5 changed files with 138 additions and 67 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,15 +16,16 @@
package org.springframework.beans; package org.springframework.beans;
import static org.junit.Assert.assertTrue;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Chris Beams
*/ */
public abstract class AbstractPropertyValuesTests extends TestCase { public abstract class AbstractPropertyValuesTests {
/** /**
* Must contain: forname=Tony surname=Blair age=50 * 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")); assertTrue("Doesn't contain tory", !pvs.contains("tory"));
PropertyValue[] ps = pvs.getPropertyValues(); PropertyValue[] ps = pvs.getPropertyValues();
Map m = new HashMap(); Map<String, String> m = new HashMap<String, String>();
m.put("forname", "Tony"); m.put("forname", "Tony");
m.put("surname", "Blair"); m.put("surname", "Blair");
m.put("age", "50"); m.put("age", "50");

View File

@ -16,26 +16,31 @@
package org.springframework.beans; package org.springframework.beans;
import static org.junit.Assert.*;
import java.beans.Introspector; import java.beans.Introspector;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor; import org.springframework.core.io.ResourceEditor;
/** /**
* Unit tests for {@link BeanUtils}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rob Harrop * @author Rob Harrop
* @author Chris Beams
* @since 19.05.2003 * @since 19.05.2003
*/ */
public class BeanUtilsTests extends TestCase { public final class BeanUtilsTests {
@Test
public void testInstantiateClass() { public void testInstantiateClass() {
// give proper class // give proper class
BeanUtils.instantiateClass(ArrayList.class); BeanUtils.instantiateClass(ArrayList.class);
@ -59,6 +64,7 @@ public class BeanUtilsTests extends TestCase {
} }
} }
@Test
public void testGetPropertyDescriptors() throws Exception { public void testGetPropertyDescriptors() throws Exception {
PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors(); PropertyDescriptor[] actual = Introspector.getBeanInfo(TestBean.class).getPropertyDescriptors();
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(TestBean.class); 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); assertEquals("Invalid number of descriptors returned", actual.length, descriptors.length);
} }
@Test
public void testBeanPropertyIsArray() { public void testBeanPropertyIsArray() {
PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class); PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(ContainerBean.class);
for (int i = 0; i < descriptors.length; i++) { for (int i = 0; i < descriptors.length; i++) {
@ -77,10 +84,12 @@ public class BeanUtilsTests extends TestCase {
} }
} }
@Test
public void testFindEditorByConvention() { public void testFindEditorByConvention() {
assertEquals(ResourceEditor.class, BeanUtils.findEditorByConvention(Resource.class).getClass()); assertEquals(ResourceEditor.class, BeanUtils.findEditorByConvention(Resource.class).getClass());
} }
@Test
public void testCopyProperties() throws Exception { public void testCopyProperties() throws Exception {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setName("rod"); tb.setName("rod");
@ -96,6 +105,7 @@ public class BeanUtilsTests extends TestCase {
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy())); assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
} }
@Test
public void testCopyPropertiesWithDifferentTypes1() throws Exception { public void testCopyPropertiesWithDifferentTypes1() throws Exception {
DerivedTestBean tb = new DerivedTestBean(); DerivedTestBean tb = new DerivedTestBean();
tb.setName("rod"); tb.setName("rod");
@ -111,6 +121,7 @@ public class BeanUtilsTests extends TestCase {
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy())); assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
} }
@Test
public void testCopyPropertiesWithDifferentTypes2() throws Exception { public void testCopyPropertiesWithDifferentTypes2() throws Exception {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
tb.setName("rod"); tb.setName("rod");
@ -126,6 +137,7 @@ public class BeanUtilsTests extends TestCase {
assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy())); assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
} }
@Test
public void testCopyPropertiesWithEditable() throws Exception { public void testCopyPropertiesWithEditable() throws Exception {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
assertTrue("Name empty", tb.getName() == null); assertTrue("Name empty", tb.getName() == null);
@ -143,6 +155,7 @@ public class BeanUtilsTests extends TestCase {
assertTrue("Touchy still empty", tb2.getTouchy() == null); assertTrue("Touchy still empty", tb2.getTouchy() == null);
} }
@Test
public void testCopyPropertiesWithIgnore() throws Exception { public void testCopyPropertiesWithIgnore() throws Exception {
TestBean tb = new TestBean(); TestBean tb = new TestBean();
assertTrue("Name empty", tb.getName() == null); assertTrue("Name empty", tb.getName() == null);
@ -160,6 +173,7 @@ public class BeanUtilsTests extends TestCase {
assertTrue("Touchy still empty", tb2.getTouchy() == null); assertTrue("Touchy still empty", tb2.getTouchy() == null);
} }
@Test
public void testCopyPropertiesWithIgnoredNonExistingProperty() { public void testCopyPropertiesWithIgnoredNonExistingProperty() {
NameAndSpecialProperty source = new NameAndSpecialProperty(); NameAndSpecialProperty source = new NameAndSpecialProperty();
source.setName("name"); source.setName("name");
@ -168,12 +182,14 @@ public class BeanUtilsTests extends TestCase {
assertEquals(target.getName(), "name"); assertEquals(target.getName(), "name");
} }
@Test
public void testResolveSimpleSignature() throws Exception { public void testResolveSimpleSignature() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomething"); Method desiredMethod = MethodSignatureBean.class.getMethod("doSomething");
assertSignatureEquals(desiredMethod, "doSomething"); assertSignatureEquals(desiredMethod, "doSomething");
assertSignatureEquals(desiredMethod, "doSomething()"); assertSignatureEquals(desiredMethod, "doSomething()");
} }
@Test
public void testResolveInvalidSignature() throws Exception { public void testResolveInvalidSignature() throws Exception {
try { try {
BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class); BeanUtils.resolveSignature("doSomething(", MethodSignatureBean.class);
@ -192,17 +208,20 @@ public class BeanUtilsTests extends TestCase {
} }
} }
@Test
public void testResolveWithAndWithoutArgList() throws Exception { public void testResolveWithAndWithoutArgList() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class}); Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class});
assertSignatureEquals(desiredMethod, "doSomethingElse"); assertSignatureEquals(desiredMethod, "doSomethingElse");
assertNull(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class)); assertNull(BeanUtils.resolveSignature("doSomethingElse()", MethodSignatureBean.class));
} }
@Test
public void testResolveTypedSignature() throws Exception { public void testResolveTypedSignature() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class}); Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingElse", new Class[]{String.class, int.class});
assertSignatureEquals(desiredMethod, "doSomethingElse(java.lang.String, int)"); assertSignatureEquals(desiredMethod, "doSomethingElse(java.lang.String, int)");
} }
@Test
public void testResolveOverloadedSignature() throws Exception { public void testResolveOverloadedSignature() throws Exception {
// test resolve with no args // test resolve with no args
Method desiredMethod = MethodSignatureBean.class.getMethod("overloaded"); 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)"); assertSignatureEquals(desiredMethod, "overloaded(java.lang.String, org.springframework.beans.factory.BeanFactory)");
} }
@Test
public void testResolveSignatureWithArray() throws Exception { public void testResolveSignatureWithArray() throws Exception {
Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAnArray", new Class[]{String[].class}); Method desiredMethod = MethodSignatureBean.class.getMethod("doSomethingWithAnArray", new Class[]{String[].class});
assertSignatureEquals(desiredMethod, "doSomethingWithAnArray(java.lang.String[])"); assertSignatureEquals(desiredMethod, "doSomethingWithAnArray(java.lang.String[])");

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,33 +16,38 @@
package org.springframework.beans; package org.springframework.beans;
import junit.framework.Assert; import static org.junit.Assert.*;
import junit.framework.TestCase;
import org.junit.Test;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
*/ */
public class BeanWrapperEnumTests extends TestCase { public final class BeanWrapperEnumTests {
@Test
public void testCustomEnum() { public void testCustomEnum() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setPropertyValue("customEnum", "VALUE_1"); bw.setPropertyValue("customEnum", "VALUE_1");
Assert.assertEquals(CustomEnum.VALUE_1, gb.getCustomEnum()); assertEquals(CustomEnum.VALUE_1, gb.getCustomEnum());
} }
@Test
public void testCustomEnumWithNull() { public void testCustomEnumWithNull() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setPropertyValue("customEnum", null); bw.setPropertyValue("customEnum", null);
Assert.assertEquals(null, gb.getCustomEnum()); assertEquals(null, gb.getCustomEnum());
} }
@Test
public void testCustomEnumWithEmptyString() { public void testCustomEnumWithEmptyString() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setPropertyValue("customEnum", ""); bw.setPropertyValue("customEnum", "");
Assert.assertEquals(null, gb.getCustomEnum()); assertEquals(null, gb.getCustomEnum());
} }
} }

View File

@ -16,6 +16,8 @@
package org.springframework.beans; package org.springframework.beans;
import static org.junit.Assert.*;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -29,8 +31,8 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import junit.framework.Assert; import junit.framework.Assert;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.beans.propertyeditors.CustomNumberEditor; import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
@ -38,14 +40,16 @@ import org.springframework.core.io.UrlResource;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 18.01.2006 * @since 18.01.2006
*/ */
public class BeanWrapperGenericsTests extends TestCase { public final class BeanWrapperGenericsTests {
@Test
public void testGenericSet() { public void testGenericSet() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
Set input = new HashSet(); Set<String> input = new HashSet<String>();
input.add("4"); input.add("4");
input.add("5"); input.add("5");
bw.setPropertyValue("integerSet", input); bw.setPropertyValue("integerSet", input);
@ -53,10 +57,11 @@ public class BeanWrapperGenericsTests extends TestCase {
assertTrue(gb.getIntegerSet().contains(new Integer(5))); assertTrue(gb.getIntegerSet().contains(new Integer(5)));
} }
@Test
public void testGenericSetWithConversionFailure() { public void testGenericSetWithConversionFailure() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
Set input = new HashSet(); Set<TestBean> input = new HashSet<TestBean>();
input.add(new TestBean()); input.add(new TestBean());
try { try {
bw.setPropertyValue("integerSet", input); bw.setPropertyValue("integerSet", input);
@ -67,10 +72,11 @@ public class BeanWrapperGenericsTests extends TestCase {
} }
} }
@Test
public void testGenericList() throws MalformedURLException { public void testGenericList() throws MalformedURLException {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
List input = new ArrayList(); List<String> input = new ArrayList<String>();
input.add("http://localhost:8080"); input.add("http://localhost:8080");
input.add("http://localhost:9090"); input.add("http://localhost:9090");
bw.setPropertyValue("resourceList", input); bw.setPropertyValue("resourceList", input);
@ -78,18 +84,20 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1));
} }
@Test
public void testGenericListElement() throws MalformedURLException { public void testGenericListElement() throws MalformedURLException {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
gb.setResourceList(new ArrayList<Resource>()); gb.setResourceList(new ArrayList<Resource>());
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setPropertyValue("resourceList[0]", "http://localhost:8080"); bw.setPropertyValue("resourceList[0]", "http://localhost:8080");
assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0));
} }
@Test
public void testGenericMap() { public void testGenericMap() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
Map input = new HashMap(); Map<String, String> input = new HashMap<String, String>();
input.put("4", "5"); input.put("4", "5");
input.put("6", "7"); input.put("6", "7");
bw.setPropertyValue("shortMap", input); bw.setPropertyValue("shortMap", input);
@ -97,8 +105,9 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); assertEquals(new Integer(7), gb.getShortMap().get(new Short("6")));
} }
@Test
public void testGenericMapElement() { public void testGenericMapElement() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
gb.setShortMap(new HashMap<Short, Integer>()); gb.setShortMap(new HashMap<Short, Integer>());
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setPropertyValue("shortMap[4]", "5"); bw.setPropertyValue("shortMap[4]", "5");
@ -106,10 +115,11 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); assertEquals(new Integer(5), gb.getShortMap().get(new Short("4")));
} }
@Test
public void testGenericMapWithKeyType() { public void testGenericMapWithKeyType() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
Map input = new HashMap(); Map<String, String> input = new HashMap<String, String>();
input.put("4", "5"); input.put("4", "5");
input.put("6", "7"); input.put("6", "7");
bw.setPropertyValue("longMap", input); bw.setPropertyValue("longMap", input);
@ -117,8 +127,9 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals("7", gb.getLongMap().get(new Long("6"))); assertEquals("7", gb.getLongMap().get(new Long("6")));
} }
@Test
public void testGenericMapElementWithKeyType() { public void testGenericMapElementWithKeyType() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
gb.setLongMap(new HashMap<Long, Integer>()); gb.setLongMap(new HashMap<Long, Integer>());
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setPropertyValue("longMap[4]", "5"); bw.setPropertyValue("longMap[4]", "5");
@ -126,15 +137,17 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals("5", bw.getPropertyValue("longMap[4]")); assertEquals("5", bw.getPropertyValue("longMap[4]"));
} }
@Test
public void testGenericMapWithCollectionValue() { public void testGenericMapWithCollectionValue() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
Map input = new HashMap(); @SuppressWarnings("unchecked")
HashSet value1 = new HashSet(); Map<String, Collection> input = new HashMap<String, Collection>();
HashSet<Integer> value1 = new HashSet<Integer>();
value1.add(new Integer(1)); value1.add(new Integer(1));
input.put("1", value1); input.put("1", value1);
ArrayList value2 = new ArrayList(); ArrayList<Boolean> value2 = new ArrayList<Boolean>();
value2.add(Boolean.TRUE); value2.add(Boolean.TRUE);
input.put("2", value2); input.put("2", value2);
bw.setPropertyValue("collectionMap", input); bw.setPropertyValue("collectionMap", input);
@ -142,17 +155,19 @@ public class BeanWrapperGenericsTests extends TestCase {
assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList); assertTrue(gb.getCollectionMap().get(new Integer(2)) instanceof ArrayList);
} }
@Test
public void testGenericMapElementWithCollectionValue() { public void testGenericMapElementWithCollectionValue() {
GenericBean gb = new GenericBean(); GenericBean<?> gb = new GenericBean<Object>();
gb.setCollectionMap(new HashMap<Number, Collection<? extends Object>>()); gb.setCollectionMap(new HashMap<Number, Collection<? extends Object>>());
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false)); bw.registerCustomEditor(Number.class, new CustomNumberEditor(Integer.class, false));
HashSet value1 = new HashSet(); HashSet<Integer> value1 = new HashSet<Integer>();
value1.add(new Integer(1)); value1.add(new Integer(1));
bw.setPropertyValue("collectionMap[1]", value1); bw.setPropertyValue("collectionMap[1]", value1);
assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet); assertTrue(gb.getCollectionMap().get(new Integer(1)) instanceof HashSet);
} }
@Test
public void testGenericListOfLists() throws MalformedURLException { public void testGenericListOfLists() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
List<List<Integer>> list = new LinkedList<List<Integer>>(); 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)); assertEquals(new Integer(5), gb.getListOfLists().get(0).get(0));
} }
@Test
public void testGenericListOfListsWithElementConversion() throws MalformedURLException { public void testGenericListOfListsWithElementConversion() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
List<List<Integer>> list = new LinkedList<List<Integer>>(); 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)); assertEquals(new Integer(5), gb.getListOfLists().get(0).get(0));
} }
@Test
public void testGenericListOfArrays() throws MalformedURLException { public void testGenericListOfArrays() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
ArrayList<String[]> list = new ArrayList<String[]>(); ArrayList<String[]> list = new ArrayList<String[]>();
@ -186,6 +203,7 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals("str3 ", gb.getListOfArrays().get(0)[1]); assertEquals("str3 ", gb.getListOfArrays().get(0)[1]);
} }
@Test
public void testGenericListOfArraysWithElementConversion() throws MalformedURLException { public void testGenericListOfArraysWithElementConversion() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
ArrayList<String[]> list = new ArrayList<String[]>(); ArrayList<String[]> list = new ArrayList<String[]>();
@ -198,6 +216,7 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals("str3", gb.getListOfArrays().get(0)[1]); assertEquals("str3", gb.getListOfArrays().get(0)[1]);
} }
@Test
public void testGenericListOfMaps() throws MalformedURLException { public void testGenericListOfMaps() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
List<Map<Integer, Long>> list = new LinkedList<Map<Integer, Long>>(); 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)); assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10));
} }
@Test
public void testGenericListOfMapsWithElementConversion() throws MalformedURLException { public void testGenericListOfMapsWithElementConversion() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
List<Map<Integer, Long>> list = new LinkedList<Map<Integer, Long>>(); 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)); assertEquals(new Long(5), gb.getListOfMaps().get(0).get(10));
} }
@Test
public void testGenericMapOfMaps() throws MalformedURLException { public void testGenericMapOfMaps() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
Map<String, Map<Integer, Long>> map = new HashMap<String, Map<Integer, Long>>(); 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)); assertEquals(new Long(5), gb.getMapOfMaps().get("mykey").get(10));
} }
@Test
public void testGenericMapOfMapsWithElementConversion() throws MalformedURLException { public void testGenericMapOfMapsWithElementConversion() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
Map<String, Map<Integer, Long>> map = new HashMap<String, Map<Integer, Long>>(); 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)); assertEquals(new Long(5), gb.getMapOfMaps().get("mykey").get(10));
} }
@Test
public void testGenericMapOfLists() throws MalformedURLException { public void testGenericMapOfLists() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); 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)); assertEquals(new Integer(5), gb.getMapOfLists().get(new Integer(1)).get(0));
} }
@Test
public void testGenericMapOfListsWithElementConversion() throws MalformedURLException { public void testGenericMapOfListsWithElementConversion() throws MalformedURLException {
GenericBean<String> gb = new GenericBean<String>(); GenericBean<String> gb = new GenericBean<String>();
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); 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)); assertEquals(new Integer(5), gb.getMapOfLists().get(new Integer(1)).get(0));
} }
@Test
public void testGenericTypeNestingMapOfInteger() throws Exception { public void testGenericTypeNestingMapOfInteger() throws Exception {
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
map.put("testKey", "100"); map.put("testKey", "100");
@ -276,6 +301,7 @@ public class BeanWrapperGenericsTests extends TestCase {
assertTrue(obj instanceof Integer); assertTrue(obj instanceof Integer);
} }
@Test
public void testGenericTypeNestingMapOfListOfInteger() throws Exception { public void testGenericTypeNestingMapOfListOfInteger() throws Exception {
Map<String, List<String>> map = new HashMap<String, List<String>>(); Map<String, List<String>> map = new HashMap<String, List<String>>();
List<String> list = Arrays.asList(new String[] {"1", "2", "3"}); List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
@ -290,6 +316,7 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(1, ((Integer) obj).intValue()); assertEquals(1, ((Integer) obj).intValue());
} }
@Test
public void testGenericTypeNestingListOfMapOfInteger() throws Exception { public void testGenericTypeNestingListOfMapOfInteger() throws Exception {
List<Map<String, String>> list = new LinkedList<Map<String, String>>(); List<Map<String, String>> list = new LinkedList<Map<String, String>>();
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
@ -305,6 +332,7 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(5, ((Integer) obj).intValue()); assertEquals(5, ((Integer) obj).intValue());
} }
@Test
public void testGenericTypeNestingMapOfListOfListOfInteger() throws Exception { public void testGenericTypeNestingMapOfListOfListOfInteger() throws Exception {
Map<String, List<List<String>>> map = new HashMap<String, List<List<String>>>(); Map<String, List<List<String>>> map = new HashMap<String, List<List<String>>>();
List<String> list = Arrays.asList(new String[] {"1", "2", "3"}); List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
@ -319,11 +347,12 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(1, ((Integer) obj).intValue()); assertEquals(1, ((Integer) obj).intValue());
} }
@Test
public void testComplexGenericMap() { public void testComplexGenericMap() {
Map inputMap = new HashMap(); Map<List<String>, List<String>> inputMap = new HashMap<List<String>, List<String>>();
List inputKey = new LinkedList(); List<String> inputKey = new LinkedList<String>();
inputKey.add("1"); inputKey.add("1");
List inputValue = new LinkedList(); List<String> inputValue = new LinkedList<String>();
inputValue.add("10"); inputValue.add("10");
inputMap.put(inputKey, inputValue); inputMap.put(inputKey, inputValue);
@ -335,11 +364,12 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0)); assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0));
} }
@Test
public void testComplexGenericMapWithCollectionConversion() { public void testComplexGenericMapWithCollectionConversion() {
Map inputMap = new HashMap(); Map<Set<String>, Set<String>> inputMap = new HashMap<Set<String>, Set<String>>();
Set inputKey = new HashSet(); Set<String> inputKey = new HashSet<String>();
inputKey.add("1"); inputKey.add("1");
Set inputValue = new HashSet(); Set<String> inputValue = new HashSet<String>();
inputValue.add("10"); inputValue.add("10");
inputMap.put(inputKey, inputValue); inputMap.put(inputKey, inputValue);
@ -351,8 +381,9 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0)); assertEquals(new Long(10), holder.getGenericMap().values().iterator().next().get(0));
} }
@Test
public void testComplexGenericIndexedMapEntry() { public void testComplexGenericIndexedMapEntry() {
List inputValue = new LinkedList(); List<String> inputValue = new LinkedList<String>();
inputValue.add("10"); inputValue.add("10");
ComplexMapHolder holder = new ComplexMapHolder(); ComplexMapHolder holder = new ComplexMapHolder();
@ -363,8 +394,9 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0)); assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0));
} }
@Test
public void testComplexGenericIndexedMapEntryWithCollectionConversion() { public void testComplexGenericIndexedMapEntryWithCollectionConversion() {
Set inputValue = new HashSet(); Set<String> inputValue = new HashSet<String>();
inputValue.add("10"); inputValue.add("10");
ComplexMapHolder holder = new ComplexMapHolder(); ComplexMapHolder holder = new ComplexMapHolder();
@ -375,8 +407,9 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0)); assertEquals(new Long(10), holder.getGenericIndexedMap().values().iterator().next().get(0));
} }
@Test
public void testComplexDerivedIndexedMapEntry() { public void testComplexDerivedIndexedMapEntry() {
List inputValue = new LinkedList(); List<String> inputValue = new LinkedList<String>();
inputValue.add("10"); inputValue.add("10");
ComplexMapHolder holder = new ComplexMapHolder(); ComplexMapHolder holder = new ComplexMapHolder();
@ -387,8 +420,9 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0)); assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0));
} }
@Test
public void testComplexDerivedIndexedMapEntryWithCollectionConversion() { public void testComplexDerivedIndexedMapEntryWithCollectionConversion() {
Set inputValue = new HashSet(); Set<String> inputValue = new HashSet<String>();
inputValue.add("10"); inputValue.add("10");
ComplexMapHolder holder = new ComplexMapHolder(); ComplexMapHolder holder = new ComplexMapHolder();
@ -399,6 +433,7 @@ public class BeanWrapperGenericsTests extends TestCase {
assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0)); assertEquals(new Long(10), holder.getDerivedIndexedMap().values().iterator().next().get(0));
} }
@Test
public void testGenericallyTypedIntegerBean() throws Exception { public void testGenericallyTypedIntegerBean() throws Exception {
GenericIntegerBean gb = new GenericIntegerBean(); GenericIntegerBean gb = new GenericIntegerBean();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
@ -409,6 +444,7 @@ public class BeanWrapperGenericsTests extends TestCase {
Assert.assertEquals(new Integer(30), gb.getGenericListProperty().get(1)); Assert.assertEquals(new Integer(30), gb.getGenericListProperty().get(1));
} }
@Test
public void testGenericallyTypedSetOfIntegerBean() throws Exception { public void testGenericallyTypedSetOfIntegerBean() throws Exception {
GenericSetOfIntegerBean gb = new GenericSetOfIntegerBean(); GenericSetOfIntegerBean gb = new GenericSetOfIntegerBean();
BeanWrapper bw = new BeanWrapperImpl(gb); BeanWrapper bw = new BeanWrapperImpl(gb);
@ -424,7 +460,7 @@ public class BeanWrapperGenericsTests extends TestCase {
public abstract Object getMapOfInteger(); public abstract Object getMapOfInteger();
public abstract Map getMapOfListOfInteger(); public abstract Map<String, List<Integer>> getMapOfListOfInteger();
public abstract void setMapOfListOfInteger(Map<String, List<Integer>> mapOfListOfInteger); 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>> { private static class DerivedMap extends HashMap<Integer, List<Long>> {
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,15 +16,20 @@
package org.springframework.beans; 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 Rod Johnson
* @author Chris Beams
*/ */
public class MutablePropertyValuesTests extends AbstractPropertyValuesTests { public final class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
@Test
public void testValid() throws Exception { public void testValid() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
@ -36,10 +41,11 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
doTestTony(deepCopy); doTestTony(deepCopy);
deepCopy.setPropertyValueAt(new PropertyValue("name", "Gordon"), 0); deepCopy.setPropertyValueAt(new PropertyValue("name", "Gordon"), 0);
doTestTony(pvs); 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(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair")); pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
@ -47,12 +53,13 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
doTestTony(pvs); doTestTony(pvs);
PropertyValue addedPv = new PropertyValue("rod", "Rod"); PropertyValue addedPv = new PropertyValue("rod", "Rod");
pvs.addPropertyValue(addedPv); pvs.addPropertyValue(addedPv);
Assert.assertTrue(pvs.getPropertyValue("rod").equals(addedPv)); assertTrue(pvs.getPropertyValue("rod").equals(addedPv));
PropertyValue changedPv = new PropertyValue("forname", "Greg"); PropertyValue changedPv = new PropertyValue("forname", "Greg");
pvs.addPropertyValue(changedPv); pvs.addPropertyValue(changedPv);
Assert.assertTrue(pvs.getPropertyValue("forename").equals(changedPv)); assertTrue(pvs.getPropertyValue("forname").equals(changedPv));
} }
@Test
public void testChangesOnEquals() throws Exception { public void testChangesOnEquals() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
@ -60,9 +67,10 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
pvs.addPropertyValue(new PropertyValue("age", "50")); pvs.addPropertyValue(new PropertyValue("age", "50"));
MutablePropertyValues pvs2 = pvs; MutablePropertyValues pvs2 = pvs;
PropertyValues changes = pvs2.changesSince(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 { public void testChangeOfOneField() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues(); MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony")); pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
@ -71,29 +79,29 @@ public class MutablePropertyValuesTests extends AbstractPropertyValuesTests {
MutablePropertyValues pvs2 = new MutablePropertyValues(pvs); MutablePropertyValues pvs2 = new MutablePropertyValues(pvs);
PropertyValues changes = pvs2.changesSince(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); changes.getPropertyValues().length == 0);
pvs2.addPropertyValue(new PropertyValue("forname", "Gordon")); pvs2.addPropertyValue(new PropertyValue("forname", "Gordon"));
changes = pvs2.changesSince(pvs); changes = pvs2.changesSince(pvs);
Assert.assertEquals("1 change", 1, changes.getPropertyValues().length); assertEquals("1 change", 1, changes.getPropertyValues().length);
PropertyValue fn = changes.getPropertyValue("forname"); PropertyValue fn = changes.getPropertyValue("forname");
Assert.assertTrue("change is forname", fn != null); assertTrue("change is forname", fn != null);
Assert.assertTrue("new value is gordon", fn.getValue().equals("Gordon")); assertTrue("new value is gordon", fn.getValue().equals("Gordon"));
MutablePropertyValues pvs3 = new MutablePropertyValues(pvs); MutablePropertyValues pvs3 = new MutablePropertyValues(pvs);
changes = pvs3.changesSince(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); changes.getPropertyValues().length == 0);
// add new // add new
pvs3.addPropertyValue(new PropertyValue("foo", "bar")); pvs3.addPropertyValue(new PropertyValue("foo", "bar"));
pvs3.addPropertyValue(new PropertyValue("fi", "fum")); pvs3.addPropertyValue(new PropertyValue("fi", "fum"));
changes = pvs3.changesSince(pvs); changes = pvs3.changesSince(pvs);
Assert.assertTrue("2 change", changes.getPropertyValues().length == 2); assertTrue("2 change", changes.getPropertyValues().length == 2);
fn = changes.getPropertyValue("foo"); fn = changes.getPropertyValue("foo");
Assert.assertTrue("change in foo", fn != null); assertTrue("change in foo", fn != null);
Assert.assertTrue("new value is bar", fn.getValue().equals("bar")); assertTrue("new value is bar", fn.getValue().equals("bar"));
} }
} }