Browse Source

Added more Scalatest specs

Abhinav Sarkar 10 years ago
parent
commit
a6a0fc792d

+ 4
- 0
SpelHelper.iml View File

@@ -22,6 +22,10 @@
22 22
       <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
23 23
       <sourceFolder url="file://$MODULE_DIR$/src/test/scala" isTestSource="true" />
24 24
       <excludeFolder url="file://$MODULE_DIR$/target/classes" />
25
+      <excludeFolder url="file://$MODULE_DIR$/target/cobertura" />
26
+      <excludeFolder url="file://$MODULE_DIR$/target/generated-classes" />
27
+      <excludeFolder url="file://$MODULE_DIR$/target/javadoc-bundle-options" />
28
+      <excludeFolder url="file://$MODULE_DIR$/target/maven-archiver" />
25 29
       <excludeFolder url="file://$MODULE_DIR$/target/site" />
26 30
       <excludeFolder url="file://$MODULE_DIR$/target/surefire-reports" />
27 31
       <excludeFolder url="file://$MODULE_DIR$/target/test-classes" />

+ 22
- 0
src/test/java/net/abhinavsarkar/spelhelper/Functions.java View File

@@ -6,6 +6,28 @@ public final class Functions {
6 6
         return str;
7 7
     }
8 8
 
9
+    static String testNonPublic(String str) {
10
+        return str;
11
+    }
12
+
13
+    public String testNonStatic(String str) {
14
+        return str;
15
+    }
16
+
17
+    public static void testVoid(String str) {
18
+        return;
19
+    }
20
+
21
+    public static String testNoArg() {
22
+        return "a";
23
+    }
24
+
25
+    public static String testContext(String str) {
26
+        if (SpelHelper.getCurrentContext() == null)
27
+            throw new AssertionError();
28
+        return str;
29
+    }
30
+
9 31
     @Override
10 32
     public boolean equals(Object o) {
11 33
         return o instanceof Functions;

+ 8
- 1
src/test/scala/net/abhinavsarkar/spelhelper/ExtensionFunctionsSpec.scala View File

@@ -7,6 +7,7 @@ import org.scalatest.junit.ShouldMatchersForJUnit
7 7
 import java.util.{Set => JSet, HashSet,
8 8
   List => JList, ArrayList,
9 9
   Map => JMap, HashMap}
10
+import org.springframework.expression.spel.SpelEvaluationException
10 11
 
11 12
 @RunWith(classOf[JUnitRunner])
12 13
 class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit {
@@ -32,4 +33,10 @@ class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit {
32 33
       new {}, classOf[JMap[String,Int]]) should equal(map)
33 34
   }
34 35
 
35
-}
36
+  "Extension Function 'map'" should "throw SpelEvaluationException" +
37
+          "if length of key and values lists is not same " in {
38
+    evaluating { new SpelHelper().evalExpression("#map(#list('a','b','c'),#list(1,2))",
39
+      new {}, classOf[JMap[String,Int]]) } should produce [SpelEvaluationException]
40
+  }
41
+
42
+}

+ 1
- 1
src/test/scala/net/abhinavsarkar/spelhelper/ImplicitMethodsSpec.scala View File

@@ -49,4 +49,4 @@ class ImplicitMethodsSpec extends FlatSpec with ShouldMatchersForJUnit {
49 49
       new {}, classOf[JList[String]]) should equal(list)
50 50
   }
51 51
   
52
-}
52
+}

+ 93
- 44
src/test/scala/net/abhinavsarkar/spelhelper/SpelHelperSpec.scala View File

@@ -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
+}

Loading…
Cancel
Save