22
22
23
23
import org .springframework .core .Ordered ;
24
24
import org .springframework .expression .Expression ;
25
+ import org .springframework .expression .spel .SpelCompilationCoverageTests ;
25
26
import org .springframework .expression .spel .SpelCompilerMode ;
26
27
import org .springframework .expression .spel .SpelParserConfiguration ;
28
+ import org .springframework .expression .spel .support .StandardEvaluationContext ;
27
29
28
- import static org .junit .Assert .assertEquals ;
30
+ import static org .junit .Assert .* ;
29
31
30
32
/**
31
33
* Tests for the {@link SpelCompiler}.
32
34
*
33
35
* @author Sam Brannen
36
+ * @author Andy Clement
34
37
* @since 5.1.14
35
38
*/
36
39
public class SpelCompilerTests {
37
40
38
- @ Test // gh-24357
41
+ @ Test // gh-24357
39
42
public void expressionCompilesWhenMethodComesFromPublicInterface () {
40
43
SpelParserConfiguration config = new SpelParserConfiguration (SpelCompilerMode .IMMEDIATE , null );
41
44
SpelExpressionParser parser = new SpelExpressionParser (config );
@@ -47,6 +50,31 @@ public void expressionCompilesWhenMethodComesFromPublicInterface() {
47
50
IntStream .rangeClosed (1 , 5 ).forEach (i -> assertEquals (42 , expression .getValue (component )));
48
51
}
49
52
53
+ @ Test // gh-25706
54
+ public void defaultMethodInvocation () {
55
+ SpelParserConfiguration config = new SpelParserConfiguration (SpelCompilerMode .IMMEDIATE , null );
56
+ SpelExpressionParser parser = new SpelExpressionParser (config );
57
+
58
+ StandardEvaluationContext context = new StandardEvaluationContext ();
59
+ Item item = new Item ();
60
+ context .setRootObject (item );
61
+
62
+ Expression expression = parser .parseExpression ("#root.isEditable2()" );
63
+ assertFalse (SpelCompiler .compile (expression ));
64
+ assertEquals (false , expression .getValue (context ));
65
+ assertTrue (SpelCompiler .compile (expression ));
66
+ SpelCompilationCoverageTests .assertIsCompiled (expression );
67
+ assertEquals (false , expression .getValue (context ));
68
+
69
+ context .setVariable ("user" , new User ());
70
+ expression = parser .parseExpression ("#root.isEditable(#user)" );
71
+ assertFalse (SpelCompiler .compile (expression ));
72
+ assertEquals (true , expression .getValue (context ));
73
+ assertTrue (SpelCompiler .compile (expression ));
74
+ SpelCompilationCoverageTests .assertIsCompiled (expression );
75
+ assertEquals (true , expression .getValue (context ));
76
+ }
77
+
50
78
51
79
static class OrderedComponent implements Ordered {
52
80
@@ -56,4 +84,40 @@ public int getOrder() {
56
84
}
57
85
}
58
86
87
+
88
+ public static class User {
89
+
90
+ boolean isAdmin () {
91
+ return true ;
92
+ }
93
+ }
94
+
95
+
96
+ public static class Item implements Editable {
97
+
98
+ // some fields
99
+ private String someField = "" ;
100
+
101
+ // some getters and setters
102
+
103
+ @ Override
104
+ public boolean hasSomeProperty () {
105
+ return someField != null ;
106
+ }
107
+ }
108
+
109
+
110
+ public interface Editable {
111
+
112
+ default boolean isEditable (User user ) {
113
+ return user .isAdmin () && hasSomeProperty ();
114
+ }
115
+
116
+ default boolean isEditable2 () {
117
+ return false ;
118
+ }
119
+
120
+ boolean hasSomeProperty ();
121
+ }
122
+
59
123
}
0 commit comments