1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.builder.xml.dynamic;
17
18 import static org.junit.jupiter.api.Assertions.*;
19
20 import java.util.HashMap;
21
22 import org.apache.ibatis.domain.blog.Author;
23 import org.apache.ibatis.domain.blog.Section;
24 import org.apache.ibatis.scripting.xmltags.ExpressionEvaluator;
25 import org.junit.jupiter.api.Test;
26
27 class ExpressionEvaluatorTest {
28
29 private ExpressionEvaluator evaluator = new ExpressionEvaluator();
30
31 @Test
32 void shouldCompareStringsReturnTrue() {
33 boolean value = evaluator.evaluateBoolean("username == 'cbegin'", new Author(1, "cbegin", "******", "cbegin@apache.org", "N/A", Section.NEWS));
34 assertTrue(value);
35 }
36
37 @Test
38 void shouldCompareStringsReturnFalse() {
39 boolean value = evaluator.evaluateBoolean("username == 'norm'", new Author(1, "cbegin", "******", "cbegin@apache.org", "N/A", Section.NEWS));
40 assertFalse(value);
41 }
42
43 @Test
44 void shouldReturnTrueIfNotNull() {
45 boolean value = evaluator.evaluateBoolean("username", new Author(1, "cbegin", "******", "cbegin@apache.org", "N/A", Section.NEWS));
46 assertTrue(value);
47 }
48
49 @Test
50 void shouldReturnFalseIfNull() {
51 boolean value = evaluator.evaluateBoolean("password", new Author(1, "cbegin", null, "cbegin@apache.org", "N/A", Section.NEWS));
52 assertFalse(value);
53 }
54
55 @Test
56 void shouldReturnTrueIfNotZero() {
57 boolean value = evaluator.evaluateBoolean("id", new Author(1, "cbegin", null, "cbegin@apache.org", "N/A", Section.NEWS));
58 assertTrue(value);
59 }
60
61 @Test
62 void shouldReturnFalseIfZero() {
63 boolean value = evaluator.evaluateBoolean("id", new Author(0, "cbegin", null, "cbegin@apache.org", "N/A", Section.NEWS));
64 assertFalse(value);
65 }
66
67 @Test
68 void shouldReturnFalseIfZeroWithScale() {
69 class Bean {
70 @SuppressWarnings("unused")
71 public double d = 0.0d;
72 }
73 assertFalse(evaluator.evaluateBoolean("d", new Bean()));
74 }
75
76 @Test
77 void shouldIterateOverIterable() {
78 final HashMap<String, String[]> parameterObject = new HashMap<String, String[]>() {{
79 put("array", new String[]{"1", "2", "3"});
80 }};
81 final Iterable<?> iterable = evaluator.evaluateIterable("array", parameterObject);
82 int i = 0;
83 for (Object o : iterable) {
84 assertEquals(String.valueOf(++i), o);
85 }
86 }
87
88
89 }