View Javadoc
1   /*
2    *    Copyright 2016-2025 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.mybatis.dynamic.sql.where.condition;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20  
21  import org.junit.jupiter.api.Test;
22  import org.mybatis.dynamic.sql.SqlBuilder;
23  
24  import java.util.NoSuchElementException;
25  
26  // series of tests to check that the Supplier based DSL methods function correctly
27  
28  class SupplierTest {
29      @Test
30      void testIsBetween() {
31          IsBetween<Integer> cond = SqlBuilder.isBetween(() -> 3).and(() -> 4);
32          assertThat(cond.value1()).isEqualTo(3);
33          assertThat(cond.value2()).isEqualTo(4);
34          assertThat(cond.isEmpty()).isFalse();
35      }
36  
37      @Test
38      void testIsBetweenWhenPresent() {
39          IsBetweenWhenPresent<Integer> cond = SqlBuilder.isBetweenWhenPresent(() -> 3).and(() -> 4);
40          assertThat(cond.value1()).isEqualTo(3);
41          assertThat(cond.value2()).isEqualTo(4);
42          assertThat(cond.isEmpty()).isFalse();
43      }
44  
45      @Test
46      void testIsBetweenWhenPresentNull() {
47          IsBetweenWhenPresent<Integer> cond = SqlBuilder.isBetweenWhenPresent(() -> (Integer) null).and(() -> null);
48          assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value1);
49          assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value2);
50          assertThat(cond.isEmpty()).isTrue();
51      }
52  
53      @Test
54      void testIsNotBetween() {
55          IsNotBetween<Integer> cond = SqlBuilder.isNotBetween(() -> 3).and(() -> 4);
56          assertThat(cond.value1()).isEqualTo(3);
57          assertThat(cond.value2()).isEqualTo(4);
58          assertThat(cond.isEmpty()).isFalse();
59      }
60  
61      @Test
62      void testIsNotBetweenWhenPresent() {
63          IsNotBetweenWhenPresent<Integer> cond = SqlBuilder.isNotBetweenWhenPresent(() -> 3).and(() -> 4);
64          assertThat(cond.value1()).isEqualTo(3);
65          assertThat(cond.value2()).isEqualTo(4);
66          assertThat(cond.isEmpty()).isFalse();
67      }
68  
69      @Test
70      void testIsNotBetweenWhenPresentNull() {
71          IsNotBetweenWhenPresent<Integer> cond = SqlBuilder.isNotBetweenWhenPresent(() -> (Integer) null).and(() -> null);
72          assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value1);
73          assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value2);
74          assertThat(cond.isEmpty()).isTrue();
75      }
76  
77      @Test
78      void testIsEqualToWhenPresent() {
79          IsEqualToWhenPresent<Integer> cond = SqlBuilder.isEqualToWhenPresent(() -> 3);
80          assertThat(cond.value()).isEqualTo(3);
81          assertThat(cond.isEmpty()).isFalse();
82      }
83  
84      @Test
85      void testIsEqualToWhenPresentNull() {
86          IsEqualToWhenPresent<Integer> cond = SqlBuilder.isEqualToWhenPresent(() -> null);
87          assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
88          assertThat(cond.isEmpty()).isTrue();
89      }
90  
91      @Test
92      void testIsNotEqualTo() {
93          IsNotEqualTo<Integer> cond = SqlBuilder.isNotEqualTo(() -> 3);
94          assertThat(cond.value()).isEqualTo(3);
95          assertThat(cond.isEmpty()).isFalse();
96      }
97  
98      @Test
99      void testIsNotEqualToWhenPresent() {
100         IsNotEqualToWhenPresent<Integer> cond = SqlBuilder.isNotEqualToWhenPresent(() -> 3);
101         assertThat(cond.value()).isEqualTo(3);
102         assertThat(cond.isEmpty()).isFalse();
103     }
104 
105     @Test
106     void testIsNotEqualToWhenPresentNull() {
107         IsNotEqualToWhenPresent<Integer> cond = SqlBuilder.isNotEqualToWhenPresent(() -> null);
108         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
109         assertThat(cond.isEmpty()).isTrue();
110     }
111 
112     @Test
113     void testIsGreaterThan() {
114         IsGreaterThan<Integer> cond = SqlBuilder.isGreaterThan(() -> 3);
115         assertThat(cond.value()).isEqualTo(3);
116         assertThat(cond.isEmpty()).isFalse();
117     }
118 
119     @Test
120     void testIsGreaterThanWhenPresent() {
121         IsGreaterThanWhenPresent<Integer> cond = SqlBuilder.isGreaterThanWhenPresent(() -> 3);
122         assertThat(cond.value()).isEqualTo(3);
123         assertThat(cond.isEmpty()).isFalse();
124     }
125 
126     @Test
127     void testIsGreaterThanWhenPresentNull() {
128         IsGreaterThanWhenPresent<Integer> cond = SqlBuilder.isGreaterThanWhenPresent(() -> null);
129         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
130         assertThat(cond.isEmpty()).isTrue();
131     }
132 
133     @Test
134     void testIsGreaterThanOrEqualTo() {
135         IsGreaterThanOrEqualTo<Integer> cond = SqlBuilder.isGreaterThanOrEqualTo(() -> 3);
136         assertThat(cond.value()).isEqualTo(3);
137         assertThat(cond.isEmpty()).isFalse();
138     }
139 
140     @Test
141     void testIsGreaterThanOrEqualToWhenPresent() {
142         IsGreaterThanOrEqualToWhenPresent<Integer> cond = SqlBuilder.isGreaterThanOrEqualToWhenPresent(() -> 3);
143         assertThat(cond.value()).isEqualTo(3);
144         assertThat(cond.isEmpty()).isFalse();
145     }
146 
147     @Test
148     void testIsGreaterThanOrEqualToWhenPresentNull() {
149         IsGreaterThanOrEqualToWhenPresent<Integer> cond = SqlBuilder.isGreaterThanOrEqualToWhenPresent(() -> null);
150         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
151         assertThat(cond.isEmpty()).isTrue();
152     }
153 
154     @Test
155     void testIsLessThan() {
156         IsLessThan<Integer> cond = SqlBuilder.isLessThan(() -> 3);
157         assertThat(cond.value()).isEqualTo(3);
158         assertThat(cond.isEmpty()).isFalse();
159     }
160 
161     @Test
162     void testIsLessThanWhenPresent() {
163         IsLessThanWhenPresent<Integer> cond = SqlBuilder.isLessThanWhenPresent(() -> 3);
164         assertThat(cond.value()).isEqualTo(3);
165         assertThat(cond.isEmpty()).isFalse();
166     }
167 
168     @Test
169     void testIsLessThanWhenPresentNull() {
170         IsLessThanWhenPresent<Integer> cond = SqlBuilder.isLessThanWhenPresent(() -> null);
171         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
172         assertThat(cond.isEmpty()).isTrue();
173     }
174 
175     @Test
176     void testIsLessThanOrEqualTo() {
177         IsLessThanOrEqualTo<Integer> cond = SqlBuilder.isLessThanOrEqualTo(() -> 3);
178         assertThat(cond.value()).isEqualTo(3);
179         assertThat(cond.isEmpty()).isFalse();
180     }
181 
182     @Test
183     void testIsLessThanOrEqualToWhenPresent() {
184         IsLessThanOrEqualToWhenPresent<Integer> cond = SqlBuilder.isLessThanOrEqualToWhenPresent(() -> 3);
185         assertThat(cond.value()).isEqualTo(3);
186         assertThat(cond.isEmpty()).isFalse();
187     }
188 
189     @Test
190     void testIsLessThanOrEqualToWhenPresentNull() {
191         IsLessThanOrEqualToWhenPresent<Integer> cond = SqlBuilder.isLessThanOrEqualToWhenPresent(() -> null);
192         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
193         assertThat(cond.isEmpty()).isTrue();
194     }
195 
196     @Test
197     void testIsLike() {
198         IsLike<String> cond = SqlBuilder.isLike(() -> "%F%");
199         assertThat(cond.value()).isEqualTo("%F%");
200         assertThat(cond.isEmpty()).isFalse();
201     }
202 
203     @Test
204     void testIsLikeCaseInsensitive() {
205         IsLikeCaseInsensitive<String> cond = SqlBuilder.isLikeCaseInsensitive(() -> "%f%");
206         assertThat(cond.value()).isEqualTo("%F%");
207         assertThat(cond.isEmpty()).isFalse();
208     }
209 
210     @Test
211     void testIsLikeCaseInsensitiveWhenPresent() {
212         IsLikeCaseInsensitiveWhenPresent<String> cond = SqlBuilder.isLikeCaseInsensitiveWhenPresent(() -> "%f%");
213         assertThat(cond.value()).isEqualTo("%F%");
214         assertThat(cond.isEmpty()).isFalse();
215     }
216 
217     @Test
218     void testIsLikeCaseInsensitiveWhenPresentNull() {
219         IsLikeCaseInsensitiveWhenPresent<String> cond = SqlBuilder.isLikeCaseInsensitiveWhenPresent(() -> null);
220         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
221         assertThat(cond.isEmpty()).isTrue();
222     }
223 
224     @Test
225     void testIsLikeWhenPresent() {
226         IsLikeWhenPresent<String> cond = SqlBuilder.isLikeWhenPresent(() -> "%F%");
227         assertThat(cond.value()).isEqualTo("%F%");
228         assertThat(cond.isEmpty()).isFalse();
229     }
230 
231     @Test
232     void testIsLikeWhenPresentNull() {
233         IsLikeWhenPresent<String> cond = SqlBuilder.isLikeWhenPresent(() -> null);
234         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
235         assertThat(cond.isEmpty()).isTrue();
236     }
237 
238     @Test
239     void testIsNotLike() {
240         IsNotLike<String> cond = SqlBuilder.isNotLike(() -> "%F%");
241         assertThat(cond.value()).isEqualTo("%F%");
242         assertThat(cond.isEmpty()).isFalse();
243     }
244 
245     @Test
246     void testIsNotLikeWhenPresent() {
247         IsNotLikeWhenPresent<String> cond = SqlBuilder.isNotLikeWhenPresent(() -> "%F%");
248         assertThat(cond.value()).isEqualTo("%F%");
249         assertThat(cond.isEmpty()).isFalse();
250     }
251 
252     @Test
253     void testIsNotLikeWhenPresentNull() {
254         IsNotLikeWhenPresent<String> cond = SqlBuilder.isNotLikeWhenPresent(() -> null);
255         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
256         assertThat(cond.isEmpty()).isTrue();
257     }
258 
259     @Test
260     void testIsNotLikeCaseInsensitive() {
261         IsNotLikeCaseInsensitive<String> cond = SqlBuilder.isNotLikeCaseInsensitive(() -> "%f%");
262         assertThat(cond.value()).isEqualTo("%F%");
263         assertThat(cond.isEmpty()).isFalse();
264     }
265 
266     @Test
267     void testIsNotLikeCaseInsensitiveWhenPresent() {
268         IsNotLikeCaseInsensitiveWhenPresent<String> cond = SqlBuilder.isNotLikeCaseInsensitiveWhenPresent(() -> "%f%");
269         assertThat(cond.value()).isEqualTo("%F%");
270         assertThat(cond.isEmpty()).isFalse();
271     }
272 
273     @Test
274     void testIsNotLikeCaseInsensitiveWhenPresentNull() {
275         IsNotLikeCaseInsensitiveWhenPresent<String> cond = SqlBuilder.isNotLikeCaseInsensitiveWhenPresent(() -> null);
276         assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(cond::value);
277         assertThat(cond.isEmpty()).isTrue();
278     }
279 }