Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
d9f5a7a6
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,发现更多精彩内容 >>
提交
d9f5a7a6
编写于
3月 09, 2009
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
custom converters
上级
0ef0ff60
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
480 addition
and
23 deletion
+480
-23
org.springframework.core/src/main/java/org/springframework/core/convert/service/GenericConversionService.java
...mework/core/convert/service/GenericConversionService.java
+203
-18
org.springframework.core/src/main/java/org/springframework/core/convert/service/SuperTwoWayConverterConverter.java
...k/core/convert/service/SuperTwoWayConverterConverter.java
+7
-4
org.springframework.core/src/test/java/org/springframework/core/convert/service/GenericConversionServiceTests.java
...k/core/convert/service/GenericConversionServiceTests.java
+270
-1
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/service/GenericConversionService.java
浏览文件 @
d9f5a7a6
/*
* Copyright 2004-200
9
the original author or authors.
* Copyright 2004-200
8
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.
...
...
@@ -40,7 +40,7 @@ import org.springframework.util.Assert;
/**
* Base implementation of a conversion service. Initially empty, e.g. no converters are registered by default.
*
* TODO
custom converters TODO handle case where no Converter/SuperConverter generic param info is available
* TODO
auto-conversion of generic collection elements
*
* @author Keith Donald
*/
...
...
@@ -54,11 +54,17 @@ public class GenericConversionService implements ConversionService {
private
final
Map
sourceClassConverters
=
new
HashMap
();
/**
* An indexed map of
Converters. Each Map.Entry key is a source class (S) that can be converted from. Each Map.Entry
*
value is a Map that defines the targetClass-to-
Converter mappings for that source.
* An indexed map of
SuperConverters. Each Map.Entry key is a source class (S) that can be converted from. Each
*
Map.Entry value is a Map that defines the targetClass-to-Super
Converter mappings for that source.
*/
private
final
Map
sourceClassSuperConverters
=
new
HashMap
();
/**
* A map of custom converters. Custom converters are assigned a unique identifier that can be used to lookup the
* converter. This allows multiple converters for the same source->target class to be registered.
*/
private
final
Map
customConverters
=
new
HashMap
();
/**
* Indexes classes by well-known aliases.
*/
...
...
@@ -88,7 +94,7 @@ public class GenericConversionService implements ConversionService {
* @param converter the converter to register
*/
public
void
addConverter
(
Converter
converter
)
{
List
typeInfo
=
getTypeInfo
(
converter
);
List
typeInfo
=
get
Required
TypeInfo
(
converter
);
Class
sourceClass
=
(
Class
)
typeInfo
.
get
(
0
);
Class
targetClass
=
(
Class
)
typeInfo
.
get
(
1
);
// index forward
...
...
@@ -104,7 +110,7 @@ public class GenericConversionService implements ConversionService {
* @param converter the super converter to register
*/
public
void
addConverter
(
SuperConverter
converter
)
{
List
typeInfo
=
getTypeInfo
(
converter
);
List
typeInfo
=
get
Required
TypeInfo
(
converter
);
Class
sourceClass
=
(
Class
)
typeInfo
.
get
(
0
);
Class
targetClass
=
(
Class
)
typeInfo
.
get
(
1
);
// index forward
...
...
@@ -117,6 +123,31 @@ public class GenericConversionService implements ConversionService {
}
}
/**
* Register the converter as a custom converter with this conversion service.
* @param id the id to assign the converter
* @param converter the converter to use a custom converter
*/
public
void
addConverter
(
String
id
,
Converter
converter
)
{
customConverters
.
put
(
id
,
converter
);
}
/**
* Adapts a {@link SuperTwoWayConverter} that converts between BS and BT class hierarchies to a {@link Converter}
* that converts between the specific BS/BT sub types S and T.
*
* TODO - I think this is going to force indexing on a getSourceClass/getTargetclass prop instead generic args
*
* @param sourceClass the source class S to convert from, which must be equal or extend BS
* @param targetClass the target type T to convert to, which must equal or extend BT
* @param converter the super two way converter
* @return a converter that converts from S to T by delegating to the super converter
*/
public
static
<
S
,
T
>
Converter
<
S
,
T
>
converterFor
(
Class
<
S
>
sourceClass
,
Class
<
T
>
targetClass
,
SuperTwoWayConverter
converter
)
{
return
new
SuperTwoWayConverterConverter
<
S
,
T
>(
converter
,
sourceClass
,
targetClass
);
}
/**
* Add a convenient alias for the target type. {@link #getClassForAlias(String)} can then be used to lookup the type
* given the alias.
...
...
@@ -126,16 +157,16 @@ public class GenericConversionService implements ConversionService {
aliasMap
.
put
(
alias
,
targetType
);
}
// implementing ConversionService
public
Object
executeConversion
(
Object
source
,
Class
targetClass
)
throws
ConversionExecutorNotFoundException
,
ConversionException
{
ConversionExecutor
conversionExecutor
=
getConversionExecutor
(
source
.
getClass
(),
targetClass
);
return
conversionExecutor
.
execute
(
source
);
return
getConversionExecutor
(
source
.
getClass
(),
targetClass
).
execute
(
source
);
}
public
Object
executeConversion
(
String
converterId
,
Object
source
,
Class
targetClass
)
throws
ConversionExecutorNotFoundException
,
ConversionException
{
ConversionExecutor
conversionExecutor
=
getConversionExecutor
(
converterId
,
source
.
getClass
(),
targetClass
);
return
conversionExecutor
.
execute
(
source
);
return
getConversionExecutor
(
converterId
,
source
.
getClass
(),
targetClass
).
execute
(
source
);
}
public
ConversionExecutor
getConversionExecutor
(
Class
sourceClass
,
Class
targetClass
)
...
...
@@ -188,9 +219,156 @@ public class GenericConversionService implements ConversionService {
}
}
public
ConversionExecutor
getConversionExecutor
(
String
converterI
d
,
Class
sourceClass
,
Class
targetClass
)
public
ConversionExecutor
getConversionExecutor
(
String
i
d
,
Class
sourceClass
,
Class
targetClass
)
throws
ConversionExecutorNotFoundException
{
throw
new
UnsupportedOperationException
(
"Not yet implemented"
);
Assert
.
hasText
(
id
,
"The id of the custom converter is required"
);
Assert
.
notNull
(
sourceClass
,
"The source class to convert from is required"
);
Assert
.
notNull
(
targetClass
,
"The target class to convert to is required"
);
Converter
converter
=
(
Converter
)
customConverters
.
get
(
id
);
if
(
converter
==
null
)
{
if
(
parent
!=
null
)
{
return
parent
.
getConversionExecutor
(
id
,
sourceClass
,
targetClass
);
}
else
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"No custom Converter found with id '"
+
id
+
"' for converting from sourceClass ["
+
sourceClass
.
getName
()
+
"] to targetClass ["
+
targetClass
.
getName
()
+
"]"
);
}
}
sourceClass
=
convertToWrapperClassIfNecessary
(
sourceClass
);
targetClass
=
convertToWrapperClassIfNecessary
(
targetClass
);
// TODO Not optimal getting this each time
List
typeInfo
=
getRequiredTypeInfo
(
converter
);
Class
converterSourceClass
=
(
Class
)
typeInfo
.
get
(
0
);
Class
converterTargetClass
=
(
Class
)
typeInfo
.
get
(
1
);
if
(
sourceClass
.
isArray
())
{
Class
sourceComponentType
=
sourceClass
.
getComponentType
();
if
(
targetClass
.
isArray
())
{
Class
targetComponentType
=
targetClass
.
getComponentType
();
if
(
converterSourceClass
.
isAssignableFrom
(
sourceComponentType
))
{
if
(!
converterTargetClass
.
equals
(
targetComponentType
))
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from an array storing elements of type ["
+
sourceComponentType
.
getName
()
+
"] to an array of storing elements of type ["
+
targetComponentType
.
getName
()
+
"]"
);
}
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
sourceComponentType
,
targetComponentType
,
converter
);
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
new
ArrayToArray
(
elementConverter
));
}
else
if
(
converterTargetClass
.
isAssignableFrom
(
sourceComponentType
))
{
if
(!
converterSourceClass
.
equals
(
targetComponentType
))
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from an array storing elements of type ["
+
sourceComponentType
.
getName
()
+
"] to an array of storing elements of type ["
+
targetComponentType
.
getName
()
+
"]"
);
}
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
sourceComponentType
,
targetComponentType
,
new
ReverseConverter
(
converter
));
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
new
ArrayToArray
(
elementConverter
));
}
else
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from an array storing elements of type ["
+
sourceComponentType
.
getName
()
+
"] to an array storing elements of type ["
+
targetComponentType
.
getName
()
+
"]"
);
}
}
else
if
(
Collection
.
class
.
isAssignableFrom
(
targetClass
))
{
if
(!
targetClass
.
isInterface
()
&&
Modifier
.
isAbstract
(
targetClass
.
getModifiers
()))
{
throw
new
IllegalArgumentException
(
"Conversion target class ["
+
targetClass
.
getName
()
+
"] is invalid; cannot convert to abstract collection types--"
+
"request an interface or concrete implementation instead"
);
}
if
(
converterSourceClass
.
isAssignableFrom
(
sourceComponentType
))
{
// type erasure has prevented us from getting the concrete type, this is best we can do for now
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
sourceComponentType
,
converterTargetClass
,
converter
);
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
new
ArrayToCollection
(
elementConverter
));
}
else
if
(
converterTargetClass
.
isAssignableFrom
(
sourceComponentType
))
{
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
sourceComponentType
,
converterSourceClass
,
new
ReverseConverter
(
converter
));
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
new
ArrayToCollection
(
elementConverter
));
}
else
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from array an storing elements type ["
+
sourceComponentType
.
getName
()
+
"] to a collection of type ["
+
targetClass
.
getName
()
+
"]"
);
}
}
}
if
(
targetClass
.
isArray
())
{
Class
targetComponentType
=
targetClass
.
getComponentType
();
if
(
Collection
.
class
.
isAssignableFrom
(
sourceClass
))
{
// type erasure limits us here as well
if
(
converterTargetClass
.
equals
(
targetComponentType
))
{
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
converterSourceClass
,
targetComponentType
,
converter
);
SuperConverter
collectionToArray
=
new
ReverseSuperConverter
(
new
ArrayToCollection
(
elementConverter
));
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
collectionToArray
);
}
else
if
(
converterSourceClass
.
equals
(
targetComponentType
))
{
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
converterTargetClass
,
targetComponentType
,
new
ReverseConverter
(
converter
));
SuperConverter
collectionToArray
=
new
ReverseSuperConverter
(
new
ArrayToCollection
(
elementConverter
));
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
collectionToArray
);
}
else
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from collection of type ["
+
sourceClass
.
getName
()
+
"] to an array storing elements of type ["
+
targetComponentType
.
getName
()
+
"]"
);
}
}
else
{
if
(
converterSourceClass
.
isAssignableFrom
(
sourceClass
))
{
if
(!
converterTargetClass
.
equals
(
targetComponentType
))
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from sourceClass ["
+
sourceClass
.
getName
()
+
"] to array holding elements of type ["
+
targetComponentType
.
getName
()
+
"]"
);
}
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
sourceClass
,
targetComponentType
,
converter
);
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
new
ObjectToArray
(
elementConverter
));
}
else
if
(
converterTargetClass
.
isAssignableFrom
(
sourceClass
))
{
if
(!
converterSourceClass
.
equals
(
targetComponentType
))
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from sourceClass ["
+
sourceClass
.
getName
()
+
"] to array holding elements of type ["
+
targetComponentType
.
getName
()
+
"]"
);
}
ConversionExecutor
elementConverter
=
new
StaticConversionExecutor
(
sourceClass
,
targetComponentType
,
new
ReverseConverter
(
converter
));
return
new
StaticSuperConversionExecutor
(
sourceClass
,
targetClass
,
new
ObjectToArray
(
elementConverter
));
}
}
}
// TODO look to factor some of this duplicated code here and above out a bit
if
(
converterSourceClass
.
isAssignableFrom
(
sourceClass
))
{
if
(!
converterTargetClass
.
equals
(
targetClass
))
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from sourceClass ["
+
sourceClass
.
getName
()
+
"] to targetClass ["
+
targetClass
.
getName
()
+
"]"
);
}
return
new
StaticConversionExecutor
(
sourceClass
,
targetClass
,
converter
);
}
else
if
(
converterTargetClass
.
isAssignableFrom
(
sourceClass
))
{
if
(!
converterSourceClass
.
equals
(
targetClass
))
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from sourceClass ["
+
sourceClass
.
getName
()
+
"] to targetClass ["
+
targetClass
.
getName
()
+
"]"
);
}
return
new
StaticConversionExecutor
(
sourceClass
,
targetClass
,
new
ReverseConverter
(
converter
));
}
else
{
throw
new
ConversionExecutorNotFoundException
(
sourceClass
,
targetClass
,
"Custom Converter with id '"
+
id
+
"' cannot convert from sourceClass ["
+
sourceClass
.
getName
()
+
"] to targetClass ["
+
targetClass
.
getName
()
+
"]"
);
}
}
public
Class
getClassForAlias
(
String
name
)
throws
IllegalArgumentException
{
...
...
@@ -208,25 +386,32 @@ public class GenericConversionService implements ConversionService {
// internal helpers
private
List
getTypeInfo
(
Object
converter
)
{
private
List
get
Required
TypeInfo
(
Object
converter
)
{
List
typeInfo
=
new
ArrayList
(
2
);
Class
classToIntrospect
=
converter
.
getClass
();
while
(
classToIntrospect
!=
null
)
{
Type
[]
genericInterfaces
=
c
onverter
.
getClass
()
.
getGenericInterfaces
();
Type
[]
genericInterfaces
=
c
lassToIntrospect
.
getGenericInterfaces
();
for
(
Type
genericInterface
:
genericInterfaces
)
{
if
(
genericInterface
instanceof
ParameterizedType
)
{
ParameterizedType
parameterizedInterface
=
(
ParameterizedType
)
genericInterface
;
if
(
Converter
.
class
.
equals
(
parameterizedInterface
.
getRawType
())
||
SuperConverter
.
class
.
isAssignableFrom
((
Class
)
parameterizedInterface
.
getRawType
()))
{
Type
s
=
parameterizedInterface
.
getActualTypeArguments
()[
0
];
Type
t
=
parameterizedInterface
.
getActualTypeArguments
()[
1
];
Class
s
=
getParameterClass
(
parameterizedInterface
.
getActualTypeArguments
()[
0
],
converter
.
getClass
());
Class
t
=
getParameterClass
(
parameterizedInterface
.
getActualTypeArguments
()[
1
],
converter
.
getClass
());
typeInfo
.
add
(
getParameterClass
(
s
,
converter
.
getClass
()));
typeInfo
.
add
(
getParameterClass
(
t
,
converter
.
getClass
()));
break
;
}
}
}
classToIntrospect
=
classToIntrospect
.
getSuperclass
();
}
if
(
typeInfo
.
size
()
!=
2
)
{
throw
new
IllegalArgumentException
(
"Unable to extract source and target class arguments from Converter ["
+
converter
.
getClass
().
getName
()
+
"]; does the Converter specify the <S, T> generic types?"
);
}
return
typeInfo
;
}
...
...
@@ -237,8 +422,8 @@ public class GenericConversionService implements ConversionService {
if
(
parameterType
instanceof
Class
)
{
return
(
Class
)
parameterType
;
}
// when would this happen?
return
null
;
throw
new
IllegalArgumentException
(
"Unable to obtain the java.lang.Class for parameterType ["
+
parameterType
+
"] on Converter ["
+
converterClass
.
getName
()
+
"]"
)
;
}
private
Map
getSourceMap
(
Class
sourceClass
)
{
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/
converter
/SuperTwoWayConverterConverter.java
→
org.springframework.core/src/main/java/org/springframework/core/convert/
service
/SuperTwoWayConverterConverter.java
浏览文件 @
d9f5a7a6
package
org.springframework.core.convert.converter
;
package
org.springframework.core.convert.service
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.core.convert.converter.SuperConverter
;
import
org.springframework.core.convert.converter.SuperTwoWayConverter
;
/**
* Adapts a {@link SuperTwoWayConverter} to the {@link Converter} interface in a type safe way. This adapter is useful
* for applying more general {@link SuperConverter} logic to a specific source/target class pair.
*/
@SuppressWarnings
(
"unchecked"
)
public
class
SuperTwoWayConverterConverter
<
S
,
T
>
implements
Converter
<
S
,
T
>
{
class
SuperTwoWayConverterConverter
<
S
,
T
>
implements
Converter
<
S
,
T
>
{
private
SuperTwoWayConverter
superConverter
;
...
...
@@ -13,8 +17,7 @@ public class SuperTwoWayConverterConverter<S, T> implements Converter<S, T> {
private
Class
targetClass
;
public
<
SCS
,
SCT
>
SuperTwoWayConverterConverter
(
SuperTwoWayConverter
<
SCS
,
SCT
>
superConverter
,
Class
<?
extends
SCS
>
sourceClass
,
Class
<?
extends
SCT
>
targetClass
)
{
public
SuperTwoWayConverterConverter
(
SuperTwoWayConverter
superConverter
,
Class
sourceClass
,
Class
targetClass
)
{
this
.
superConverter
=
superConverter
;
this
.
sourceClass
=
sourceClass
;
this
.
targetClass
=
targetClass
;
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/service/GenericConversionServiceTests.java
浏览文件 @
d9f5a7a6
/*
* Copyright 2004-2008 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.service
;
import
java.security.Principal
;
import
java.util.AbstractList
;
import
java.util.ArrayList
;
import
java.util.Collection
;
...
...
@@ -8,6 +24,7 @@ import java.util.List;
import
junit.framework.TestCase
;
import
org.springframework.core.convert.ConversionException
;
import
org.springframework.core.convert.ConversionExecutionException
;
import
org.springframework.core.convert.ConversionExecutor
;
import
org.springframework.core.convert.ConversionExecutorNotFoundException
;
...
...
@@ -15,7 +32,6 @@ import org.springframework.core.convert.converter.Converter;
import
org.springframework.core.convert.converter.NumberToNumber
;
import
org.springframework.core.convert.converter.StringToEnum
;
import
org.springframework.core.convert.converter.StringToInteger
;
import
org.springframework.core.convert.service.GenericConversionService
;
public
class
GenericConversionServiceTests
extends
TestCase
{
...
...
@@ -250,4 +266,257 @@ public class GenericConversionServiceTests extends TestCase {
}
}
public
void
testAddConverterNoSourceTargetClassInfoAvailable
()
{
try
{
service
.
addConverter
(
new
Converter
()
{
public
Object
convert
(
Object
source
)
throws
Exception
{
return
source
;
}
public
Object
convertBack
(
Object
target
)
throws
Exception
{
return
target
;
}
});
fail
(
"Should have failed"
);
}
catch
(
IllegalArgumentException
e
)
{
}
}
public
void
testCustomConverterConversionForwardIndex
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
String
.
class
,
Principal
.
class
);
assertEquals
(
"keith"
,
((
Principal
)
executor
.
execute
(
"keith"
)).
getName
());
}
public
void
testCustomConverterConversionReverseIndex
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
Principal
.
class
,
String
.
class
);
assertEquals
(
"keith"
,
executor
.
execute
(
new
Principal
()
{
public
String
getName
()
{
return
"keith"
;
}
}));
}
public
void
testCustomConverterConversionForSameType
()
{
service
.
addConverter
(
"trimmer"
,
new
Trimmer
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"trimmer"
,
String
.
class
,
String
.
class
);
assertEquals
(
"a string"
,
executor
.
execute
(
"a string "
));
}
public
void
testCustomConverterLookupNotCompatibleSource
()
{
service
.
addConverter
(
"trimmer"
,
new
Trimmer
());
try
{
service
.
getConversionExecutor
(
"trimmer"
,
Object
.
class
,
String
.
class
);
fail
(
"Should have failed"
);
}
catch
(
ConversionException
e
)
{
}
}
public
void
testCustomConverterLookupNotCompatibleTarget
()
{
service
.
addConverter
(
"trimmer"
,
new
Trimmer
());
try
{
service
.
getConversionExecutor
(
"trimmer"
,
String
.
class
,
Object
.
class
);
}
catch
(
ConversionException
e
)
{
}
}
public
void
testCustomConverterLookupNotCompatibleTargetReverse
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
try
{
service
.
getConversionExecutor
(
"princy"
,
Principal
.
class
,
Integer
.
class
);
}
catch
(
ConversionException
e
)
{
}
}
public
void
testCustomConverterConversionArrayToArray
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
String
[].
class
,
Principal
[].
class
);
Principal
[]
p
=
(
Principal
[])
executor
.
execute
(
new
String
[]
{
"princy1"
,
"princy2"
});
assertEquals
(
"princy1"
,
p
[
0
].
getName
());
assertEquals
(
"princy2"
,
p
[
1
].
getName
());
}
public
void
testCustomConverterConversionArrayToArrayReverse
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
Principal
[].
class
,
String
[].
class
);
final
Principal
princy1
=
new
Principal
()
{
public
String
getName
()
{
return
"princy1"
;
}
};
final
Principal
princy2
=
new
Principal
()
{
public
String
getName
()
{
return
"princy2"
;
}
};
String
[]
p
=
(
String
[])
executor
.
execute
(
new
Principal
[]
{
princy1
,
princy2
});
assertEquals
(
"princy1"
,
p
[
0
]);
assertEquals
(
"princy2"
,
p
[
1
]);
}
public
void
testCustomConverterLookupArrayToArrayBogusSource
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
try
{
service
.
getConversionExecutor
(
"princy"
,
Integer
[].
class
,
Principal
[].
class
);
fail
(
"Should have failed"
);
}
catch
(
ConversionExecutorNotFoundException
e
)
{
}
}
public
void
testCustomConverterLookupArrayToArrayBogusTarget
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
try
{
service
.
getConversionExecutor
(
"princy"
,
Principal
[].
class
,
Integer
[].
class
);
}
catch
(
ConversionExecutorNotFoundException
e
)
{
}
}
public
void
testCustomConverterConversionArrayToCollection
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
String
[].
class
,
List
.
class
);
List
list
=
(
List
)
executor
.
execute
(
new
String
[]
{
"princy1"
,
"princy2"
});
assertEquals
(
"princy1"
,
((
Principal
)
list
.
get
(
0
)).
getName
());
assertEquals
(
"princy2"
,
((
Principal
)
list
.
get
(
1
)).
getName
());
}
public
void
testCustomConverterConversionArrayToCollectionReverse
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
Principal
[].
class
,
List
.
class
);
final
Principal
princy1
=
new
Principal
()
{
public
String
getName
()
{
return
"princy1"
;
}
};
final
Principal
princy2
=
new
Principal
()
{
public
String
getName
()
{
return
"princy2"
;
}
};
List
p
=
(
List
)
executor
.
execute
(
new
Principal
[]
{
princy1
,
princy2
});
assertEquals
(
"princy1"
,
p
.
get
(
0
));
assertEquals
(
"princy2"
,
p
.
get
(
1
));
}
public
void
testCustomConverterLookupArrayToCollectionBogusSource
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
try
{
service
.
getConversionExecutor
(
"princy"
,
Integer
[].
class
,
List
.
class
);
fail
(
"Should have failed"
);
}
catch
(
ConversionExecutorNotFoundException
e
)
{
}
}
public
void
testCustomConverterLookupCollectionToArray
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
List
.
class
,
Principal
[].
class
);
List
princyList
=
new
ArrayList
();
princyList
.
add
(
"princy1"
);
princyList
.
add
(
"princy2"
);
Principal
[]
p
=
(
Principal
[])
executor
.
execute
(
princyList
);
assertEquals
(
"princy1"
,
p
[
0
].
getName
());
assertEquals
(
"princy2"
,
p
[
1
].
getName
());
}
public
void
testCustomConverterLookupCollectionToArrayReverse
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
List
.
class
,
String
[].
class
);
final
Principal
princy1
=
new
Principal
()
{
public
String
getName
()
{
return
"princy1"
;
}
};
final
Principal
princy2
=
new
Principal
()
{
public
String
getName
()
{
return
"princy2"
;
}
};
List
princyList
=
new
ArrayList
();
princyList
.
add
(
princy1
);
princyList
.
add
(
princy2
);
String
[]
p
=
(
String
[])
executor
.
execute
(
princyList
);
assertEquals
(
"princy1"
,
p
[
0
]);
assertEquals
(
"princy2"
,
p
[
1
]);
}
public
void
testtestCustomConverterLookupCollectionToArrayBogusTarget
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
try
{
service
.
getConversionExecutor
(
"princy"
,
List
.
class
,
Integer
[].
class
);
fail
(
"Should have failed"
);
}
catch
(
ConversionExecutorNotFoundException
e
)
{
}
}
public
void
testCustomConverterConversionObjectToArray
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
String
.
class
,
Principal
[].
class
);
Principal
[]
p
=
(
Principal
[])
executor
.
execute
(
"princy1"
);
assertEquals
(
"princy1"
,
p
[
0
].
getName
());
}
public
void
testCustomConverterConversionObjectToArrayReverse
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
ConversionExecutor
executor
=
service
.
getConversionExecutor
(
"princy"
,
Principal
.
class
,
String
[].
class
);
final
Principal
princy1
=
new
Principal
()
{
public
String
getName
()
{
return
"princy1"
;
}
};
String
[]
p
=
(
String
[])
executor
.
execute
(
princy1
);
assertEquals
(
"princy1"
,
p
[
0
]);
}
public
void
testCustomConverterLookupObjectToArrayBogusSource
()
{
service
.
addConverter
(
"princy"
,
new
CustomTwoWayConverter
());
try
{
service
.
getConversionExecutor
(
"princy"
,
Integer
.
class
,
Principal
[].
class
);
fail
(
"Should have failed"
);
}
catch
(
ConversionExecutorNotFoundException
e
)
{
}
}
private
static
class
CustomTwoWayConverter
implements
Converter
<
String
,
Principal
>
{
public
Principal
convert
(
final
String
source
)
throws
Exception
{
return
new
Principal
()
{
public
String
getName
()
{
return
(
String
)
source
;
}
};
}
public
String
convertBack
(
Principal
target
)
throws
Exception
{
return
((
Principal
)
target
).
getName
();
}
}
private
static
class
Trimmer
implements
Converter
<
String
,
String
>
{
public
String
convert
(
String
source
)
throws
Exception
{
return
((
String
)
source
).
trim
();
}
public
String
convertBack
(
String
target
)
throws
Exception
{
throw
new
UnsupportedOperationException
(
"Will never run"
);
}
}
public
void
testSuperTwoWayConverterConverterAdaption
()
{
// this fails at the moment
//service.addConverter(GenericConversionService.converterFor(String.class, FooEnum.class, new StringToEnum()));
//assertEquals(FooEnum.BAR, service.executeConversion("BAR", FooEnum.class));
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录