提交 cf2e8b59 编写于 作者: T terrymanu

For #673: add AbstractBaseSQLParsingEngineTest

上级 45a96d0d
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package io.shardingjdbc.core.parsing;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration;
import lombok.AccessLevel;
import lombok.Getter;
import org.junit.BeforeClass;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
public abstract class AbstractBaseSQLParsingEngineTest {
@Getter(AccessLevel.PROTECTED)
private static ShardingRule shardingRule;
@BeforeClass
public static void setUp() throws IOException {
shardingRule = buildShardingRule();
}
private static ShardingRule buildShardingRule() throws IOException {
URL url = SQLParsingEngineTest.class.getClassLoader().getResource("yaml/parser-rule.yaml");
Preconditions.checkNotNull(url, "Cannot found parser rule yaml configuration.");
YamlShardingConfiguration yamlShardingConfig = YamlShardingConfiguration.unmarshal(new File(url.getFile()));
return yamlShardingConfig.getShardingRule(yamlShardingConfig.getDataSources().keySet());
}
protected static Collection<DatabaseType> getDatabaseTypes(final String databaseTypes) {
if (Strings.isNullOrEmpty(databaseTypes)) {
return Sets.newHashSet(DatabaseType.values());
}
Set<DatabaseType> result = new HashSet<>();
for (String each : databaseTypes.split(",")) {
result.add(DatabaseType.valueOf(each));
}
return result;
}
}
......@@ -17,53 +17,28 @@
package io.shardingjdbc.core.parsing;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.parsing.parser.exception.SQLParsingUnsupportedException;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration;
import io.shardingjdbc.test.sql.SQLCase;
import io.shardingjdbc.test.sql.SQLCasesLoader;
import lombok.AllArgsConstructor;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
@AllArgsConstructor
@RunWith(Parameterized.class)
public final class ParsingEngineForUnsupportedSQLTest {
private static ShardingRule shardingRule;
public final class ParsingEngineForUnsupportedSQLTest extends AbstractBaseSQLParsingEngineTest {
private String testCaseName;
private DatabaseType databaseType;
@BeforeClass
public static void setUp() throws IOException {
shardingRule = buildShardingRule();
}
private static ShardingRule buildShardingRule() throws IOException {
URL url = SQLParsingEngineTest.class.getClassLoader().getResource("yaml/parser-rule.yaml");
Preconditions.checkNotNull(url, "Cannot found parser rule yaml configuration.");
YamlShardingConfiguration yamlShardingConfig = YamlShardingConfiguration.unmarshal(new File(url.getFile()));
return yamlShardingConfig.getShardingRule(yamlShardingConfig.getDataSources().keySet());
}
@Parameters(name = "{0}In{1}")
public static Collection<Object[]> getTestParameters() {
Collection<Object[]> result = new ArrayList<>();
......@@ -84,19 +59,8 @@ public final class ParsingEngineForUnsupportedSQLTest {
return result;
}
private static Set<DatabaseType> getDatabaseTypes(final String databaseTypes) {
if (Strings.isNullOrEmpty(databaseTypes)) {
return Sets.newHashSet(DatabaseType.values());
}
Set<DatabaseType> result = new HashSet<>();
for (String each : databaseTypes.split(",")) {
result.add(DatabaseType.valueOf(each));
}
return result;
}
@Test(expected = SQLParsingUnsupportedException.class)
public void assertUnsupportedSQL() {
new SQLParsingEngine(databaseType, SQLCasesLoader.getInstance().getUnsupportedSQL(testCaseName), shardingRule).parse();
new SQLParsingEngine(databaseType, SQLCasesLoader.getInstance().getUnsupportedSQL(testCaseName), getShardingRule()).parse();
}
}
......@@ -25,12 +25,9 @@ import io.shardingjdbc.core.parsing.parser.jaxb.helper.ParserAssertHelper;
import io.shardingjdbc.core.parsing.parser.jaxb.helper.ParserJAXBHelper;
import io.shardingjdbc.core.parsing.parser.sql.SQLStatement;
import io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.util.SQLPlaceholderUtil;
import io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration;
import io.shardingjdbc.test.sql.SQLCasesLoader;
import lombok.RequiredArgsConstructor;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
......@@ -39,19 +36,14 @@ import org.junit.runners.Parameterized.Parameters;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
@RequiredArgsConstructor
@RunWith(Parameterized.class)
public final class SQLParsingEngineTest {
private static ShardingRule shardingRule;
public final class SQLParsingEngineTest extends AbstractBaseSQLParsingEngineTest {
private final String testCaseName;
......@@ -59,18 +51,6 @@ public final class SQLParsingEngineTest {
private final Assert assertObj;
@BeforeClass
public static void setUp() throws IOException {
shardingRule = buildShardingRule();
}
private static ShardingRule buildShardingRule() throws IOException {
URL url = SQLParsingEngineTest.class.getClassLoader().getResource("yaml/parser-rule.yaml");
Preconditions.checkNotNull(url, "Cannot found parser rule yaml configuration.");
YamlShardingConfiguration yamlShardingConfig = YamlShardingConfiguration.unmarshal(new File(url.getFile()));
return yamlShardingConfig.getShardingRule(yamlShardingConfig.getDataSources().keySet());
}
@Parameters(name = "{0}In{1}")
public static Collection<Object[]> getTestParameters() throws JAXBException {
Collection<Object[]> result = new LinkedList<>();
......@@ -94,7 +74,7 @@ public final class SQLParsingEngineTest {
private static List<Object[]> getTestParameters(final Assert assertObj) {
List<Object[]> result = new LinkedList<>();
for (DatabaseType each : getDataBaseTypes(SQLCasesLoader.getInstance().getDatabaseTypes(assertObj.getId()))) {
for (DatabaseType each : getDatabaseTypes(SQLCasesLoader.getInstance().getDatabaseTypes(assertObj.getId()))) {
result.add(getTestParameters(assertObj, each));
}
return result;
......@@ -108,27 +88,16 @@ public final class SQLParsingEngineTest {
return result;
}
private static Collection<DatabaseType> getDataBaseTypes(final Collection<String> databaseTypes) {
if (databaseTypes.isEmpty()) {
return Arrays.asList(DatabaseType.values());
}
Collection<DatabaseType> result = new LinkedHashSet<>(databaseTypes.size());
for (String each : databaseTypes) {
result.add(DatabaseType.valueOf(each));
}
return result;
}
@Test
public void assertLiteralSQL() {
assertSQLStatement(new SQLParsingEngine(databaseType, SQLPlaceholderUtil.replaceStatement(
SQLCasesLoader.getInstance().getSQL(testCaseName), ParserJAXBHelper.getParameters(assertObj.getParameters())), shardingRule).parse(), false);
SQLCasesLoader.getInstance().getSQL(testCaseName), ParserJAXBHelper.getParameters(assertObj.getParameters())), getShardingRule()).parse(), false);
}
@Test
public void assertPlaceholderSQL() {
for (DatabaseType each : getDataBaseTypes(SQLCasesLoader.getInstance().getDatabaseTypes(testCaseName))) {
assertSQLStatement(new SQLParsingEngine(each, SQLPlaceholderUtil.replacePreparedStatement(SQLCasesLoader.getInstance().getSQL(testCaseName)), shardingRule).parse(), true);
for (DatabaseType each : getDatabaseTypes(SQLCasesLoader.getInstance().getDatabaseTypes(testCaseName))) {
assertSQLStatement(new SQLParsingEngine(each, SQLPlaceholderUtil.replacePreparedStatement(SQLCasesLoader.getInstance().getSQL(testCaseName)), getShardingRule()).parse(), true);
}
}
......
......@@ -17,6 +17,8 @@
package io.shardingjdbc.core.integrate.jaxb.helper;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.integrate.jaxb.SQLAssert;
......@@ -28,11 +30,11 @@ import javax.xml.bind.JAXBException;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public final class SQLAssertJAXBHelper {
......@@ -80,7 +82,7 @@ public final class SQLAssertJAXBHelper {
List<Object[]> result = new ArrayList<>();
for (int i = 0; i < asserts.getSqlAsserts().size(); i++) {
SQLAssert assertObj = asserts.getSqlAsserts().get(i);
for (DatabaseType each : getDataBaseTypes(SQLCasesLoader.getInstance().getDatabaseTypes(assertObj.getId()))) {
for (DatabaseType each : getDatabaseTypes(SQLCasesLoader.getInstance().getDatabaseTypes(assertObj.getId()))) {
result.add(getDataParameter(assertObj, each));
}
}
......@@ -95,17 +97,6 @@ public final class SQLAssertJAXBHelper {
}
}
private static Collection<DatabaseType> getDataBaseTypes(final Collection<String> databaseTypes) {
if (databaseTypes.isEmpty()) {
return Arrays.asList(DatabaseType.values());
}
Collection<DatabaseType> result = new LinkedHashSet<>(databaseTypes.size());
for (String each : databaseTypes) {
result.add(DatabaseType.valueOf(each));
}
return result;
}
private static Object[] getDataParameter(final SQLAssert sqlAssert, final DatabaseType dbType) {
final Object[] result = new Object[4];
result[0] = sqlAssert.getId();
......@@ -126,4 +117,15 @@ public final class SQLAssertJAXBHelper {
default: return false;
}
}
private static Collection<DatabaseType> getDatabaseTypes(final String databaseTypes) {
if (Strings.isNullOrEmpty(databaseTypes)) {
return Sets.newHashSet(DatabaseType.values());
}
Set<DatabaseType> result = new HashSet<>();
for (String each : databaseTypes.split(",")) {
result.add(DatabaseType.valueOf(each));
}
return result;
}
}
......@@ -18,8 +18,6 @@
package io.shardingjdbc.test.sql;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
......@@ -30,10 +28,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
......@@ -74,7 +71,7 @@ public final class SQLCasesLoader {
}
private static Map<String, SQLCase> loadSQLCasesFromJar(final String path, final File file) throws IOException, JAXBException {
Map<String, SQLCase> result = new HashMap<>(65536, 1);
Map<String, SQLCase> result = new TreeMap<>();
try (JarFile jar = new JarFile(file)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
......@@ -88,7 +85,7 @@ public final class SQLCasesLoader {
}
private static Map<String, SQLCase> loadSQLCasesFromTargetDirectory(final String path) throws FileNotFoundException, JAXBException {
Map<String, SQLCase> result = new HashMap<>(65536, 1);
Map<String, SQLCase> result = new TreeMap<>();
URL url = SQLCasesLoader.class.getClassLoader().getResource(path);
if (null == url) {
return result;
......@@ -176,9 +173,8 @@ public final class SQLCasesLoader {
* @param id SQL ID
* @return database types
*/
public Collection<String> getDatabaseTypes(final String id) {
public String getDatabaseTypes(final String id) {
Preconditions.checkState(sqlCaseMap.containsKey(id), "Can't find SQL of id: " + id);
String databaseTypes = sqlCaseMap.get(id).getDatabaseTypes();
return Strings.isNullOrEmpty(databaseTypes) ? Collections.<String>emptyList() : Splitter.on(',').trimResults().splitToList(databaseTypes);
return sqlCaseMap.get(id).getDatabaseTypes();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册