diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java index 5139667dd355b..0f55212af5c7f 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java @@ -17,11 +17,14 @@ package org.apache.shardingsphere.infra.binder.context.statement.dml; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection; +import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ExpressionProjection; +import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.SubqueryProjection; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter; -import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; @@ -64,6 +67,7 @@ import java.util.Arrays; import java.util.Collections; +import java.util.Optional; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -409,6 +413,55 @@ private void assertSetWhere(final SelectStatement selectStatement) { assertThat(actual.getWhereSegments(), is(Collections.singletonList(whereSegment))); } + @Test + void assertFindColumnProjectionWithoutExpandProjections() { + SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + + when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.emptyList()); + + Optional result = selectStatementContext.findColumnProjection(1); + + assertFalse(result.isPresent()); + } + + @Test + void assertFindColumnProjectionInvalidProjectionType() { + ExpressionProjection projection = mock(ExpressionProjection.class); + SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + + when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection)); + + Optional result = selectStatementContext.findColumnProjection(1); + + assertFalse(result.isPresent()); + } + + @Test + void assertFindColumnProjectionFromColumnProjection() { + ColumnProjection projection = mock(ColumnProjection.class); + SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + + when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection)); + + Optional result = selectStatementContext.findColumnProjection(1); + + assertTrue(result.isPresent()); + } + + @Test + void assertFindColumnProjectionFromSubqueryProjection() { + SubqueryProjection projection = mock(SubqueryProjection.class, RETURNS_DEEP_STUBS); + ColumnProjection columnProjection = mock(ColumnProjection.class); + SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); + + when(projection.getProjection()).thenReturn(columnProjection); + when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection)); + + Optional result = selectStatementContext.findColumnProjection(1); + + assertTrue(result.isPresent()); + } + @Test void assertContainsSubqueryForMySQL() { assertContainsSubquery(new MySQLSelectStatement(), new MySQLSelectStatement()); diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/SelectStatementBinderTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/SelectStatementBinderTest.java index a9603932b8938..4e2cc0242aae3 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/SelectStatementBinderTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/SelectStatementBinderTest.java @@ -17,11 +17,6 @@ package org.apache.shardingsphere.infra.binder.statement; -import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection; -import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection; -import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ExpressionProjection; -import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.SubqueryProjection; -import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; @@ -45,85 +40,19 @@ import java.sql.Types; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.Optional; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; class SelectStatementBinderTest { - @Test - void assertFindColumnProjectionWithoutExpandProjections() { - SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); - - when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.emptyList()); - - Optional result = selectStatementContext.findColumnProjection(1); - - assertFalse(result.isPresent()); - } - - @Test - void assertFindColumnProjectionInvalidColumnIndex() { - Projection projection = mock(Projection.class); - SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); - - when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection)); - - Optional result = selectStatementContext.findColumnProjection(2); - - assertFalse(result.isPresent()); - } - - @Test - void assertFindColumnProjectionInvalidProjectionType() { - ExpressionProjection projection = mock(ExpressionProjection.class); - SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); - - when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection)); - - Optional result = selectStatementContext.findColumnProjection(1); - - assertFalse(result.isPresent()); - } - - @Test - void assertFindColumnProjectionFromColumnProjection() { - ColumnProjection projection = mock(ColumnProjection.class); - SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); - - when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection)); - - Optional result = selectStatementContext.findColumnProjection(1); - - assertTrue(result.isPresent()); - assertThat(result.get(), is(projection)); - } - - @Test - void assertFindColumnProjectionFromSubqueryProjection() { - SubqueryProjection projection = mock(SubqueryProjection.class, RETURNS_DEEP_STUBS); - ColumnProjection columnProjection = mock(ColumnProjection.class); - SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS); - - when(projection.getProjection()).thenReturn(columnProjection); - when(selectStatementContext.getProjectionsContext().getExpandProjections()).thenReturn(Collections.singletonList(projection)); - - Optional result = selectStatementContext.findColumnProjection(1); - - assertTrue(result.isPresent()); - assertThat(result.get(), is(columnProjection)); - } - @Test void assertBind() { SelectStatement selectStatement = new MySQLSelectStatement();