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