Skip to content

Commit 2e0927e

Browse files
authored
Merge pull request mybatis#3258 from mawen12/feature-sqlnode-test
Add sqlnode test
2 parents f72fa71 + 47f48f5 commit 2e0927e

11 files changed

+841
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
package org.apache.ibatis.scripting.xmltags;
17+
18+
import java.util.Arrays;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
22+
import org.apache.ibatis.domain.blog.Author;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.jupiter.api.Assertions.*;
27+
import static org.mockito.Mockito.*;
28+
29+
/**
30+
* <pre>{@code
31+
* SELECT *
32+
* FROM BLOG
33+
* WHERE state = 'active'
34+
* <choose>
35+
* <when test="title != null">
36+
* AND title like #{title}
37+
* </when>
38+
* <when test="author != null && author.username != null">
39+
* AND author_name like #{author.username}
40+
* </when>
41+
* <otherwise>
42+
* AND featured = 1
43+
* </otherwise>
44+
* </choose>
45+
* }</pre>
46+
*
47+
* @author <a href="[email protected]">mawen12</a>
48+
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#choose-when-otherwise">choose</a>
49+
*/
50+
class ChooseSqlNodeTest extends SqlNodeTest{
51+
52+
private static final String FIRST_TEXT = " AND title like #{title}";
53+
private static final String SECOND_TEXT = " AND author_name like #{author.username}";
54+
private static final String OTHERWISE_TEXT = " AND featured = 1";
55+
56+
private SqlNode sqlNode;
57+
58+
@BeforeEach
59+
void setup() {
60+
SqlNode first = new IfSqlNode(new StaticTextSqlNode(FIRST_TEXT), "title != null");
61+
SqlNode second = new IfSqlNode(new StaticTextSqlNode(SECOND_TEXT), "author != null && author.username != null");
62+
List<SqlNode> ifNodes = Arrays.asList(first, second);
63+
64+
SqlNode defaultNode = new StaticTextSqlNode(OTHERWISE_TEXT);
65+
66+
this.sqlNode = new ChooseSqlNode(ifNodes, defaultNode);
67+
}
68+
69+
@Test
70+
@Override
71+
public void shouldApply() throws Exception {
72+
when(context.getBindings()).thenReturn(new HashMap<>() {{
73+
put("title", "abc");
74+
put("author", new Author(1, "mybatis", "***", null, null, null));
75+
}});
76+
77+
boolean result = sqlNode.apply(context);
78+
79+
assertTrue(result);
80+
verify(context).appendSql(FIRST_TEXT);
81+
}
82+
83+
@Test
84+
public void shouldAppendSecond() throws Exception {
85+
when(context.getBindings()).thenReturn(new HashMap<>() {{
86+
put("author", new Author(1, "mybatis", "***", null, null, null));
87+
}});
88+
89+
boolean result = sqlNode.apply(context);
90+
91+
assertTrue(result);
92+
verify(context).appendSql(SECOND_TEXT);
93+
}
94+
95+
@Test
96+
public void shouldAppendOtherwise() throws Exception {
97+
when(context.getBindings()).thenReturn(new HashMap<>());
98+
99+
boolean result = sqlNode.apply(context);
100+
101+
assertTrue(result);
102+
verify(context).appendSql(OTHERWISE_TEXT);
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
package org.apache.ibatis.scripting.xmltags;
17+
18+
import java.util.Arrays;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
import org.mockito.ArgumentCaptor;
25+
26+
import static org.junit.jupiter.api.Assertions.*;
27+
import static org.mockito.Mockito.*;
28+
29+
/**
30+
* <pre>{@code
31+
* SELECT *
32+
* FROM POST
33+
* <where>
34+
* <foreach item="item" index="index" collection="list" open="ID in (" separator="," close=")" nullable="true">
35+
* #{item}
36+
* </foreach>
37+
* </where>
38+
* }</pre>
39+
*
40+
* @author <a href="[email protected]">mawen12</a>
41+
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#foreach">foreach</a>
42+
*/
43+
class ForEachSqlNodeTest extends SqlNodeTest{
44+
45+
private SqlNode sqlNode;
46+
47+
@BeforeEach
48+
void setup() {
49+
SqlNode contents = new StaticTextSqlNode("#{name}");
50+
this.sqlNode = new ForEachSqlNode(configuration, contents, "list", "index", "item", "ID in (", ")", ",");
51+
}
52+
53+
@Test
54+
@Override
55+
public void shouldApply() throws Exception {
56+
ArgumentCaptor<String> bindKeyCaptor = ArgumentCaptor.forClass(String.class);
57+
ArgumentCaptor<Object> bindValueCaptor = ArgumentCaptor.forClass(Object.class);
58+
doNothing().when(context).bind(bindKeyCaptor.capture(), bindValueCaptor.capture());
59+
60+
when(context.getBindings()).thenReturn(new HashMap<>() {{
61+
put("list", Arrays.asList("a", "b", "c"));
62+
}});
63+
64+
boolean result = sqlNode.apply(context);
65+
66+
assertTrue(result);
67+
verify(context).appendSql("ID in (");
68+
verify(context).appendSql(")");
69+
70+
List<String> allKeyValues = bindKeyCaptor.getAllValues();
71+
List<Object> allValValues = bindValueCaptor.getAllValues();
72+
assertEquals(Arrays.asList("index", "__frch_index_0", "item", "__frch_item_0",
73+
"index", "__frch_index_0", "item", "__frch_item_0",
74+
"index", "__frch_index_0", "item", "__frch_item_0"), allKeyValues);
75+
assertEquals(Arrays.asList(0, 0, "a", "a",
76+
1, 1, "b", "b",
77+
2, 2, "c", "c"), allValValues);
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
package org.apache.ibatis.scripting.xmltags;
17+
18+
import java.util.HashMap;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.*;
24+
import static org.mockito.Mockito.*;
25+
26+
/**
27+
* <pre>{@code
28+
* <if test="title != null>
29+
* AND title like #{title}
30+
* </if>
31+
* }</pre>
32+
*
33+
* @author <a href="[email protected]">mawen12</a>
34+
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#if">if</a>
35+
*/
36+
class IfSqlNodeTest extends SqlNodeTest {
37+
38+
private static final String CONDITION = "title != null";
39+
private static final String TEXT = "AND title like #{title}";
40+
41+
private SqlNode sqlNode;
42+
43+
@BeforeEach
44+
void setup() {
45+
SqlNode contents = new StaticTextSqlNode(TEXT);
46+
this.sqlNode = new IfSqlNode(contents, CONDITION);
47+
}
48+
49+
@Test
50+
@Override
51+
public void shouldApply() throws Exception {
52+
when(context.getBindings()).thenReturn(new HashMap<>() {{
53+
put("title", "ENGLISH");
54+
}});
55+
56+
boolean result = sqlNode.apply(context);
57+
58+
assertTrue(result);
59+
verify(context).appendSql(TEXT);
60+
}
61+
62+
@Test
63+
public void shouldAppendNone() {
64+
when(context.getBindings()).thenReturn(new HashMap<>() {{
65+
put("title", null);
66+
}});
67+
68+
boolean result = sqlNode.apply(context);
69+
70+
assertFalse(result);
71+
verify(context, never()).appendSql(TEXT);
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
package org.apache.ibatis.scripting.xmltags;
17+
18+
import java.util.Arrays;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.mockito.Mockito.*;
24+
25+
/**
26+
* @author <a href="[email protected]">mawen12</a>
27+
*/
28+
class MixedSqlNodeTest extends SqlNodeTest{
29+
30+
private static final String FIRST_TEXT = "abc";
31+
private static final String SECOND_TEXT = "bcd";
32+
private SqlNode sqlNode;
33+
34+
@BeforeEach
35+
void setup() {
36+
SqlNode first = new StaticTextSqlNode(FIRST_TEXT);
37+
SqlNode second = new StaticTextSqlNode(SECOND_TEXT);
38+
this.sqlNode = new MixedSqlNode(Arrays.asList(first, second));
39+
}
40+
41+
@Test
42+
@Override
43+
public void shouldApply() throws Exception {
44+
sqlNode.apply(context);
45+
46+
verify(context).appendSql("abc");
47+
verify(context).appendSql("bcd");
48+
}
49+
}

0 commit comments

Comments
 (0)