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

zhangliang 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 4c24b78  Add more unit test for EncryptTokenGenerateBuilder (#15001)
4c24b78 is described below

commit 4c24b78b0e38bbafe1f4e37ccd7a8d5bf43810f3
Author: liguoping <[email protected]>
AuthorDate: Sun Jan 23 17:09:43 2022 +0800

    Add more unit test for EncryptTokenGenerateBuilder (#15001)
    
    * Add more unit test for EncryptTokenGenerateBuilder
    
    * update
---
 .../token/EncryptTokenGenerateBuilderTest.java     | 107 +++++++++++++++++++++
 1 file changed, 107 insertions(+)

diff --git 
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java
 
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilderTest.java
new file mode 100644
index 0000000..90c54f2
--- /dev/null
+++ 
b/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 assertGetSQLTokenGenerators() throws IllegalAccessException {
+        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 IllegalAccessException {
+        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 IllegalAccessException {
+        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 each : searchClass.getDeclaredFields()) {
+                if (fieldName.equals(each.getName()) && 
fieldType.equals(each.getType())) {
+                    return each;
+                }
+            }
+            searchClass = searchClass.getSuperclass();
+        }
+        throw new IllegalStateException("No such field in class");
+    }
+}

Reply via email to