From 7ee16bc8cdb49ae696f43d5891c4282b32b1317e Mon Sep 17 00:00:00 2001
From: mawen12 <1181963012mw@gmail.com>
Date: Mon, 30 Sep 2024 18:55:25 +0800
Subject: [PATCH 1/2] add SqlNode test cases
---
.../scripting/xmltags/ChooseSqlNodeTest.java | 104 +++++++++++++++++
.../scripting/xmltags/ForEachSqlNodeTest.java | 79 +++++++++++++
.../scripting/xmltags/IfSqlNodeTest.java | 73 ++++++++++++
.../scripting/xmltags/MixedSqlNodeTest.java | 49 ++++++++
.../scripting/xmltags/SetSqlNodeTest.java | 107 +++++++++++++++++
.../ibatis/scripting/xmltags/SqlNodeTest.java | 36 ++++++
.../xmltags/StaticTextSqlNodeTest.java | 43 +++++++
.../scripting/xmltags/TextSqlNodeTest.java | 64 ++++++++++
.../scripting/xmltags/TrimSqlNodeTest.java | 109 ++++++++++++++++++
.../scripting/xmltags/VarDeclSqlNodeTest.java | 70 +++++++++++
.../scripting/xmltags/WhereSqlNodeTest.java | 107 +++++++++++++++++
11 files changed, 841 insertions(+)
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/ChooseSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/ForEachSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/IfSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/MixedSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/SetSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/SqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/StaticTextSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/TextSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/TrimSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/VarDeclSqlNodeTest.java
create mode 100644 src/test/java/org/apache/ibatis/scripting/xmltags/WhereSqlNodeTest.java
diff --git a/src/test/java/org/apache/ibatis/scripting/xmltags/ChooseSqlNodeTest.java b/src/test/java/org/apache/ibatis/scripting/xmltags/ChooseSqlNodeTest.java
new file mode 100644
index 00000000000..6d3992d823a
--- /dev/null
+++ b/src/test/java/org/apache/ibatis/scripting/xmltags/ChooseSqlNodeTest.java
@@ -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.*;
+
+/**
+ *
{@code
+ * SELECT *
+ * FROM BLOG
+ * WHERE state = 'active'
+ *
+ *
+ * AND title like #{title}
+ *
+ *
+ * AND author_name like #{author.username}
+ *
+ *
+ * AND featured = 1
+ *
+ *
+ * }
+ *
+ * @author mawen12
+ * @see choose
+ */
+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 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);
+ }
+}
diff --git a/src/test/java/org/apache/ibatis/scripting/xmltags/ForEachSqlNodeTest.java b/src/test/java/org/apache/ibatis/scripting/xmltags/ForEachSqlNodeTest.java
new file mode 100644
index 00000000000..1d668e7e38c
--- /dev/null
+++ b/src/test/java/org/apache/ibatis/scripting/xmltags/ForEachSqlNodeTest.java
@@ -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.*;
+
+/**
+ *