View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
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  }