soulasuna commented on a change in pull request #12159: URL: https://github.com/apache/shardingsphere/pull/12159#discussion_r700808368
########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/DistinctProjectionPrefixTokenGeneratorTest.java ########## @@ -0,0 +1,63 @@ +/* + * 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.select.projection.impl.AggregationDistinctProjection; +import org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext; +import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; +import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.DistinctProjectionPrefixTokenGenerator; +import org.apache.shardingsphere.sharding.rewrite.token.pojo.DistinctProjectionPrefixToken; +import org.junit.Test; + +import java.util.LinkedList; +import java.util.List; + +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 DistinctProjectionPrefixTokenGeneratorTest { + + private static final int TEST_START_INDEX = 1; + + @Test + public void assertIsGenerateSQLToken() { + CreateDatabaseStatementContext createDatabaseStatementContext = mock(CreateDatabaseStatementContext.class); + DistinctProjectionPrefixTokenGenerator distinctProjectionPrefixTokenGenerator = new DistinctProjectionPrefixTokenGenerator(); + assertFalse(distinctProjectionPrefixTokenGenerator.isGenerateSQLToken(createDatabaseStatementContext)); + SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + List<AggregationDistinctProjection> aggregationDistinctProjectionList = new LinkedList<>(); + when(selectStatementContext.getProjectionsContext().getAggregationDistinctProjections()).thenReturn(aggregationDistinctProjectionList); + assertFalse(distinctProjectionPrefixTokenGenerator.isGenerateSQLToken(selectStatementContext)); + aggregationDistinctProjectionList.add(mock(AggregationDistinctProjection.class)); + assertTrue(distinctProjectionPrefixTokenGenerator.isGenerateSQLToken(selectStatementContext)); + } + + @Test + public void assertGenerateSQLToken() { + SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + when(selectStatementContext.getProjectionsContext().getStartIndex()).thenReturn(TEST_START_INDEX); Review comment: If `TEST_START_INDEX` are used in only one method, please use local variable substitution. ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ConstraintTokenGeneratorTest.java ########## @@ -0,0 +1,77 @@ +/* + * 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.AlterTableStatementContext; +import org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext; +import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ConstraintTokenGenerator; +import org.apache.shardingsphere.sharding.rewrite.token.pojo.ConstraintToken; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment; +import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; +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; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +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); + ConstraintTokenGenerator constraintTokenGenerator = new ConstraintTokenGenerator(); + assertFalse(constraintTokenGenerator.isGenerateSQLToken(createDatabaseStatementContext)); + AlterTableStatementContext alterTableStatementContext = mock(AlterTableStatementContext.class); + Collection<ConstraintSegment> constraintSegmentCollection = new LinkedList<>(); + when(alterTableStatementContext.getConstraints()).thenReturn(constraintSegmentCollection); + assertFalse(constraintTokenGenerator.isGenerateSQLToken(alterTableStatementContext)); + constraintSegmentCollection.add(mock(ConstraintSegment.class)); + assertTrue(constraintTokenGenerator.isGenerateSQLToken(alterTableStatementContext)); + } + + @Test + public void assertGenerateSQLTokens() { + ConstraintSegment constraintSegment = mock(ConstraintSegment.class); + when(constraintSegment.getStartIndex()).thenReturn(TEST_START_INDEX); + when(constraintSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX); + IdentifierValue constraintIdentifier = mock(IdentifierValue.class); + when(constraintSegment.getIdentifier()).thenReturn(constraintIdentifier); + Collection<ConstraintSegment> constraintSegmentCollection = new LinkedList<>(); + constraintSegmentCollection.add(constraintSegment); + AlterTableStatementContext alterTableStatementContext = mock(AlterTableStatementContext.class); + when(alterTableStatementContext.getConstraints()).thenReturn(constraintSegmentCollection); + ShardingRule shardingRule = mock(ShardingRule.class); + ConstraintTokenGenerator constraintTokenGenerator = new ConstraintTokenGenerator(); + 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)); Review comment: `result.stream().collect(Collectors.toList())` can be replaced with `new ArrayList()` ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ProjectionsTokenGeneratorTest.java ########## @@ -0,0 +1,165 @@ +/* + * 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.select.projection.Projection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationDistinctProjection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.DerivedProjection; +import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext; +import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.route.context.RouteMapper; +import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ProjectionsTokenGenerator; +import org.apache.shardingsphere.sharding.rewrite.token.pojo.ProjectionsToken; +import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection; +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment; +import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; +import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; + +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.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class ProjectionsTokenGeneratorTest { + + private static final String TEST_AGGREGATION_DISTINCT_PROJECTION_DISTINCT_INNER_EXPRESSION = "TEST_AGGREGATION_DISTINCT_PROJECTION_DISTINCT_INNER_EXPRESSION"; + + private static final String TEST_AGGREGATION_DISTINCT_PROJECTION_ALIAS = "TEST_AGGREGATION_DISTINCT_PROJECTION_ALIAS"; + + 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); + + @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); Review comment: If `TEST_ACTUAL_TABLE_NAME ` is used in only one method, please use local variable substitution. ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ProjectionsTokenGeneratorTest.java ########## @@ -0,0 +1,165 @@ +/* + * 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.select.projection.Projection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationDistinctProjection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.DerivedProjection; +import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext; +import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.route.context.RouteMapper; +import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ProjectionsTokenGenerator; +import org.apache.shardingsphere.sharding.rewrite.token.pojo.ProjectionsToken; +import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection; +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment; +import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; +import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; + +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.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class ProjectionsTokenGeneratorTest { + + private static final String TEST_AGGREGATION_DISTINCT_PROJECTION_DISTINCT_INNER_EXPRESSION = "TEST_AGGREGATION_DISTINCT_PROJECTION_DISTINCT_INNER_EXPRESSION"; + + private static final String TEST_AGGREGATION_DISTINCT_PROJECTION_ALIAS = "TEST_AGGREGATION_DISTINCT_PROJECTION_ALIAS"; + + 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); Review comment: The initialization of the `RouteUnit `object can be placed in the setup method. ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java ########## @@ -0,0 +1,80 @@ +/* + * 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.AlterIndexStatementContext; +import org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext; +import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; +import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.IndexTokenGenerator; +import org.apache.shardingsphere.sharding.rewrite.token.pojo.IndexToken; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment; +import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; +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; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +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); + IndexTokenGenerator indexTokenGenerator = new IndexTokenGenerator(); + assertFalse(indexTokenGenerator.isGenerateSQLToken(createDatabaseStatementContext)); + AlterIndexStatementContext alterIndexStatementContext = mock(AlterIndexStatementContext.class); + Collection<IndexSegment> indexSegmentCollection = new LinkedList<>(); + when(alterIndexStatementContext.getIndexes()).thenReturn(indexSegmentCollection); + assertFalse(indexTokenGenerator.isGenerateSQLToken(alterIndexStatementContext)); + indexSegmentCollection.add(mock(IndexSegment.class)); + assertTrue(indexTokenGenerator.isGenerateSQLToken(alterIndexStatementContext)); + } + + @Test + public void assertGenerateSQLTokens() { + IndexSegment indexSegment = mock(IndexSegment.class); + when(indexSegment.getStartIndex()).thenReturn(TEST_START_INDEX); + when(indexSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX); + IdentifierValue identifierValue = mock(IdentifierValue.class); + when(indexSegment.getIdentifier()).thenReturn(identifierValue); + Collection<IndexSegment> indexSegmentCollection = new LinkedList<>(); + indexSegmentCollection.add(indexSegment); + AlterIndexStatementContext alterIndexStatementContext = mock(AlterIndexStatementContext.class); + when(alterIndexStatementContext.getIndexes()).thenReturn(indexSegmentCollection); + IndexTokenGenerator indexTokenGenerator = new IndexTokenGenerator(); + ShardingRule shardingRule = mock(ShardingRule.class); + indexTokenGenerator.setShardingRule(shardingRule); + ShardingSphereSchema schema = mock(ShardingSphereSchema.class); + 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)); Review comment: `result.stream().collect(Collectors.toList())` can be replaced with `new ArrayList()` ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java ########## @@ -0,0 +1,80 @@ +/* + * 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.AlterIndexStatementContext; +import org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext; +import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; +import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.IndexTokenGenerator; +import org.apache.shardingsphere.sharding.rewrite.token.pojo.IndexToken; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment; +import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; +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; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +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); + IndexTokenGenerator indexTokenGenerator = new IndexTokenGenerator(); + assertFalse(indexTokenGenerator.isGenerateSQLToken(createDatabaseStatementContext)); + AlterIndexStatementContext alterIndexStatementContext = mock(AlterIndexStatementContext.class); + Collection<IndexSegment> indexSegmentCollection = new LinkedList<>(); + when(alterIndexStatementContext.getIndexes()).thenReturn(indexSegmentCollection); + assertFalse(indexTokenGenerator.isGenerateSQLToken(alterIndexStatementContext)); + indexSegmentCollection.add(mock(IndexSegment.class)); + assertTrue(indexTokenGenerator.isGenerateSQLToken(alterIndexStatementContext)); + } + + @Test + public void assertGenerateSQLTokens() { + IndexSegment indexSegment = mock(IndexSegment.class); + when(indexSegment.getStartIndex()).thenReturn(TEST_START_INDEX); + when(indexSegment.getStopIndex()).thenReturn(TEST_STOP_INDEX); Review comment: If `TEST_START_INDEX` and `TEST_STOP_INDEX` are used in only one method, please use local variable substitution. ########## File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/ProjectionsTokenGeneratorTest.java ########## @@ -0,0 +1,165 @@ +/* + * 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.select.projection.Projection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationDistinctProjection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection; +import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.DerivedProjection; +import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext; +import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext; +import org.apache.shardingsphere.infra.route.context.RouteContext; +import org.apache.shardingsphere.infra.route.context.RouteMapper; +import org.apache.shardingsphere.infra.route.context.RouteUnit; +import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ProjectionsTokenGenerator; +import org.apache.shardingsphere.sharding.rewrite.token.pojo.ProjectionsToken; +import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection; +import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment; +import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment; +import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; +import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement; +import org.junit.Before; +import org.junit.Test; + +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; + +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.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class ProjectionsTokenGeneratorTest { + + private static final String TEST_AGGREGATION_DISTINCT_PROJECTION_DISTINCT_INNER_EXPRESSION = "TEST_AGGREGATION_DISTINCT_PROJECTION_DISTINCT_INNER_EXPRESSION"; + + private static final String TEST_AGGREGATION_DISTINCT_PROJECTION_ALIAS = "TEST_AGGREGATION_DISTINCT_PROJECTION_ALIAS"; + + 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); + + @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); + Collection<RouteMapper> routeMapperCollection = new LinkedList<>(); + routeMapperCollection.add(routeMapper); + when(routeUnit.getTableMappers()).thenReturn(routeMapperCollection); + } + + @Test + public void assertIsGenerateSQLToken() { + ProjectionsTokenGenerator projectionsTokenGenerator = getProjectionsTokenGenerator(); + InsertStatementContext insertStatementContext = mock(InsertStatementContext.class); + assertFalse(projectionsTokenGenerator.isGenerateSQLToken(insertStatementContext)); + SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + when(selectStatementContext.getProjectionsContext().getProjections()).thenReturn(Collections.emptyList()); + assertFalse(projectionsTokenGenerator.isGenerateSQLToken(selectStatementContext)); + AggregationProjection aggregationProjection = getAggregationProjection(); + Collection<Projection> projectionCollection = new LinkedList<>(); + projectionCollection.add(aggregationProjection); + when(selectStatementContext.getProjectionsContext().getProjections()).thenReturn(projectionCollection); + assertTrue(projectionsTokenGenerator.isGenerateSQLToken(selectStatementContext)); + } + + @Test + public void assertGenerateSQLToken() { + Collection<Projection> projectionCollection = new LinkedList<>(); + AggregationProjection aggregationProjection = getAggregationProjection(); + projectionCollection.add(aggregationProjection); + DerivedProjection derivedProjection = getDerivedProjection(); + projectionCollection.add(derivedProjection); + DerivedProjection otherDerivedProjection = getOtherDerivedProjection(); + 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); Review comment: If `TEST_STOP_INDEX ` is used in only one method, please use local variable substitution. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
