You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/06/18 07:39:05 UTC
[shardingsphere] branch master updated: Add more test cases for
shardingsphere-sql-parser-engine module (#10796) (#10839)
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 d4d2d7f Add more test cases for shardingsphere-sql-parser-engine module (#10796) (#10839)
d4d2d7f is described below
commit d4d2d7ff17a1ad9041caf74bbee914718af54c6b
Author: liguoping <xd...@163.com>
AuthorDate: Fri Jun 18 15:38:40 2021 +0800
Add more test cases for shardingsphere-sql-parser-engine module (#10796) (#10839)
* add DatabaseTypedSQLParserFacadeRegistryTest
* modify DatabaseTypedSQLParserFacadeRegistryTest method
* add SQLParserEngineTest
* CheckStyle fixed
* CheckStyle 2 fixed
---
.../DatabaseTypedSQLParserFacadeRegistryTest.java | 41 +++++++++++++++
.../sql/parser/core/SQLParserEngineTest.java | 60 ++++++++++++++++++++++
.../sql/parser/fixture/DatabaseLexer.java | 24 +++++++++
.../sql/parser/fixture/DatabaseParser.java | 29 +++++++++++
.../sql/parser/fixture/DatabaseParserFacade.java | 40 +++++++++++++++
...ere.sql.parser.spi.DatabaseTypedSQLParserFacade | 18 +++++++
6 files changed, 212 insertions(+)
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/DatabaseTypedSQLParserFacadeRegistryTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/DatabaseTypedSQLParserFacadeRegistryTest.java
new file mode 100644
index 0000000..4f0107e
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/DatabaseTypedSQLParserFacadeRegistryTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.core;
+
+import org.apache.shardingsphere.sql.parser.core.database.parser.DatabaseTypedSQLParserFacadeRegistry;
+import org.apache.shardingsphere.sql.parser.fixture.DatabaseLexer;
+import org.apache.shardingsphere.sql.parser.fixture.DatabaseParser;
+import org.apache.shardingsphere.sql.parser.fixture.DatabaseParserFacade;
+import org.apache.shardingsphere.sql.parser.spi.DatabaseTypedSQLParserFacade;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class DatabaseTypedSQLParserFacadeRegistryTest {
+
+ @Test
+ public void assertGetFacade() {
+ DatabaseTypedSQLParserFacade databaseTypedSQLParserFacade = DatabaseTypedSQLParserFacadeRegistry.getFacade("DATABASE");
+ assertThat(databaseTypedSQLParserFacade.getClass(), equalTo(DatabaseParserFacade.class));
+ assertThat(databaseTypedSQLParserFacade.getDatabaseType(), is("DATABASE"));
+ assertThat(databaseTypedSQLParserFacade.getLexerClass(), equalTo(DatabaseLexer.class));
+ assertThat(databaseTypedSQLParserFacade.getParserClass(), equalTo(DatabaseParser.class));
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/SQLParserEngineTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/SQLParserEngineTest.java
new file mode 100644
index 0000000..ff46f68
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/core/SQLParserEngineTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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.core;
+
+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 static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.times;
+
+public class SQLParserEngineTest {
+
+ @Test
+ public void assertParse() {
+ ParseTree parseTree = mock(ParseTree.class);
+
+ SQLParserExecutor sqlParserExecutor = mock(SQLParserExecutor.class);
+ when(sqlParserExecutor.parse("")).thenReturn(parseTree);
+
+ 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);
+ }
+ }
+ );
+
+ parseTreeCache.getUnchecked("");
+ verify(sqlParserExecutor, times(1)).parse("");
+ parseTreeCache.getUnchecked("");
+ verify(sqlParserExecutor, times(1)).parse("");
+ sqlParserExecutor.parse("");
+ verify(sqlParserExecutor, times(2)).parse("");
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseLexer.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseLexer.java
new file mode 100644
index 0000000..8d98885
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseLexer.java
@@ -0,0 +1,24 @@
+/*
+ * 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.fixture;
+
+import org.apache.shardingsphere.sql.parser.api.parser.SQLLexer;
+
+public class DatabaseLexer implements SQLLexer {
+
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseParser.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseParser.java
new file mode 100644
index 0000000..d06a591
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseParser.java
@@ -0,0 +1,29 @@
+/*
+ * 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.fixture;
+
+import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
+import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
+
+public class DatabaseParser implements SQLParser {
+
+ @Override
+ public ASTNode parse() {
+ return null;
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseParserFacade.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseParserFacade.java
new file mode 100644
index 0000000..fefbb1c
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/java/org/apache/shardingsphere/sql/parser/fixture/DatabaseParserFacade.java
@@ -0,0 +1,40 @@
+/*
+ * 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.fixture;
+
+import org.apache.shardingsphere.sql.parser.api.parser.SQLLexer;
+import org.apache.shardingsphere.sql.parser.api.parser.SQLParser;
+import org.apache.shardingsphere.sql.parser.spi.DatabaseTypedSQLParserFacade;
+
+public class DatabaseParserFacade implements DatabaseTypedSQLParserFacade {
+
+ @Override
+ public String getDatabaseType() {
+ return "DATABASE";
+ }
+
+ @Override
+ public Class<? extends SQLLexer> getLexerClass() {
+ return DatabaseLexer.class;
+ }
+
+ @Override
+ public Class<? extends SQLParser> getParserClass() {
+ return DatabaseParser.class;
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/resources/META-INF/services/org.apache.shardingsphere.sql.parser.spi.DatabaseTypedSQLParserFacade b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/resources/META-INF/services/org.apache.shardingsphere.sql.parser.spi.DatabaseTypedSQLParserFacade
new file mode 100644
index 0000000..6ace5a0
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/test/resources/META-INF/services/org.apache.shardingsphere.sql.parser.spi.DatabaseTypedSQLParserFacade
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.sql.parser.fixture.DatabaseParserFacade