You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2025/07/04 14:47:22 UTC

(shardingsphere) branch master updated: Refactor ShardingIndexTokenGeneratorTest (#35887)

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 9e9dce3caf0 Refactor ShardingIndexTokenGeneratorTest (#35887)
9e9dce3caf0 is described below

commit 9e9dce3caf09fb760af67a689ff74a3602a2e56c
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Jul 4 22:47:16 2025 +0800

    Refactor ShardingIndexTokenGeneratorTest (#35887)
---
 .../impl/ShardingIndexTokenGeneratorTest.java         | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)

diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingIndexTokenGeneratorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingIndexTokenGeneratorTest.java
index 0d5e766764f..f6693551499 100644
--- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingIndexTokenGeneratorTest.java
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingIndexTokenGeneratorTest.java
@@ -36,13 +36,11 @@ import org.mockito.internal.configuration.plugins.Plugins;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -59,26 +57,21 @@ class ShardingIndexTokenGeneratorTest {
     
     @Test
     void assertIsNotGenerateSQLTokenWithEmptyIndex() {
-        CommonSQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class, RETURNS_DEEP_STUBS);
-        when(sqlStatementContext.getSqlStatement()).thenReturn(new AlterIndexStatement());
-        when(sqlStatementContext.getDatabaseType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"));
+        CommonSQLStatementContext sqlStatementContext = new CommonSQLStatementContext(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"), new AlterIndexStatement());
         assertFalse(generator.isGenerateSQLToken(sqlStatementContext));
     }
     
     @Test
     void assertIsGenerateSQLToken() {
-        CommonSQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class, RETURNS_DEEP_STUBS);
         AlterIndexStatement sqlStatement = new AlterIndexStatement();
         sqlStatement.setIndex(mock(IndexSegment.class));
-        when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement);
-        when(sqlStatementContext.getDatabaseType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"));
+        CommonSQLStatementContext sqlStatementContext = new CommonSQLStatementContext(TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"), sqlStatement);
         assertTrue(generator.isGenerateSQLToken(sqlStatementContext));
     }
     
     @Test
     void assertGenerateSQLTokensWithNotIndexContextAvailable() {
-        CommonSQLStatementContext sqlStatementContext = mock(CommonSQLStatementContext.class, RETURNS_DEEP_STUBS);
-        when(sqlStatementContext.getSqlStatement()).thenReturn(new AlterIndexStatement());
+        CommonSQLStatementContext sqlStatementContext = new CommonSQLStatementContext(mock(), new AlterIndexStatement());
         Collection<SQLToken> actual = generator.generateSQLTokens(sqlStatementContext);
         assertTrue(actual.isEmpty());
     }
@@ -105,13 +98,9 @@ class ShardingIndexTokenGeneratorTest {
     }
     
     private CommonSQLStatementContext mockAlterIndexStatementContext(final IndexSegment indexSegment) {
-        CommonSQLStatementContext result = mock(CommonSQLStatementContext.class, RETURNS_DEEP_STUBS);
         AlterIndexStatement sqlStatement = new AlterIndexStatement();
         sqlStatement.setIndex(indexSegment);
-        when(result.getSqlStatement()).thenReturn(sqlStatement);
-        when(result.getTablesContext().getSchemaName()).thenReturn(Optional.empty());
-        when(result.getDatabaseType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
-        return result;
+        return new CommonSQLStatementContext(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), sqlStatement);
     }
     
     private void assertTokens(final Collection<SQLToken> actual, final ShardingSphereSchema schema) throws ReflectiveOperationException {