From a6a0fc792d793dda46f90e1bee139d2a2135f392 Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Fri, 28 May 2010 13:24:11 +0530 Subject: [PATCH] Added more Scalatest specs --- SpelHelper.iml | 4 + .../abhinavsarkar/spelhelper/Functions.java | 22 +++ .../spelhelper/ExtensionFunctionsSpec.scala | 9 +- .../spelhelper/ImplicitMethodsSpec.scala | 2 +- .../spelhelper/SpelHelperSpec.scala | 137 ++++++++++++------ 5 files changed, 128 insertions(+), 46 deletions(-) diff --git a/SpelHelper.iml b/SpelHelper.iml index 8970c06..bb9d4a8 100644 --- a/SpelHelper.iml +++ b/SpelHelper.iml @@ -22,6 +22,10 @@ + + + + diff --git a/src/test/java/net/abhinavsarkar/spelhelper/Functions.java b/src/test/java/net/abhinavsarkar/spelhelper/Functions.java index dbc637a..96a2d44 100644 --- a/src/test/java/net/abhinavsarkar/spelhelper/Functions.java +++ b/src/test/java/net/abhinavsarkar/spelhelper/Functions.java @@ -6,6 +6,28 @@ public final class Functions { return str; } + static String testNonPublic(String str) { + return str; + } + + public String testNonStatic(String str) { + return str; + } + + public static void testVoid(String str) { + return; + } + + public static String testNoArg() { + return "a"; + } + + public static String testContext(String str) { + if (SpelHelper.getCurrentContext() == null) + throw new AssertionError(); + return str; + } + @Override public boolean equals(Object o) { return o instanceof Functions; diff --git a/src/test/scala/net/abhinavsarkar/spelhelper/ExtensionFunctionsSpec.scala b/src/test/scala/net/abhinavsarkar/spelhelper/ExtensionFunctionsSpec.scala index d1b397d..6a1a502 100644 --- a/src/test/scala/net/abhinavsarkar/spelhelper/ExtensionFunctionsSpec.scala +++ b/src/test/scala/net/abhinavsarkar/spelhelper/ExtensionFunctionsSpec.scala @@ -7,6 +7,7 @@ import org.scalatest.junit.ShouldMatchersForJUnit import java.util.{Set => JSet, HashSet, List => JList, ArrayList, Map => JMap, HashMap} +import org.springframework.expression.spel.SpelEvaluationException @RunWith(classOf[JUnitRunner]) class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit { @@ -32,4 +33,10 @@ class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit { new {}, classOf[JMap[String,Int]]) should equal(map) } -} \ No newline at end of file + "Extension Function 'map'" should "throw SpelEvaluationException" + + "if length of key and values lists is not same " in { + evaluating { new SpelHelper().evalExpression("#map(#list('a','b','c'),#list(1,2))", + new {}, classOf[JMap[String,Int]]) } should produce [SpelEvaluationException] + } + +} diff --git a/src/test/scala/net/abhinavsarkar/spelhelper/ImplicitMethodsSpec.scala b/src/test/scala/net/abhinavsarkar/spelhelper/ImplicitMethodsSpec.scala index faf510a..013573b 100644 --- a/src/test/scala/net/abhinavsarkar/spelhelper/ImplicitMethodsSpec.scala +++ b/src/test/scala/net/abhinavsarkar/spelhelper/ImplicitMethodsSpec.scala @@ -49,4 +49,4 @@ class ImplicitMethodsSpec extends FlatSpec with ShouldMatchersForJUnit { new {}, classOf[JList[String]]) should equal(list) } -} \ No newline at end of file +} diff --git a/src/test/scala/net/abhinavsarkar/spelhelper/SpelHelperSpec.scala b/src/test/scala/net/abhinavsarkar/spelhelper/SpelHelperSpec.scala index a4e09e8..d62e6af 100644 --- a/src/test/scala/net/abhinavsarkar/spelhelper/SpelHelperSpec.scala +++ b/src/test/scala/net/abhinavsarkar/spelhelper/SpelHelperSpec.scala @@ -2,53 +2,102 @@ package net.abhinavsarkar.spelhelper import org.scalatest.junit.JUnitRunner import org.junit.runner.RunWith -import org.scalatest.Spec +import org.scalatest.FlatSpec import org.scalatest.junit.ShouldMatchersForJUnit +import org.springframework.expression.spel.SpelEvaluationException @RunWith(classOf[JUnitRunner]) -class SpelHelperSpec extends Spec with ShouldMatchersForJUnit { - describe("SpelHelper") { - - it ("should register and evaluate functions ") { - new SpelHelper() - .registerFunctionsFromClass(classOf[Functions]) - .evalExpression( - "#test('check')", new {}, classOf[String]) should equal("check") - } - - it ("should register implicit methods ") { - new SpelHelper() - .registerImplicitMethodsFromClass(classOf[Functions]) - .lookupImplicitMethod("java.lang.String.test") should equal( - classOf[Functions].getMethod("test", classOf[String])) - } - - it ("should register implicit constructors ") { - new SpelHelper() - .registerConstructorsFromClass(classOf[Functions]) - .lookupImplicitConstructor("Functions[]") should equal( - classOf[Functions].getConstructor()) - } - - it ("should evaluate implicit methods ") { - new SpelHelper() - .registerImplicitMethodsFromClass(classOf[Functions]) - .evalExpression( - "'check'.test()", new {}, classOf[String]) should equal("check") - } - - it ("should evaluate implicit constructors ") { - new SpelHelper() - .registerConstructorsFromClass(classOf[Functions]) - .evalExpression( - "new Functions()", new {}, classOf[Functions]) should equal(new Functions) - } - - it ("should evaluate implicit properties ") { - new SpelHelper().evalExpression( - "'abc'.hashCode", new {}, classOf[int]) should equal("abc".hashCode) - } +class SpelHelperSpec extends FlatSpec with ShouldMatchersForJUnit { + "SpelHelper" should "register and evaluate functions " in { + new SpelHelper() + .registerFunctionsFromClass(classOf[Functions]) + .evalExpression( + "#test('check')", new {}, classOf[String]) should equal ("check") } -} + it should "not register non public methods " in { + val spelHelper = new SpelHelper() + .registerFunctionsFromClass(classOf[Functions]) + evaluating { spelHelper.evalExpression("#testNonPublic('check')", + new {}, classOf[String]) } should produce [SpelEvaluationException] + } + + it should "not register non static methods " in { + val spelHelper = new SpelHelper() + .registerFunctionsFromClass(classOf[Functions]) + evaluating { spelHelper.evalExpression("#testNonStatic('check')", + new {}, classOf[String]) } should produce [SpelEvaluationException] + } + + it should "not register void methods " in { + val spelHelper = new SpelHelper() + .registerFunctionsFromClass(classOf[Functions]) + evaluating { spelHelper.evalExpression("#testVoid('check')", + new {}, classOf[String]) } should produce [SpelEvaluationException] + } + + it should "register implicit methods " in { + new SpelHelper() + .registerImplicitMethodsFromClass(classOf[Functions]) + .lookupImplicitMethod("java.lang.String.test") should equal( + classOf[Functions].getMethod("test", classOf[String])) + } + + it should "not register methods with no args as implicit methods " in { + new SpelHelper() + .registerImplicitMethodsFromClass(classOf[Functions]) + .lookupImplicitMethod("java.lang.String.testNoArg") should be (null); + } + + it should "register implicit constructors " in { + new SpelHelper() + .registerConstructorsFromClass(classOf[Functions]) + .lookupImplicitConstructor("Functions[]") should equal( + classOf[Functions].getConstructor()) + } + + it should "evaluate implicit methods " in { + new SpelHelper() + .registerImplicitMethodsFromClass(classOf[Functions]) + .evalExpression( + "'check'.test()", new {}, classOf[String]) should equal ("check") + } + + it should "evaluate implicit constructors " in { + new SpelHelper() + .registerConstructorsFromClass(classOf[Functions]) + .evalExpression( + "new Functions()", new {}, classOf[Functions]) should equal (new Functions) + } + + it should "evaluate implicit properties " in { + new SpelHelper().evalExpression( + "'abc'.hashCode", new {}, classOf[int]) should equal ("abc".hashCode) + } + + it should "evaluate multiple expressions " in { + new SpelHelper().evalExpressions( + Array("#s='check'", "#s"), new {}, classOf[String]) should equal ("check") + } + + it should "throw IllegalArgumentException when trying to evaluate " + + "blank multiple expressions " in { + evaluating { new SpelHelper().evalExpressions( + Array[String](), new {}, classOf[String]) } should produce [IllegalArgumentException] + } + + it should "return evaluation context inside a method called " + + "from SpEL expression " in { + new SpelHelper() + .registerFunctionsFromClass(classOf[Functions]) + .evalExpression( + "#testContext('check')", new {}, classOf[String]) should equal ("check") + } + + it should "not return evaluation context outside a method called " + + "from SpEL expression " in { + SpelHelper.getCurrentContext should be (null) + } + +}