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

panjuan 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 542fbab  [ISSUE #10992] Add unit test for 
shardingsphere-sql-parser-engine  (#11107)
542fbab is described below

commit 542fbabd72dda8ce3e5b6e8fd424ac1ec075a81d
Author: ZZQ的 <[email protected]>
AuthorDate: Mon Jul 5 13:20:27 2021 +0800

    [ISSUE #10992] Add unit test for shardingsphere-sql-parser-engine  (#11107)
    
    * [ISSUE ##10992] Add unit test for SQLParserEngine
    
    * [ISSUE ##10992] Perfect SQLParserEngine
    
    * [ISSUE ##10992] Modify the extra blank lines. delete annotations.
    
    * [ISSUE ##10992] Format code.
    
    * [ISSUE #10992] Add unit test for SQLParserFactoryTest
    
    * Define test classes as final classes
---
 .../sql/parser/api/SQLParserEngineTest.java        | 74 ++++++++++++++++++++++
 .../SQLParserFactoryTest.java}                     | 24 +++++--
 .../sql/parser/fixture/LexerFixture.java           | 24 ++++++-
 .../sql/parser/fixture/ParserFixture.java          | 29 ++++++++-
 4 files changed, 143 insertions(+), 8 deletions(-)

diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngineTest.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngineTest.java
new file mode 100644
index 0000000..b24da02
--- /dev/null
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/api/SQLParserEngineTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.sql.parser.api;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import org.antlr.v4.runtime.tree.ParseTree;
+import 
org.apache.shardingsphere.sql.parser.core.database.parser.SQLParserExecutor;
+import org.junit.Test;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public final class SQLParserEngineTest {
+    
+    private static final String SQL = "SELECT COUNT(*) FROM user";
+    
+    @Test
+    public void assertParse() throws NoSuchFieldException, 
IllegalAccessException {
+        SQLParserExecutor sqlParserExecutor = mock(SQLParserExecutor.class);
+        when(sqlParserExecutor.parse(SQL)).thenReturn(mock(ParseTree.class));
+        SQLParserEngine sqlParserEngine = new SQLParserEngine("H2");
+        Field sqlParserExecutorFiled = 
sqlParserEngine.getClass().getDeclaredField("sqlParserExecutor");
+        Field modifiersField = Field.class.getDeclaredField("modifiers");
+        modifiersField.setAccessible(true);
+        modifiersField.setInt(sqlParserExecutorFiled, 
sqlParserExecutorFiled.getModifiers() & ~Modifier.FINAL);
+        Field modifiersField2 = Field.class.getDeclaredField("modifiers");
+        modifiersField2.setAccessible(true);
+        Field parseTreeCacheField = 
sqlParserEngine.getClass().getDeclaredField("parseTreeCache");
+        modifiersField2.setInt(parseTreeCacheField, 
sqlParserExecutorFiled.getModifiers() & ~Modifier.FINAL);
+        sqlParserExecutorFiled.setAccessible(true);
+        parseTreeCacheField.setAccessible(true);
+        sqlParserExecutorFiled.set(sqlParserEngine, sqlParserExecutor);
+        LoadingCache<String, ParseTree> parseTreeCache = 
CacheBuilder.newBuilder().softValues().initialCapacity(128)
+                .maximumSize(1024).concurrencyLevel(4).build(new 
CacheLoader<String, ParseTree>() {
+                    @ParametersAreNonnullByDefault
+                    @Override
+                    public ParseTree load(final String sql) {
+                        return sqlParserExecutor.parse(sql);
+                    }
+                });
+        parseTreeCacheField.set(sqlParserEngine, parseTreeCache);
+        sqlParserEngine.parse(SQL, true);
+        verify(sqlParserExecutor, times(1)).parse(SQL);
+        sqlParserEngine.parse(SQL, true);
+        verify(sqlParserExecutor, times(1)).parse(SQL);
+        sqlParserEngine.parse(SQL, false);
+        verify(sqlParserExecutor, times(2)).parse(SQL);
+    }
+    
+}
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/ParserFixture.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/SQLParserFactoryTest.java
similarity index 51%
copy from 
shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/ParserFixture.java
copy to 
shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/SQLParserFactoryTest.java
index 4396e34..b2eaf95 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/ParserFixture.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/SQLParserFactoryTest.java
@@ -15,15 +15,27 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sql.parser.fixture;
+package org.apache.shardingsphere.sql.parser.core;
 
+import org.apache.shardingsphere.sql.parser.api.parser.SQLLexer;
 import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
-import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
+import org.apache.shardingsphere.sql.parser.fixture.LexerFixture;
+import org.apache.shardingsphere.sql.parser.fixture.ParserFixture;
+import org.junit.Test;
 
-public final class ParserFixture implements SQLParser {
+import static org.mockito.Mockito.mock;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.CoreMatchers.instanceOf;
+
+public final class SQLParserFactoryTest {
+    
+    private static final String SQL = "SELECT COUNT(*) FROM user";
     
-    @Override
-    public ASTNode parse() {
-        return null;
+    @Test
+    public void newInstance() {
+        SQLLexer sqlLexer = mock(LexerFixture.class);
+        SQLParser sqlParser = mock(ParserFixture.class);
+        SQLParser result = SQLParserFactory.newInstance(SQL, 
sqlLexer.getClass(), sqlParser.getClass());
+        assertThat(result, instanceOf(ParserFixture.class));
     }
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/LexerFixture.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/LexerFixture.java
index baf3768..42575f1 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/LexerFixture.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/LexerFixture.java
@@ -17,7 +17,29 @@
 
 package org.apache.shardingsphere.sql.parser.fixture;
 
+import org.antlr.v4.runtime.CharStream;
+import org.antlr.v4.runtime.Lexer;
+import org.antlr.v4.runtime.atn.ATN;
 import org.apache.shardingsphere.sql.parser.api.parser.SQLLexer;
 
-public final class LexerFixture implements SQLLexer {
+public final class LexerFixture extends Lexer implements SQLLexer {
+    
+    public LexerFixture(final CharStream input) {
+        super(input);
+    }
+    
+    @Override
+    public String[] getRuleNames() {
+        return new String[0];
+    }
+    
+    @Override
+    public String getGrammarFileName() {
+        return null;
+    }
+    
+    @Override
+    public ATN getATN() {
+        return null;
+    }
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/ParserFixture.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/ParserFixture.java
index 4396e34..8d8c147 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/ParserFixture.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/ParserFixture.java
@@ -17,13 +17,40 @@
 
 package org.apache.shardingsphere.sql.parser.fixture;
 
+import org.antlr.v4.runtime.Parser;
+import org.antlr.v4.runtime.TokenStream;
+import org.antlr.v4.runtime.atn.ATN;
 import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
 import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 
-public final class ParserFixture implements SQLParser {
+public final class ParserFixture extends Parser implements SQLParser {
+    
+    public ParserFixture(final TokenStream input) {
+        super(input);
+    }
     
     @Override
     public ASTNode parse() {
         return null;
     }
+    
+    @Override
+    public String[] getTokenNames() {
+        return new String[0];
+    }
+    
+    @Override
+    public String[] getRuleNames() {
+        return new String[0];
+    }
+    
+    @Override
+    public String getGrammarFileName() {
+        return null;
+    }
+    
+    @Override
+    public ATN getATN() {
+        return null;
+    }
 }

Reply via email to