This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new bf30ff2 add unit test for SQLRewriteContext (6746) (#6831)
bf30ff2 is described below
commit bf30ff286ee6af377f4dc980489430406fc3ed90
Author: 孙念君 Sun Nianjun <[email protected]>
AuthorDate: Sun Aug 16 14:26:18 2020 +0800
add unit test for SQLRewriteContext (6746) (#6831)
* Add: add test case for StatementContext (#6746)
* Add: add test case for generateTokens (#6746)
* Add: add test case for generateTokens (#6746)
* Add: final modifier (#6746)
* Refactor: refactor the test sql (#6746)
---
.../rewrite/context/SQLRewriteContextTest.java | 101 +++++++++++++++++++++
1 file changed, 101 insertions(+)
diff --git
a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContextTest.java
b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContextTest.java
new file mode 100644
index 0000000..938c4ac
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContextTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * http://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.shardingsphere.infra.rewrite.context;
+
+import com.google.common.collect.Lists;
+import
org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
+import
org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
+import
org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
+import
org.apache.shardingsphere.infra.rewrite.sql.token.generator.OptionalSQLTokenGenerator;
+import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
+import
org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import
org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.sql.parser.binder.statement.dml.InsertStatementContext;
+import
org.apache.shardingsphere.sql.parser.binder.statement.dml.SelectStatementContext;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class SQLRewriteContextTest {
+
+ @Mock
+ private SQLStatementContext sqlStatementContext;
+
+ @Mock
+ private SchemaMetaData schemaMetaData;
+
+ @Mock
+ private SQLToken sqlToken;
+
+ @Mock
+ private OptionalSQLTokenGenerator optionalSQLTokenGenerator;
+
+ @Mock
+ private CollectionSQLTokenGenerator collectionSQLTokenGenerator;
+
+ @Before
+ public void setUp() {
+
when(optionalSQLTokenGenerator.generateSQLToken(sqlStatementContext)).thenReturn(sqlToken);
+
when(optionalSQLTokenGenerator.isGenerateSQLToken(sqlStatementContext)).thenReturn(true);
+
when(collectionSQLTokenGenerator.generateSQLTokens(sqlStatementContext)).thenReturn(Lists.newArrayList(sqlToken));
+
when(collectionSQLTokenGenerator.isGenerateSQLToken(sqlStatementContext)).thenReturn(true);
+ }
+
+ @Test
+ public void assertInsertStatementContext() {
+ InsertStatementContext statementContext =
mock(InsertStatementContext.class);
+ SQLRewriteContext sqlRewriteContext = new
SQLRewriteContext(mock(SchemaMetaData.class), statementContext, "INSERT INTO
tbl VALUES (?)", Collections.singletonList(1));
+ assertThat(sqlRewriteContext.getParameterBuilder(),
instanceOf(GroupedParameterBuilder.class));
+ }
+
+ @Test
+ public void assertNotInsertStatementContext() {
+ SelectStatementContext statementContext =
mock(SelectStatementContext.class);
+ SQLRewriteContext sqlRewriteContext = new
SQLRewriteContext(mock(SchemaMetaData.class), statementContext, "SELECT * FROM
tbl WHERE id = ?", Collections.singletonList(1));
+ assertThat(sqlRewriteContext.getParameterBuilder(),
instanceOf(StandardParameterBuilder.class));
+ }
+
+ @Test
+ public void assertGenerateOptionalSQLToken() {
+ SQLRewriteContext sqlRewriteContext = new
SQLRewriteContext(schemaMetaData, sqlStatementContext, "INSERT INTO tbl VALUES
(?)", Collections.singletonList(1));
+
sqlRewriteContext.addSQLTokenGenerators(Lists.newArrayList(optionalSQLTokenGenerator));
+ sqlRewriteContext.generateSQLTokens();
+ assertFalse(sqlRewriteContext.getSqlTokens().isEmpty());
+ assertThat(sqlRewriteContext.getSqlTokens().get(0),
instanceOf(SQLToken.class));
+ }
+
+ @Test
+ public void assertGenerateCollectionSQLToken() {
+ SQLRewriteContext sqlRewriteContext = new
SQLRewriteContext(schemaMetaData, sqlStatementContext, "INSERT INTO tbl VALUES
(?)", Collections.singletonList(1));
+
sqlRewriteContext.addSQLTokenGenerators(Lists.newArrayList(collectionSQLTokenGenerator));
+ sqlRewriteContext.generateSQLTokens();
+ assertFalse(sqlRewriteContext.getSqlTokens().isEmpty());
+ assertThat(sqlRewriteContext.getSqlTokens().get(0),
instanceOf(SQLToken.class));
+ }
+}