Added more Scalatest specs

master
Abhinav Sarkar 2010-05-28 13:24:11 +05:30
parent 988ca2dc66
commit a6a0fc792d
5 changed files with 128 additions and 46 deletions

View File

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

View File

@ -6,6 +6,28 @@ public final class Functions {
return str; 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
return o instanceof Functions; return o instanceof Functions;

View File

@ -7,6 +7,7 @@ import org.scalatest.junit.ShouldMatchersForJUnit
import java.util.{Set => JSet, HashSet, import java.util.{Set => JSet, HashSet,
List => JList, ArrayList, List => JList, ArrayList,
Map => JMap, HashMap} Map => JMap, HashMap}
import org.springframework.expression.spel.SpelEvaluationException
@RunWith(classOf[JUnitRunner]) @RunWith(classOf[JUnitRunner])
class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit { class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit {
@ -32,4 +33,10 @@ class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit {
new {}, classOf[JMap[String,Int]]) should equal(map) new {}, classOf[JMap[String,Int]]) should equal(map)
} }
} "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]
}
}

View File

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

View File

@ -2,53 +2,102 @@ package net.abhinavsarkar.spelhelper
import org.scalatest.junit.JUnitRunner import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.scalatest.Spec import org.scalatest.FlatSpec
import org.scalatest.junit.ShouldMatchersForJUnit import org.scalatest.junit.ShouldMatchersForJUnit
import org.springframework.expression.spel.SpelEvaluationException
@RunWith(classOf[JUnitRunner]) @RunWith(classOf[JUnitRunner])
class SpelHelperSpec extends Spec with ShouldMatchersForJUnit { class SpelHelperSpec extends FlatSpec 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)
}
"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)
}
}