terrymanu commented on a change in pull request #15001:
URL: https://github.com/apache/shardingsphere/pull/15001#discussion_r790165791



##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.encrypt.rewrite.token;
+
+import 
org.apache.shardingsphere.encrypt.rewrite.aware.QueryWithCipherColumnAware;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptOrderByItemTokenGenerator;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptPredicateColumnTokenGenerator;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptProjectionTokenGenerator;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.encrypt.rule.aware.EncryptRuleAware;
+import 
org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
+import 
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import 
org.apache.shardingsphere.infra.rewrite.sql.token.generator.SQLTokenGenerator;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.Collections;
+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.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 EncryptTokenGenerateBuilderTest {
+    
+    private EncryptRule encryptRule;
+    
+    @Before
+    public void setup() {
+        encryptRule = mock(EncryptRule.class, RETURNS_DEEP_STUBS);
+        
when(encryptRule.findEncryptTable(anyString()).isPresent()).thenReturn(true);
+    }
+    
+    @Test
+    public void test1() throws Exception {
+        SelectStatementContext selectStatementContext = 
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+        
when(selectStatementContext.getAllTables().isEmpty()).thenReturn(false);
+        
when(selectStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singletonList("table"));
+        
when(selectStatementContext.getOrderByContext().getItems()).thenReturn(Collections.singletonList(mock(OrderByItem.class)));
+        
when(selectStatementContext.getGroupByContext().getItems()).thenReturn(Collections.emptyList());
+        when(selectStatementContext.isContainsJoinQuery()).thenReturn(true);
+        EncryptTokenGenerateBuilder encryptTokenGenerateBuilder = new 
EncryptTokenGenerateBuilder(encryptRule, selectStatementContext);
+        Collection<SQLTokenGenerator> sqlTokenGenerators = 
encryptTokenGenerateBuilder.getSQLTokenGenerators();
+        assertThat(sqlTokenGenerators.size(), is(3));
+        Iterator<SQLTokenGenerator> iterator = sqlTokenGenerators.iterator();
+        SQLTokenGenerator item1 = iterator.next();
+        assertThat(item1, instanceOf(EncryptProjectionTokenGenerator.class));
+        assertSqlTokenGenerator(item1);
+        SQLTokenGenerator item2 = iterator.next();
+        assertThat(item2, 
instanceOf(EncryptPredicateColumnTokenGenerator.class));
+        assertSqlTokenGenerator(item2);
+        SQLTokenGenerator item3 = iterator.next();
+        assertThat(item3, instanceOf(EncryptOrderByItemTokenGenerator.class));
+        assertSqlTokenGenerator(item3);
+    }
+    
+    private void assertSqlTokenGenerator(final SQLTokenGenerator 
sqlTokenGenerator) throws Exception {
+        if (sqlTokenGenerator instanceof EncryptRuleAware) {
+            assertField(sqlTokenGenerator, encryptRule, "encryptRule");
+        }
+        if (sqlTokenGenerator instanceof QueryWithCipherColumnAware) {
+            assertField(sqlTokenGenerator, 
encryptRule.isQueryWithCipherColumn(), "queryWithCipherColumn");
+        }
+    }
+    
+    private void assertField(final SQLTokenGenerator sqlTokenGenerator, final 
Object filedInstance, final String fieldName) throws Exception {

Review comment:
       Exception thrown is too broad, could you throw more accurate exception?

##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.encrypt.rewrite.token;
+
+import 
org.apache.shardingsphere.encrypt.rewrite.aware.QueryWithCipherColumnAware;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptOrderByItemTokenGenerator;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptPredicateColumnTokenGenerator;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptProjectionTokenGenerator;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.encrypt.rule.aware.EncryptRuleAware;
+import 
org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
+import 
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import 
org.apache.shardingsphere.infra.rewrite.sql.token.generator.SQLTokenGenerator;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.Collections;
+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.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 EncryptTokenGenerateBuilderTest {
+    
+    private EncryptRule encryptRule;
+    
+    @Before
+    public void setup() {
+        encryptRule = mock(EncryptRule.class, RETURNS_DEEP_STUBS);
+        
when(encryptRule.findEncryptTable(anyString()).isPresent()).thenReturn(true);
+    }
+    
+    @Test
+    public void test1() throws Exception {

Review comment:
       The name of method `test1` is not make sense, could you rename it?

##########
File path: 
shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.encrypt.rewrite.token;
+
+import 
org.apache.shardingsphere.encrypt.rewrite.aware.QueryWithCipherColumnAware;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptOrderByItemTokenGenerator;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptPredicateColumnTokenGenerator;
+import 
org.apache.shardingsphere.encrypt.rewrite.token.generator.impl.EncryptProjectionTokenGenerator;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+import org.apache.shardingsphere.encrypt.rule.aware.EncryptRuleAware;
+import 
org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
+import 
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import 
org.apache.shardingsphere.infra.rewrite.sql.token.generator.SQLTokenGenerator;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.Collections;
+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.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 EncryptTokenGenerateBuilderTest {
+    
+    private EncryptRule encryptRule;
+    
+    @Before
+    public void setup() {
+        encryptRule = mock(EncryptRule.class, RETURNS_DEEP_STUBS);
+        
when(encryptRule.findEncryptTable(anyString()).isPresent()).thenReturn(true);
+    }
+    
+    @Test
+    public void test1() throws Exception {
+        SelectStatementContext selectStatementContext = 
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+        
when(selectStatementContext.getAllTables().isEmpty()).thenReturn(false);
+        
when(selectStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singletonList("table"));
+        
when(selectStatementContext.getOrderByContext().getItems()).thenReturn(Collections.singletonList(mock(OrderByItem.class)));
+        
when(selectStatementContext.getGroupByContext().getItems()).thenReturn(Collections.emptyList());
+        when(selectStatementContext.isContainsJoinQuery()).thenReturn(true);
+        EncryptTokenGenerateBuilder encryptTokenGenerateBuilder = new 
EncryptTokenGenerateBuilder(encryptRule, selectStatementContext);
+        Collection<SQLTokenGenerator> sqlTokenGenerators = 
encryptTokenGenerateBuilder.getSQLTokenGenerators();
+        assertThat(sqlTokenGenerators.size(), is(3));
+        Iterator<SQLTokenGenerator> iterator = sqlTokenGenerators.iterator();
+        SQLTokenGenerator item1 = iterator.next();
+        assertThat(item1, instanceOf(EncryptProjectionTokenGenerator.class));
+        assertSqlTokenGenerator(item1);
+        SQLTokenGenerator item2 = iterator.next();
+        assertThat(item2, 
instanceOf(EncryptPredicateColumnTokenGenerator.class));
+        assertSqlTokenGenerator(item2);
+        SQLTokenGenerator item3 = iterator.next();
+        assertThat(item3, instanceOf(EncryptOrderByItemTokenGenerator.class));
+        assertSqlTokenGenerator(item3);
+    }
+    
+    private void assertSqlTokenGenerator(final SQLTokenGenerator 
sqlTokenGenerator) throws Exception {
+        if (sqlTokenGenerator instanceof EncryptRuleAware) {
+            assertField(sqlTokenGenerator, encryptRule, "encryptRule");
+        }
+        if (sqlTokenGenerator instanceof QueryWithCipherColumnAware) {
+            assertField(sqlTokenGenerator, 
encryptRule.isQueryWithCipherColumn(), "queryWithCipherColumn");
+        }
+    }
+    
+    private void assertField(final SQLTokenGenerator sqlTokenGenerator, final 
Object filedInstance, final String fieldName) throws Exception {
+        Field field = findField(sqlTokenGenerator.getClass(), fieldName, 
filedInstance.getClass());
+        field.setAccessible(true);
+        assertNotNull(field.get(sqlTokenGenerator));
+        assertThat(field.get(sqlTokenGenerator), is(filedInstance));
+    }
+    
+    private Field findField(final Class<?> clazz, final String fieldName, 
final Class<?> fieldType) {
+        Class<?> searchClass = clazz;
+        while (null != searchClass && !Object.class.equals(searchClass)) {
+            for (final Field field : searchClass.getDeclaredFields()) {

Review comment:
       `field` should name as `each`




-- 
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]


Reply via email to