parent
555b0cc383
commit
ce4a80d105
@ -0,0 +1,13 @@ |
||||
package net.abhinavsarkar.spelhelper; |
||||
|
||||
public final class Functions { |
||||
|
||||
public static String test(String str) { |
||||
return str; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object o) { |
||||
return o instanceof Functions; |
||||
} |
||||
} |
@ -1,43 +0,0 @@ |
||||
package net.abhinavsarkar.spelhelper; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
public class SpelHelperTest { |
||||
|
||||
@Test |
||||
public void testRegisteredFunction() { |
||||
Assert.assertEquals( |
||||
Arrays.asList("abhinav", "mini", "dan"), |
||||
new SpelHelper().evalExpression( |
||||
"#list('abhinav','mini','dan')", new Object(), List.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void testImplicitMethod() { |
||||
Assert.assertEquals( |
||||
Arrays.asList("abhinav", "dan", "mini"), |
||||
new SpelHelper().evalExpression( |
||||
"#list('abhinav','mini','dan').sorted", new Object(), List.class)); |
||||
} |
||||
|
||||
public static final class ConstructorTest { |
||||
@Override |
||||
public boolean equals(final Object o) { |
||||
return o instanceof ConstructorTest; |
||||
} |
||||
} |
||||
|
||||
@Test |
||||
public void testImplicitConstructor() { |
||||
Assert.assertEquals( |
||||
new ConstructorTest(), |
||||
new SpelHelper() |
||||
.registerConstructorsFromClass(ConstructorTest.class) |
||||
.evalExpression("new ConstructorTest()", new Object(), ConstructorTest.class)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,54 @@ |
||||
package net.abhinavsarkar.spelhelper |
||||
|
||||
import org.scalatest.junit.JUnitRunner |
||||
import org.junit.runner.RunWith |
||||
import org.scalatest.Spec |
||||
import org.scalatest.junit.ShouldMatchersForJUnit |
||||
|
||||
@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) |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue