You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/01/21 02:19:59 UTC

[shardingsphere] branch master updated: Add more unit test for ShardingTokenGenerateBuilder (#14931)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 d91ed60  Add more unit test for ShardingTokenGenerateBuilder (#14931)
d91ed60 is described below

commit d91ed60ebbd60a00b7bf7a5a42f52ab7288785d2
Author: liguoping <xd...@163.com>
AuthorDate: Fri Jan 21 10:18:53 2022 +0800

    Add more unit test for ShardingTokenGenerateBuilder (#14931)
    
    * Add more unit test for ShardingTokenGenerateBuilder
    
    * Add more unit test for ShardingTokenGenerateBuilder
    
    * assert each sqlTokenGenerator
    
    * remove ()
---
 .../pojo/ShardingTokenGenerateBuilderTest.java     | 95 ++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenGenerateBuilderTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenGenerateBuilderTest.java
new file mode 100644
index 0000000..e449490
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenGenerateBuilderTest.java
@@ -0,0 +1,95 @@
+/*
+ * 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.pojo;
+
+import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.rewrite.sql.token.generator.SQLTokenGenerator;
+import org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware.RouteContextAware;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.AggregationDistinctTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.DistinctProjectionPrefixTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.ShardingRemoveTokenGenerator;
+import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.TableTokenGenerator;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.aware.ShardingRuleAware;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.Iterator;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class ShardingTokenGenerateBuilderTest {
+    
+    private ShardingRule shardingRule;
+    
+    private RouteContext routeContext;
+    
+    @Before
+    public void setup() {
+        shardingRule = mock(ShardingRule.class);
+        routeContext = mock(RouteContext.class);
+    }
+    
+    @Test
+    public void assertGetSQLTokenGenerators() throws Exception {
+        when(routeContext.containsTableSharding()).thenReturn(true);
+        SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+        when(sqlStatementContext.getProjectionsContext().getAggregationProjections().isEmpty()).thenReturn(false);
+        ShardingTokenGenerateBuilder shardingTokenGenerateBuilder = new ShardingTokenGenerateBuilder(shardingRule, routeContext, sqlStatementContext);
+        Collection<SQLTokenGenerator> sqlTokenGenerators = shardingTokenGenerateBuilder.getSQLTokenGenerators();
+        assertThat(sqlTokenGenerators.size(), is(4));
+        Iterator<SQLTokenGenerator> iterator = sqlTokenGenerators.iterator();
+        SQLTokenGenerator tableTokenGenerator = iterator.next();
+        assertThat(tableTokenGenerator, instanceOf(TableTokenGenerator.class));
+        assertSqlTokenGenerator(tableTokenGenerator);
+        SQLTokenGenerator distinctProjectionPrefixTokenGenerator = iterator.next();
+        assertThat(distinctProjectionPrefixTokenGenerator, instanceOf(DistinctProjectionPrefixTokenGenerator.class));
+        assertSqlTokenGenerator(distinctProjectionPrefixTokenGenerator);
+        SQLTokenGenerator aggregationDistinctTokenGenerator = iterator.next();
+        assertThat(aggregationDistinctTokenGenerator, instanceOf(AggregationDistinctTokenGenerator.class));
+        assertSqlTokenGenerator(aggregationDistinctTokenGenerator);
+        SQLTokenGenerator shardingRemoveTokenGenerator = iterator.next();
+        assertThat(shardingRemoveTokenGenerator, instanceOf(ShardingRemoveTokenGenerator.class));
+        assertSqlTokenGenerator(shardingRemoveTokenGenerator);
+    }
+    
+    private void assertSqlTokenGenerator(final SQLTokenGenerator sqlTokenGenerator) throws Exception {
+        if (sqlTokenGenerator instanceof ShardingRuleAware) {
+            assertField(sqlTokenGenerator, shardingRule, "shardingRule");
+        }
+        if (sqlTokenGenerator instanceof RouteContextAware) {
+            assertField(sqlTokenGenerator, routeContext, "routeContext");
+        }
+    }
+    
+    private void assertField(final SQLTokenGenerator sqlTokenGenerator, final Object filedInstance, final String fieldName) throws Exception {
+        Field field = sqlTokenGenerator.getClass().getDeclaredField(fieldName);
+        field.setAccessible(true);
+        assertNotNull(field.get(sqlTokenGenerator));
+        assertThat(field.get(sqlTokenGenerator), is(filedInstance));
+    }
+}