Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
c6db7c41
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,发现更多精彩内容 >>
提交
c6db7c41
编写于
8月 17, 2008
作者:
A
Andy Clement
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
improved javadoc, error handling and testing of matches
上级
bb2ade5f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
68 addition
and
15 deletion
+68
-15
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java
.../springframework/expression/spel/ast/OperatorMatches.java
+28
-11
org.springframework.expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java
.../org/springframework/expression/spel/EvaluationTests.java
+25
-1
org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionTestCase.java
...g/springframework/expression/spel/ExpressionTestCase.java
+14
-2
org.springframework.expression/src/test/java/org/springframework/expression/spel/LiteralTests.java
...ava/org/springframework/expression/spel/LiteralTests.java
+1
-1
未找到文件。
org.springframework.expression/src/main/java/org/springframework/expression/spel/ast/OperatorMatches.java
浏览文件 @
c6db7c41
...
@@ -21,13 +21,13 @@ import java.util.regex.PatternSyntaxException;
...
@@ -21,13 +21,13 @@ import java.util.regex.PatternSyntaxException;
import
org.antlr.runtime.Token
;
import
org.antlr.runtime.Token
;
import
org.springframework.expression.EvaluationException
;
import
org.springframework.expression.EvaluationException
;
import
org.springframework.expression.spel.ExpressionState
;
import
org.springframework.expression.spel.SpelException
;
import
org.springframework.expression.spel.SpelException
;
import
org.springframework.expression.spel.SpelMessages
;
import
org.springframework.expression.spel.SpelMessages
;
import
org.springframework.expression.spel.ExpressionState
;
// TODO what should be the difference between like and matches?
/**
/**
* Implements the matches operator.
* Implements the matches operator. Matches takes two operands. The first is a string and the second is a java regex. It
* will return true when getValue() is called if the first operand matches the regex.
*
*
* @author Andy Clement
* @author Andy Clement
*/
*/
...
@@ -38,21 +38,38 @@ public class OperatorMatches extends Operator {
...
@@ -38,21 +38,38 @@ public class OperatorMatches extends Operator {
}
}
@Override
@Override
public
Object
getValue
(
ExpressionState
state
)
throws
EvaluationException
{
public
String
getOperatorName
()
{
Object
left
=
getLeftOperand
().
getValue
(
state
);
return
"matches"
;
}
/**
* Check the first operand matches the regex specified as the second operand.
*
* @param state the expression state
* @return true if the first operand matches the regex specified as the second operand, otherwise false
* @throws EvaluationException if there is a problem evaluating the expression (e.g. the regex is invalid)
*/
@Override
public
Boolean
getValue
(
ExpressionState
state
)
throws
EvaluationException
{
SpelNode
leftOp
=
getLeftOperand
();
SpelNode
rightOp
=
getRightOperand
();
Object
left
=
leftOp
.
getValue
(
state
,
String
.
class
);
Object
right
=
getRightOperand
().
getValue
(
state
);
Object
right
=
getRightOperand
().
getValue
(
state
);
try
{
try
{
if
(!(
left
instanceof
String
))
{
throw
new
SpelException
(
leftOp
.
getCharPositionInLine
(),
SpelMessages
.
INVALID_FIRST_OPERAND_FOR_LIKE_OPERATOR
,
left
);
}
if
(!(
right
instanceof
String
))
{
throw
new
SpelException
(
rightOp
.
getCharPositionInLine
(),
SpelMessages
.
INVALID_SECOND_OPERAND_FOR_LIKE_OPERATOR
,
right
);
}
Pattern
pattern
=
Pattern
.
compile
((
String
)
right
);
Pattern
pattern
=
Pattern
.
compile
((
String
)
right
);
Matcher
matcher
=
pattern
.
matcher
((
String
)
left
);
Matcher
matcher
=
pattern
.
matcher
((
String
)
left
);
return
matcher
.
matches
();
return
matcher
.
matches
();
}
catch
(
PatternSyntaxException
pse
)
{
}
catch
(
PatternSyntaxException
pse
)
{
throw
new
SpelException
(
pse
,
SpelMessages
.
INVALID_PATTERN
,
right
);
throw
new
SpelException
(
rightOp
.
getCharPositionInLine
(),
pse
,
SpelMessages
.
INVALID_PATTERN
,
right
);
}
}
}
}
@Override
public
String
getOperatorName
()
{
return
"matches"
;
}
}
}
org.springframework.expression/src/test/java/org/springframework/expression/spel/EvaluationTests.java
浏览文件 @
c6db7c41
...
@@ -101,6 +101,18 @@ public class EvaluationTests extends ExpressionTestCase {
...
@@ -101,6 +101,18 @@ public class EvaluationTests extends ExpressionTestCase {
evaluate
(
"{1, 2, 3, 4, 5} is T(List)"
,
"true"
,
Boolean
.
class
);
evaluate
(
"{1, 2, 3, 4, 5} is T(List)"
,
"true"
,
Boolean
.
class
);
}
}
public
void
testRelOperatorsIs04
()
{
evaluate
(
"null is T(String)"
,
"false"
,
Boolean
.
class
);
}
public
void
testRelOperatorsIs05
()
{
evaluate
(
"null is T(Integer)"
,
"false"
,
Boolean
.
class
);
}
public
void
testRelOperatorsIs06
()
{
evaluateAndCheckError
(
"'A' is null"
,
SpelMessages
.
IS_OPERATOR_NEEDS_CLASS_OPERAND
,
7
,
"null"
);
}
public
void
testRelOperatorsMatches01
()
{
public
void
testRelOperatorsMatches01
()
{
evaluate
(
"'5.0067' matches '^-?\\d+(\\.\\d{2})?$'"
,
"false"
,
Boolean
.
class
);
evaluate
(
"'5.0067' matches '^-?\\d+(\\.\\d{2})?$'"
,
"false"
,
Boolean
.
class
);
}
}
...
@@ -109,6 +121,18 @@ public class EvaluationTests extends ExpressionTestCase {
...
@@ -109,6 +121,18 @@ public class EvaluationTests extends ExpressionTestCase {
evaluate
(
"'5.00' matches '^-?\\d+(\\.\\d{2})?$'"
,
"true"
,
Boolean
.
class
);
evaluate
(
"'5.00' matches '^-?\\d+(\\.\\d{2})?$'"
,
"true"
,
Boolean
.
class
);
}
}
public
void
testRelOperatorsMatches03
()
{
evaluateAndCheckError
(
"null matches '^.*$'"
,
SpelMessages
.
INVALID_FIRST_OPERAND_FOR_LIKE_OPERATOR
,
0
,
null
);
}
public
void
testRelOperatorsMatches04
()
{
evaluateAndCheckError
(
"'abc' matches null"
,
SpelMessages
.
INVALID_SECOND_OPERAND_FOR_LIKE_OPERATOR
,
14
,
null
);
}
public
void
testRelOperatorsMatches05
()
{
evaluate
(
"27 matches '^.*2.*$'"
,
true
,
Boolean
.
class
);
// conversion int>string
}
// mathematical operators
// mathematical operators
public
void
testMathOperatorAdd01
()
{
public
void
testMathOperatorAdd01
()
{
evaluate
(
"2 + 4"
,
"6"
,
Integer
.
class
);
evaluate
(
"2 + 4"
,
"6"
,
Integer
.
class
);
...
@@ -336,7 +360,7 @@ public class EvaluationTests extends ExpressionTestCase {
...
@@ -336,7 +360,7 @@ public class EvaluationTests extends ExpressionTestCase {
SpelMessages
.
RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN
);
SpelMessages
.
RESULT_OF_SELECTION_CRITERIA_IS_NOT_BOOLEAN
);
}
}
// TODO
3 Q
Is $index within projection/selection useful or just cute?
// TODO Is $index within projection/selection useful or just cute?
public
void
testSelectionUsingIndex
()
{
public
void
testSelectionUsingIndex
()
{
evaluate
(
"{1,2,3,4,5,6,7,8,9,10}.?{$index > 5 }"
,
"[7, 8, 9, 10]"
,
ArrayList
.
class
);
evaluate
(
"{1,2,3,4,5,6,7,8,9,10}.?{$index > 5 }"
,
"[7, 8, 9, 10]"
,
ArrayList
.
class
);
}
}
...
...
org.springframework.expression/src/test/java/org/springframework/expression/spel/ExpressionTestCase.java
浏览文件 @
c6db7c41
...
@@ -164,7 +164,7 @@ public abstract class ExpressionTestCase extends TestCase {
...
@@ -164,7 +164,7 @@ public abstract class ExpressionTestCase extends TestCase {
assertEquals
(
"Type of the result was not as expected. Expected '"
+
expectedClassOfResult
assertEquals
(
"Type of the result was not as expected. Expected '"
+
expectedClassOfResult
+
"' but result was of type '"
+
resultType
+
"'"
,
expectedClassOfResult
+
"' but result was of type '"
+
resultType
+
"'"
,
expectedClassOfResult
.
equals
/* isAssignableFrom */
(
resultType
),
true
);
.
equals
/* isAssignableFrom */
(
resultType
),
true
);
// TODO
4
isAssignableFrom would allow some room for compatibility
// TODO isAssignableFrom would allow some room for compatibility
// in the above expression...
// in the above expression...
boolean
isWritable
=
e
.
isWritable
(
eContext
);
boolean
isWritable
=
e
.
isWritable
(
eContext
);
...
@@ -243,7 +243,19 @@ public abstract class ExpressionTestCase extends TestCase {
...
@@ -243,7 +243,19 @@ public abstract class ExpressionTestCase extends TestCase {
+
" properties of the exception, it only has "
+
inserts
.
length
+
" inserts"
);
+
" properties of the exception, it only has "
+
inserts
.
length
+
" inserts"
);
}
}
for
(
int
i
=
1
;
i
<
otherProperties
.
length
;
i
++)
{
for
(
int
i
=
1
;
i
<
otherProperties
.
length
;
i
++)
{
if
(!
inserts
[
i
-
1
].
equals
(
otherProperties
[
i
]))
{
if
(
otherProperties
[
i
]
==
null
)
{
if
(
inserts
[
i
-
1
]
!=
null
)
{
ex
.
printStackTrace
();
fail
(
"Insert does not match, expected 'null' but insert value was '"
+
inserts
[
i
-
1
]
+
"'"
);
}
}
else
if
(
inserts
[
i
-
1
]
==
null
)
{
if
(
otherProperties
[
i
]
!=
null
)
{
ex
.
printStackTrace
();
fail
(
"Insert does not match, expected '"
+
otherProperties
[
i
]
+
"' but insert value was 'null'"
);
}
}
else
if
(!
inserts
[
i
-
1
].
equals
(
otherProperties
[
i
]))
{
ex
.
printStackTrace
();
ex
.
printStackTrace
();
fail
(
"Insert does not match, expected '"
+
otherProperties
[
i
]
+
"' but insert value was '"
fail
(
"Insert does not match, expected '"
+
otherProperties
[
i
]
+
"' but insert value was '"
+
inserts
[
i
-
1
]
+
"'"
);
+
inserts
[
i
-
1
]
+
"'"
);
...
...
org.springframework.expression/src/test/java/org/springframework/expression/spel/LiteralTests.java
浏览文件 @
c6db7c41
...
@@ -97,7 +97,7 @@ public class LiteralTests extends ExpressionTestCase {
...
@@ -97,7 +97,7 @@ public class LiteralTests extends ExpressionTestCase {
evaluate
(
"null"
,
null
,
null
);
evaluate
(
"null"
,
null
,
null
);
}
}
// TODO
3
'default' format for date varies too much, we need to standardize on a format for EL
// TODO 'default' format for date varies too much, we need to standardize on a format for EL
// public void testLiteralDate01() {
// public void testLiteralDate01() {
// eval("date('3-Feb-2008 4:50:20 PM').getTime()>0", "true", Boolean.class);
// eval("date('3-Feb-2008 4:50:20 PM').getTime()>0", "true", Boolean.class);
// }
// }
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录