diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java index 52da8c3ca0..84c93c8b99 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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,11 +16,11 @@ package org.springframework.core.convert.converter; -import java.util.Set; - import org.springframework.core.convert.TypeDescriptor; import org.springframework.util.Assert; +import java.util.Set; + /** * Generic converter interface for converting between two or more types. * @@ -87,6 +87,24 @@ public interface GenericConverter { public Class getTargetType() { return this.targetType; } - } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || obj.getClass() != ConvertiblePair.class) { + return false; + } + ConvertiblePair other = (ConvertiblePair) obj; + return this.sourceType.equals(other.sourceType) && this.targetType.equals(other.targetType); + + } + + @Override + public int hashCode() { + return this.sourceType.hashCode() * 31 + this.targetType.hashCode(); + } + } } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java index 3bdddb92d5..aad03be80f 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java @@ -1,8 +1,20 @@ -package org.springframework.core.convert.support; +/* + * Copyright 2002-2011 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.assertFalse; -import static org.junit.Assert.assertTrue; +package org.springframework.core.convert.support; import java.io.File; import java.io.IOException; @@ -11,11 +23,14 @@ import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.LinkedList; import java.util.List; +import java.util.Vector; import org.junit.Before; import org.junit.Test; + import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeDescriptor; @@ -23,6 +38,11 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; +import static org.junit.Assert.*; + +/** + * @author Keith Donald + */ public class CollectionToCollectionConverterTests { private GenericConversionService conversionService = new GenericConversionService(); @@ -258,11 +278,31 @@ public class CollectionToCollectionConverterTests { // TODO Auto-generated method stub return null; } - } public static class TestResource extends BaseResource { } -} + @Test + public void convertEmptyVector_shouldReturnEmptyArrayList() { + Vector vector = new Vector(); + vector.add("Element"); + testCollectionConversionToArrayList(vector); + } + + @Test + public void convertNonEmptyVector_shouldReturnNonEmptyArrayList() { + Vector vector = new Vector(); + vector.add("Element"); + testCollectionConversionToArrayList(vector); + } + + private void testCollectionConversionToArrayList(Collection aSource) { + Object myConverted = (new CollectionToCollectionConverter(new GenericConversionService())).convert( + aSource, TypeDescriptor.forObject(aSource), TypeDescriptor.forObject(new ArrayList())); + assertTrue(myConverted instanceof ArrayList); + assertEquals(aSource.size(), ((ArrayList) myConverted).size()); + } + +} diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 361224d553..eecfa3f010 100644 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -16,33 +16,37 @@ package org.springframework.core.convert.support; -import static junit.framework.Assert.assertTrue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; - import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import org.junit.Test; + import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.io.DescriptiveResource; import org.springframework.core.io.Resource; import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; +import static junit.framework.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.fail; + /** * @author Keith Donald * @author Juergen Hoeller @@ -549,5 +553,27 @@ public class GenericConversionServiceTests { public Collection stringToCollection; + @Test + public void testConvertiblePairsInSet() throws Exception { + Set set = new HashSet(); + set.add(new GenericConverter.ConvertiblePair(Number.class, String.class)); + assert set.contains(new GenericConverter.ConvertiblePair(Number.class, String.class)); + } + + @Test + public void testConvertiblePairEqualsAndHash() throws Exception { + GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); + GenericConverter.ConvertiblePair pairEqual = new GenericConverter.ConvertiblePair(Number.class, String.class); + assertEquals(pair, pairEqual); + assertEquals(pair.hashCode(), pairEqual.hashCode()); + } + + @Test + public void testConvertiblePairDifferentEqualsAndHash() throws Exception { + GenericConverter.ConvertiblePair pair = new GenericConverter.ConvertiblePair(Number.class, String.class); + GenericConverter.ConvertiblePair pairOpposite = new GenericConverter.ConvertiblePair(String.class, Number.class); + assertFalse(pair.equals(pairOpposite)); + assertFalse(pair.hashCode() == pairOpposite.hashCode()); + } } diff --git a/org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java b/org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java deleted file mode 100644 index ab1ea31e16..0000000000 --- a/org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.springframework.core.convert.support; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Vector; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.springframework.core.convert.TypeDescriptor; - -public class Spr7728Tests -{ - private CollectionToCollectionConverter theConverter; - private Vector theSrcVector; - private TypeDescriptor theTargetType; - - @Before - public void setup() - { - theSrcVector = new Vector(); - theTargetType = TypeDescriptor.forObject(new ArrayList()); - theConverter = new CollectionToCollectionConverter(new GenericConversionService()); - } - - @Test - public void convertEmptyVector_shouldReturnEmptyArrayList() - throws Exception - { - theSrcVector.add("Element"); - testCollectionConversionToArrayList(theSrcVector); - } - - @Test - public void convertNonEmptyVector_shouldReturnNonEmptyArrayList() - throws Exception - { - testCollectionConversionToArrayList(theSrcVector); - } - - private void testCollectionConversionToArrayList(Collection aSource) - { - Object myConverted = theConverter.convert(aSource, TypeDescriptor.forObject(aSource), theTargetType); - Assert.assertTrue(myConverted instanceof ArrayList); - Assert.assertEquals(aSource.size(), ((ArrayList) myConverted).size()); - } - -} \ No newline at end of file