Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
576b8fec
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
576b8fec
编写于
7月 19, 2011
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ConvertiblePair implements equals and hashCode (SPR-8459)
上级
69c9abf2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
102 addition
and
66 deletion
+102
-66
org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java
...ingframework/core/convert/converter/GenericConverter.java
+22
-4
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java
...convert/support/CollectionToCollectionConverterTests.java
+46
-6
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
...k/core/convert/support/GenericConversionServiceTests.java
+34
-8
org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java
...rg/springframework/core/convert/support/Spr7728Tests.java
+0
-48
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/converter/GenericConverter.java
浏览文件 @
576b8fec
/*
* Copyright 2002-20
09
the original author or authors.
* Copyright 2002-20
11
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
();
}
}
}
org.springframework.core/src/test/java/org/springframework/core/convert/support/CollectionToCollectionConverterTests.java
浏览文件 @
576b8fec
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
());
}
}
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java
浏览文件 @
576b8fec
...
...
@@ -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
());
}
}
org.springframework.core/src/test/java/org/springframework/core/convert/support/Spr7728Tests.java
已删除
100644 → 0
浏览文件 @
69c9abf2
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录