|
| 1 | +/* |
| 2 | + * Copyright 2009-2024 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 | + |
| 17 | +package org.apache.ibatis.scripting.xmltags; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.*; |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import org.apache.ibatis.mapping.SqlSource; |
| 26 | +import org.apache.ibatis.parsing.XPathParser; |
| 27 | +import org.apache.ibatis.session.Configuration; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +class XMLScriptBuilderTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + void shouldEmptyTextNodesRemoved() throws Exception { |
| 34 | + String xml = """ |
| 35 | + <script> |
| 36 | + select * from user |
| 37 | + <if test="_parameter != null"> |
| 38 | + where id = 1 |
| 39 | + </if> |
| 40 | + <if test="_parameter == null"> |
| 41 | + where id > 0 |
| 42 | + </if> |
| 43 | + </script> |
| 44 | + """; |
| 45 | + SqlSource sqlSource = new XMLScriptBuilder(new Configuration(), new XPathParser(xml).evalNode("/script")) |
| 46 | + .parseScriptNode(); |
| 47 | + |
| 48 | + Field rootSqlNodeFld = DynamicSqlSource.class.getDeclaredField("rootSqlNode"); |
| 49 | + rootSqlNodeFld.setAccessible(true); |
| 50 | + MixedSqlNode sqlNode = (MixedSqlNode) rootSqlNodeFld.get(sqlSource); |
| 51 | + |
| 52 | + Field contentsFld = MixedSqlNode.class.getDeclaredField("contents"); |
| 53 | + contentsFld.setAccessible(true); |
| 54 | + @SuppressWarnings("unchecked") |
| 55 | + List<SqlNode> contents = (List<SqlNode>) contentsFld.get(sqlNode); |
| 56 | + |
| 57 | + assertEquals(3, contents.size()); |
| 58 | + assertThat(contents.get(0)).isInstanceOf(StaticTextSqlNode.class); |
| 59 | + assertThat(contents.get(1)).isInstanceOf(IfSqlNode.class); |
| 60 | + assertThat(contents.get(2)).isInstanceOf(IfSqlNode.class); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments