View Javadoc
1   /*
2    *    Copyright 2016-2026 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.util;
17  
18  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
19  
20  import org.junit.jupiter.api.Test;
21  import org.mybatis.dynamic.sql.SqlBuilder;
22  import org.mybatis.dynamic.sql.SqlColumn;
23  import org.mybatis.dynamic.sql.SqlTable;
24  
25  class ColumnMappingVisitorTest {
26  
27      @Test
28      void testThatGeneralInsertVisitorErrorsForColumnToColumnMapping() {
29          TestTable table = new TestTable();
30          GeneralInsertVisitor tv = new GeneralInsertVisitor();
31          ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
32  
33          assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
34                  .withMessage("Internal Error 4");
35      }
36  
37      @Test
38      void testThatGeneralInsertVisitorErrorsForSelectMapping() {
39          TestTable table = new TestTable();
40          GeneralInsertVisitor tv = new GeneralInsertVisitor();
41          SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
42  
43          assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
44                  .withMessage("Internal Error 1");
45      }
46  
47      @Test
48      void testThatGeneralInsertVisitorErrorsForPropertyMapping() {
49          TestTable table = new TestTable();
50          GeneralInsertVisitor tv = new GeneralInsertVisitor();
51          PropertyMapping mapping = PropertyMapping.of(table.id, "id");
52  
53          assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
54                  .withMessage("Internal Error 2");
55      }
56  
57      @Test
58      void testThatGeneralInsertVisitorErrorsForPropertyWhenPresentMapping() {
59          TestTable table = new TestTable();
60          GeneralInsertVisitor tv = new GeneralInsertVisitor();
61          PropertyWhenPresentMapping mapping = PropertyWhenPresentMapping.of(table.id, "id", () -> 3);
62  
63          assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
64                  .withMessage("Internal Error 3");
65      }
66  
67      @Test
68      void testThatGeneralInsertVisitorErrorsForRowMapping() {
69          TestTable table = new TestTable();
70          GeneralInsertVisitor tv = new GeneralInsertVisitor();
71          RowMapping mapping = RowMapping.of(table.id);
72  
73          assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
74                  .withMessage("Internal Error 14");
75      }
76  
77      @Test
78      void testThatInsertVisitorErrorsForColumnToColumnMapping() {
79          TestTable table = new TestTable();
80          InsertVisitor tv = new InsertVisitor();
81          ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
82  
83          assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
84                  .withMessage("Internal Error 9");
85      }
86  
87      @Test
88      void testThatInsertVisitorErrorsForSelectMapping() {
89          TestTable table = new TestTable();
90          InsertVisitor tv = new InsertVisitor();
91          SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
92  
93          assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
94                  .withMessage("Internal Error 8");
95      }
96  
97      @Test
98      void testThatInsertVisitorErrorsForValueMapping() {
99          TestTable table = new TestTable();
100         InsertVisitor tv = new InsertVisitor();
101         ValueMapping<Integer> mapping = ValueMapping.of(table.id, () -> 3);
102 
103         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
104                 .withMessage("Internal Error 5");
105     }
106 
107     @Test
108     void testThatInsertVisitorErrorsForValueOrNullMapping() {
109         TestTable table = new TestTable();
110         InsertVisitor tv = new InsertVisitor();
111         ValueOrNullMapping<Integer> mapping = ValueOrNullMapping.of(table.id, () -> 3);
112 
113         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
114                 .withMessage("Internal Error 6");
115     }
116 
117     @Test
118     void testThatInsertVisitorErrorsForValueWhenPresentMapping() {
119         TestTable table = new TestTable();
120         InsertVisitor tv = new InsertVisitor();
121         ValueWhenPresentMapping<Integer> mapping = ValueWhenPresentMapping.of(table.id, () -> 3);
122 
123         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
124                 .withMessage("Internal Error 7");
125     }
126 
127     @Test
128     void testThatMultiRowInsertVisitorErrorsForColumnToColumnMapping() {
129         TestTable table = new TestTable();
130         MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
131         ColumnToColumnMapping mapping = ColumnToColumnMapping.of(table.id, table.description);
132 
133         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
134                 .withMessage("Internal Error 9");
135     }
136 
137     @Test
138     void testThatMultiRowInsertVisitorErrorsForSelectMapping() {
139         TestTable table = new TestTable();
140         MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
141         SelectMapping mapping = SelectMapping.of(table.id, SqlBuilder.select(table.id).from(table));
142 
143         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
144                 .withMessage("Internal Error 8");
145     }
146 
147     @Test
148     void testThatMultiRowInsertVisitorErrorsForValueMapping() {
149         TestTable table = new TestTable();
150         MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
151         ValueMapping<Integer> mapping = ValueMapping.of(table.id, () -> 3);
152 
153         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
154                 .withMessage("Internal Error 5");
155     }
156 
157     @Test
158     void testThatMultiRowInsertVisitorErrorsForValueWhenPresentMapping() {
159         TestTable table = new TestTable();
160         MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
161         ValueWhenPresentMapping<Integer> mapping = ValueWhenPresentMapping.of(table.id, () -> 3);
162 
163         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
164                 .withMessage("Internal Error 7");
165     }
166 
167     @Test
168     void testThatMultiRowInsertVisitorErrorsForPropertyWhenPresentMapping() {
169         TestTable table = new TestTable();
170         MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
171         PropertyWhenPresentMapping mapping = PropertyWhenPresentMapping.of(table.id, "id", () -> 3);
172 
173         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
174                 .withMessage("Internal Error 12");
175     }
176 
177     @Test
178     void testThatUpdateVisitorErrorsForPropertyMapping() {
179         TestTable table = new TestTable();
180         UpdateVisitor tv = new UpdateVisitor();
181         PropertyMapping mapping = PropertyMapping.of(table.id, "id");
182 
183         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
184                 .withMessage("Internal Error 10");
185     }
186 
187     @Test
188     void testThatUpdateVisitorErrorsForPropertyWhenPresentMapping() {
189         TestTable table = new TestTable();
190         UpdateVisitor tv = new UpdateVisitor();
191         PropertyWhenPresentMapping mapping = PropertyWhenPresentMapping.of(table.id, "id", () -> 3);
192 
193         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
194                 .withMessage("Internal Error 11");
195     }
196 
197     @Test
198     void testThatUpdateVisitorErrorsForRowMapping() {
199         TestTable table = new TestTable();
200         UpdateVisitor tv = new UpdateVisitor();
201         RowMapping mapping = RowMapping.of(table.id);
202 
203         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
204                 .withMessage("Internal Error 15");
205     }
206 
207     @Test
208     void testThatUpdateVisitorErrorsForMappedColumnMapping() {
209         TestTable table = new TestTable();
210         UpdateVisitor tv = new UpdateVisitor();
211         MappedColumnMapping mapping = MappedColumnMapping.of(table.id);
212 
213         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
214                 .withMessage("Internal Error 19");
215     }
216 
217     @Test
218     void testThatUpdateVisitorErrorsForMappedWhenPresentColumnMapping() {
219         TestTable table = new TestTable();
220         UpdateVisitor tv = new UpdateVisitor();
221         MappedColumnWhenPresentMapping mapping = MappedColumnWhenPresentMapping.of(table.id, () -> 1);
222 
223         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
224                 .withMessage("Internal Error 20");
225     }
226 
227     @Test
228     void testThatGeneralInsertVisitorErrorsForMappedColumnMapping() {
229         TestTable table = new TestTable();
230         GeneralInsertVisitor tv = new GeneralInsertVisitor();
231         MappedColumnMapping mapping = MappedColumnMapping.of(table.id);
232 
233         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
234                 .withMessage("Internal Error 16");
235     }
236 
237     @Test
238     void testThatGeneralInsertVisitorErrorsForMappedWhenPresentColumnMapping() {
239         TestTable table = new TestTable();
240         GeneralInsertVisitor tv = new GeneralInsertVisitor();
241         MappedColumnWhenPresentMapping mapping = MappedColumnWhenPresentMapping.of(table.id, () -> 1);
242 
243         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
244                 .withMessage("Internal Error 17");
245     }
246 
247     @Test
248     void testThatMultiRowInsertVisitorErrorsForMappedColumnWhenPresentMapping() {
249         TestTable table = new TestTable();
250         MultiRowInsertVisitor tv = new MultiRowInsertVisitor();
251         MappedColumnWhenPresentMapping mapping = MappedColumnWhenPresentMapping.of(table.id, () -> 1);
252 
253         assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> tv.visit(mapping))
254                 .withMessage("Internal Error 18");
255     }
256 
257     private static class TestTable extends SqlTable {
258         public final SqlColumn<Integer> id;
259         public final SqlColumn<String> description;
260 
261         public TestTable() {
262             super("Test");
263 
264             id = column("id");
265             description = column("description");
266         }
267     }
268 
269     private static class GeneralInsertVisitor extends GeneralInsertMappingVisitor<String> {
270         @Override
271         public String visit(NullMapping mapping) {
272             return "Null Mapping";
273         }
274 
275         @Override
276         public String visit(ConstantMapping mapping) {
277             return "Constant Mapping";
278         }
279 
280         @Override
281         public String visit(StringConstantMapping mapping) {
282             return "String Constant Mapping";
283         }
284 
285         @Override
286         public <R> String visit(ValueMapping<R> mapping) {
287             return "Value Mapping";
288         }
289 
290         @Override
291         public <R> String visit(ValueOrNullMapping<R> mapping) {
292             return "Value or Null Mapping";
293         }
294 
295         @Override
296         public <R> String visit(ValueWhenPresentMapping<R> mapping) {
297             return "Value When Present Mapping";
298         }
299     }
300 
301     private static class InsertVisitor extends InsertMappingVisitor<String> {
302         @Override
303         public String visit(NullMapping mapping) {
304             return "Null Mapping";
305         }
306 
307         @Override
308         public String visit(ConstantMapping mapping) {
309             return "Constant Mapping";
310         }
311 
312         @Override
313         public String visit(StringConstantMapping mapping) {
314             return "String Constant Mapping";
315         }
316 
317         @Override
318         public String visit(PropertyMapping mapping) {
319             return "Property Mapping";
320         }
321 
322         @Override
323         public String visit(PropertyWhenPresentMapping mapping) {
324             return "Property When Present Mapping";
325         }
326 
327         @Override
328         public String visit(RowMapping mapping) {
329             return "Row Mapping";
330         }
331 
332         @Override
333         public String visit(MappedColumnMapping mapping) {
334             return "Mapped Column Mapping";
335         }
336 
337         @Override
338         public String visit(MappedColumnWhenPresentMapping mapping) {
339             return "Mapped Column When Present Mapping";
340         }
341     }
342 
343     private static class UpdateVisitor extends UpdateMappingVisitor<String> {
344         @Override
345         public String visit(NullMapping mapping) {
346             return "Null Mapping";
347         }
348 
349         @Override
350         public String visit(ConstantMapping mapping) {
351             return "Constant Mapping";
352         }
353 
354         @Override
355         public String visit(StringConstantMapping mapping) {
356             return "String Constant Mapping";
357         }
358 
359         @Override
360         public <R> String visit(ValueMapping<R> mapping) {
361             return "Value Mapping";
362         }
363 
364         @Override
365         public <R> String visit(ValueOrNullMapping<R> mapping) {
366             return "Value or Null Mapping";
367         }
368 
369         @Override
370         public <R> String visit(ValueWhenPresentMapping<R> mapping) {
371             return "Value When Present Mapping";
372         }
373 
374         @Override
375         public String visit(SelectMapping mapping) {
376             return "Select Mapping";
377         }
378 
379         @Override
380         public String visit(ColumnToColumnMapping columnMapping) {
381             return "Column to Column Mapping";
382         }
383     }
384 
385     private static class MultiRowInsertVisitor extends MultiRowInsertMappingVisitor<String> {
386 
387         @Override
388         public String visit(NullMapping mapping) {
389             return "Null Mapping";
390         }
391 
392         @Override
393         public String visit(ConstantMapping mapping) {
394             return "Constant Mapping";
395         }
396 
397         @Override
398         public String visit(StringConstantMapping mapping) {
399             return "String Constant Mapping";
400         }
401 
402         @Override
403         public String visit(PropertyMapping mapping) {
404             return "Property Mapping";
405         }
406 
407         @Override
408         public String visit(RowMapping mapping) {
409             return "Row Mapping";
410         }
411 
412         @Override
413         public String visit(MappedColumnMapping mapping) {
414             return "Mapped Column Mapping";
415         }
416     }
417 }