From 3613701321a4ecefa912d5e75fb640574ea5b112 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 15 Nov 2024 14:44:52 +0800 Subject: [PATCH 1/3] Add more test cases on TokenUtils --- .../rewrite/token/pojo/TokenUtilsTest.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtilsTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtilsTest.java index 49cf9693a8541..30f11ce3b88b5 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtilsTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtilsTest.java @@ -31,6 +31,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +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; @@ -38,27 +39,33 @@ class TokenUtilsTest { + @Test + void assertGetLogicAndActualTablesWithNotTableAvailable() { + Map actual = TokenUtils.getLogicAndActualTableMap(mock(RouteUnit.class), mock(SQLStatementContext.class), mock(ShardingRule.class)); + assertTrue(actual.isEmpty()); + } + @Test void assertGetLogicAndActualTablesFromRouteUnit() { Map actual = TokenUtils.getLogicAndActualTableMap(getRouteUnit(), mockSQLStatementContext(), mockShardingRule()); - assertThat(actual.get("foo_table"), is("foo_table_0")); - assertThat(actual.get("bar_table"), is("bar_table_0")); + assertThat(actual.get("foo_tbl"), is("foo_tbl_0")); + assertThat(actual.get("bar_tbl"), is("bar_tbl_0")); } private RouteUnit getRouteUnit() { - return new RouteUnit(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0"), Collections.singleton(new RouteMapper("foo_table", "foo_table_0"))); + return new RouteUnit(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0"), Collections.singleton(new RouteMapper("foo_tbl", "foo_tbl_0"))); } private static SQLStatementContext mockSQLStatementContext() { SQLStatementContext result = mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class).defaultAnswer(RETURNS_DEEP_STUBS)); - when(((TableAvailable) result).getTablesContext().getTableNames()).thenReturn(Arrays.asList("foo_table", "bar_table")); + when(((TableAvailable) result).getTablesContext().getTableNames()).thenReturn(Arrays.asList("foo_tbl", "bar_tbl")); return result; } private static ShardingRule mockShardingRule() { ShardingRule result = mock(ShardingRule.class); - when(result.getLogicAndActualTablesFromBindingTable(DefaultDatabase.LOGIC_NAME, "foo_table", "foo_table_0", Arrays.asList("foo_table", "bar_table"))) - .thenReturn(Collections.singletonMap("bar_table", "bar_table_0")); + when(result.getLogicAndActualTablesFromBindingTable(DefaultDatabase.LOGIC_NAME, "foo_tbl", "foo_tbl_0", Arrays.asList("foo_tbl", "bar_tbl"))) + .thenReturn(Collections.singletonMap("bar_tbl", "bar_tbl_0")); return result; } } From c2cee36c4aca902726d474805986a32f89e71cb0 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 15 Nov 2024 14:50:19 +0800 Subject: [PATCH 2/3] Rename ShardingTokenUtils --- .../sharding/rewrite/token/pojo/ConstraintToken.java | 2 +- .../sharding/rewrite/token/pojo/CursorToken.java | 2 +- .../sharding/rewrite/token/pojo/IndexToken.java | 2 +- .../rewrite/token/pojo/ShardingTableToken.java | 2 +- .../pojo/{TokenUtils.java => ShardingTokenUtils.java} | 10 +++++----- ...TokenUtilsTest.java => ShardingTokenUtilsTest.java} | 6 +++--- 6 files changed, 12 insertions(+), 12 deletions(-) rename features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/{TokenUtils.java => ShardingTokenUtils.java} (88%) rename features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/{TokenUtilsTest.java => ShardingTokenUtilsTest.java} (89%) diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ConstraintToken.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ConstraintToken.java index 1bd8a83264835..b99df39e3c93c 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ConstraintToken.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ConstraintToken.java @@ -58,7 +58,7 @@ public String toString(final RouteUnit routeUnit) { private String getConstraintValue(final RouteUnit routeUnit) { StringBuilder result = new StringBuilder(identifier.getValue()); - Map logicAndActualTables = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule); + Map logicAndActualTables = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule); ((TableAvailable) sqlStatementContext).getTablesContext().getTableNames().stream().findFirst().map(logicAndActualTables::get).ifPresent(optional -> result.append('_').append(optional)); return result.toString(); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/CursorToken.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/CursorToken.java index a6b7678867cb2..86fe4cb1a7401 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/CursorToken.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/CursorToken.java @@ -57,7 +57,7 @@ public String toString(final RouteUnit routeUnit) { } private String getCursorValue(final RouteUnit routeUnit) { - Map logicAndActualTables = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule); + Map logicAndActualTables = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule); String actualTableName = logicAndActualTables.values().stream().sorted().findFirst().orElse(null); return Strings.isNullOrEmpty(actualTableName) ? identifier.getValue() : identifier.getValue() + "_" + actualTableName; } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java index 0aa6b4da5c366..11a6fe414ec1c 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java @@ -73,7 +73,7 @@ private String getIndexValue(final RouteUnit routeUnit) { if (logicTableName.isPresent() && !shardingRule.isShardingTable(logicTableName.get())) { return identifier.getValue(); } - Map logicAndActualTables = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule); + Map logicAndActualTables = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule); String actualTableName = logicTableName.map(logicAndActualTables::get).orElseGet(() -> logicAndActualTables.isEmpty() ? null : logicAndActualTables.values().iterator().next()); return IndexMetaDataUtils.getActualIndexName(identifier.getValue(), actualTableName); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableToken.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableToken.java index 78b57967d84c4..3430c1457a270 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableToken.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableToken.java @@ -54,7 +54,7 @@ public String toString(final RouteUnit routeUnit) { } private String getActualTableName(final RouteUnit routeUnit) { - String actualTableName = TokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule).get(tableName.getValue()); + String actualTableName = ShardingTokenUtils.getLogicAndActualTableMap(routeUnit, sqlStatementContext, shardingRule).get(tableName.getValue()); actualTableName = null == actualTableName ? tableName.getValue() : actualTableName; return tableName.getQuoteCharacter().wrap(actualTableName); } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtils.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenUtils.java similarity index 88% rename from features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtils.java rename to features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenUtils.java index 7837931db05e2..ee4cebf3dd155 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtils.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenUtils.java @@ -31,20 +31,20 @@ import java.util.Map; /** - * Token utils. + * Sharding token utils. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TokenUtils { +public final class ShardingTokenUtils { /** * Get logic and actual table map. * * @param routeUnit route unit * @param sqlStatementContext SQL statement context - * @param shardingRule sharding rule + * @param rule sharding rule * @return key is logic table name, values is actual table belong to this data source */ - public static Map getLogicAndActualTableMap(final RouteUnit routeUnit, final SQLStatementContext sqlStatementContext, final ShardingRule shardingRule) { + public static Map getLogicAndActualTableMap(final RouteUnit routeUnit, final SQLStatementContext sqlStatementContext, final ShardingRule rule) { if (!(sqlStatementContext instanceof TableAvailable)) { return Collections.emptyMap(); } @@ -52,7 +52,7 @@ public static Map getLogicAndActualTableMap(final RouteUnit rout Map result = new CaseInsensitiveMap<>(tableNames.size(), 1F); for (RouteMapper each : routeUnit.getTableMappers()) { result.put(each.getLogicName(), each.getActualName()); - result.putAll(shardingRule.getLogicAndActualTablesFromBindingTable(routeUnit.getDataSourceMapper().getLogicName(), each.getLogicName(), each.getActualName(), tableNames)); + result.putAll(rule.getLogicAndActualTablesFromBindingTable(routeUnit.getDataSourceMapper().getLogicName(), each.getLogicName(), each.getActualName(), tableNames)); } return result; } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtilsTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenUtilsTest.java similarity index 89% rename from features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtilsTest.java rename to features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenUtilsTest.java index 30f11ce3b88b5..90684b16957e7 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/TokenUtilsTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenUtilsTest.java @@ -37,17 +37,17 @@ import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; -class TokenUtilsTest { +class ShardingTokenUtilsTest { @Test void assertGetLogicAndActualTablesWithNotTableAvailable() { - Map actual = TokenUtils.getLogicAndActualTableMap(mock(RouteUnit.class), mock(SQLStatementContext.class), mock(ShardingRule.class)); + Map actual = ShardingTokenUtils.getLogicAndActualTableMap(mock(RouteUnit.class), mock(SQLStatementContext.class), mock(ShardingRule.class)); assertTrue(actual.isEmpty()); } @Test void assertGetLogicAndActualTablesFromRouteUnit() { - Map actual = TokenUtils.getLogicAndActualTableMap(getRouteUnit(), mockSQLStatementContext(), mockShardingRule()); + Map actual = ShardingTokenUtils.getLogicAndActualTableMap(getRouteUnit(), mockSQLStatementContext(), mockShardingRule()); assertThat(actual.get("foo_tbl"), is("foo_tbl_0")); assertThat(actual.get("bar_tbl"), is("bar_tbl_0")); } From 25169b574a28dd36790a94ff788aa0deae110cc8 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Fri, 15 Nov 2024 17:31:55 +0800 Subject: [PATCH 3/3] Add more test cases on ShardingTableToken --- .../token/pojo/ShardingTableTokenTest.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableTokenTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableTokenTest.java index 03e139f7d95f3..9e21ea838ef75 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableTokenTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTableTokenTest.java @@ -17,9 +17,7 @@ package org.apache.shardingsphere.sharding.rewrite.token.pojo; -import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; -import org.apache.shardingsphere.infra.binder.context.type.TableAvailable; -import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; import org.apache.shardingsphere.infra.route.context.RouteUnit; import org.apache.shardingsphere.sharding.rule.ShardingRule; @@ -33,17 +31,27 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.withSettings; class ShardingTableTokenTest { @Test - void assertToString() { - ShardingTableToken tableToken = new ShardingTableToken(0, 0, new IdentifierValue("t_order"), - mock(SQLStatementContext.class, withSettings().extraInterfaces(TableAvailable.class).defaultAnswer(RETURNS_DEEP_STUBS)), mock(ShardingRule.class)); - RouteUnit routeUnit = mock(RouteUnit.class); - when(routeUnit.getTableMappers()).thenReturn(Collections.singletonList(new RouteMapper("t_order", "t_order_0"))); - when(routeUnit.getDataSourceMapper()).thenReturn(new RouteMapper(DefaultDatabase.LOGIC_NAME, "ds_0")); - assertThat(tableToken.toString(routeUnit), is("t_order_0")); + void assertToStringWithActualTableName() { + assertThat(createTableToken("foo_tbl").toString(mockRouteUnit()), is("foo_tbl_0")); + } + + @Test + void assertToStringWithOriginalTableName() { + assertThat(createTableToken("bar_tbl").toString(mockRouteUnit()), is("bar_tbl")); + } + + private ShardingTableToken createTableToken(final String tableName) { + return new ShardingTableToken(0, 0, new IdentifierValue(tableName), mock(SelectStatementContext.class, RETURNS_DEEP_STUBS), mock(ShardingRule.class)); + } + + private RouteUnit mockRouteUnit() { + RouteUnit result = mock(RouteUnit.class); + when(result.getDataSourceMapper()).thenReturn(new RouteMapper("foo_ds", "foo_ds_0")); + when(result.getTableMappers()).thenReturn(Collections.singletonList(new RouteMapper("foo_tbl", "foo_tbl_0"))); + return result; } }