|
| 1 | +package io.github.intellinside.cache.tags.annotation; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import java.lang.reflect.Method; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 8 | + |
| 9 | +public class SpelExpressionEvaluatorTest { |
| 10 | + private final SpelExpressionEvaluator evaluator = new SpelExpressionEvaluator(); |
| 11 | + |
| 12 | + static class Sample { |
| 13 | + public void foo(String userId, int count) { |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + static class Result { |
| 18 | + private final int id; |
| 19 | + |
| 20 | + Result(int id) { |
| 21 | + this.id = id; |
| 22 | + } |
| 23 | + |
| 24 | + public int getId() { |
| 25 | + return id; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void evaluateWithArgsIndex() throws Exception { |
| 31 | + Method method = Sample.class.getMethod("foo", String.class, int.class); |
| 32 | + String out = evaluator.evaluate("'cache:' + #args[0]", method, null, "123", 5); |
| 33 | + assertEquals("cache:123", out); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void evaluateWithResultProperty() throws Exception { |
| 38 | + Method method = Sample.class.getMethod("foo", String.class, int.class); |
| 39 | + Result result = new Result(42); |
| 40 | + String out = evaluator.evaluate("'result:' + #result.id", method, result, "x", 1); |
| 41 | + assertEquals("result:42", out); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void evaluateWithTemplateParserContext() throws Exception { |
| 46 | + Method method = Sample.class.getMethod("foo", String.class, int.class); |
| 47 | + String out = evaluator.evaluate("User: #{#args[0]}", method, null, "7", 2); |
| 48 | + assertEquals("User: 7", out); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void evaluateWithParamName() throws Exception { |
| 53 | + Method method = Sample.class.getMethod("foo", String.class, int.class); |
| 54 | + String out = evaluator.evaluate("'cache:' + #userId", method, null, "abc", 3); |
| 55 | + assertEquals("cache:abc", out); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void evaluateWithParamNameTemplate() throws Exception { |
| 60 | + Method method = Sample.class.getMethod("foo", String.class, int.class); |
| 61 | + String out = evaluator.evaluate("User: #{#userId}", method, null, "xyz", 4); |
| 62 | + assertEquals("User: xyz", out); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void evaluateMethodVariable() throws Exception { |
| 67 | + Method method = Sample.class.getMethod("foo", String.class, int.class); |
| 68 | + String out = evaluator.evaluate("#method.name", method, null, "a", 0); |
| 69 | + assertEquals(method.getName(), out); |
| 70 | + } |
| 71 | +} |
0 commit comments