Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
d966965a
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,发现更多精彩内容 >>
提交
d966965a
编写于
4月 08, 2009
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
default converter tests
上级
5e46fab3
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
220 addition
and
51 deletion
+220
-51
org.springframework.core/src/main/java/org/springframework/core/convert/converter/NumberToNumber.java
...pringframework/core/convert/converter/NumberToNumber.java
+1
-1
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java
...gframework/core/convert/converter/StringToBigDecimal.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java
...gframework/core/convert/converter/StringToBigInteger.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java
...ringframework/core/convert/converter/StringToBoolean.java
+15
-26
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java
.../springframework/core/convert/converter/StringToByte.java
+3
-3
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java
...ngframework/core/convert/converter/StringToCharacter.java
+3
-3
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java
...pringframework/core/convert/converter/StringToDouble.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToEnum.java
.../springframework/core/convert/converter/StringToEnum.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java
...springframework/core/convert/converter/StringToFloat.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java
...ringframework/core/convert/converter/StringToInteger.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java
...pringframework/core/convert/converter/StringToLocale.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java
.../springframework/core/convert/converter/StringToLong.java
+2
-2
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java
...springframework/core/convert/converter/StringToShort.java
+2
-2
org.springframework.core/src/test/java/org/springframework/core/convert/converters/DefaultConverterTests.java
...mework/core/convert/converters/DefaultConverterTests.java
+180
-0
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/converter/NumberToNumber.java
浏览文件 @
d966965a
...
...
@@ -37,7 +37,7 @@ import org.springframework.util.NumberUtils;
*/
public
class
NumberToNumber
implements
SuperConverter
<
Number
,
Number
>
{
public
<
RT
extends
Number
>
RT
convert
(
Number
source
,
Class
<
RT
>
targetClass
)
throws
Exception
{
public
<
RT
extends
Number
>
RT
convert
(
Number
source
,
Class
<
RT
>
targetClass
)
{
return
NumberUtils
.
convertNumberToTargetClass
(
source
,
targetClass
);
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java
浏览文件 @
d966965a
...
...
@@ -24,11 +24,11 @@ import java.math.BigDecimal;
*/
public
class
StringToBigDecimal
implements
Converter
<
String
,
BigDecimal
>
{
public
BigDecimal
convert
(
String
source
)
throws
Exception
{
public
BigDecimal
convert
(
String
source
)
{
return
new
BigDecimal
(
source
);
}
public
String
convertBack
(
BigDecimal
target
)
throws
Exception
{
public
String
convertBack
(
BigDecimal
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java
浏览文件 @
d966965a
...
...
@@ -24,11 +24,11 @@ import java.math.BigInteger;
*/
public
class
StringToBigInteger
implements
Converter
<
String
,
BigInteger
>
{
public
BigInteger
convert
(
String
source
)
throws
Exception
{
public
BigInteger
convert
(
String
source
)
{
return
new
BigInteger
(
source
);
}
public
String
convertBack
(
BigInteger
target
)
throws
Exception
{
public
String
convertBack
(
BigInteger
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java
浏览文件 @
d966965a
...
...
@@ -15,6 +15,8 @@
*/
package
org.springframework.core.convert.converter
;
import
org.springframework.util.Assert
;
/**
* Converts String to a Boolean. The trueString and falseStrings are configurable.
*
...
...
@@ -23,10 +25,6 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToBoolean
implements
Converter
<
String
,
Boolean
>
{
private
static
final
String
VALUE_TRUE
=
"true"
;
private
static
final
String
VALUE_FALSE
=
"false"
;
private
String
trueString
;
private
String
falseString
;
...
...
@@ -35,47 +33,38 @@ public class StringToBoolean implements Converter<String, Boolean> {
* Create a StringToBoolean converter with the default 'true' and 'false' strings.
*/
public
StringToBoolean
()
{
this
(
"true"
,
"false"
);
}
/**
* Create a StringToBoolean converter configured with specific values for true and false strings.
* @param trueString special true string to use
* @param falseString special false string to use
* @param trueString special true string to use
(required)
* @param falseString special false string to use
(required)
*/
public
StringToBoolean
(
String
trueString
,
String
falseString
)
{
Assert
.
hasText
(
trueString
,
"The true string is required"
);
Assert
.
hasText
(
falseString
,
"The false string is required"
);
this
.
trueString
=
trueString
;
this
.
falseString
=
falseString
;
}
public
Boolean
convert
(
String
source
)
throws
Exception
{
if
(
trueString
!=
null
&&
source
.
equals
(
trueString
))
{
return
Boolean
.
TRUE
;
}
else
if
(
falseString
!=
null
&&
source
.
equals
(
falseString
))
{
return
Boolean
.
FALSE
;
}
else
if
(
trueString
==
null
&&
source
.
equals
(
VALUE_TRUE
))
{
public
Boolean
convert
(
String
source
)
{
if
(
source
.
equals
(
trueString
))
{
return
Boolean
.
TRUE
;
}
else
if
(
falseString
==
null
&&
source
.
equals
(
VALUE_FALSE
))
{
}
else
if
(
source
.
equals
(
falseString
))
{
return
Boolean
.
FALSE
;
}
else
{
throw
new
IllegalArgumentException
(
"Invalid boolean
value ["
+
source
+
"]
"
);
throw
new
IllegalArgumentException
(
"Invalid boolean
string '"
+
source
+
"'; expected '"
+
trueString
+
"' or '"
+
falseString
+
"'
"
);
}
}
public
String
convertBack
(
Boolean
target
)
throws
Exception
{
public
String
convertBack
(
Boolean
target
)
{
if
(
Boolean
.
TRUE
.
equals
(
target
))
{
if
(
trueString
!=
null
)
{
return
trueString
;
}
else
{
return
VALUE_TRUE
;
}
return
trueString
;
}
else
if
(
Boolean
.
FALSE
.
equals
(
target
))
{
if
(
falseString
!=
null
)
{
return
falseString
;
}
else
{
return
VALUE_FALSE
;
}
return
falseString
;
}
else
{
throw
new
IllegalArgumentException
(
"Invalid boolean value
["
+
target
+
"]"
);
throw
new
IllegalArgumentException
(
"Invalid boolean value
"
+
target
);
}
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java
浏览文件 @
d966965a
...
...
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToByte
implements
Converter
<
String
,
Byte
>
{
public
Byte
convert
(
String
source
)
throws
Exception
{
return
new
Byte
(
source
);
public
Byte
convert
(
String
source
)
{
return
Byte
.
valueOf
(
source
);
}
public
String
convertBack
(
Byte
target
)
throws
Exception
{
public
String
convertBack
(
Byte
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java
浏览文件 @
d966965a
...
...
@@ -22,14 +22,14 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToCharacter
implements
Converter
<
String
,
Character
>
{
public
Character
convert
(
String
source
)
throws
Exception
{
public
Character
convert
(
String
source
)
{
if
(
source
.
length
()
!=
1
)
{
throw
new
IllegalArgumentException
(
"To be a Character the String '"
+
source
+
"' must have a length of 1"
);
}
return
new
Character
(
source
.
charAt
(
0
));
return
Character
.
valueOf
(
source
.
charAt
(
0
));
}
public
String
convertBack
(
Character
target
)
throws
Exception
{
public
String
convertBack
(
Character
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java
浏览文件 @
d966965a
...
...
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToDouble
implements
Converter
<
String
,
Double
>
{
public
Double
convert
(
String
source
)
throws
Exception
{
public
Double
convert
(
String
source
)
{
return
Double
.
valueOf
(
source
);
}
public
String
convertBack
(
Double
target
)
throws
Exception
{
public
String
convertBack
(
Double
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToEnum.java
浏览文件 @
d966965a
...
...
@@ -24,11 +24,11 @@ package org.springframework.core.convert.converter;
@SuppressWarnings
(
"unchecked"
)
public
class
StringToEnum
implements
SuperTwoWayConverter
<
String
,
Enum
>
{
public
<
RT
extends
Enum
>
RT
convert
(
String
source
,
Class
<
RT
>
targetClass
)
throws
Exception
{
public
<
RT
extends
Enum
>
RT
convert
(
String
source
,
Class
<
RT
>
targetClass
)
{
return
(
RT
)
Enum
.
valueOf
(
targetClass
,
source
);
}
public
<
RS
extends
String
>
RS
convertBack
(
Enum
target
,
Class
<
RS
>
sourceClass
)
throws
Exception
{
public
<
RS
extends
String
>
RS
convertBack
(
Enum
target
,
Class
<
RS
>
sourceClass
)
{
return
(
RS
)
target
.
name
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java
浏览文件 @
d966965a
...
...
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToFloat
implements
Converter
<
String
,
Float
>
{
public
Float
convert
(
String
source
)
throws
Exception
{
public
Float
convert
(
String
source
)
{
return
Float
.
valueOf
(
source
);
}
public
String
convertBack
(
Float
target
)
throws
Exception
{
public
String
convertBack
(
Float
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java
浏览文件 @
d966965a
...
...
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToInteger
implements
Converter
<
String
,
Integer
>
{
public
Integer
convert
(
String
source
)
throws
Exception
{
public
Integer
convert
(
String
source
)
{
return
Integer
.
valueOf
(
source
);
}
public
String
convertBack
(
Integer
target
)
throws
Exception
{
public
String
convertBack
(
Integer
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java
浏览文件 @
d966965a
...
...
@@ -26,11 +26,11 @@ import org.springframework.util.StringUtils;
*/
public
class
StringToLocale
implements
Converter
<
String
,
Locale
>
{
public
Locale
convert
(
String
source
)
throws
Exception
{
public
Locale
convert
(
String
source
)
{
return
StringUtils
.
parseLocaleString
(
source
);
}
public
String
convertBack
(
Locale
target
)
throws
Exception
{
public
String
convertBack
(
Locale
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java
浏览文件 @
d966965a
...
...
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToLong
implements
Converter
<
String
,
Long
>
{
public
Long
convert
(
String
source
)
throws
Exception
{
public
Long
convert
(
String
source
)
{
return
Long
.
valueOf
(
source
);
}
public
String
convertBack
(
Long
target
)
throws
Exception
{
public
String
convertBack
(
Long
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java
浏览文件 @
d966965a
...
...
@@ -22,11 +22,11 @@ package org.springframework.core.convert.converter;
*/
public
class
StringToShort
implements
Converter
<
String
,
Short
>
{
public
Short
convert
(
String
source
)
throws
Exception
{
public
Short
convert
(
String
source
)
{
return
Short
.
valueOf
(
source
);
}
public
String
convertBack
(
Short
target
)
throws
Exception
{
public
String
convertBack
(
Short
target
)
{
return
target
.
toString
();
}
...
...
org.springframework.core/src/test/java/org/springframework/core/convert/converters/DefaultConverterTests.java
0 → 100644
浏览文件 @
d966965a
package
org.springframework.core.convert.converters
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
fail
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.util.Locale
;
import
org.junit.Test
;
import
org.springframework.core.convert.converter.NumberToNumber
;
import
org.springframework.core.convert.converter.StringToBigDecimal
;
import
org.springframework.core.convert.converter.StringToBigInteger
;
import
org.springframework.core.convert.converter.StringToBoolean
;
import
org.springframework.core.convert.converter.StringToByte
;
import
org.springframework.core.convert.converter.StringToCharacter
;
import
org.springframework.core.convert.converter.StringToDouble
;
import
org.springframework.core.convert.converter.StringToEnum
;
import
org.springframework.core.convert.converter.StringToFloat
;
import
org.springframework.core.convert.converter.StringToInteger
;
import
org.springframework.core.convert.converter.StringToLocale
;
import
org.springframework.core.convert.converter.StringToLong
;
import
org.springframework.core.convert.converter.StringToShort
;
/**
* Tests for the default converters in the converters package.
s */
public
class
DefaultConverterTests
{
@Test
public
void
testStringToByte
()
throws
Exception
{
StringToByte
b
=
new
StringToByte
();
assertEquals
(
Byte
.
valueOf
(
"1"
),
b
.
convert
(
"1"
));
assertEquals
(
"1"
,
b
.
convertBack
(
Byte
.
valueOf
(
"1"
)));
}
@Test
public
void
testStringToCharacter
()
{
StringToCharacter
c
=
new
StringToCharacter
();
assertEquals
(
Character
.
valueOf
(
'1'
),
c
.
convert
(
"1"
));
assertEquals
(
"1"
,
c
.
convertBack
(
Character
.
valueOf
(
'1'
)));
}
@Test
public
void
testStringToBoolean
()
{
StringToBoolean
c
=
new
StringToBoolean
();
assertEquals
(
Boolean
.
valueOf
(
true
),
c
.
convert
(
"true"
));
assertEquals
(
Boolean
.
valueOf
(
false
),
c
.
convert
(
"false"
));
assertEquals
(
"true"
,
c
.
convertBack
(
Boolean
.
TRUE
));
assertEquals
(
"false"
,
c
.
convertBack
(
Boolean
.
FALSE
));
}
@Test
public
void
testStringToBooleanCustomString
()
{
StringToBoolean
c
=
new
StringToBoolean
(
"yes"
,
"no"
);
assertEquals
(
Boolean
.
valueOf
(
true
),
c
.
convert
(
"yes"
));
assertEquals
(
Boolean
.
valueOf
(
false
),
c
.
convert
(
"no"
));
assertEquals
(
"yes"
,
c
.
convertBack
(
Boolean
.
TRUE
));
assertEquals
(
"no"
,
c
.
convertBack
(
Boolean
.
FALSE
));
}
@Test
public
void
testStringToBooleanInvalidValue
()
{
StringToBoolean
c
=
new
StringToBoolean
(
"yes"
,
"no"
);
try
{
c
.
convert
(
"true"
);
fail
(
"Should have failed"
);
}
catch
(
IllegalArgumentException
e
)
{
}
}
@Test
public
void
testStringToShort
()
{
StringToShort
c
=
new
StringToShort
();
assertEquals
(
Short
.
valueOf
(
"1"
),
c
.
convert
(
"1"
));
assertEquals
(
"1"
,
c
.
convertBack
(
Short
.
valueOf
(
"1"
)));
}
@Test
public
void
testStringToInteger
()
{
StringToInteger
c
=
new
StringToInteger
();
assertEquals
(
Integer
.
valueOf
(
"1"
),
c
.
convert
(
"1"
));
assertEquals
(
"1"
,
c
.
convertBack
(
Integer
.
valueOf
(
"1"
)));
}
@Test
public
void
testStringToLong
()
{
StringToLong
c
=
new
StringToLong
();
assertEquals
(
Long
.
valueOf
(
"1"
),
c
.
convert
(
"1"
));
assertEquals
(
"1"
,
c
.
convertBack
(
Long
.
valueOf
(
"1"
)));
}
@Test
public
void
testStringToFloat
()
{
StringToFloat
c
=
new
StringToFloat
();
assertEquals
(
Float
.
valueOf
(
"1.0"
),
c
.
convert
(
"1.0"
));
assertEquals
(
"1.0"
,
c
.
convertBack
(
Float
.
valueOf
(
"1.0"
)));
}
@Test
public
void
testStringToDouble
()
{
StringToDouble
c
=
new
StringToDouble
();
assertEquals
(
Double
.
valueOf
(
"1.0"
),
c
.
convert
(
"1.0"
));
assertEquals
(
"1.0"
,
c
.
convertBack
(
Double
.
valueOf
(
"1.0"
)));
}
@Test
public
void
testStringToBigInteger
()
{
StringToBigInteger
c
=
new
StringToBigInteger
();
assertEquals
(
new
BigInteger
(
"1"
),
c
.
convert
(
"1"
));
assertEquals
(
"1"
,
c
.
convertBack
(
new
BigInteger
(
"1"
)));
}
@Test
public
void
testStringToBigDouble
()
{
StringToBigDecimal
c
=
new
StringToBigDecimal
();
assertEquals
(
new
BigDecimal
(
"1.0"
),
c
.
convert
(
"1.0"
));
assertEquals
(
"1.0"
,
c
.
convertBack
(
new
BigDecimal
(
"1.0"
)));
}
@Test
public
void
testStringToEnum
()
{
StringToEnum
c
=
new
StringToEnum
();
assertEquals
(
Foo
.
BAR
,
c
.
convert
(
"BAR"
,
Foo
.
class
));
assertEquals
(
"BAR"
,
c
.
convertBack
(
Foo
.
BAR
,
String
.
class
));
}
public
static
enum
Foo
{
BAR
,
BAZ
;
}
@Test
public
void
testStringToLocale
()
{
StringToLocale
c
=
new
StringToLocale
();
assertEquals
(
Locale
.
ENGLISH
,
c
.
convert
(
"en"
));
assertEquals
(
"en"
,
c
.
convertBack
(
Locale
.
ENGLISH
));
}
@Test
public
void
testNumberToNumber
()
{
NumberToNumber
n
=
new
NumberToNumber
();
assertEquals
(
Long
.
valueOf
(
1
),
n
.
convert
(
Integer
.
valueOf
(
1
),
Long
.
class
));
}
@Test
public
void
testNumberToNumberNotSupportedNumber
()
{
NumberToNumber
n
=
new
NumberToNumber
();
try
{
n
.
convert
(
Integer
.
valueOf
(
1
),
CustomNumber
.
class
);
fail
(
"Should have failed"
);
}
catch
(
IllegalArgumentException
e
)
{
}
}
public
static
class
CustomNumber
extends
Number
{
@Override
public
double
doubleValue
()
{
return
0
;
}
@Override
public
float
floatValue
()
{
return
0
;
}
@Override
public
int
intValue
()
{
return
0
;
}
@Override
public
long
longValue
()
{
return
0
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录