revised core conversion package for BeanWrapper/BeanFactory integration

This commit is contained in:
Juergen Hoeller
2009-08-09 00:46:49 +00:00
parent e9823b57b4
commit 45a0cadf8e
70 changed files with 1265 additions and 1113 deletions

View File

@@ -1,21 +1,39 @@
/*
* Copyright 2002-2009 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.
* 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.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ArrayToArray;
import org.springframework.core.convert.support.DefaultTypeConverter;
import org.springframework.core.convert.TypeDescriptor;
/**
* @author Keith Donald
*/
public class ArrayToArrayTests {
@Test
public void testArrayToArrayConversion() {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
ArrayToArray c = new ArrayToArray(TypeDescriptor.valueOf(String[].class), TypeDescriptor.valueOf(Integer[].class), service);
Integer[] result = (Integer[]) c.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer(1), result[0]);
assertEquals(new Integer(2), result[1]);
assertEquals(new Integer(3), result[2]);
}
}

View File

@@ -1,6 +1,20 @@
package org.springframework.core.convert.support;
/*
* Copyright 2002-2009 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.
* 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.
*/
import static org.junit.Assert.assertEquals;
package org.springframework.core.convert.support;
import java.util.Collection;
import java.util.LinkedList;
@@ -8,26 +22,30 @@ import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ArrayToCollection;
import org.springframework.core.convert.support.DefaultTypeConverter;
import org.springframework.core.convert.TypeDescriptor;
/**
* @author Keith Donald
*/
public class ArrayToCollectionTests {
@Test
public void testArrayToCollectionConversion() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("bindTarget")), service);
List result = (List) c.execute(new String[] { "1", "2", "3" });
assertEquals(new Integer(1), result.get(0));
assertEquals(new Integer(2), result.get(1));
assertEquals(new Integer(3), result.get(2));
Collection result = (Collection) c.execute(new String[] { "1", "2", "3" });
assertEquals(3, result.size());
assertTrue(result.contains(1));
assertTrue(result.contains(2));
assertTrue(result.contains(3));
}
@Test
public void testArrayToSetConversion() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("setTarget")), service);
Set result = (Set) c.execute(new String[] { "1" });
assertEquals("1", result.iterator().next());
@@ -35,7 +53,7 @@ public class ArrayToCollectionTests {
@Test
public void testArrayToSortedSetConversion() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("sortedSetTarget")), service);
SortedSet result = (SortedSet) c.execute(new String[] { "1" });
assertEquals(new Integer(1), result.iterator().next());
@@ -43,7 +61,7 @@ public class ArrayToCollectionTests {
@Test
public void testArrayToCollectionImplConversion() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("implTarget")), service);
LinkedList result = (LinkedList) c.execute(new String[] { "1" });
assertEquals("1", result.iterator().next());
@@ -51,7 +69,7 @@ public class ArrayToCollectionTests {
@Test
public void testArrayToNonGenericCollectionConversionNullElement() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
ArrayToCollection c = new ArrayToCollection(TypeDescriptor.valueOf(String[].class), new TypeDescriptor(getClass().getField("listTarget")), service);
List result = (List) c.execute(new Integer[] { null, new Integer(1) });
assertEquals(null, result.get(0));

View File

@@ -1,20 +1,37 @@
package org.springframework.core.convert.support;
/*
* Copyright 2002-2009 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.
* 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.
*/
import static org.junit.Assert.assertEquals;
package org.springframework.core.convert.support;
import java.util.ArrayList;
import java.util.Collection;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.CollectionToArray;
import org.springframework.core.convert.support.DefaultTypeConverter;
import org.springframework.core.convert.TypeDescriptor;
/**
* @author Keith Donald
*/
public class CollectionToArrayTests {
@Test
public void testCollectionToArrayConversion() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
CollectionToArray c = new CollectionToArray(new TypeDescriptor(getClass().getField("bindTarget")),
TypeDescriptor.valueOf(Integer[].class), service);
bindTarget.add("1");
@@ -28,7 +45,7 @@ public class CollectionToArrayTests {
@Test
public void testCollectionToArrayConversionNoGenericInfo() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor
.valueOf(Integer[].class), service);
bindTarget.add("1");
@@ -42,7 +59,7 @@ public class CollectionToArrayTests {
@Test
public void testCollectionToArrayConversionNoGenericInfoNullElement() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
CollectionToArray c = new CollectionToArray(TypeDescriptor.valueOf(Collection.class), TypeDescriptor
.valueOf(Integer[].class), service);
bindTarget.add(null);

View File

@@ -1,85 +1,104 @@
package org.springframework.core.convert.support;
/*
* Copyright 2002-2009 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.
* 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.
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
package org.springframework.core.convert.support;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.CollectionToCollection;
import org.springframework.core.convert.support.DefaultTypeConverter;
import org.springframework.core.convert.TypeDescriptor;
/**
* @author Keith Donald
*/
public class CollectionToCollectionTests {
@Test
public void testCollectionToCollectionConversion() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
CollectionToCollection c = new CollectionToCollection(new TypeDescriptor(getClass().getField("bindTarget")),
new TypeDescriptor(getClass().getField("integerTarget")), service);
bindTarget.add("1");
bindTarget.add("2");
bindTarget.add("3");
List result = (List) c.execute(bindTarget);
assertEquals(new Integer(1), result.get(0));
assertEquals(new Integer(2), result.get(1));
assertEquals(new Integer(3), result.get(2));
Collection result = (Collection) c.execute(bindTarget);
assertEquals(3, result.size());
assertTrue(result.contains(1));
assertTrue(result.contains(2));
assertTrue(result.contains(3));
}
@Test
public void testCollectionToCollectionConversionNoGenericInfo() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
TypeDescriptor.valueOf(List.class), service);
bindTarget.add("1");
bindTarget.add("2");
bindTarget.add("3");
List result = (List) c.execute(bindTarget);
assertEquals("1", result.get(0));
assertEquals("2", result.get(1));
assertEquals("3", result.get(2));
Collection result = (Collection) c.execute(bindTarget);
assertEquals(3, result.size());
assertTrue(result.contains("1"));
assertTrue(result.contains("2"));
assertTrue(result.contains("3"));
}
@Test
public void testCollectionToCollectionConversionNoGenericInfoSource() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
new TypeDescriptor(getClass().getField("integerTarget")), service);
bindTarget.add("1");
bindTarget.add("2");
bindTarget.add("3");
List result = (List) c.execute(bindTarget);
assertEquals(new Integer(1), result.get(0));
assertEquals(new Integer(2), result.get(1));
assertEquals(new Integer(3), result.get(2));
Collection result = (Collection) c.execute(bindTarget);
assertEquals(3, result.size());
assertTrue(result.contains(1));
assertTrue(result.contains(2));
assertTrue(result.contains(3));
}
@Test
public void testCollectionToCollectionConversionNoGenericInfoSourceNullValues() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
public void testCollectionToCollectionConversionNoGenericInfoSourceNullValue() throws Exception {
DefaultConversionService service = new DefaultConversionService();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
new TypeDescriptor(getClass().getField("integerTarget")), service);
bindTarget.add(null);
bindTarget.add("1");
bindTarget.add("2");
bindTarget.add(null);
bindTarget.add("3");
List result = (List) c.execute(bindTarget);
assertEquals(null, result.get(0));
assertEquals(new Integer(1), result.get(1));
assertEquals(new Integer(2), result.get(2));
assertEquals(null, result.get(3));
assertEquals(new Integer(3), result.get(4));
Collection result = (Collection) c.execute(bindTarget);
Iterator it = result.iterator();
assertEquals(null, it.next());
assertEquals(new Integer(1), it.next());
assertEquals(new Integer(2), it.next());
assertEquals(new Integer(3), it.next());
}
@Test
public void testCollectionToCollectionConversionNoGenericInfoSourceEmpty() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
CollectionToCollection c = new CollectionToCollection(TypeDescriptor.valueOf(Collection.class),
new TypeDescriptor(getClass().getField("integerTarget")), service);
List result = (List) c.execute(bindTarget);
Collection result = (Collection) c.execute(bindTarget);
assertTrue(result.isEmpty());
}
@@ -87,5 +106,4 @@ public class CollectionToCollectionTests {
public Collection<String> bindTarget = new ArrayList<String>();
public List<Integer> integerTarget = new ArrayList<Integer>();
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2002-2009 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.
* 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.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
@@ -27,8 +43,10 @@ import org.springframework.core.convert.support.StringToShort;
/**
* Tests for the default converters in the converters package.
s */
public class DefaultTypeConverterTests {
* @author Keith Donald
*/
public class DefaultConversionServiceTests {
@Test
public void testStringToByte() throws Exception {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert.support;
import static junit.framework.Assert.assertEquals;
@@ -32,9 +33,12 @@ import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
public class GenericTypeConverterTests {
/**
* @author Keith Donald
*/
public class GenericConversionServiceTests {
private GenericTypeConverter converter = new GenericTypeConverter();
private GenericConversionService converter = new GenericConversionService();
@Test
public void executeConversion() {
@@ -151,7 +155,7 @@ public class GenericTypeConverterTests {
@Test
public void convertArrayToListGenericTypeConversion() throws Exception {
converter.add(new StringToInteger());
List<Integer> result = converter.convert(new String[] { "1", "2", "3" }, new TypeDescriptor<List<Integer>>(getClass().getDeclaredField("genericList")));
List<Integer> result = (List<Integer>) converter.convert(new String[] { "1", "2", "3" }, new TypeDescriptor(getClass().getDeclaredField("genericList")));
assertEquals(new Integer("1"), result.get(0));
assertEquals(new Integer("2"), result.get(1));
assertEquals(new Integer("3"), result.get(2));
@@ -208,7 +212,7 @@ public class GenericTypeConverterTests {
foo.put("2", "BAZ");
converter.add(new StringToInteger());
converter.add(new StringToEnumFactory().getConverter(FooEnum.class));
Map<String, FooEnum> map = converter.convert(foo, new TypeDescriptor<Map<String, FooEnum>>(getClass().getField("genericMap")));
Map<String, FooEnum> map = (Map<String, FooEnum>) converter.convert(foo, new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(map.get(1), FooEnum.BAR);
assertEquals(map.get(2), FooEnum.BAZ);
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2002-2009 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.
* 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.springframework.core.convert.support;
import static org.junit.Assert.assertEquals;
@@ -7,16 +23,19 @@ import java.util.Map;
import org.junit.Test;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultTypeConverter;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.MapToMap;
/**
* @author Keith Donald
*/
public class MapToMapTests {
@Test
public void testMapToMapConversion() throws Exception {
DefaultTypeConverter converter = new DefaultTypeConverter();
MapToMap c = new MapToMap(new TypeDescriptor<Map<String, String>>(getClass().getField("source")),
new TypeDescriptor<Map<String, FooEnum>>(getClass().getField("bindTarget")), converter);
DefaultConversionService converter = new DefaultConversionService();
MapToMap c = new MapToMap(new TypeDescriptor(getClass().getField("source")),
new TypeDescriptor(getClass().getField("bindTarget")), converter);
source.put("1", "BAR");
source.put("2", "BAZ");
Map<String, FooEnum> result = (Map<String, FooEnum>) c.execute(source);
@@ -26,7 +45,7 @@ public class MapToMapTests {
@Test
public void testMapToMapConversionNoGenericInfoOnSource() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class),
new TypeDescriptor(getClass().getField("bindTarget")), service);
source.put("1", "BAR");
@@ -38,7 +57,7 @@ public class MapToMapTests {
@Test
public void testMapToMapConversionNoGenericInfo() throws Exception {
DefaultTypeConverter service = new DefaultTypeConverter();
DefaultConversionService service = new DefaultConversionService();
MapToMap c = new MapToMap(TypeDescriptor.valueOf(Map.class),
TypeDescriptor.valueOf(Map.class), service);
source.put("1", "BAR");

View File

@@ -1,62 +0,0 @@
package org.springframework.util;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import junit.framework.TestCase;
/**
* Unit tests for {@link org.springframework.binding.collection.SharedMapDecorator}.
*/
public class SharedMapDecoratorTests extends TestCase {
private SharedMapDecorator map = new SharedMapDecorator(new HashMap());
public void testGetPutRemove() {
assertTrue(map.size() == 0);
assertTrue(map.isEmpty());
assertNull(map.get("foo"));
assertFalse(map.containsKey("foo"));
map.put("foo", "bar");
assertTrue(map.size() == 1);
assertFalse(map.isEmpty());
assertNotNull(map.get("foo"));
assertTrue(map.containsKey("foo"));
assertTrue(map.containsValue("bar"));
assertEquals("bar", map.get("foo"));
map.remove("foo");
assertTrue(map.size() == 0);
assertNull(map.get("foo"));
}
public void testPutAll() {
Map all = new HashMap();
all.put("foo", "bar");
all.put("bar", "baz");
map.putAll(all);
assertTrue(map.size() == 2);
}
public void testEntrySet() {
map.put("foo", "bar");
map.put("bar", "baz");
Set entrySet = map.entrySet();
assertTrue(entrySet.size() == 2);
}
public void testKeySet() {
map.put("foo", "bar");
map.put("bar", "baz");
Set keySet = map.keySet();
assertTrue(keySet.size() == 2);
}
public void testValues() {
map.put("foo", "bar");
map.put("bar", "baz");
Collection values = map.values();
assertTrue(values.size() == 2);
}
}