Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
fengjueding
spring-framework
提交
7c3a1849
S
spring-framework
项目概览
fengjueding
/
spring-framework
与 Fork 源项目一致
从无法访问的项目Fork
通知
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,发现更多精彩内容 >>
提交
7c3a1849
编写于
4月 26, 2021
作者:
S
Sam Brannen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Polish SpEL SelectionAndProjectionTests
上级
01e50fb6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
60 addition
and
119 deletion
+60
-119
spring-expression/src/test/java/org/springframework/expression/spel/SelectionAndProjectionTests.java
...ramework/expression/spel/SelectionAndProjectionTests.java
+60
-119
未找到文件。
spring-expression/src/test/java/org/springframework/expression/spel/SelectionAndProjectionTests.java
浏览文件 @
7c3a1849
/*
* Copyright 2002-202
0
the original author or authors.
* Copyright 2002-202
1
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.
...
...
@@ -17,7 +17,6 @@
package
org.springframework.expression.spel
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -40,98 +39,79 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @author Juergen Hoeller
*/
public
class
SelectionAndProjectionTests
{
class
SelectionAndProjectionTests
{
@Test
public
void
selectionWithList
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
void
selectionWithList
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.?[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ListTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
List
;
assertThat
(
condition
).
isTrue
();
List
<?>
list
=
(
List
<?>)
value
;
assertThat
(
list
.
size
()).
isEqualTo
(
5
);
assertThat
(
list
.
get
(
0
)).
isEqualTo
(
0
);
assertThat
(
list
.
get
(
1
)).
isEqualTo
(
1
);
assertThat
(
list
.
get
(
2
)).
isEqualTo
(
2
);
assertThat
(
list
.
get
(
3
)).
isEqualTo
(
3
);
assertThat
(
list
.
get
(
4
)).
isEqualTo
(
4
);
assertThat
(
value
).
isInstanceOf
(
List
.
class
);
List
<
Integer
>
list
=
(
List
<
Integer
>)
value
;
assertThat
(
list
).
containsExactly
(
0
,
1
,
2
,
3
,
4
);
}
@Test
public
void
selectFirstItemInList
()
throws
Exception
{
void
selectFirstItemInList
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.^[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ListTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
0
);
}
@Test
public
void
selectLastItemInList
()
throws
Exception
{
void
selectLastItemInList
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.$[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ListTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
4
);
}
@Test
public
void
selectionWithSet
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
void
selectionWithSet
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.?[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
SetTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
List
;
assertThat
(
condition
).
isTrue
();
List
<?>
list
=
(
List
<?>)
value
;
assertThat
(
list
.
size
()).
isEqualTo
(
5
);
assertThat
(
list
.
get
(
0
)).
isEqualTo
(
0
);
assertThat
(
list
.
get
(
1
)).
isEqualTo
(
1
);
assertThat
(
list
.
get
(
2
)).
isEqualTo
(
2
);
assertThat
(
list
.
get
(
3
)).
isEqualTo
(
3
);
assertThat
(
list
.
get
(
4
)).
isEqualTo
(
4
);
assertThat
(
value
).
isInstanceOf
(
List
.
class
);
List
<
Integer
>
list
=
(
List
<
Integer
>)
value
;
assertThat
(
list
).
containsExactly
(
0
,
1
,
2
,
3
,
4
);
}
@Test
public
void
selectFirstItemInSet
()
throws
Exception
{
void
selectFirstItemInSet
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.^[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
SetTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
0
);
}
@Test
public
void
selectLastItemInSet
()
throws
Exception
{
void
selectLastItemInSet
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.$[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
SetTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
4
);
}
@Test
public
void
selectionWithIterable
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
void
selectionWithIterable
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.?[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
IterableTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
List
;
assertThat
(
condition
).
isTrue
();
List
<?>
list
=
(
List
<?>)
value
;
assertThat
(
list
.
size
()).
isEqualTo
(
5
);
assertThat
(
list
.
get
(
0
)).
isEqualTo
(
0
);
assertThat
(
list
.
get
(
1
)).
isEqualTo
(
1
);
assertThat
(
list
.
get
(
2
)).
isEqualTo
(
2
);
assertThat
(
list
.
get
(
3
)).
isEqualTo
(
3
);
assertThat
(
list
.
get
(
4
)).
isEqualTo
(
4
);
assertThat
(
value
).
isInstanceOf
(
List
.
class
);
List
<
Integer
>
list
=
(
List
<
Integer
>)
value
;
assertThat
(
list
).
containsExactly
(
0
,
1
,
2
,
3
,
4
);
}
@Test
public
void
selectionWithArray
()
throws
Exception
{
void
selectionWithArray
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.?[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ArrayTestBean
());
Object
value
=
expression
.
getValue
(
context
);
...
...
@@ -139,36 +119,29 @@ public class SelectionAndProjectionTests {
TypedValue
typedValue
=
new
TypedValue
(
value
);
assertThat
(
typedValue
.
getTypeDescriptor
().
getElementTypeDescriptor
().
getType
()).
isEqualTo
(
Integer
.
class
);
Integer
[]
array
=
(
Integer
[])
value
;
assertThat
(
array
.
length
).
isEqualTo
(
5
);
assertThat
(
array
[
0
]).
isEqualTo
(
0
);
assertThat
(
array
[
1
]).
isEqualTo
(
1
);
assertThat
(
array
[
2
]).
isEqualTo
(
2
);
assertThat
(
array
[
3
]).
isEqualTo
(
3
);
assertThat
(
array
[
4
]).
isEqualTo
(
4
);
assertThat
(
array
).
containsExactly
(
0
,
1
,
2
,
3
,
4
);
}
@Test
public
void
selectFirstItemInArray
()
throws
Exception
{
void
selectFirstItemInArray
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.^[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ArrayTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
0
);
}
@Test
public
void
selectLastItemInArray
()
throws
Exception
{
void
selectLastItemInArray
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"integers.$[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ArrayTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
4
);
}
@Test
public
void
selectionWithPrimitiveArray
()
throws
Exception
{
void
selectionWithPrimitiveArray
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"ints.?[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ArrayTestBean
());
Object
value
=
expression
.
getValue
(
context
);
...
...
@@ -176,51 +149,41 @@ public class SelectionAndProjectionTests {
TypedValue
typedValue
=
new
TypedValue
(
value
);
assertThat
(
typedValue
.
getTypeDescriptor
().
getElementTypeDescriptor
().
getType
()).
isEqualTo
(
Integer
.
class
);
Integer
[]
array
=
(
Integer
[])
value
;
assertThat
(
array
.
length
).
isEqualTo
(
5
);
assertThat
(
array
[
0
]).
isEqualTo
(
0
);
assertThat
(
array
[
1
]).
isEqualTo
(
1
);
assertThat
(
array
[
2
]).
isEqualTo
(
2
);
assertThat
(
array
[
3
]).
isEqualTo
(
3
);
assertThat
(
array
[
4
]).
isEqualTo
(
4
);
assertThat
(
array
).
containsExactly
(
0
,
1
,
2
,
3
,
4
);
}
@Test
public
void
selectFirstItemInPrimitiveArray
()
throws
Exception
{
void
selectFirstItemInPrimitiveArray
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"ints.^[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ArrayTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
0
);
}
@Test
public
void
selectLastItemInPrimitiveArray
()
throws
Exception
{
void
selectLastItemInPrimitiveArray
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"ints.$[#this<5]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
ArrayTestBean
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
Integer
;
assertThat
(
condition
).
isTrue
();
assertThat
(
value
).
isInstanceOf
(
Integer
.
class
);
assertThat
(
value
).
isEqualTo
(
4
);
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
selectionWithMap
()
{
void
selectionWithMap
()
{
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
MapTestBean
());
ExpressionParser
parser
=
new
SpelExpressionParser
();
Expression
exp
=
parser
.
parseExpression
(
"colors.?[key.startsWith('b')]"
);
Map
<
String
,
String
>
colorsMap
=
(
Map
<
String
,
String
>)
exp
.
getValue
(
context
);
assertThat
(
colorsMap
.
size
()).
isEqualTo
(
3
);
assertThat
(
colorsMap
.
containsKey
(
"beige"
)).
isTrue
();
assertThat
(
colorsMap
.
containsKey
(
"blue"
)).
isTrue
();
assertThat
(
colorsMap
.
containsKey
(
"brown"
)).
isTrue
();
assertThat
(
colorsMap
).
containsOnlyKeys
(
"beige"
,
"blue"
,
"brown"
);
}
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
selectFirstItemInMap
()
{
void
selectFirstItemInMap
()
{
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
MapTestBean
());
ExpressionParser
parser
=
new
SpelExpressionParser
();
...
...
@@ -232,7 +195,7 @@ public class SelectionAndProjectionTests {
@Test
@SuppressWarnings
(
"unchecked"
)
public
void
selectLastItemInMap
()
{
void
selectLastItemInMap
()
{
EvaluationContext
context
=
new
StandardEvaluationContext
(
new
MapTestBean
());
ExpressionParser
parser
=
new
SpelExpressionParser
();
...
...
@@ -243,52 +206,43 @@ public class SelectionAndProjectionTests {
}
@Test
public
void
projectionWithList
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
void
projectionWithList
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"#testList.![wrapper.value]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
();
context
.
setVariable
(
"testList"
,
IntegerTestBean
.
createList
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
List
;
assertThat
(
condition
).
isTrue
();
List
<?>
list
=
(
List
<?>)
value
;
assertThat
(
list
.
size
()).
isEqualTo
(
3
);
assertThat
(
list
.
get
(
0
)).
isEqualTo
(
5
);
assertThat
(
list
.
get
(
1
)).
isEqualTo
(
6
);
assertThat
(
list
.
get
(
2
)).
isEqualTo
(
7
);
assertThat
(
value
).
isInstanceOf
(
List
.
class
);
List
<
Integer
>
list
=
(
List
<
Integer
>)
value
;
assertThat
(
list
).
containsExactly
(
5
,
6
,
7
);
}
@Test
public
void
projectionWithSet
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
void
projectionWithSet
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"#testList.![wrapper.value]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
();
context
.
setVariable
(
"testList"
,
IntegerTestBean
.
createSet
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
List
;
assertThat
(
condition
).
isTrue
();
List
<?>
list
=
(
List
<?>)
value
;
assertThat
(
list
.
size
()).
isEqualTo
(
3
);
assertThat
(
list
.
get
(
0
)).
isEqualTo
(
5
);
assertThat
(
list
.
get
(
1
)).
isEqualTo
(
6
);
assertThat
(
list
.
get
(
2
)).
isEqualTo
(
7
);
assertThat
(
value
).
isInstanceOf
(
List
.
class
);
List
<
Integer
>
list
=
(
List
<
Integer
>)
value
;
assertThat
(
list
).
containsExactly
(
5
,
6
,
7
);
}
@Test
public
void
projectionWithIterable
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
void
projectionWithIterable
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"#testList.![wrapper.value]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
();
context
.
setVariable
(
"testList"
,
IntegerTestBean
.
createIterable
());
Object
value
=
expression
.
getValue
(
context
);
boolean
condition
=
value
instanceof
List
;
assertThat
(
condition
).
isTrue
();
List
<?>
list
=
(
List
<?>)
value
;
assertThat
(
list
.
size
()).
isEqualTo
(
3
);
assertThat
(
list
.
get
(
0
)).
isEqualTo
(
5
);
assertThat
(
list
.
get
(
1
)).
isEqualTo
(
6
);
assertThat
(
list
.
get
(
2
)).
isEqualTo
(
7
);
assertThat
(
value
).
isInstanceOf
(
List
.
class
);
List
<
Integer
>
list
=
(
List
<
Integer
>)
value
;
assertThat
(
list
).
containsExactly
(
5
,
6
,
7
);
}
@Test
public
void
projectionWithArray
()
throws
Exception
{
void
projectionWithArray
()
throws
Exception
{
Expression
expression
=
new
SpelExpressionParser
().
parseRaw
(
"#testArray.![wrapper.value]"
);
EvaluationContext
context
=
new
StandardEvaluationContext
();
context
.
setVariable
(
"testArray"
,
IntegerTestBean
.
createArray
());
...
...
@@ -297,10 +251,7 @@ public class SelectionAndProjectionTests {
TypedValue
typedValue
=
new
TypedValue
(
value
);
assertThat
(
typedValue
.
getTypeDescriptor
().
getElementTypeDescriptor
().
getType
()).
isEqualTo
(
Number
.
class
);
Number
[]
array
=
(
Number
[])
value
;
assertThat
(
array
.
length
).
isEqualTo
(
3
);
assertThat
(
array
[
0
]).
isEqualTo
(
5
);
assertThat
(
array
[
1
]).
isEqualTo
(
5.9f
);
assertThat
(
array
[
2
]).
isEqualTo
(
7
);
assertThat
(
array
).
containsExactly
(
5
,
5.9f
,
7
);
}
...
...
@@ -347,12 +298,7 @@ public class SelectionAndProjectionTests {
}
public
Iterable
<
Integer
>
getIntegers
()
{
return
new
Iterable
<
Integer
>()
{
@Override
public
Iterator
<
Integer
>
iterator
()
{
return
integers
.
iterator
();
}
};
return
integers:
:
iterator
;
}
}
...
...
@@ -429,12 +375,7 @@ public class SelectionAndProjectionTests {
static
Iterable
<
IntegerTestBean
>
createIterable
()
{
final
Set
<
IntegerTestBean
>
set
=
createSet
();
return
new
Iterable
<
IntegerTestBean
>()
{
@Override
public
Iterator
<
IntegerTestBean
>
iterator
()
{
return
set
.
iterator
();
}
};
return
set:
:
iterator
;
}
static
IntegerTestBean
[]
createArray
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录