Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
f54395b2
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,体验更适合开发者的 AI 搜索 >>
提交
f54395b2
编写于
9月 04, 2019
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish BeanUtilsTests
上级
80364355
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
47 addition
and
67 deletion
+47
-67
spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java
...c/test/java/org/springframework/beans/BeanUtilsTests.java
+47
-67
未找到文件。
spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java
浏览文件 @
f54395b2
...
...
@@ -26,9 +26,10 @@ import java.time.DayOfWeek;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Locale
;
import
java.util.stream.Stream
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.params.ParameterizedTest
;
import
org.junit.jupiter.params.provider.ValueSource
;
import
org.springframework.beans.factory.BeanFactory
;
import
org.springframework.beans.propertyeditors.CustomDateEditor
;
...
...
@@ -53,22 +54,22 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @author Sam Brannen
* @since 19.05.2003
*/
public
class
BeanUtilsTests
{
class
BeanUtilsTests
{
@Test
public
void
testInstantiateClassGivenInterface
()
{
void
testInstantiateClassGivenInterface
()
{
assertThatExceptionOfType
(
FatalBeanException
.
class
).
isThrownBy
(()
->
BeanUtils
.
instantiateClass
(
List
.
class
));
}
@Test
public
void
testInstantiateClassGivenClassWithoutDefaultConstructor
()
{
void
testInstantiateClassGivenClassWithoutDefaultConstructor
()
{
assertThatExceptionOfType
(
FatalBeanException
.
class
).
isThrownBy
(()
->
BeanUtils
.
instantiateClass
(
CustomDateEditor
.
class
));
}
@Test
// gh-22531
public
void
testInstantiateClassWithOptionalNullableType
()
throws
NoSuchMethodException
{
void
testInstantiateClassWithOptionalNullableType
()
throws
NoSuchMethodException
{
Constructor
<
BeanWithNullableTypes
>
ctor
=
BeanWithNullableTypes
.
class
.
getDeclaredConstructor
(
Integer
.
class
,
Boolean
.
class
,
String
.
class
);
BeanWithNullableTypes
bean
=
BeanUtils
.
instantiateClass
(
ctor
,
null
,
null
,
"foo"
);
...
...
@@ -78,7 +79,7 @@ public class BeanUtilsTests {
}
@Test
// gh-22531
public
void
testInstantiateClassWithOptionalPrimitiveType
()
throws
NoSuchMethodException
{
void
testInstantiateClassWithOptionalPrimitiveType
()
throws
NoSuchMethodException
{
Constructor
<
BeanWithPrimitiveTypes
>
ctor
=
BeanWithPrimitiveTypes
.
class
.
getDeclaredConstructor
(
int
.
class
,
boolean
.
class
,
String
.
class
);
BeanWithPrimitiveTypes
bean
=
BeanUtils
.
instantiateClass
(
ctor
,
null
,
null
,
"foo"
);
assertThat
(
bean
.
getCounter
()).
isEqualTo
(
0
);
...
...
@@ -87,14 +88,14 @@ public class BeanUtilsTests {
}
@Test
// gh-22531
public
void
testInstantiateClassWithMoreArgsThanParameters
()
throws
NoSuchMethodException
{
void
testInstantiateClassWithMoreArgsThanParameters
()
throws
NoSuchMethodException
{
Constructor
<
BeanWithPrimitiveTypes
>
ctor
=
BeanWithPrimitiveTypes
.
class
.
getDeclaredConstructor
(
int
.
class
,
boolean
.
class
,
String
.
class
);
assertThatExceptionOfType
(
BeanInstantiationException
.
class
).
isThrownBy
(()
->
BeanUtils
.
instantiateClass
(
ctor
,
null
,
null
,
"foo"
,
null
));
}
@Test
public
void
testGetPropertyDescriptors
()
throws
Exception
{
void
testGetPropertyDescriptors
()
throws
Exception
{
PropertyDescriptor
[]
actual
=
Introspector
.
getBeanInfo
(
TestBean
.
class
).
getPropertyDescriptors
();
PropertyDescriptor
[]
descriptors
=
BeanUtils
.
getPropertyDescriptors
(
TestBean
.
class
);
assertThat
(
descriptors
).
as
(
"Descriptors should not be null"
).
isNotNull
();
...
...
@@ -102,7 +103,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testBeanPropertyIsArray
()
{
void
testBeanPropertyIsArray
()
{
PropertyDescriptor
[]
descriptors
=
BeanUtils
.
getPropertyDescriptors
(
ContainerBean
.
class
);
for
(
PropertyDescriptor
descriptor
:
descriptors
)
{
if
(
"containedBeans"
.
equals
(
descriptor
.
getName
()))
{
...
...
@@ -113,12 +114,12 @@ public class BeanUtilsTests {
}
@Test
public
void
testFindEditorByConvention
()
{
void
testFindEditorByConvention
()
{
assertThat
(
BeanUtils
.
findEditorByConvention
(
Resource
.
class
).
getClass
()).
isEqualTo
(
ResourceEditor
.
class
);
}
@Test
public
void
testCopyProperties
()
throws
Exception
{
void
testCopyProperties
()
throws
Exception
{
TestBean
tb
=
new
TestBean
();
tb
.
setName
(
"rod"
);
tb
.
setAge
(
32
);
...
...
@@ -134,7 +135,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testCopyPropertiesWithDifferentTypes1
()
throws
Exception
{
void
testCopyPropertiesWithDifferentTypes1
()
throws
Exception
{
DerivedTestBean
tb
=
new
DerivedTestBean
();
tb
.
setName
(
"rod"
);
tb
.
setAge
(
32
);
...
...
@@ -150,7 +151,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testCopyPropertiesWithDifferentTypes2
()
throws
Exception
{
void
testCopyPropertiesWithDifferentTypes2
()
throws
Exception
{
TestBean
tb
=
new
TestBean
();
tb
.
setName
(
"rod"
);
tb
.
setAge
(
32
);
...
...
@@ -166,7 +167,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testCopyPropertiesWithEditable
()
throws
Exception
{
void
testCopyPropertiesWithEditable
()
throws
Exception
{
TestBean
tb
=
new
TestBean
();
assertThat
(
tb
.
getName
()
==
null
).
as
(
"Name empty"
).
isTrue
();
tb
.
setAge
(
32
);
...
...
@@ -184,7 +185,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testCopyPropertiesWithIgnore
()
throws
Exception
{
void
testCopyPropertiesWithIgnore
()
throws
Exception
{
TestBean
tb
=
new
TestBean
();
assertThat
(
tb
.
getName
()
==
null
).
as
(
"Name empty"
).
isTrue
();
tb
.
setAge
(
32
);
...
...
@@ -202,7 +203,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testCopyPropertiesWithIgnoredNonExistingProperty
()
{
void
testCopyPropertiesWithIgnoredNonExistingProperty
()
{
NameAndSpecialProperty
source
=
new
NameAndSpecialProperty
();
source
.
setName
(
"name"
);
TestBean
target
=
new
TestBean
();
...
...
@@ -211,7 +212,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testCopyPropertiesWithInvalidProperty
()
{
void
testCopyPropertiesWithInvalidProperty
()
{
InvalidProperty
source
=
new
InvalidProperty
();
source
.
setName
(
"name"
);
source
.
setFlag1
(
true
);
...
...
@@ -224,39 +225,39 @@ public class BeanUtilsTests {
}
@Test
public
void
testResolveSimpleSignature
()
throws
Exception
{
void
testResolveSimpleSignature
()
throws
Exception
{
Method
desiredMethod
=
MethodSignatureBean
.
class
.
getMethod
(
"doSomething"
);
assertSignatureEquals
(
desiredMethod
,
"doSomething"
);
assertSignatureEquals
(
desiredMethod
,
"doSomething()"
);
}
@Test
public
void
testResolveInvalidSignatureEndParen
()
{
void
testResolveInvalidSignatureEndParen
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
BeanUtils
.
resolveSignature
(
"doSomething("
,
MethodSignatureBean
.
class
));
}
@Test
public
void
testResolveInvalidSignatureStartParen
()
{
void
testResolveInvalidSignatureStartParen
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
BeanUtils
.
resolveSignature
(
"doSomething)"
,
MethodSignatureBean
.
class
));
}
@Test
public
void
testResolveWithAndWithoutArgList
()
throws
Exception
{
void
testResolveWithAndWithoutArgList
()
throws
Exception
{
Method
desiredMethod
=
MethodSignatureBean
.
class
.
getMethod
(
"doSomethingElse"
,
String
.
class
,
int
.
class
);
assertSignatureEquals
(
desiredMethod
,
"doSomethingElse"
);
assertThat
(
BeanUtils
.
resolveSignature
(
"doSomethingElse()"
,
MethodSignatureBean
.
class
)).
isNull
();
}
@Test
public
void
testResolveTypedSignature
()
throws
Exception
{
void
testResolveTypedSignature
()
throws
Exception
{
Method
desiredMethod
=
MethodSignatureBean
.
class
.
getMethod
(
"doSomethingElse"
,
String
.
class
,
int
.
class
);
assertSignatureEquals
(
desiredMethod
,
"doSomethingElse(java.lang.String, int)"
);
}
@Test
public
void
testResolveOverloadedSignature
()
throws
Exception
{
void
testResolveOverloadedSignature
()
throws
Exception
{
// test resolve with no args
Method
desiredMethod
=
MethodSignatureBean
.
class
.
getMethod
(
"overloaded"
);
assertSignatureEquals
(
desiredMethod
,
"overloaded()"
);
...
...
@@ -271,7 +272,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testResolveSignatureWithArray
()
throws
Exception
{
void
testResolveSignatureWithArray
()
throws
Exception
{
Method
desiredMethod
=
MethodSignatureBean
.
class
.
getMethod
(
"doSomethingWithAnArray"
,
String
[].
class
);
assertSignatureEquals
(
desiredMethod
,
"doSomethingWithAnArray(java.lang.String[])"
);
...
...
@@ -280,7 +281,7 @@ public class BeanUtilsTests {
}
@Test
public
void
testSPR6063
()
{
void
testSPR6063
()
{
PropertyDescriptor
[]
descrs
=
BeanUtils
.
getPropertyDescriptors
(
Bean
.
class
);
PropertyDescriptor
keyDescr
=
BeanUtils
.
getPropertyDescriptor
(
Bean
.
class
,
"value"
);
...
...
@@ -292,57 +293,36 @@ public class BeanUtilsTests {
}
}
@Test
public
void
isSimpleValueType
()
{
Stream
.
of
(
boolean
.
class
,
char
.
class
,
byte
.
class
,
short
.
class
,
int
.
class
,
long
.
class
,
float
.
class
,
double
.
class
,
Boolean
.
class
,
Character
.
class
,
Byte
.
class
,
Short
.
class
,
Integer
.
class
,
Long
.
class
,
Float
.
class
,
Double
.
class
,
DayOfWeek
.
class
,
String
.
class
,
Date
.
class
,
URI
.
class
,
URL
.
class
,
Locale
.
class
,
Class
.
class
).
forEach
(
this
::
assertIsSimpleValueType
);
Stream
.
of
(
int
[].
class
,
Object
.
class
,
List
.
class
,
void
.
class
,
Void
.
class
)
.
forEach
(
this
::
assertIsNotSimpleValueType
);
}
@Test
public
void
isSimpleProperty
()
{
Stream
.
of
(
boolean
.
class
,
char
.
class
,
byte
.
class
,
short
.
class
,
int
.
class
,
long
.
class
,
float
.
class
,
double
.
class
,
Boolean
.
class
,
Character
.
class
,
Byte
.
class
,
Short
.
class
,
Integer
.
class
,
Long
.
class
,
Float
.
class
,
Double
.
class
,
DayOfWeek
.
class
,
String
.
class
,
Date
.
class
,
URI
.
class
,
URL
.
class
,
Locale
.
class
,
Class
.
class
,
boolean
[].
class
,
Boolean
[].
class
,
Date
[].
class
).
forEach
(
this
::
assertIsSimpleProperty
);
Stream
.
of
(
Object
.
class
,
List
.
class
,
void
.
class
,
Void
.
class
)
.
forEach
(
this
::
assertIsNotSimpleProperty
);
}
private
void
assertIsSimpleValueType
(
Class
<?>
type
)
{
@ParameterizedTest
@ValueSource
(
classes
=
{
boolean
.
class
,
char
.
class
,
byte
.
class
,
short
.
class
,
int
.
class
,
long
.
class
,
float
.
class
,
double
.
class
,
Boolean
.
class
,
Character
.
class
,
Byte
.
class
,
Short
.
class
,
Integer
.
class
,
Long
.
class
,
Float
.
class
,
Double
.
class
,
DayOfWeek
.
class
,
String
.
class
,
Date
.
class
,
URI
.
class
,
URL
.
class
,
Locale
.
class
,
Class
.
class
})
void
isSimpleValueType
(
Class
<?>
type
)
{
assertThat
(
BeanUtils
.
isSimpleValueType
(
type
)).
as
(
"Type ["
+
type
.
getName
()
+
"] should be a simple value type"
).
isTrue
();
}
private
void
assertIsNotSimpleValueType
(
Class
<?>
type
)
{
@ParameterizedTest
@ValueSource
(
classes
=
{
int
[].
class
,
Object
.
class
,
List
.
class
,
void
.
class
,
Void
.
class
})
void
isNotSimpleValueType
(
Class
<?>
type
)
{
assertThat
(
BeanUtils
.
isSimpleValueType
(
type
)).
as
(
"Type ["
+
type
.
getName
()
+
"] should not be a simple value type"
).
isFalse
();
}
private
void
assertIsSimpleProperty
(
Class
<?>
type
)
{
@ParameterizedTest
@ValueSource
(
classes
=
{
boolean
.
class
,
char
.
class
,
byte
.
class
,
short
.
class
,
int
.
class
,
long
.
class
,
float
.
class
,
double
.
class
,
Boolean
.
class
,
Character
.
class
,
Byte
.
class
,
Short
.
class
,
Integer
.
class
,
Long
.
class
,
Float
.
class
,
Double
.
class
,
DayOfWeek
.
class
,
String
.
class
,
Date
.
class
,
URI
.
class
,
URL
.
class
,
Locale
.
class
,
Class
.
class
,
boolean
[].
class
,
Boolean
[].
class
,
Date
[].
class
})
void
isSimpleProperty
(
Class
<?>
type
)
{
assertThat
(
BeanUtils
.
isSimpleProperty
(
type
)).
as
(
"Type ["
+
type
.
getName
()
+
"] should be a simple property"
).
isTrue
();
}
private
void
assertIsNotSimpleProperty
(
Class
<?>
type
)
{
@ParameterizedTest
@ValueSource
(
classes
=
{
Object
.
class
,
List
.
class
,
void
.
class
,
Void
.
class
})
void
isNotSimpleProperty
(
Class
<?>
type
)
{
assertThat
(
BeanUtils
.
isSimpleProperty
(
type
)).
as
(
"Type ["
+
type
.
getName
()
+
"] should not be a simple property"
).
isFalse
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录