|
@@ -2,53 +2,102 @@ package net.abhinavsarkar.spelhelper
|
2
|
2
|
|
3
|
3
|
import org.scalatest.junit.JUnitRunner
|
4
|
4
|
import org.junit.runner.RunWith
|
5
|
|
-import org.scalatest.Spec
|
|
5
|
+import org.scalatest.FlatSpec
|
6
|
6
|
import org.scalatest.junit.ShouldMatchersForJUnit
|
|
7
|
+import org.springframework.expression.spel.SpelEvaluationException
|
7
|
8
|
|
8
|
9
|
@RunWith(classOf[JUnitRunner])
|
9
|
|
-class SpelHelperSpec extends Spec with ShouldMatchersForJUnit {
|
10
|
|
- describe("SpelHelper") {
|
11
|
|
-
|
12
|
|
- it ("should register and evaluate functions ") {
|
13
|
|
- new SpelHelper()
|
14
|
|
- .registerFunctionsFromClass(classOf[Functions])
|
15
|
|
- .evalExpression(
|
16
|
|
- "#test('check')", new {}, classOf[String]) should equal("check")
|
17
|
|
- }
|
18
|
|
-
|
19
|
|
- it ("should register implicit methods ") {
|
20
|
|
- new SpelHelper()
|
21
|
|
- .registerImplicitMethodsFromClass(classOf[Functions])
|
22
|
|
- .lookupImplicitMethod("java.lang.String.test") should equal(
|
23
|
|
- classOf[Functions].getMethod("test", classOf[String]))
|
24
|
|
- }
|
25
|
|
-
|
26
|
|
- it ("should register implicit constructors ") {
|
27
|
|
- new SpelHelper()
|
28
|
|
- .registerConstructorsFromClass(classOf[Functions])
|
29
|
|
- .lookupImplicitConstructor("Functions[]") should equal(
|
30
|
|
- classOf[Functions].getConstructor())
|
31
|
|
- }
|
32
|
|
-
|
33
|
|
- it ("should evaluate implicit methods ") {
|
34
|
|
- new SpelHelper()
|
35
|
|
- .registerImplicitMethodsFromClass(classOf[Functions])
|
36
|
|
- .evalExpression(
|
37
|
|
- "'check'.test()", new {}, classOf[String]) should equal("check")
|
38
|
|
- }
|
39
|
|
-
|
40
|
|
- it ("should evaluate implicit constructors ") {
|
41
|
|
- new SpelHelper()
|
42
|
|
- .registerConstructorsFromClass(classOf[Functions])
|
43
|
|
- .evalExpression(
|
44
|
|
- "new Functions()", new {}, classOf[Functions]) should equal(new Functions)
|
45
|
|
- }
|
46
|
|
-
|
47
|
|
- it ("should evaluate implicit properties ") {
|
48
|
|
- new SpelHelper().evalExpression(
|
49
|
|
- "'abc'.hashCode", new {}, classOf[int]) should equal("abc".hashCode)
|
50
|
|
- }
|
|
10
|
+class SpelHelperSpec extends FlatSpec with ShouldMatchersForJUnit {
|
51
|
11
|
|
|
12
|
+ "SpelHelper" should "register and evaluate functions " in {
|
|
13
|
+ new SpelHelper()
|
|
14
|
+ .registerFunctionsFromClass(classOf[Functions])
|
|
15
|
+ .evalExpression(
|
|
16
|
+ "#test('check')", new {}, classOf[String]) should equal ("check")
|
|
17
|
+ }
|
|
18
|
+
|
|
19
|
+ it should "not register non public methods " in {
|
|
20
|
+ val spelHelper = new SpelHelper()
|
|
21
|
+ .registerFunctionsFromClass(classOf[Functions])
|
|
22
|
+ evaluating { spelHelper.evalExpression("#testNonPublic('check')",
|
|
23
|
+ new {}, classOf[String]) } should produce [SpelEvaluationException]
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ it should "not register non static methods " in {
|
|
27
|
+ val spelHelper = new SpelHelper()
|
|
28
|
+ .registerFunctionsFromClass(classOf[Functions])
|
|
29
|
+ evaluating { spelHelper.evalExpression("#testNonStatic('check')",
|
|
30
|
+ new {}, classOf[String]) } should produce [SpelEvaluationException]
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ it should "not register void methods " in {
|
|
34
|
+ val spelHelper = new SpelHelper()
|
|
35
|
+ .registerFunctionsFromClass(classOf[Functions])
|
|
36
|
+ evaluating { spelHelper.evalExpression("#testVoid('check')",
|
|
37
|
+ new {}, classOf[String]) } should produce [SpelEvaluationException]
|
52
|
38
|
}
|
53
|
|
-}
|
54
|
39
|
|
|
40
|
+ it should "register implicit methods " in {
|
|
41
|
+ new SpelHelper()
|
|
42
|
+ .registerImplicitMethodsFromClass(classOf[Functions])
|
|
43
|
+ .lookupImplicitMethod("java.lang.String.test") should equal(
|
|
44
|
+ classOf[Functions].getMethod("test", classOf[String]))
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ it should "not register methods with no args as implicit methods " in {
|
|
48
|
+ new SpelHelper()
|
|
49
|
+ .registerImplicitMethodsFromClass(classOf[Functions])
|
|
50
|
+ .lookupImplicitMethod("java.lang.String.testNoArg") should be (null);
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ it should "register implicit constructors " in {
|
|
54
|
+ new SpelHelper()
|
|
55
|
+ .registerConstructorsFromClass(classOf[Functions])
|
|
56
|
+ .lookupImplicitConstructor("Functions[]") should equal(
|
|
57
|
+ classOf[Functions].getConstructor())
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ it should "evaluate implicit methods " in {
|
|
61
|
+ new SpelHelper()
|
|
62
|
+ .registerImplicitMethodsFromClass(classOf[Functions])
|
|
63
|
+ .evalExpression(
|
|
64
|
+ "'check'.test()", new {}, classOf[String]) should equal ("check")
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ it should "evaluate implicit constructors " in {
|
|
68
|
+ new SpelHelper()
|
|
69
|
+ .registerConstructorsFromClass(classOf[Functions])
|
|
70
|
+ .evalExpression(
|
|
71
|
+ "new Functions()", new {}, classOf[Functions]) should equal (new Functions)
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ it should "evaluate implicit properties " in {
|
|
75
|
+ new SpelHelper().evalExpression(
|
|
76
|
+ "'abc'.hashCode", new {}, classOf[int]) should equal ("abc".hashCode)
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ it should "evaluate multiple expressions " in {
|
|
80
|
+ new SpelHelper().evalExpressions(
|
|
81
|
+ Array("#s='check'", "#s"), new {}, classOf[String]) should equal ("check")
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ it should "throw IllegalArgumentException when trying to evaluate " +
|
|
85
|
+ "blank multiple expressions " in {
|
|
86
|
+ evaluating { new SpelHelper().evalExpressions(
|
|
87
|
+ Array[String](), new {}, classOf[String]) } should produce [IllegalArgumentException]
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ it should "return evaluation context inside a method called " +
|
|
91
|
+ "from SpEL expression " in {
|
|
92
|
+ new SpelHelper()
|
|
93
|
+ .registerFunctionsFromClass(classOf[Functions])
|
|
94
|
+ .evalExpression(
|
|
95
|
+ "#testContext('check')", new {}, classOf[String]) should equal ("check")
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ it should "not return evaluation context outside a method called " +
|
|
99
|
+ "from SpEL expression " in {
|
|
100
|
+ SpelHelper.getCurrentContext should be (null)
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+}
|