This is an automated email from the ASF dual-hosted git repository.
panjuan 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 b99668c Issue10968 (#12159)
b99668c is described below
commit b99668c759ca546ed605f4e29d17c6504240485c
Author: fwhdzh <[email protected]>
AuthorDate: Wed Sep 8 15:23:51 2021 +0800
Issue10968 (#12159)
* Migrate YAML Configuration change history from
4.1.2.Configuration Manual->YAML Configuration->Change History to
7.2.API Change History->7.2.1.ShardingSphere-JDBC->YAML Configuration;
* Add content of change history
* add unit test of ShardingGeneratedKeyInsertValueParameterRewriter.
* define the constant as static final.
* Add test code of ShardingPaginationParameterRewriter and
GeneratedKeyAssignmentTokenGenerator.
remove ShardingGeneratedKeyInsertValueParameterRewriterTest from *.rewrite
to *.rewrite.parameter.
* add final.
* Add unit test of GeneratedKeyForUseDefaultInsertColumnsTokenGenerator,
GeneratedKeyInsertColumnTokenGenerator and
GeneratedKeyInsertValuesTokenGenerator.
* Add unit test of AggregationDistinctTokenGenerator.
* Add unit test of ConstraintTokenGenerator and
DistinctProjectionPrefixTokenGenerator.
* Add unit test of IndexTokenGenerator and OffsetTokenGenerator.
* Add unit test of OrderByTokenGenerator.
* Add unit test of ProjectionsTokenGenerator.
* Add unit test of RowCountTokenGenerator and
ShardingInsertValuesTokenGenerator.
* Add unit test of RowCountTokenGenerator and
ShardingInsertValuesTokenGenerator.
* modify the problem mentioned.
* add unit test of TableTokenGenerator.
* Minimize the use of variables:
If a variable is only used in one method, I use local variables instead of
static variables.
If a variable of String type is only used once, I use the content of the
String directly instead of a variable.
If a variable of Int or Long type is only used once, I still use a local
variables in afraid of confusion of magic number.
If a variable is used in many method, I use it as static variable.
---
.../AggregationDistinctTokenGeneratorTest.java | 28 +++----
.../token/ConstraintTokenGeneratorTest.java | 13 ++--
...DistinctProjectionPrefixTokenGeneratorTest.java | 5 +-
.../GeneratedKeyAssignmentTokenGeneratorTest.java | 14 ++--
...rUseDefaultInsertColumnsTokenGeneratorTest.java | 12 ++-
...GeneratedKeyInsertColumnTokenGeneratorTest.java | 12 ++-
.../rewrite/token/IndexTokenGeneratorTest.java | 13 ++--
.../rewrite/token/OffsetTokenGeneratorTest.java | 17 ++---
.../rewrite/token/OrderByTokenGeneratorTest.java | 7 +-
.../token/ProjectionsTokenGeneratorTest.java | 12 ++-
...orTest.java => RowCountTokenGeneratorTest.java} | 53 ++++++-------
.../ShardingInsertValuesTokenGeneratorTest.java | 87 ++++++++++++++++++++++
.../rewrite/token/TableTokenGeneratorTest.java | 62 +++++++++++++++
13 files changed, 226 insertions(+), 109 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/AggregationDistinctTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/AggregationDistinctTokenGeneratorTest.java
index 71e5f1c..b8c0bb4 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/AggregationDistinctTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/AggregationDistinctTokenGeneratorTest.java
@@ -39,16 +39,6 @@ import static org.mockito.Mockito.when;
public final class AggregationDistinctTokenGeneratorTest {
- public static final String TEST_ALIAS = "AVG_DERIVED_COUNT_0";
-
- public static final String TEST_ERROR_ALIAS = "TEST_ERROR_ALIAS";
-
- private static final int TEST_START_INDEX = 0;
-
- private static final int TEST_STOP_INDEX = 2;
-
- private static final String TEST_DISTINCT_INNER_EXPRESSION =
"TEST_DISTINCT_INNER_EXPRESSION";
-
@Test
public void assertIsGenerateSQLToken() {
AggregationDistinctTokenGenerator aggregationDistinctTokenGenerator =
new AggregationDistinctTokenGenerator();
@@ -61,20 +51,24 @@ public final class AggregationDistinctTokenGeneratorTest {
@Test
public void assertGenerateSQLToken() {
AggregationDistinctProjection aggregationDistinctProjection =
mock(AggregationDistinctProjection.class);
-
when(aggregationDistinctProjection.getAlias()).thenReturn(Optional.of(TEST_ALIAS));
-
when(aggregationDistinctProjection.getStartIndex()).thenReturn(TEST_START_INDEX);
-
when(aggregationDistinctProjection.getStopIndex()).thenReturn(TEST_STOP_INDEX);
-
when(aggregationDistinctProjection.getDistinctInnerExpression()).thenReturn(TEST_DISTINCT_INNER_EXPRESSION);
+ final String testAlias = "AVG_DERIVED_COUNT_0";
+
when(aggregationDistinctProjection.getAlias()).thenReturn(Optional.of(testAlias));
+ final int testStartIndex = 0;
+
when(aggregationDistinctProjection.getStartIndex()).thenReturn(testStartIndex);
+ final int testStopIndex = 2;
+
when(aggregationDistinctProjection.getStopIndex()).thenReturn(testStopIndex);
+ final String testDistinctInnerExpression =
"TEST_DISTINCT_INNER_EXPRESSION";
+
when(aggregationDistinctProjection.getDistinctInnerExpression()).thenReturn(testDistinctInnerExpression);
List<AggregationDistinctProjection> aggregationDistinctProjectionList
= new LinkedList<>();
aggregationDistinctProjectionList.add(aggregationDistinctProjection);
SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
when(selectStatementContext.getProjectionsContext().getAggregationDistinctProjections()).thenReturn(aggregationDistinctProjectionList);
AggregationDistinctTokenGenerator aggregationDistinctTokenGenerator =
new AggregationDistinctTokenGenerator();
List<AggregationDistinctToken> generateSQLTokensResult =
aggregationDistinctTokenGenerator.generateSQLTokens(selectStatementContext).stream().collect(Collectors.toList());
- assertThat(generateSQLTokensResult.get(0).toString(),
is(TEST_DISTINCT_INNER_EXPRESSION + " AS " + TEST_ALIAS));
-
when(aggregationDistinctProjection.getAlias()).thenReturn(Optional.of(TEST_ERROR_ALIAS));
+ assertThat(generateSQLTokensResult.get(0).toString(),
is(testDistinctInnerExpression + " AS " + testAlias));
+
when(aggregationDistinctProjection.getAlias()).thenReturn(Optional.of("TEST_ERROR_ALIAS"));
generateSQLTokensResult =
aggregationDistinctTokenGenerator.generateSQLTokens(selectStatementContext).stream().collect(Collectors.toList());
- assertThat(generateSQLTokensResult.get(0).toString(),
is(TEST_DISTINCT_INNER_EXPRESSION));
+ assertThat(generateSQLTokensResult.get(0).toString(),
is(testDistinctInnerExpression));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ConstraintTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ConstraintTokenGeneratorTest.java
index 76a1e23..5d0b052 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ConstraintTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ConstraintTokenGeneratorTest.java
@@ -28,7 +28,6 @@ import org.junit.Test;
import java.util.Collection;
import java.util.LinkedList;
-import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
@@ -39,10 +38,6 @@ import static org.mockito.Mockito.when;
public final class ConstraintTokenGeneratorTest {
- private static final int TEST_START_INDEX = 1;
-
- private static final int TEST_STOP_INDEX = 3;
-
@Test
public void assertIsGenerateSQLToken() {
CreateDatabaseStatementContext createDatabaseStatementContext =
mock(CreateDatabaseStatementContext.class);
@@ -59,8 +54,10 @@ public final class ConstraintTokenGeneratorTest {
@Test
public void assertGenerateSQLTokens() {
ConstraintSegment constraintSegment = mock(ConstraintSegment.class);
- when(constraintSegment.getStartIndex()).thenReturn(TEST_START_INDEX);
- when(constraintSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStartIndex = 1;
+ when(constraintSegment.getStartIndex()).thenReturn(testStartIndex);
+ final int testStopIndex = 3;
+ when(constraintSegment.getStopIndex()).thenReturn(testStopIndex);
IdentifierValue constraintIdentifier = mock(IdentifierValue.class);
when(constraintSegment.getIdentifier()).thenReturn(constraintIdentifier);
Collection<ConstraintSegment> constraintSegmentCollection = new
LinkedList<>();
@@ -72,6 +69,6 @@ public final class ConstraintTokenGeneratorTest {
constraintTokenGenerator.setShardingRule(shardingRule);
Collection<ConstraintToken> result =
constraintTokenGenerator.generateSQLTokens(alterTableStatementContext);
assertThat(result.size(), is(1));
-
assertThat(result.stream().collect(Collectors.toList()).get(0).getStartIndex(),
is(TEST_START_INDEX));
+ assertThat((new LinkedList<>(result)).get(0).getStartIndex(),
is(testStartIndex));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/DistinctProjectionPrefixTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/DistinctProjectionPrefixTokenGeneratorTest.java
index 5cb2e93..37b5cfa 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/DistinctProjectionPrefixTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/DistinctProjectionPrefixTokenGeneratorTest.java
@@ -37,8 +37,6 @@ import static org.mockito.Mockito.when;
public final class DistinctProjectionPrefixTokenGeneratorTest {
- private static final int TEST_START_INDEX = 1;
-
@Test
public void assertIsGenerateSQLToken() {
CreateDatabaseStatementContext createDatabaseStatementContext =
mock(CreateDatabaseStatementContext.class);
@@ -55,7 +53,8 @@ public final class DistinctProjectionPrefixTokenGeneratorTest
{
@Test
public void assertGenerateSQLToken() {
SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
-
when(selectStatementContext.getProjectionsContext().getStartIndex()).thenReturn(TEST_START_INDEX);
+ final int testStartIndex = 1;
+
when(selectStatementContext.getProjectionsContext().getStartIndex()).thenReturn(testStartIndex);
DistinctProjectionPrefixTokenGenerator
distinctProjectionPrefixTokenGenerator = new
DistinctProjectionPrefixTokenGenerator();
DistinctProjectionPrefixToken distinctProjectionPrefixToken =
distinctProjectionPrefixTokenGenerator.generateSQLToken(selectStatementContext);
assertThat(distinctProjectionPrefixToken.toString(), is("DISTINCT "));
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyAssignmentTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyAssignmentTokenGeneratorTest.java
index 8d12d6b..3cba10d 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyAssignmentTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyAssignmentTokenGeneratorTest.java
@@ -39,25 +39,21 @@ import static org.mockito.Mockito.when;
public final class GeneratedKeyAssignmentTokenGeneratorTest {
- private static final int TEST_STOP_INDEX = 2;
-
- private static final String TEST_COLUMN_NAME = "testColumnName";
-
- private static final int TEST_GENERATED_VALUE = 4;
-
@Test
public void assertGenerateSQLToken() {
GeneratedKeyContext generatedKeyContext =
mock(GeneratedKeyContext.class, RETURNS_DEEP_STUBS);
- when(generatedKeyContext.getColumnName()).thenReturn(TEST_COLUMN_NAME);
+ when(generatedKeyContext.getColumnName()).thenReturn("testColumnName");
Collection<Comparable<?>> testGeneratedValuesCollection = new
LinkedList<>();
- testGeneratedValuesCollection.add(TEST_GENERATED_VALUE);
+ final int testGeneratedValue = 4;
+ testGeneratedValuesCollection.add(testGeneratedValue);
when(generatedKeyContext.getGeneratedValues()).thenReturn(testGeneratedValuesCollection);
InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class);
when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.of(generatedKeyContext));
MySQLInsertStatement insertStatement =
mock(MySQLInsertStatement.class);
when(insertStatementContext.getSqlStatement()).thenReturn(insertStatement);
SetAssignmentSegment setAssignmentSegment =
mock(SetAssignmentSegment.class);
- when(setAssignmentSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStopIndex = 2;
+ when(setAssignmentSegment.getStopIndex()).thenReturn(testStopIndex);
when(insertStatement.getSetAssignment()).thenReturn(Optional.of(setAssignmentSegment));
List<Object> testParameters = new LinkedList<>();
GeneratedKeyAssignmentTokenGenerator
generatedKeyAssignmentTokenGenerator = new
GeneratedKeyAssignmentTokenGenerator();
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyForUseDefaultInsertColumnsTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyForUseDefaultInsertColumnsTokenGeneratorTest.java
index 7462a0e..32b1c39 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyForUseDefaultInsertColumnsTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyForUseDefaultInsertColumnsTokenGeneratorTest.java
@@ -34,21 +34,19 @@ import static org.mockito.Mockito.when;
public final class GeneratedKeyForUseDefaultInsertColumnsTokenGeneratorTest {
- private static final int TEST_STOP_INDEX = 4;
-
- private static final String TEST_COLUMN_NAME = "TEST_COLUMN_NAME";
-
@Test
public void assertGenerateSQLToken() {
InsertColumnsSegment insertColumnsSegment =
mock(InsertColumnsSegment.class);
- when(insertColumnsSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStopIndex = 4;
+ when(insertColumnsSegment.getStopIndex()).thenReturn(testStopIndex);
InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
when(insertStatementContext.getSqlStatement().getInsertColumns()).thenReturn(Optional.of(insertColumnsSegment));
GeneratedKeyContext generatedKeyContext =
mock(GeneratedKeyContext.class);
- when(generatedKeyContext.getColumnName()).thenReturn(TEST_COLUMN_NAME);
+ final String testColumnName = "TEST_COLUMN_NAME";
+ when(generatedKeyContext.getColumnName()).thenReturn(testColumnName);
when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.of(generatedKeyContext));
when(insertStatementContext.getColumnNames()).thenReturn(Collections.emptyList());
GeneratedKeyForUseDefaultInsertColumnsTokenGenerator
generatedKeyForUseDefaultInsertColumnsTokenGenerator = new
GeneratedKeyForUseDefaultInsertColumnsTokenGenerator();
-
assertThat(generatedKeyForUseDefaultInsertColumnsTokenGenerator.generateSQLToken(insertStatementContext).toString(),
is(("(" + TEST_COLUMN_NAME) + ")"));
+
assertThat(generatedKeyForUseDefaultInsertColumnsTokenGenerator.generateSQLToken(insertStatementContext).toString(),
is(("(" + testColumnName) + ")"));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyInsertColumnTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyInsertColumnTokenGeneratorTest.java
index 4c40255..7c961cf 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyInsertColumnTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/GeneratedKeyInsertColumnTokenGeneratorTest.java
@@ -33,20 +33,18 @@ import static org.mockito.Mockito.when;
public final class GeneratedKeyInsertColumnTokenGeneratorTest {
- private static final int TEST_STOP_INDEX = 4;
-
- private static final String TEST_COLUMN_NAME = "TEST_COLUMN_NAME";
-
@Test
public void assertGenerateSQLToken() {
GeneratedKeyContext generatedKeyContext =
mock(GeneratedKeyContext.class);
- when(generatedKeyContext.getColumnName()).thenReturn(TEST_COLUMN_NAME);
+ final String testColumnName = "TEST_COLUMN_NAME";
+ when(generatedKeyContext.getColumnName()).thenReturn(testColumnName);
InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
when(insertStatementContext.getGeneratedKeyContext()).thenReturn(Optional.of(generatedKeyContext));
InsertColumnsSegment insertColumnsSegment =
mock(InsertColumnsSegment.class);
- when(insertColumnsSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStopIndex = 4;
+ when(insertColumnsSegment.getStopIndex()).thenReturn(testStopIndex);
when(insertStatementContext.getSqlStatement().getInsertColumns()).thenReturn(Optional.of(insertColumnsSegment));
GeneratedKeyInsertColumnTokenGenerator
generatedKeyInsertColumnTokenGenerator = new
GeneratedKeyInsertColumnTokenGenerator();
-
assertThat(generatedKeyInsertColumnTokenGenerator.generateSQLToken(insertStatementContext).toString(),
is(", " + TEST_COLUMN_NAME));
+
assertThat(generatedKeyInsertColumnTokenGenerator.generateSQLToken(insertStatementContext).toString(),
is(", " + testColumnName));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java
index 644ddaf..ef79cb9 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java
@@ -29,7 +29,6 @@ import org.junit.Test;
import java.util.Collection;
import java.util.LinkedList;
-import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
@@ -40,10 +39,6 @@ import static org.mockito.Mockito.when;
public final class IndexTokenGeneratorTest {
- private static final int TEST_START_INDEX = 1;
-
- private static final int TEST_STOP_INDEX = 3;
-
@Test
public void assertIsGenerateSQLToken() {
CreateDatabaseStatementContext createDatabaseStatementContext =
mock(CreateDatabaseStatementContext.class);
@@ -60,8 +55,10 @@ public final class IndexTokenGeneratorTest {
@Test
public void assertGenerateSQLTokens() {
IndexSegment indexSegment = mock(IndexSegment.class);
- when(indexSegment.getStartIndex()).thenReturn(TEST_START_INDEX);
- when(indexSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStartIndex = 1;
+ when(indexSegment.getStartIndex()).thenReturn(testStartIndex);
+ final int testStopIndex = 3;
+ when(indexSegment.getStopIndex()).thenReturn(testStopIndex);
IdentifierValue identifierValue = mock(IdentifierValue.class);
when(indexSegment.getIdentifier()).thenReturn(identifierValue);
Collection<IndexSegment> indexSegmentCollection = new LinkedList<>();
@@ -75,6 +72,6 @@ public final class IndexTokenGeneratorTest {
indexTokenGenerator.setSchema(schema);
Collection<IndexToken> result =
indexTokenGenerator.generateSQLTokens(alterIndexStatementContext);
assertThat(result.size(), is(1));
-
assertThat(result.stream().collect(Collectors.toList()).get(0).getStartIndex(),
is(TEST_START_INDEX));
+ assertThat((new LinkedList<>(result)).get(0).getStartIndex(),
is(testStartIndex));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OffsetTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OffsetTokenGeneratorTest.java
index 1c3f7bf..4582c76 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OffsetTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OffsetTokenGeneratorTest.java
@@ -38,12 +38,6 @@ import static org.mockito.Mockito.when;
public final class OffsetTokenGeneratorTest {
- private static final int TEST_START_INDEX = 1;
-
- private static final int TEST_STOP_INDEX = 3;
-
- private static final long TEST_REVISED_OFFSET = 2;
-
@Test
public void assertIsGenerateSQLToken() {
InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class);
@@ -64,14 +58,17 @@ public final class OffsetTokenGeneratorTest {
@Test
public void assertGenerateSQLToken() {
PaginationValueSegment paginationValueSegment =
mock(PaginationValueSegment.class);
-
when(paginationValueSegment.getStartIndex()).thenReturn(TEST_START_INDEX);
-
when(paginationValueSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStartIndex = 1;
+
when(paginationValueSegment.getStartIndex()).thenReturn(testStartIndex);
+ final int testStopIndex = 3;
+ when(paginationValueSegment.getStopIndex()).thenReturn(testStopIndex);
PaginationContext paginationContext = mock(PaginationContext.class);
when(paginationContext.getOffsetSegment()).thenReturn(Optional.of(paginationValueSegment));
-
when(paginationContext.getRevisedOffset()).thenReturn(TEST_REVISED_OFFSET);
+ final long testRevisedOffset = 2;
+
when(paginationContext.getRevisedOffset()).thenReturn(testRevisedOffset);
SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class);
when(selectStatementContext.getPaginationContext()).thenReturn(paginationContext);
OffsetTokenGenerator offsetTokenGenerator = new OffsetTokenGenerator();
-
assertThat(offsetTokenGenerator.generateSQLToken(selectStatementContext).toString(),
is(String.valueOf(TEST_REVISED_OFFSET)));
+
assertThat(offsetTokenGenerator.generateSQLToken(selectStatementContext).toString(),
is(String.valueOf(testRevisedOffset)));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OrderByTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OrderByTokenGeneratorTest.java
index 90544da..7e852fa 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OrderByTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OrderByTokenGeneratorTest.java
@@ -43,9 +43,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class OrderByTokenGeneratorTest {
-
- private static final int TEST_STOP_INDEX = 2;
-
+
private static final String TEST_COLUMN_ORDER_BY_ITEM_SEGMENT_COLUMN_LABEL
= "TEST_COLUMN_ORDER_BY_ITEM_SEGMENT_COLUMN_LABEL";
private static final String
TEST_EXPRESSION_ORDER_BY_ITEM_SEGMENT_COLUMN_LABEL =
"TEST_EXPRESSION_ORDER_BY_ITEM_SEGMENT_COLUMN_LABEL";
@@ -69,7 +67,8 @@ public final class OrderByTokenGeneratorTest {
@Test
public void assertGenerateSQLToken() {
WindowSegment windowSegment = mock(WindowSegment.class);
- when(windowSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStopIndex = 2;
+ when(windowSegment.getStopIndex()).thenReturn(testStopIndex);
MySQLSelectStatement mySQLSelectStatement =
mock(MySQLSelectStatement.class);
when(mySQLSelectStatement.getWindow()).thenReturn(Optional.of(windowSegment));
SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ProjectionsTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ProjectionsTokenGeneratorTest.java
index 086f8ea..35bcf04 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ProjectionsTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ProjectionsTokenGeneratorTest.java
@@ -59,27 +59,24 @@ public final class ProjectionsTokenGeneratorTest {
private static final String TEST_DERIVED_PROJECTION_ALIAS =
"TEST_DERIVED_PROJECTION_ALIAS";
- private static final int TEST_STOP_INDEX = 2;
-
private static final String TEST_LOGIC_TABLE_NAME =
"TEST_LOGIC_TABLE_NAME";
- private static final String TEST_ACTUAL_TABLE_NAME =
"TEST_ACTUAL_TABLE_NAME";
-
private static final String TEST_ACTUAL_TABLE_NAME_WRAPPED =
"TEST_ACTUAL_TABLE_NAME_WRAPPED";
private static final String TEST_OTHER_DERIVED_PROJECTION_ALIAS =
"TEST_OTHER_DERIVED_PROJECTION_ALIAS";
private static final String TEST_OTHER_DERIVED_PROJECTION_EXPRESSION =
"TEST_OTHER_DERIVED_PROJECTION_EXPRESSION";
- private RouteUnit routeUnit = mock(RouteUnit.class);
+ private RouteUnit routeUnit;
@Before
public void setup() {
RouteMapper routeMapper = mock(RouteMapper.class);
when(routeMapper.getLogicName()).thenReturn(TEST_LOGIC_TABLE_NAME);
- when(routeMapper.getActualName()).thenReturn(TEST_ACTUAL_TABLE_NAME);
+ when(routeMapper.getActualName()).thenReturn("TEST_ACTUAL_TABLE_NAME");
Collection<RouteMapper> routeMapperCollection = new LinkedList<>();
routeMapperCollection.add(routeMapper);
+ routeUnit = mock(RouteUnit.class);
when(routeUnit.getTableMappers()).thenReturn(routeMapperCollection);
}
@@ -109,7 +106,8 @@ public final class ProjectionsTokenGeneratorTest {
projectionCollection.add(otherDerivedProjection);
SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
when(selectStatementContext.getProjectionsContext().getProjections()).thenReturn(projectionCollection);
-
when(selectStatementContext.getProjectionsContext().getStopIndex()).thenReturn(TEST_STOP_INDEX);
+ final int testStopIndex = 2;
+
when(selectStatementContext.getProjectionsContext().getStopIndex()).thenReturn(testStopIndex);
when(selectStatementContext.getSqlStatement()).thenReturn(new
MySQLSelectStatement());
ProjectionsTokenGenerator projectionsTokenGenerator =
getProjectionsTokenGenerator();
ProjectionsToken projectionsToken =
projectionsTokenGenerator.generateSQLToken(selectStatementContext);
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OffsetTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/RowCountTokenGeneratorTest.java
similarity index 51%
copy from
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OffsetTokenGeneratorTest.java
copy to
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/RowCountTokenGeneratorTest.java
index 1c3f7bf..fe42c97 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/OffsetTokenGeneratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/RowCountTokenGeneratorTest.java
@@ -20,10 +20,10 @@ package org.apache.shardingsphere.sharding.rewrite.token;
import
org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.OffsetTokenGenerator;
+import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.RowCountTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.pojo.RowCountToken;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.NumberLiteralPaginationValueSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.PaginationValueSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.ParameterMarkerPaginationValueSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
import org.junit.Test;
import java.util.Optional;
@@ -36,42 +36,37 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class OffsetTokenGeneratorTest {
-
- private static final int TEST_START_INDEX = 1;
-
- private static final int TEST_STOP_INDEX = 3;
-
- private static final long TEST_REVISED_OFFSET = 2;
+public final class RowCountTokenGeneratorTest {
@Test
public void assertIsGenerateSQLToken() {
InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class);
- OffsetTokenGenerator offsetTokenGenerator = new OffsetTokenGenerator();
-
assertFalse(offsetTokenGenerator.isGenerateSQLToken(insertStatementContext));
+ RowCountTokenGenerator rowCountTokenGenerator = new
RowCountTokenGenerator();
+
assertFalse(rowCountTokenGenerator.isGenerateSQLToken(insertStatementContext));
SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
-
when(selectStatementContext.getPaginationContext().getOffsetSegment().isPresent()).thenReturn(Boolean.FALSE);
-
assertFalse(offsetTokenGenerator.isGenerateSQLToken(selectStatementContext));
-
when(selectStatementContext.getPaginationContext().getOffsetSegment().isPresent()).thenReturn(Boolean.TRUE);
- ParameterMarkerPaginationValueSegment
parameterMarkerPaginationValueSegment =
mock(ParameterMarkerPaginationValueSegment.class);
-
when(selectStatementContext.getPaginationContext().getOffsetSegment().get()).thenReturn(parameterMarkerPaginationValueSegment);
-
assertFalse(offsetTokenGenerator.isGenerateSQLToken(selectStatementContext));
+
when(selectStatementContext.getPaginationContext().getRowCountSegment().isPresent()).thenReturn(Boolean.FALSE);
NumberLiteralPaginationValueSegment
numberLiteralPaginationValueSegment =
mock(NumberLiteralPaginationValueSegment.class);
-
when(selectStatementContext.getPaginationContext().getOffsetSegment().get()).thenReturn(numberLiteralPaginationValueSegment);
-
assertTrue(offsetTokenGenerator.isGenerateSQLToken(selectStatementContext));
+
assertFalse(rowCountTokenGenerator.isGenerateSQLToken(selectStatementContext));
+
when(selectStatementContext.getPaginationContext().getRowCountSegment()).thenReturn(Optional.of(numberLiteralPaginationValueSegment));
+
assertTrue(rowCountTokenGenerator.isGenerateSQLToken(selectStatementContext));
}
@Test
public void assertGenerateSQLToken() {
- PaginationValueSegment paginationValueSegment =
mock(PaginationValueSegment.class);
-
when(paginationValueSegment.getStartIndex()).thenReturn(TEST_START_INDEX);
-
when(paginationValueSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX);
- PaginationContext paginationContext = mock(PaginationContext.class);
-
when(paginationContext.getOffsetSegment()).thenReturn(Optional.of(paginationValueSegment));
-
when(paginationContext.getRevisedOffset()).thenReturn(TEST_REVISED_OFFSET);
- SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class);
+ final int testOffsetSegmentValue = 12;
+ NumberLiteralLimitValueSegment offsetSegment = new
NumberLiteralLimitValueSegment(1, 2, testOffsetSegmentValue);
+ final int testRowCountSegmentValue = 8;
+ NumberLiteralLimitValueSegment rowCountSegment = new
NumberLiteralLimitValueSegment(4, 5, testRowCountSegmentValue);
+ PaginationContext paginationContext = new
PaginationContext(offsetSegment, rowCountSegment, null);
+ SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
when(selectStatementContext.getPaginationContext()).thenReturn(paginationContext);
- OffsetTokenGenerator offsetTokenGenerator = new OffsetTokenGenerator();
-
assertThat(offsetTokenGenerator.generateSQLToken(selectStatementContext).toString(),
is(String.valueOf(TEST_REVISED_OFFSET)));
+
when(selectStatementContext.getGroupByContext().getItems().isEmpty()).thenReturn(Boolean.FALSE);
+
when(selectStatementContext.isSameGroupByAndOrderByItems()).thenReturn(Boolean.FALSE);
+ RowCountTokenGenerator rowCountTokenGenerator = new
RowCountTokenGenerator();
+ RowCountToken rowCountToken =
rowCountTokenGenerator.generateSQLToken(selectStatementContext);
+ assertThat(rowCountToken.toString(),
is(String.valueOf(Integer.MAX_VALUE)));
+
when(selectStatementContext.isSameGroupByAndOrderByItems()).thenReturn(Boolean.TRUE);
+ rowCountToken =
rowCountTokenGenerator.generateSQLToken(selectStatementContext);
+ assertThat(rowCountToken.toString(),
is(String.valueOf(testOffsetSegmentValue + testRowCountSegmentValue)));
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingInsertValuesTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingInsertValuesTokenGeneratorTest.java
new file mode 100644
index 0000000..6bde5d1
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ShardingInsertValuesTokenGeneratorTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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.sharding.rewrite.token;
+
+import
org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
+import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import
org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.InsertValuesToken;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ShardingInsertValuesTokenGenerator;
+import
org.apache.shardingsphere.sharding.rewrite.token.pojo.ShardingInsertValue;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ShardingInsertValuesTokenGeneratorTest {
+
+ @Test
+ public void assertIsGenerateSQLToken() {
+ SelectStatementContext selectStatementContext =
mock(SelectStatementContext.class);
+ ShardingInsertValuesTokenGenerator shardingInsertValuesTokenGenerator
= new ShardingInsertValuesTokenGenerator();
+
assertFalse(shardingInsertValuesTokenGenerator.isGenerateSQLToken(selectStatementContext));
+ InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
+
when(insertStatementContext.getSqlStatement().getValues().isEmpty()).thenReturn(Boolean.TRUE);
+
assertFalse(shardingInsertValuesTokenGenerator.isGenerateSQLToken(insertStatementContext));
+
when(insertStatementContext.getSqlStatement().getValues().isEmpty()).thenReturn(Boolean.FALSE);
+
assertTrue(shardingInsertValuesTokenGenerator.isGenerateSQLToken(insertStatementContext));
+ }
+
+ @Test
+ public void assertGenerateSQLToken() {
+ List<ExpressionSegment> expressionSegmentList = new LinkedList<>();
+ InsertValuesSegment insertValuesSegment = new InsertValuesSegment(1,
2, expressionSegmentList);
+ Collection<InsertValuesSegment> insertValuesSegmentCollection = new
LinkedList<>();
+ insertValuesSegmentCollection.add(insertValuesSegment);
+ InsertValueContext insertValueContext = new
InsertValueContext(expressionSegmentList, null, 4);
+ List<InsertValueContext> insertValueContextList = new LinkedList<>();
+ insertValueContextList.add(insertValueContext);
+ InsertStatementContext insertStatementContext =
mock(InsertStatementContext.class, RETURNS_DEEP_STUBS);
+
when(insertStatementContext.getInsertValueContexts()).thenReturn(insertValueContextList);
+
when(insertStatementContext.getSqlStatement().getValues()).thenReturn(insertValuesSegmentCollection);
+ ShardingInsertValuesTokenGenerator shardingInsertValuesTokenGenerator
= new ShardingInsertValuesTokenGenerator();
+ InsertValuesToken insertValuesToken =
shardingInsertValuesTokenGenerator.generateSQLToken(insertStatementContext);
+ assertThat(insertValuesToken.getInsertValues().size(), is(1));
+ Collection<DataNode> dataNodes = new LinkedList<>();
+ final String testDatasource = "testDatasource";
+ final String testTable = "testTable";
+ dataNodes.add(new DataNode(testDatasource, testTable));
+ RouteContext routeContext = new RouteContext();
+ routeContext.getOriginalDataNodes().add(dataNodes);
+ shardingInsertValuesTokenGenerator.setRouteContext(routeContext);
+ insertValuesToken =
shardingInsertValuesTokenGenerator.generateSQLToken(insertStatementContext);
+ assertThat(insertValuesToken.getInsertValues().get(0),
instanceOf(ShardingInsertValue.class));
+ ShardingInsertValue generatedShardingInsertValue =
(ShardingInsertValue) insertValuesToken.getInsertValues().get(0);
+ assertThat((new
LinkedList<>(generatedShardingInsertValue.getDataNodes())).get(0).getDataSourceName(),
is(testDatasource));
+ assertThat((new
LinkedList<>(generatedShardingInsertValue.getDataNodes())).get(0).getTableName(),
is(testTable));
+ }
+}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/TableTokenGeneratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/TableTokenGeneratorTest.java
new file mode 100644
index 0000000..eb66110
--- /dev/null
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/TableTokenGeneratorTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.sharding.rewrite.token;
+
+import
org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.ddl.CreateTableStatementContext;
+import
org.apache.shardingsphere.sharding.rewrite.token.generator.impl.TableTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.pojo.TableToken;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.TableRule;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class TableTokenGeneratorTest {
+
+ @Test
+ public void assertGenerateSQLToken() {
+ ShardingRule shardingRule = mock(ShardingRule.class);
+
when(shardingRule.findTableRule(anyString())).thenReturn(Optional.of(mock(TableRule.class)));
+ TableTokenGenerator tableTokenGenerator = new TableTokenGenerator();
+ tableTokenGenerator.setShardingRule(shardingRule);
+ CreateDatabaseStatementContext createDatabaseStatementContext =
mock(CreateDatabaseStatementContext.class);
+
assertThat(tableTokenGenerator.generateSQLTokens(createDatabaseStatementContext),
is(Collections.emptyList()));
+ final int testStartIndex = 3;
+ TableNameSegment tableNameSegment = new
TableNameSegment(testStartIndex, 8, new IdentifierValue("test"));
+ SimpleTableSegment simpleTableSegment = new
SimpleTableSegment(tableNameSegment);
+ Collection<SimpleTableSegment> simpleTableSegmentCollection = new
LinkedList<>();
+ simpleTableSegmentCollection.add(simpleTableSegment);
+ CreateTableStatementContext createTableStatementContext =
mock(CreateTableStatementContext.class);
+
when(createTableStatementContext.getAllTables()).thenReturn(simpleTableSegmentCollection);
+ Collection<TableToken> result =
tableTokenGenerator.generateSQLTokens(createTableStatementContext);
+ assertThat((new LinkedList<>(result)).get(0).getStartIndex(),
is(testStartIndex));
+ }
+}