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.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     private static class TestTable extends SqlTable {
208         public final SqlColumn<Integer> id;
209         public final SqlColumn<String> description;
210 
211         public TestTable() {
212             super("Test");
213 
214             id = column("id");
215             description = column("description");
216         }
217     }
218 
219     private static class GeneralInsertVisitor extends GeneralInsertMappingVisitor<String> {
220         @Override
221         public String visit(NullMapping mapping) {
222             return "Null Mapping";
223         }
224 
225         @Override
226         public String visit(ConstantMapping mapping) {
227             return "Constant Mapping";
228         }
229 
230         @Override
231         public String visit(StringConstantMapping mapping) {
232             return "String Constant Mapping";
233         }
234 
235         @Override
236         public <R> String visit(ValueMapping<R> mapping) {
237             return "Value Mapping";
238         }
239 
240         @Override
241         public <R> String visit(ValueOrNullMapping<R> mapping) {
242             return "Value or Null Mapping";
243         }
244 
245         @Override
246         public <R> String visit(ValueWhenPresentMapping<R> mapping) {
247             return "Value When Present Mapping";
248         }
249     }
250 
251     private static class InsertVisitor extends InsertMappingVisitor<String> {
252         @Override
253         public String visit(NullMapping mapping) {
254             return "Null Mapping";
255         }
256 
257         @Override
258         public String visit(ConstantMapping mapping) {
259             return "Constant Mapping";
260         }
261 
262         @Override
263         public String visit(StringConstantMapping mapping) {
264             return "String Constant Mapping";
265         }
266 
267         @Override
268         public String visit(PropertyMapping mapping) {
269             return "Property Mapping";
270         }
271 
272         @Override
273         public String visit(PropertyWhenPresentMapping mapping) {
274             return "Property When Present Mapping";
275         }
276 
277         @Override
278         public String visit(RowMapping mapping) {
279             return "Row Mapping";
280         }
281     }
282 
283     private static class UpdateVisitor extends UpdateMappingVisitor<String> {
284         @Override
285         public String visit(NullMapping mapping) {
286             return "Null Mapping";
287         }
288 
289         @Override
290         public String visit(ConstantMapping mapping) {
291             return "Constant Mapping";
292         }
293 
294         @Override
295         public String visit(StringConstantMapping mapping) {
296             return "String Constant Mapping";
297         }
298 
299         @Override
300         public <R> String visit(ValueMapping<R> mapping) {
301             return "Value Mapping";
302         }
303 
304         @Override
305         public <R> String visit(ValueOrNullMapping<R> mapping) {
306             return "Value or Null Mapping";
307         }
308 
309         @Override
310         public <R> String visit(ValueWhenPresentMapping<R> mapping) {
311             return "Value When Present Mapping";
312         }
313 
314         @Override
315         public String visit(SelectMapping mapping) {
316             return "Select Mapping";
317         }
318 
319         @Override
320         public String visit(ColumnToColumnMapping columnMapping) {
321             return "Column to Column Mapping";
322         }
323     }
324 
325     private static class MultiRowInsertVisitor extends MultiRowInsertMappingVisitor<String> {
326 
327         @Override
328         public String visit(NullMapping mapping) {
329             return "Null Mapping";
330         }
331 
332         @Override
333         public String visit(ConstantMapping mapping) {
334             return "Constant Mapping";
335         }
336 
337         @Override
338         public String visit(StringConstantMapping mapping) {
339             return "String Constant Mapping";
340         }
341 
342         @Override
343         public String visit(PropertyMapping mapping) {
344             return "Property Mapping";
345         }
346 
347         @Override
348         public String visit(RowMapping mapping) {
349             return "Row Mapping";
350         }
351 
352     }
353 }