Skip to content

Commit 035ee49

Browse files
committed
Added unit test
1 parent 7b6e7c1 commit 035ee49

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

spring-cache-tags/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
<artifactId>spring-cache-tags</artifactId>
1414

1515
<properties>
16+
<maven-compiler-plugin.version>3.14.1</maven-compiler-plugin.version>
17+
<maven-surefire-plugin.version>3.5.4</maven-surefire-plugin.version>
1618
</properties>
1719

1820
<dependencies>
@@ -36,5 +38,34 @@
3638
<groupId>jakarta.annotation</groupId>
3739
<artifactId>jakarta.annotation-api</artifactId>
3840
</dependency>
41+
<!-- Test dependencies -->
42+
<dependency>
43+
<groupId>org.junit.jupiter</groupId>
44+
<artifactId>junit-jupiter</artifactId>
45+
<scope>test</scope>
46+
</dependency>
3947
</dependencies>
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-compiler-plugin</artifactId>
53+
<version>${maven-compiler-plugin.version}</version>
54+
<configuration>
55+
<compilerArgs>
56+
<arg>-parameters</arg>
57+
</compilerArgs>
58+
<release>${java.version}</release>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>${maven-surefire-plugin.version}</version>
65+
<configuration>
66+
<useModulePath>false</useModulePath>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
4071
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)