This is an automated email from the ASF dual-hosted git repository.
totalo 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 658bd34ead7 Refactor CommonSQLStatementContext (#32106)
658bd34ead7 is described below
commit 658bd34ead72fce963218798c6fafe9573d3d14c
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jul 15 00:09:33 2024 +0800
Refactor CommonSQLStatementContext (#32106)
---
.../statement/CommonSQLStatementContext.java | 48 +---------------------
.../dal/AnalyzeTableStatementContextTest.java | 13 ++----
.../dal/OptimizeTableStatementContextTest.java | 12 ++----
.../dal/ShowColumnsStatementContextTest.java | 13 +++---
.../dal/ShowCreateTableStatementContextTest.java | 11 ++---
.../dal/ShowIndexStatementContextTest.java | 11 ++---
.../dcl/DenyUserStatementContextTest.java | 10 ++---
.../statement/dcl/GrantStatementContextTest.java | 19 +++------
.../statement/dcl/RevokeStatementContextTest.java | 19 +++------
.../ddl/AlterTableStatementContextTest.java | 26 ++++++------
.../ddl/AlterViewStatementContextTest.java | 12 +++---
.../ddl/CreateViewStatementContextTest.java | 9 ++--
.../ddl/DropViewStatementContextTest.java | 8 ++--
.../statement/ddl/PrepareStatementContextTest.java | 17 ++++----
.../ddl/TruncateStatementContextTest.java | 12 +++---
15 files changed, 75 insertions(+), 165 deletions(-)
diff --git
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/CommonSQLStatementContext.java
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/CommonSQLStatementContext.java
index d78c728c419..47fb41bef8b 100644
---
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/CommonSQLStatementContext.java
+++
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/statement/CommonSQLStatementContext.java
@@ -19,19 +19,7 @@ package
org.apache.shardingsphere.infra.binder.context.statement;
import lombok.Getter;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import
org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.clickhouse.ClickHouseStatement;
-import org.apache.shardingsphere.sql.parser.statement.doris.DorisStatement;
-import org.apache.shardingsphere.sql.parser.statement.hive.HiveStatement;
-import org.apache.shardingsphere.sql.parser.statement.mysql.MySQLStatement;
-import
org.apache.shardingsphere.sql.parser.statement.opengauss.OpenGaussStatement;
-import org.apache.shardingsphere.sql.parser.statement.oracle.OracleStatement;
-import
org.apache.shardingsphere.sql.parser.statement.postgresql.PostgreSQLStatement;
-import org.apache.shardingsphere.sql.parser.statement.presto.PrestoStatement;
-import org.apache.shardingsphere.sql.parser.statement.sql92.SQL92Statement;
-import
org.apache.shardingsphere.sql.parser.statement.sqlserver.SQLServerStatement;
/**
* Common SQL statement context.
@@ -45,40 +33,6 @@ public abstract class CommonSQLStatementContext implements
SQLStatementContext {
protected CommonSQLStatementContext(final SQLStatement sqlStatement) {
this.sqlStatement = sqlStatement;
- databaseType = getDatabaseType(sqlStatement);
- }
-
- private DatabaseType getDatabaseType(final SQLStatement sqlStatement) {
- if (sqlStatement instanceof MySQLStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "MySQL");
- }
- if (sqlStatement instanceof PostgreSQLStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
- }
- if (sqlStatement instanceof OracleStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "Oracle");
- }
- if (sqlStatement instanceof SQLServerStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "SQLServer");
- }
- if (sqlStatement instanceof OpenGaussStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "openGauss");
- }
- if (sqlStatement instanceof ClickHouseStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "ClickHouse");
- }
- if (sqlStatement instanceof DorisStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "Doris");
- }
- if (sqlStatement instanceof HiveStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "Hive");
- }
- if (sqlStatement instanceof PrestoStatement) {
- return TypedSPILoader.getService(DatabaseType.class, "Presto");
- }
- if (sqlStatement instanceof SQL92Statement) {
- return TypedSPILoader.getService(DatabaseType.class, "SQL92");
- }
- throw new
UnsupportedSQLOperationException(sqlStatement.getClass().getName());
+ databaseType = sqlStatement.getDatabaseType();
}
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/AnalyzeTableStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/AnalyzeTableStatementContextTest.java
index c63b9fd54ef..7c3927b59ad 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/AnalyzeTableStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/AnalyzeTableStatementContextTest.java
@@ -28,35 +28,28 @@ import
org.apache.shardingsphere.sql.parser.statement.postgresql.dal.PostgreSQLA
import org.junit.jupiter.api.Test;
import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class AnalyzeTableStatementContextTest {
@Test
void assertMysqlNewInstance() {
- assertNewInstance(mock(MySQLAnalyzeTableStatement.class));
+ assertNewInstance(new MySQLAnalyzeTableStatement());
}
@Test
void assertPostgreSQLNewInstance() {
- assertNewInstance(mock(PostgreSQLAnalyzeTableStatement.class));
+ assertNewInstance(new PostgreSQLAnalyzeTableStatement());
}
private void assertNewInstance(final AnalyzeTableStatement
analyzeTableStatement) {
SimpleTableSegment table1 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
SimpleTableSegment table2 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_2")));
- List<SimpleTableSegment> tables = new LinkedList<>();
- tables.add(table1);
- tables.add(table2);
- when(analyzeTableStatement.getTables()).thenReturn(tables);
+ analyzeTableStatement.getTables().addAll(Arrays.asList(table1,
table2));
AnalyzeTableStatementContext actual = new
AnalyzeTableStatementContext(analyzeTableStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
assertThat(actual.getSqlStatement(), is(analyzeTableStatement));
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/OptimizeTableStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/OptimizeTableStatementContextTest.java
index a231643f236..7394d053fba 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/OptimizeTableStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/OptimizeTableStatementContextTest.java
@@ -25,26 +25,22 @@ import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.Iden
import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLOptimizeTableStatement;
import org.junit.jupiter.api.Test;
-import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class OptimizeTableStatementContextTest {
@Test
void assertNewInstance() {
- MySQLOptimizeTableStatement optimizeTableStatement =
mock(MySQLOptimizeTableStatement.class);
- Collection<SimpleTableSegment> tables = Collections.singletonList(new
SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("tbl_1"))));
- when(optimizeTableStatement.getTables()).thenReturn(tables);
- OptimizeTableStatementContext actual = new
OptimizeTableStatementContext(optimizeTableStatement,
DefaultDatabase.LOGIC_NAME);
+ MySQLOptimizeTableStatement sqlStatement = new
MySQLOptimizeTableStatement();
+ sqlStatement.getTables().add(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1"))));
+ OptimizeTableStatementContext actual = new
OptimizeTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
- assertThat(actual.getSqlStatement(), is(optimizeTableStatement));
+ assertThat(actual.getSqlStatement(), is(sqlStatement));
assertThat(actual.getTablesContext().getSimpleTables().stream().map(each ->
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()),
is(Collections.singletonList("tbl_1")));
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowColumnsStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowColumnsStatementContextTest.java
index 1384bf6cd3b..8937ccdcc08 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowColumnsStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowColumnsStatementContextTest.java
@@ -28,29 +28,26 @@ import
org.apache.shardingsphere.sql.parser.statement.mysql.dal.MySQLShowColumns
import org.junit.jupiter.api.Test;
import java.util.Collections;
-import java.util.Optional;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class ShowColumnsStatementContextTest {
@Test
void assertNewInstance() {
- MySQLShowColumnsStatement showColumnsStatement =
mock(MySQLShowColumnsStatement.class);
+ MySQLShowColumnsStatement sqlStatement = new
MySQLShowColumnsStatement();
String tableName = "tbl_1";
String databaseName = "sharding_db";
SimpleTableSegment table = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue(tableName)));
FromDatabaseSegment fromDatabase = new FromDatabaseSegment(0, 0, new
DatabaseSegment(0, 0, new IdentifierValue(databaseName)));
- when(showColumnsStatement.getTable()).thenReturn(table);
-
when(showColumnsStatement.getFromDatabase()).thenReturn(Optional.of(fromDatabase));
- ShowColumnsStatementContext actual = new
ShowColumnsStatementContext(showColumnsStatement, DefaultDatabase.LOGIC_NAME);
+ sqlStatement.setTable(table);
+ sqlStatement.setFromDatabase(fromDatabase);
+ ShowColumnsStatementContext actual = new
ShowColumnsStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
- assertThat(actual.getSqlStatement(), is(showColumnsStatement));
+ assertThat(actual.getSqlStatement(), is(sqlStatement));
assertThat(actual.getTablesContext().getSimpleTables().stream().map(each ->
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()),
is(Collections.singletonList(tableName)));
assertThat(actual.getRemoveSegments(),
is(Collections.singletonList(fromDatabase)));
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowCreateTableStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowCreateTableStatementContextTest.java
index e72793b5827..a0770817469 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowCreateTableStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowCreateTableStatementContextTest.java
@@ -31,19 +31,16 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class ShowCreateTableStatementContextTest {
@Test
void assertNewInstance() {
- MySQLShowCreateTableStatement showCreateTableStatement =
mock(MySQLShowCreateTableStatement.class);
- SimpleTableSegment table = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
- when(showCreateTableStatement.getTable()).thenReturn(table);
- ShowCreateTableStatementContext actual = new
ShowCreateTableStatementContext(showCreateTableStatement,
DefaultDatabase.LOGIC_NAME);
+ MySQLShowCreateTableStatement sqlStatement = new
MySQLShowCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0,
0, new IdentifierValue("tbl_1"))));
+ ShowCreateTableStatementContext actual = new
ShowCreateTableStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
- assertThat(actual.getSqlStatement(), is(showCreateTableStatement));
+ assertThat(actual.getSqlStatement(), is(sqlStatement));
assertThat(actual.getTablesContext().getSimpleTables().stream().map(each ->
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()),
is(Collections.singletonList("tbl_1")));
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowIndexStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowIndexStatementContextTest.java
index e2ae6ace09d..e37cfeb88e9 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowIndexStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dal/ShowIndexStatementContextTest.java
@@ -31,19 +31,16 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class ShowIndexStatementContextTest {
@Test
void assertNewInstance() {
- MySQLShowIndexStatement showIndexStatement =
mock(MySQLShowIndexStatement.class);
- SimpleTableSegment table = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
- when(showIndexStatement.getTable()).thenReturn(table);
- ShowIndexStatementContext actual = new
ShowIndexStatementContext(showIndexStatement, DefaultDatabase.LOGIC_NAME);
+ MySQLShowIndexStatement sqlStatement = new MySQLShowIndexStatement();
+ sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0,
0, new IdentifierValue("tbl_1"))));
+ ShowIndexStatementContext actual = new
ShowIndexStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
- assertThat(actual.getSqlStatement(), is(showIndexStatement));
+ assertThat(actual.getSqlStatement(), is(sqlStatement));
assertThat(actual.getTablesContext().getSimpleTables().stream().map(each ->
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()),
is(Collections.singletonList("tbl_1")));
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/DenyUserStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/DenyUserStatementContextTest.java
index c1f8532821f..70afe700fb7 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/DenyUserStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/DenyUserStatementContextTest.java
@@ -31,19 +31,17 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class DenyUserStatementContextTest {
@Test
void assertNewInstance() {
- SQLServerDenyUserStatement sqlServerDenyUserStatement =
mock(SQLServerDenyUserStatement.class);
+ SQLServerDenyUserStatement sqlStatement = new
SQLServerDenyUserStatement();
SimpleTableSegment table = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
- when(sqlServerDenyUserStatement.getTable()).thenReturn(table);
- DenyUserStatementContext actual = new
DenyUserStatementContext(sqlServerDenyUserStatement,
DefaultDatabase.LOGIC_NAME);
+ sqlStatement.setTable(table);
+ DenyUserStatementContext actual = new
DenyUserStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
- assertThat(actual.getSqlStatement(), is(sqlServerDenyUserStatement));
+ assertThat(actual.getSqlStatement(), is(sqlStatement));
assertThat(actual.getTablesContext().getSimpleTables().stream().map(each ->
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()),
is(Collections.singletonList("tbl_1")));
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/GrantStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/GrantStatementContextTest.java
index 09a55054241..17fc380431b 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/GrantStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/GrantStatementContextTest.java
@@ -31,50 +31,43 @@ import
org.apache.shardingsphere.sql.parser.statement.sqlserver.dcl.SQLServerGra
import org.junit.jupiter.api.Test;
import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class GrantStatementContextTest {
@Test
void assertMySQLNewInstance() {
- assertNewInstance(mock(MySQLGrantStatement.class));
+ assertNewInstance(new MySQLGrantStatement());
}
@Test
void assertPostgreSQLNewInstance() {
- assertNewInstance(mock(PostgreSQLGrantStatement.class));
+ assertNewInstance(new PostgreSQLGrantStatement());
}
@Test
void assertOracleNewInstance() {
- assertNewInstance(mock(OracleGrantStatement.class));
+ assertNewInstance(new OracleGrantStatement());
}
@Test
void assertSQLServerNewInstance() {
- assertNewInstance(mock(SQLServerGrantStatement.class));
+ assertNewInstance(new SQLServerGrantStatement());
}
@Test
void assertSQL92NewInstance() {
- assertNewInstance(mock(SQL92GrantStatement.class));
+ assertNewInstance(new SQL92GrantStatement());
}
private void assertNewInstance(final GrantStatement grantStatement) {
SimpleTableSegment table1 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
SimpleTableSegment table2 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_2")));
- List<SimpleTableSegment> tables = new LinkedList<>();
- tables.add(table1);
- tables.add(table2);
- when(grantStatement.getTables()).thenReturn(tables);
+ grantStatement.getTables().addAll(Arrays.asList(table1, table2));
GrantStatementContext actual = new
GrantStatementContext(grantStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
assertThat(actual.getSqlStatement(), is(grantStatement));
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/RevokeStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/RevokeStatementContextTest.java
index 53d64ce8d1e..b338c19d245 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/RevokeStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dcl/RevokeStatementContextTest.java
@@ -31,50 +31,43 @@ import
org.apache.shardingsphere.sql.parser.statement.sqlserver.dcl.SQLServerRev
import org.junit.jupiter.api.Test;
import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class RevokeStatementContextTest {
@Test
void assertMySQLNewInstance() {
- assertNewInstance(mock(MySQLRevokeStatement.class));
+ assertNewInstance(new MySQLRevokeStatement());
}
@Test
void assertPostgreSQLNewInstance() {
- assertNewInstance(mock(PostgreSQLRevokeStatement.class));
+ assertNewInstance(new PostgreSQLRevokeStatement());
}
@Test
void assertOracleNewInstance() {
- assertNewInstance(mock(OracleRevokeStatement.class));
+ assertNewInstance(new OracleRevokeStatement());
}
@Test
void assertSQLServerNewInstance() {
- assertNewInstance(mock(SQLServerRevokeStatement.class));
+ assertNewInstance(new SQLServerRevokeStatement());
}
@Test
void assertSQL92NewInstance() {
- assertNewInstance(mock(SQL92RevokeStatement.class));
+ assertNewInstance(new SQL92RevokeStatement());
}
private void assertNewInstance(final RevokeStatement revokeStatement) {
SimpleTableSegment table1 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
SimpleTableSegment table2 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_2")));
- List<SimpleTableSegment> tables = new LinkedList<>();
- tables.add(table1);
- tables.add(table2);
- when(revokeStatement.getTables()).thenReturn(tables);
+ revokeStatement.getTables().addAll(Arrays.asList(table1, table2));
RevokeStatementContext actual = new
RevokeStatementContext(revokeStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
assertThat(actual.getSqlStatement(), is(revokeStatement));
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterTableStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterTableStatementContextTest.java
index 1aaaf3411e4..1c78fed7865 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterTableStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterTableStatementContextTest.java
@@ -57,43 +57,43 @@ class AlterTableStatementContextTest {
@Test
void assertMySQLNewInstance() {
- assertNewInstance(mock(MySQLAlterTableStatement.class));
+ assertNewInstance(new MySQLAlterTableStatement());
}
@Test
void assertPostgreSQLNewInstance() {
- assertNewInstance(mock(PostgreSQLAlterTableStatement.class));
+ assertNewInstance(new PostgreSQLAlterTableStatement());
}
@Test
void assertOracleNewInstance() {
- assertNewInstance(mock(OracleAlterTableStatement.class));
+ assertNewInstance(new OracleAlterTableStatement());
}
@Test
void assertSQLServerNewInstance() {
- assertNewInstance(mock(SQLServerAlterTableStatement.class));
+ assertNewInstance(new SQLServerAlterTableStatement());
}
@Test
void assertSQL92NewInstance() {
- assertNewInstance(mock(SQL92AlterTableStatement.class));
+ assertNewInstance(new SQL92AlterTableStatement());
}
private void assertNewInstance(final AlterTableStatement
alterTableStatement) {
SimpleTableSegment table = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
SimpleTableSegment renameTable = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("rename_tbl_1")));
- when(alterTableStatement.getTable()).thenReturn(table);
-
when(alterTableStatement.getRenameTable()).thenReturn(Optional.of(renameTable));
+ alterTableStatement.setTable(table);
+ alterTableStatement.setRenameTable(renameTable);
Collection<SimpleTableSegment> referencedTables =
Collections.singletonList(table);
ColumnDefinitionSegment columnDefinition =
mock(ColumnDefinitionSegment.class);
when(columnDefinition.getReferencedTables()).thenReturn(referencedTables);
AddColumnDefinitionSegment addColumnDefinition =
mock(AddColumnDefinitionSegment.class);
when(addColumnDefinition.getColumnDefinitions()).thenReturn(Collections.singletonList(columnDefinition));
-
when(alterTableStatement.getAddColumnDefinitions()).thenReturn(Collections.singletonList(addColumnDefinition));
+ alterTableStatement.getAddColumnDefinitions().add(addColumnDefinition);
ModifyColumnDefinitionSegment modifyColumnDefinition =
mock(ModifyColumnDefinitionSegment.class);
when(modifyColumnDefinition.getColumnDefinition()).thenReturn(columnDefinition);
-
when(alterTableStatement.getModifyColumnDefinitions()).thenReturn(Collections.singletonList(modifyColumnDefinition));
+
alterTableStatement.getModifyColumnDefinitions().add(modifyColumnDefinition);
ConstraintDefinitionSegment constraintDefinition =
mock(ConstraintDefinitionSegment.class);
when(constraintDefinition.getReferencedTable()).thenReturn(Optional.of(table));
when(constraintDefinition.getIndexName()).thenReturn(Optional.of(new
IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("index")))));
@@ -101,16 +101,16 @@ class AlterTableStatementContextTest {
when(addConstraintDefinition.getConstraintDefinition()).thenReturn(constraintDefinition);
ConstraintSegment constraint = new ConstraintSegment(0, 0, new
IdentifierValue("constraint"));
when(addConstraintDefinition.getConstraintDefinition().getConstraintName()).thenReturn(Optional.of(constraint));
-
when(alterTableStatement.getAddConstraintDefinitions()).thenReturn(Collections.singletonList(addConstraintDefinition));
+
alterTableStatement.getAddConstraintDefinitions().add(addConstraintDefinition);
ValidateConstraintDefinitionSegment validateConstraintDefinition =
mock(ValidateConstraintDefinitionSegment.class);
when(validateConstraintDefinition.getConstraintName()).thenReturn(constraint);
-
when(alterTableStatement.getValidateConstraintDefinitions()).thenReturn(Collections.singletonList(validateConstraintDefinition));
+
alterTableStatement.getValidateConstraintDefinitions().add(validateConstraintDefinition);
DropConstraintDefinitionSegment dropConstraintDefinition =
mock(DropConstraintDefinitionSegment.class);
when(dropConstraintDefinition.getConstraintName()).thenReturn(constraint);
-
when(alterTableStatement.getDropConstraintDefinitions()).thenReturn(Collections.singletonList(dropConstraintDefinition));
+
alterTableStatement.getDropConstraintDefinitions().add(dropConstraintDefinition);
DropIndexDefinitionSegment dropIndexDefinitionSegment =
mock(DropIndexDefinitionSegment.class);
when(dropIndexDefinitionSegment.getIndexSegment()).thenReturn(new
IndexSegment(0, 0, new IndexNameSegment(0, 0, new
IdentifierValue("drop_index"))));
-
when(alterTableStatement.getDropIndexDefinitions()).thenReturn(Collections.singletonList(dropIndexDefinitionSegment));
+
alterTableStatement.getDropIndexDefinitions().add(dropIndexDefinitionSegment);
AlterTableStatementContext actual = new
AlterTableStatementContext(alterTableStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
assertThat(actual.getSqlStatement(), is(alterTableStatement));
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterViewStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterViewStatementContextTest.java
index 78cbcd839a7..337b8606bbb 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterViewStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/AlterViewStatementContextTest.java
@@ -51,17 +51,17 @@ class AlterViewStatementContextTest {
void assertMySQLNewInstance() {
SelectStatement select = mock(MySQLSelectStatement.class);
when(select.getFrom()).thenReturn(Optional.of(view));
- MySQLAlterViewStatement alterViewStatement =
mock(MySQLAlterViewStatement.class);
- when(alterViewStatement.getView()).thenReturn(view);
-
when(alterViewStatement.getSelectStatement()).thenReturn(Optional.of(select));
+ MySQLAlterViewStatement alterViewStatement = new
MySQLAlterViewStatement();
+ alterViewStatement.setView(view);
+ alterViewStatement.setSelect(select);
assertNewInstance(alterViewStatement);
}
@Test
void assertPostgreSQLNewInstance() {
- PostgreSQLAlterViewStatement alterViewStatement =
mock(PostgreSQLAlterViewStatement.class);
- when(alterViewStatement.getView()).thenReturn(view);
- when(alterViewStatement.getRenameView()).thenReturn(Optional.of(new
SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("view")))));
+ PostgreSQLAlterViewStatement alterViewStatement = new
PostgreSQLAlterViewStatement();
+ alterViewStatement.setView(view);
+ alterViewStatement.setRenameView(new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("view"))));
assertNewInstance(alterViewStatement);
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/CreateViewStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/CreateViewStatementContextTest.java
index 9280fb817c1..f87448bed05 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/CreateViewStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/CreateViewStatementContextTest.java
@@ -32,24 +32,23 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class CreateViewStatementContextTest {
@Test
void assertMySQLNewInstance() {
- assertNewInstance(mock(MySQLCreateViewStatement.class));
+ assertNewInstance(new MySQLCreateViewStatement());
}
@Test
void assertPostgreSQLNewInstance() {
- assertNewInstance(mock(PostgreSQLCreateViewStatement.class));
+ assertNewInstance(new PostgreSQLCreateViewStatement());
}
private void assertNewInstance(final CreateViewStatement
createViewStatement) {
SimpleTableSegment view = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("view")));
- when(createViewStatement.getView()).thenReturn(view);
-
when(createViewStatement.getSelect()).thenReturn(mock(SelectStatement.class));
+ createViewStatement.setView(view);
+ createViewStatement.setSelect(mock(SelectStatement.class));
CreateViewStatementContext actual = new
CreateViewStatementContext(createViewStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
assertThat(actual.getSqlStatement(), is(createViewStatement));
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/DropViewStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/DropViewStatementContextTest.java
index b3f9b877a28..7b5b8828498 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/DropViewStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/DropViewStatementContextTest.java
@@ -32,25 +32,23 @@ import java.util.Arrays;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class DropViewStatementContextTest {
@Test
void assertMySQLNewInstance() {
- assertNewInstance(mock(MySQLDropViewStatement.class));
+ assertNewInstance(new MySQLDropViewStatement());
}
@Test
void assertPostgreSQLNewInstance() {
- assertNewInstance(mock(PostgreSQLDropViewStatement.class));
+ assertNewInstance(new PostgreSQLDropViewStatement());
}
private void assertNewInstance(final DropViewStatement dropViewStatement) {
SimpleTableSegment table1 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
SimpleTableSegment table2 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_2")));
- when(dropViewStatement.getViews()).thenReturn(Arrays.asList(table1,
table2));
+ dropViewStatement.getViews().addAll(Arrays.asList(table1, table2));
DropViewStatementContext actual = new
DropViewStatementContext(dropViewStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
assertThat(actual.getSqlStatement(), is(dropViewStatement));
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/PrepareStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/PrepareStatementContextTest.java
index 832d15dfaa8..45bd4d5758e 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/PrepareStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/PrepareStatementContextTest.java
@@ -39,14 +39,11 @@ import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
-import java.util.Optional;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class PrepareStatementContextTest {
@@ -62,14 +59,14 @@ class PrepareStatementContextTest {
@Test
void assertNewInstance() {
- PostgreSQLPrepareStatement prepareStatement =
mock(PostgreSQLPrepareStatement.class);
-
when(prepareStatement.getSelect()).thenReturn(Optional.of(getSelect()));
-
when(prepareStatement.getInsert()).thenReturn(Optional.of(getInsert()));
-
when(prepareStatement.getUpdate()).thenReturn(Optional.of(getUpdate()));
-
when(prepareStatement.getDelete()).thenReturn(Optional.of(getDelete()));
- PrepareStatementContext actual = new
PrepareStatementContext(prepareStatement, DefaultDatabase.LOGIC_NAME);
+ PostgreSQLPrepareStatement sqlStatement = new
PostgreSQLPrepareStatement();
+ sqlStatement.setSelect(getSelect());
+ sqlStatement.setInsert(getInsert());
+ sqlStatement.setUpdate(getUpdate());
+ sqlStatement.setDelete(getDelete());
+ PrepareStatementContext actual = new
PrepareStatementContext(sqlStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
- assertThat(actual.getSqlStatement(), is(prepareStatement));
+ assertThat(actual.getSqlStatement(), is(sqlStatement));
assertThat(actual.getTablesContext().getSimpleTables().stream().map(each ->
each.getTableName().getIdentifier().getValue()).collect(Collectors.toList()),
is(Arrays.asList("tbl_1", "tbl_1", "tbl_1", "tbl_1")));
}
diff --git
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/TruncateStatementContextTest.java
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/TruncateStatementContextTest.java
index ad7ea3439c3..12990c14a64 100644
---
a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/TruncateStatementContextTest.java
+++
b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/ddl/TruncateStatementContextTest.java
@@ -35,35 +35,33 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class TruncateStatementContextTest {
@Test
void assertMySQLNewInstance() {
- assertNewInstance(mock(MySQLTruncateStatement.class));
+ assertNewInstance(new MySQLTruncateStatement());
}
@Test
void assertPostgreSQLNewInstance() {
- assertNewInstance(mock(PostgreSQLTruncateStatement.class));
+ assertNewInstance(new PostgreSQLTruncateStatement());
}
@Test
void assertOracleNewInstance() {
- assertNewInstance(mock(OracleTruncateStatement.class));
+ assertNewInstance(new OracleTruncateStatement());
}
@Test
void assertSQLServerNewInstance() {
- assertNewInstance(mock(SQLServerTruncateStatement.class));
+ assertNewInstance(new SQLServerTruncateStatement());
}
private void assertNewInstance(final TruncateStatement truncateStatement) {
SimpleTableSegment table1 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_1")));
SimpleTableSegment table2 = new SimpleTableSegment(new
TableNameSegment(0, 0, new IdentifierValue("tbl_2")));
- when(truncateStatement.getTables()).thenReturn(Arrays.asList(table1,
table2));
+ truncateStatement.getTables().addAll(Arrays.asList(table1, table2));
TruncateStatementContext actual = new
TruncateStatementContext(truncateStatement, DefaultDatabase.LOGIC_NAME);
assertThat(actual, instanceOf(CommonSQLStatementContext.class));
assertThat(actual.getSqlStatement(), is(truncateStatement));