提交 576b8fec 编写于 作者: J Juergen Hoeller

ConvertiblePair implements equals and hashCode (SPR-8459)

上级 69c9abf2
/*
* 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();
}
}
}
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<String> vector = new Vector<String>();
vector.add("Element");
testCollectionConversionToArrayList(vector);
}
@Test
public void convertNonEmptyVector_shouldReturnNonEmptyArrayList() {
Vector<String> vector = new Vector<String>();
vector.add("Element");
testCollectionConversionToArrayList(vector);
}
private void testCollectionConversionToArrayList(Collection<String> 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());
}
}
......@@ -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<Integer> stringToCollection;
@Test
public void testConvertiblePairsInSet() throws Exception {
Set<GenericConverter.ConvertiblePair> set = new HashSet<GenericConverter.ConvertiblePair>();
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());
}
}
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<String> theSrcVector;
private TypeDescriptor theTargetType;
@Before
public void setup()
{
theSrcVector = new Vector<String>();
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<String> 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册