-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3258 from mawen12/feature-sqlnode-test
Add sqlnode test
- Loading branch information
Showing
11 changed files
with
841 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/test/java/org/apache/ibatis/scripting/xmltags/ChooseSqlNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.scripting.xmltags; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.domain.blog.Author; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* <pre>{@code | ||
* SELECT * | ||
* FROM BLOG | ||
* WHERE state = 'active' | ||
* <choose> | ||
* <when test="title != null"> | ||
* AND title like #{title} | ||
* </when> | ||
* <when test="author != null && author.username != null"> | ||
* AND author_name like #{author.username} | ||
* </when> | ||
* <otherwise> | ||
* AND featured = 1 | ||
* </otherwise> | ||
* </choose> | ||
* }</pre> | ||
* | ||
* @author <a href="1181963012mw@gmail.com">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#choose-when-otherwise">choose</a> | ||
*/ | ||
class ChooseSqlNodeTest extends SqlNodeTest{ | ||
|
||
private static final String FIRST_TEXT = " AND title like #{title}"; | ||
private static final String SECOND_TEXT = " AND author_name like #{author.username}"; | ||
private static final String OTHERWISE_TEXT = " AND featured = 1"; | ||
|
||
private SqlNode sqlNode; | ||
|
||
@BeforeEach | ||
void setup() { | ||
SqlNode first = new IfSqlNode(new StaticTextSqlNode(FIRST_TEXT), "title != null"); | ||
SqlNode second = new IfSqlNode(new StaticTextSqlNode(SECOND_TEXT), "author != null && author.username != null"); | ||
List<SqlNode> ifNodes = Arrays.asList(first, second); | ||
|
||
SqlNode defaultNode = new StaticTextSqlNode(OTHERWISE_TEXT); | ||
|
||
this.sqlNode = new ChooseSqlNode(ifNodes, defaultNode); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldApply() throws Exception { | ||
when(context.getBindings()).thenReturn(new HashMap<>() {{ | ||
put("title", "abc"); | ||
put("author", new Author(1, "mybatis", "***", null, null, null)); | ||
}}); | ||
|
||
boolean result = sqlNode.apply(context); | ||
|
||
assertTrue(result); | ||
verify(context).appendSql(FIRST_TEXT); | ||
} | ||
|
||
@Test | ||
public void shouldAppendSecond() throws Exception { | ||
when(context.getBindings()).thenReturn(new HashMap<>() {{ | ||
put("author", new Author(1, "mybatis", "***", null, null, null)); | ||
}}); | ||
|
||
boolean result = sqlNode.apply(context); | ||
|
||
assertTrue(result); | ||
verify(context).appendSql(SECOND_TEXT); | ||
} | ||
|
||
@Test | ||
public void shouldAppendOtherwise() throws Exception { | ||
when(context.getBindings()).thenReturn(new HashMap<>()); | ||
|
||
boolean result = sqlNode.apply(context); | ||
|
||
assertTrue(result); | ||
verify(context).appendSql(OTHERWISE_TEXT); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/test/java/org/apache/ibatis/scripting/xmltags/ForEachSqlNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.scripting.xmltags; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* <pre>{@code | ||
* SELECT * | ||
* FROM POST | ||
* <where> | ||
* <foreach item="item" index="index" collection="list" open="ID in (" separator="," close=")" nullable="true"> | ||
* #{item} | ||
* </foreach> | ||
* </where> | ||
* }</pre> | ||
* | ||
* @author <a href="1181963012mw@gmail.com">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#foreach">foreach</a> | ||
*/ | ||
class ForEachSqlNodeTest extends SqlNodeTest{ | ||
|
||
private SqlNode sqlNode; | ||
|
||
@BeforeEach | ||
void setup() { | ||
SqlNode contents = new StaticTextSqlNode("#{name}"); | ||
this.sqlNode = new ForEachSqlNode(configuration, contents, "list", "index", "item", "ID in (", ")", ","); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldApply() throws Exception { | ||
ArgumentCaptor<String> bindKeyCaptor = ArgumentCaptor.forClass(String.class); | ||
ArgumentCaptor<Object> bindValueCaptor = ArgumentCaptor.forClass(Object.class); | ||
doNothing().when(context).bind(bindKeyCaptor.capture(), bindValueCaptor.capture()); | ||
|
||
when(context.getBindings()).thenReturn(new HashMap<>() {{ | ||
put("list", Arrays.asList("a", "b", "c")); | ||
}}); | ||
|
||
boolean result = sqlNode.apply(context); | ||
|
||
assertTrue(result); | ||
verify(context).appendSql("ID in ("); | ||
verify(context).appendSql(")"); | ||
|
||
List<String> allKeyValues = bindKeyCaptor.getAllValues(); | ||
List<Object> allValValues = bindValueCaptor.getAllValues(); | ||
assertEquals(Arrays.asList("index", "__frch_index_0", "item", "__frch_item_0", | ||
"index", "__frch_index_0", "item", "__frch_item_0", | ||
"index", "__frch_index_0", "item", "__frch_item_0"), allKeyValues); | ||
assertEquals(Arrays.asList(0, 0, "a", "a", | ||
1, 1, "b", "b", | ||
2, 2, "c", "c"), allValValues); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/test/java/org/apache/ibatis/scripting/xmltags/IfSqlNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.scripting.xmltags; | ||
|
||
import java.util.HashMap; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* <pre>{@code | ||
* <if test="title != null> | ||
* AND title like #{title} | ||
* </if> | ||
* }</pre> | ||
* | ||
* @author <a href="1181963012mw@gmail.com">mawen12</a> | ||
* @see <a href="https://mybatis.org/mybatis-3/dynamic-sql.html#if">if</a> | ||
*/ | ||
class IfSqlNodeTest extends SqlNodeTest { | ||
|
||
private static final String CONDITION = "title != null"; | ||
private static final String TEXT = "AND title like #{title}"; | ||
|
||
private SqlNode sqlNode; | ||
|
||
@BeforeEach | ||
void setup() { | ||
SqlNode contents = new StaticTextSqlNode(TEXT); | ||
this.sqlNode = new IfSqlNode(contents, CONDITION); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldApply() throws Exception { | ||
when(context.getBindings()).thenReturn(new HashMap<>() {{ | ||
put("title", "ENGLISH"); | ||
}}); | ||
|
||
boolean result = sqlNode.apply(context); | ||
|
||
assertTrue(result); | ||
verify(context).appendSql(TEXT); | ||
} | ||
|
||
@Test | ||
public void shouldAppendNone() { | ||
when(context.getBindings()).thenReturn(new HashMap<>() {{ | ||
put("title", null); | ||
}}); | ||
|
||
boolean result = sqlNode.apply(context); | ||
|
||
assertFalse(result); | ||
verify(context, never()).appendSql(TEXT); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/test/java/org/apache/ibatis/scripting/xmltags/MixedSqlNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.scripting.xmltags; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* @author <a href="1181963012mw@gmail.com">mawen12</a> | ||
*/ | ||
class MixedSqlNodeTest extends SqlNodeTest{ | ||
|
||
private static final String FIRST_TEXT = "abc"; | ||
private static final String SECOND_TEXT = "bcd"; | ||
private SqlNode sqlNode; | ||
|
||
@BeforeEach | ||
void setup() { | ||
SqlNode first = new StaticTextSqlNode(FIRST_TEXT); | ||
SqlNode second = new StaticTextSqlNode(SECOND_TEXT); | ||
this.sqlNode = new MixedSqlNode(Arrays.asList(first, second)); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void shouldApply() throws Exception { | ||
sqlNode.apply(context); | ||
|
||
verify(context).appendSql("abc"); | ||
verify(context).appendSql("bcd"); | ||
} | ||
} |
Oops, something went wrong.