View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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 }