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 2023/05/12 10:13:29 UTC
[shardingsphere] branch master updated: Revise standalone-mode-repository-jdbc module (#25614)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 d26202a5467 Revise standalone-mode-repository-jdbc module (#25614)
d26202a5467 is described below
commit d26202a54673267958cbfc754d8bd122fb1b4dbc
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri May 12 18:13:19 2023 +0800
Revise standalone-mode-repository-jdbc module (#25614)
* Refactor ExternalCaseSettings
* Revise #25161
---
.../standalone/repository/provider/jdbc/pom.xml | 37 +++----
.../repository/standalone/jdbc/JDBCRepository.java | 22 ++--
.../jdbc/props/JDBCRepositoryProperties.java | 2 +-
.../JDBCRepositorySQL.java} | 40 ++-----
.../JDBCRepositorySQLLoader.java} | 38 +++----
.../resources/JAXB/JDBCRepositoryProvider/H2.xml | 26 -----
.../FIXTURE.xml => main/resources/sql/H2.xml} | 2 +-
.../{JAXB/JDBCRepositoryProvider => sql}/MySQL.xml | 0
.../standalone/jdbc/JDBCRepositoryTest.java | 69 +++++++------
.../jdbc/provider/H2JDBCRepositoryTest.java | 74 -------------
.../provider/MySQLJDBCRepositoryProviderTest.java | 115 ---------------------
.../JDBCRepositorySQLLoaderTest.java} | 14 ++-
...standalone.jdbc.provider.JDBCRepositoryProvider | 18 ----
13 files changed, 96 insertions(+), 361 deletions(-)
diff --git a/mode/type/standalone/repository/provider/jdbc/pom.xml b/mode/type/standalone/repository/provider/jdbc/pom.xml
index 81b4ee1db15..ac78aa438dd 100644
--- a/mode/type/standalone/repository/provider/jdbc/pom.xml
+++ b/mode/type/standalone/repository/provider/jdbc/pom.xml
@@ -25,6 +25,7 @@
<version>5.3.3-SNAPSHOT</version>
</parent>
<artifactId>shardingsphere-standalone-mode-repository-jdbc</artifactId>
+ <name>${project.artifactId}</name>
<dependencies>
<dependency>
@@ -40,6 +41,22 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>javax.xml.bind</groupId>
+ <artifactId>jaxb-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.xml.bind</groupId>
+ <artifactId>jaxb-core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.sun.xml.bind</groupId>
+ <artifactId>jaxb-impl</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javax.activation</groupId>
+ <artifactId>javax.activation-api</artifactId>
+ </dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
@@ -57,25 +74,5 @@
<artifactId>mysql-connector-java</artifactId>
<scope>test</scope>
</dependency>
-
- <dependency>
- <groupId>javax.xml.bind</groupId>
- <artifactId>jaxb-api</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.sun.xml.bind</groupId>
- <artifactId>jaxb-core</artifactId>
- </dependency>
-
- <dependency>
- <groupId>com.sun.xml.bind</groupId>
- <artifactId>jaxb-impl</artifactId>
- </dependency>
-
- <dependency>
- <groupId>javax.activation</groupId>
- <artifactId>javax.activation-api</artifactId>
- </dependency>
</dependencies>
</project>
diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
index c7161aa2b05..c25f69f4742 100644
--- a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
+++ b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepository.java
@@ -24,8 +24,8 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.mode.repository.standalone.StandalonePersistRepository;
import org.apache.shardingsphere.mode.repository.standalone.jdbc.props.JDBCRepositoryProperties;
import org.apache.shardingsphere.mode.repository.standalone.jdbc.props.JDBCRepositoryPropertyKey;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProviderLoader;
+import org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQL;
+import org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader;
import java.sql.Connection;
import java.sql.PreparedStatement;
@@ -48,7 +48,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
private static final String SEPARATOR = "/";
- private JDBCRepositoryProvider provider;
+ private JDBCRepositorySQL repositorySQL;
private HikariDataSource dataSource;
@@ -56,9 +56,9 @@ public final class JDBCRepository implements StandalonePersistRepository {
@Override
public void init(final Properties props) {
JDBCRepositoryProperties jdbcRepositoryProps = new JDBCRepositoryProperties(props);
- provider = JDBCRepositoryProviderLoader.load(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PROVIDER));
+ repositorySQL = JDBCRepositorySQLLoader.load(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PROVIDER));
dataSource = new HikariDataSource();
- dataSource.setDriverClassName(provider.getDriverClassName());
+ dataSource.setDriverClassName(repositorySQL.getDriverClassName());
dataSource.setJdbcUrl(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.JDBC_URL));
dataSource.setUsername(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.USERNAME));
dataSource.setPassword(jdbcRepositoryProps.getValue(JDBCRepositoryPropertyKey.PASSWORD));
@@ -70,7 +70,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
statement.execute("DROP TABLE IF EXISTS `repository`");
}
// Finish TODO
- statement.execute(provider.getCreateTableSQL());
+ statement.execute(repositorySQL.getCreateTableSQL());
}
}
@@ -78,7 +78,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
public String getDirectly(final String key) {
try (
Connection connection = dataSource.getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(provider.getSelectByKeySQL())) {
+ PreparedStatement preparedStatement = connection.prepareStatement(repositorySQL.getSelectByKeySQL())) {
preparedStatement.setString(1, key);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
@@ -95,7 +95,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
public List<String> getChildrenKeys(final String key) {
try (
Connection connection = dataSource.getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(provider.getSelectByParentKeySQL())) {
+ PreparedStatement preparedStatement = connection.prepareStatement(repositorySQL.getSelectByParentKeySQL())) {
preparedStatement.setString(1, key);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
List<String> resultChildren = new LinkedList<>();
@@ -152,7 +152,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
private void insert(final String key, final String value, final String parent) throws SQLException {
try (
Connection connection = dataSource.getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(provider.getInsertSQL())) {
+ PreparedStatement preparedStatement = connection.prepareStatement(repositorySQL.getInsertSQL())) {
preparedStatement.setString(1, UUID.randomUUID().toString());
preparedStatement.setString(2, key);
preparedStatement.setString(3, value);
@@ -165,7 +165,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
public void update(final String key, final String value) {
try (
Connection connection = dataSource.getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(provider.getUpdateSQL())) {
+ PreparedStatement preparedStatement = connection.prepareStatement(repositorySQL.getUpdateSQL())) {
preparedStatement.setString(1, value);
preparedStatement.setString(2, key);
preparedStatement.executeUpdate();
@@ -178,7 +178,7 @@ public final class JDBCRepository implements StandalonePersistRepository {
public void delete(final String key) {
try (
Connection connection = dataSource.getConnection();
- PreparedStatement preparedStatement = connection.prepareStatement(provider.getDeleteSQL())) {
+ PreparedStatement preparedStatement = connection.prepareStatement(repositorySQL.getDeleteSQL())) {
preparedStatement.setString(1, key);
preparedStatement.executeUpdate();
} catch (final SQLException ex) {
diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/props/JDBCRepositoryProperties.java b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/props/JDBCRepositoryProperties.java
index 577d5070a18..c2588fa4069 100644
--- a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/props/JDBCRepositoryProperties.java
+++ b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/props/JDBCRepositoryProperties.java
@@ -24,7 +24,7 @@ import java.util.Properties;
/**
* JDBC repository properties.
*/
-public class JDBCRepositoryProperties extends TypedProperties<JDBCRepositoryPropertyKey> {
+public final class JDBCRepositoryProperties extends TypedProperties<JDBCRepositoryPropertyKey> {
public JDBCRepositoryProperties(final Properties props) {
super(JDBCRepositoryPropertyKey.class, props);
diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProvider.java b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQL.java
similarity index 74%
rename from mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProvider.java
rename to mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQL.java
index 313e9805f93..d4369adf9b1 100644
--- a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProvider.java
+++ b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQL.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.repository.standalone.jdbc.provider;
+package org.apache.shardingsphere.mode.repository.standalone.jdbc.sql;
import lombok.Getter;
@@ -24,64 +24,36 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
- * JDBC repository provider.
+ * JDBC repository SQL.
*/
@XmlRootElement(name = "sql")
@Getter
-public class JDBCRepositoryProvider {
+public final class JDBCRepositorySQL {
- /**
- * JDBCRepositoryProvider type.
- */
- @XmlAttribute(name = "type", required = true)
+ @XmlAttribute(required = true)
private String type;
- /**
- * JDBCRepositoryProvider driverClassName.
- */
@XmlAttribute(name = "driver-class-name", required = true)
private String driverClassName;
- /**
- * Whether it is the default provider.
- */
- @XmlAttribute(name = "is-default")
- private Boolean isDefault = false;
+ @XmlAttribute(name = "default")
+ private boolean isDefault;
- /**
- * Create table SQL.
- */
@XmlElement(name = "create-table", required = true)
private String createTableSQL;
- /**
- * Select by key SQL.
- */
@XmlElement(name = "select-by-key", required = true)
private String selectByKeySQL;
- /**
- * Select by parent key SQL.
- */
@XmlElement(name = "select-by-parent", required = true)
private String selectByParentKeySQL;
- /**
- * Insert SQL.
- */
@XmlElement(name = "insert", required = true)
private String insertSQL;
- /**
- * Update SQL.
- */
@XmlElement(name = "update", required = true)
private String updateSQL;
- /**
- * Delete SQL.
- */
@XmlElement(name = "delete", required = true)
private String deleteSQL;
-
}
diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProviderLoader.java b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java
similarity index 68%
rename from mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProviderLoader.java
rename to mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java
index 9c19565a852..67421471f69 100644
--- a/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProviderLoader.java
+++ b/mode/type/standalone/repository/provider/jdbc/src/main/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoader.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.repository.standalone.jdbc.provider;
+package org.apache.shardingsphere.mode.repository.standalone.jdbc.sql;
import lombok.SneakyThrows;
@@ -38,32 +38,32 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
- * JDBC repository provider Loader.
+ * JDBC repository SQL Loader.
*/
-public final class JDBCRepositoryProviderLoader {
+public final class JDBCRepositorySQLLoader {
- private static final String ROOT_DIRECTORY = "JAXB/JDBCRepositoryProvider/";
+ private static final String ROOT_DIRECTORY = "sql";
private static final String FILE_EXTENSION = ".xml";
/**
- * Load JDBCRepositoryProvider.
+ * Load JDBC repository SQL.
*
- * @param type type of JDBCRepositoryProvider
- * @return loaded JDBCRepositoryProvider
+ * @param type type of JDBC repository SQL
+ * @return loaded JDBC repository SQL
*/
@SneakyThrows({JAXBException.class, IOException.class, URISyntaxException.class})
- public static JDBCRepositoryProvider load(final String type) {
- File file = new File(JDBCRepositoryProviderLoader.class.getProtectionDomain().getCodeSource().getLocation().getPath());
+ public static JDBCRepositorySQL load(final String type) {
+ File file = new File(JDBCRepositorySQLLoader.class.getProtectionDomain().getCodeSource().getLocation().getPath());
return file.isFile() ? loadFromJar(file, type) : loadFromDirectory(type);
}
- private static JDBCRepositoryProvider loadFromDirectory(final String type) throws URISyntaxException, IOException {
- Enumeration<URL> urls = JDBCRepositoryProviderLoader.class.getClassLoader().getResources(ROOT_DIRECTORY);
+ private static JDBCRepositorySQL loadFromDirectory(final String type) throws URISyntaxException, IOException {
+ Enumeration<URL> urls = JDBCRepositorySQLLoader.class.getClassLoader().getResources(ROOT_DIRECTORY);
if (null == urls) {
return null;
}
- final JDBCRepositoryProvider[] result = new JDBCRepositoryProvider[1];
+ final JDBCRepositorySQL[] result = new JDBCRepositorySQL[1];
final boolean[] gotIt = new boolean[1];
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
@@ -73,9 +73,9 @@ public final class JDBCRepositoryProviderLoader {
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
if (file.toString().endsWith(FILE_EXTENSION)) {
- JDBCRepositoryProvider provider = (JDBCRepositoryProvider) JAXBContext.newInstance(JDBCRepositoryProvider.class).createUnmarshaller()
+ JDBCRepositorySQL provider = (JDBCRepositorySQL) JAXBContext.newInstance(JDBCRepositorySQL.class).createUnmarshaller()
.unmarshal(Files.newInputStream(file.toFile().toPath()));
- if (Objects.equals(provider.getIsDefault(), true)) {
+ if (Objects.equals(provider.isDefault(), true)) {
result[0] = provider;
}
if (Objects.equals(provider.getType(), type)) {
@@ -94,8 +94,8 @@ public final class JDBCRepositoryProviderLoader {
return result[0];
}
- private static JDBCRepositoryProvider loadFromJar(final File file, final String type) throws JAXBException, IOException {
- JDBCRepositoryProvider defaultProvider = null;
+ private static JDBCRepositorySQL loadFromJar(final File file, final String type) throws JAXBException, IOException {
+ JDBCRepositorySQL defaultProvider = null;
try (JarFile jar = new JarFile(file)) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
@@ -103,9 +103,9 @@ public final class JDBCRepositoryProviderLoader {
if (!name.startsWith(ROOT_DIRECTORY) || !name.endsWith(FILE_EXTENSION)) {
continue;
}
- final InputStream inputStream = JDBCRepositoryProviderLoader.class.getClassLoader().getResourceAsStream(name);
- JDBCRepositoryProvider provider = (JDBCRepositoryProvider) JAXBContext.newInstance(JDBCRepositoryProvider.class).createUnmarshaller().unmarshal(inputStream);
- if (Objects.equals(provider.getIsDefault(), true)) {
+ final InputStream inputStream = JDBCRepositorySQLLoader.class.getClassLoader().getResourceAsStream(name);
+ JDBCRepositorySQL provider = (JDBCRepositorySQL) JAXBContext.newInstance(JDBCRepositorySQL.class).createUnmarshaller().unmarshal(inputStream);
+ if (Objects.equals(provider.isDefault(), true)) {
defaultProvider = provider;
}
if (Objects.equals(provider.getType(), type)) {
diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/resources/JAXB/JDBCRepositoryProvider/H2.xml b/mode/type/standalone/repository/provider/jdbc/src/main/resources/JAXB/JDBCRepositoryProvider/H2.xml
deleted file mode 100644
index e2eab06f60a..00000000000
--- a/mode/type/standalone/repository/provider/jdbc/src/main/resources/JAXB/JDBCRepositoryProvider/H2.xml
+++ /dev/null
@@ -1,26 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<!--
- ~ 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.
- -->
-
-<sql type="H2" driver-class-name="org.h2.Driver" is-default="true">
- <create-table>CREATE TABLE IF NOT EXISTS `repository`(id varchar(36) PRIMARY KEY, `key` TEXT, `value` TEXT, parent TEXT)</create-table>
- <select-by-key>SELECT `value` FROM `repository` WHERE `key` = ?</select-by-key>
- <select-by-parent>SELECT `key` FROM `repository` WHERE parent = ?</select-by-parent>
- <insert>INSERT INTO `repository` VALUES(?, ?, ?, ?)</insert>
- <update>UPDATE `repository` SET `value` = ? WHERE `key` = ?</update>
- <delete>DELETE FROM `repository` WHERE `key` = ?</delete>
-</sql>
diff --git a/mode/type/standalone/repository/provider/jdbc/src/test/resources/JAXB/JDBCRepositoryProvider/FIXTURE.xml b/mode/type/standalone/repository/provider/jdbc/src/main/resources/sql/H2.xml
similarity index 95%
rename from mode/type/standalone/repository/provider/jdbc/src/test/resources/JAXB/JDBCRepositoryProvider/FIXTURE.xml
rename to mode/type/standalone/repository/provider/jdbc/src/main/resources/sql/H2.xml
index 6fe8b60ddff..ea5f5a8ee28 100644
--- a/mode/type/standalone/repository/provider/jdbc/src/test/resources/JAXB/JDBCRepositoryProvider/FIXTURE.xml
+++ b/mode/type/standalone/repository/provider/jdbc/src/main/resources/sql/H2.xml
@@ -16,7 +16,7 @@
~ limitations under the License.
-->
-<sql type="FIXTURE" driver-class-name="org.h2.Driver">
+<sql type="H2" driver-class-name="org.h2.Driver" default="true">
<create-table>CREATE TABLE IF NOT EXISTS `repository`(id varchar(36) PRIMARY KEY, `key` TEXT, `value` TEXT, parent TEXT)</create-table>
<select-by-key>SELECT `value` FROM `repository` WHERE `key` = ?</select-by-key>
<select-by-parent>SELECT `key` FROM `repository` WHERE parent = ?</select-by-parent>
diff --git a/mode/type/standalone/repository/provider/jdbc/src/main/resources/JAXB/JDBCRepositoryProvider/MySQL.xml b/mode/type/standalone/repository/provider/jdbc/src/main/resources/sql/MySQL.xml
similarity index 100%
rename from mode/type/standalone/repository/provider/jdbc/src/main/resources/JAXB/JDBCRepositoryProvider/MySQL.xml
rename to mode/type/standalone/repository/provider/jdbc/src/main/resources/sql/MySQL.xml
diff --git a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryTest.java b/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryTest.java
index 7520e33123b..9fecd1063c2 100644
--- a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryTest.java
+++ b/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/JDBCRepositoryTest.java
@@ -18,8 +18,8 @@
package org.apache.shardingsphere.mode.repository.standalone.jdbc;
import com.zaxxer.hikari.HikariDataSource;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProviderLoader;
+import org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQL;
+import org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.AfterEach;
@@ -33,6 +33,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
@@ -50,7 +51,7 @@ import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class JDBCRepositoryTest {
- private JDBCRepositoryProvider fixture;
+ private JDBCRepositorySQL repositorySQL;
@Mock
private Connection mockJdbcConnection;
@@ -72,7 +73,7 @@ class JDBCRepositoryTest {
private JDBCRepository repository;
@BeforeEach
- void setup() throws Exception {
+ void setup() throws SQLException {
mockedConstruction = mockConstruction(HikariDataSource.class, (mock, context) -> when(mock.getConnection()).thenReturn(mockJdbcConnection));
when(mockJdbcConnection.createStatement()).thenReturn(mockStatement);
repository = new JDBCRepository();
@@ -80,9 +81,9 @@ class JDBCRepositoryTest {
new Property("jdbc_url", "jdbc:h2:mem:config;DB_CLOSE_DELAY=0;DATABASE_TO_UPPER=false;MODE=MYSQL"),
new Property("username", "sa"),
new Property("password", ""),
- new Property("provider", "FIXTURE"));
+ new Property("provider", "H2"));
repository.init(props);
- fixture = JDBCRepositoryProviderLoader.load("FIXTURE");
+ repositorySQL = JDBCRepositorySQLLoader.load("H2");
}
@AfterEach
@@ -92,14 +93,14 @@ class JDBCRepositoryTest {
@Test
void assertInit() throws Exception {
- verify(mockStatement).execute(fixture.getCreateTableSQL());
+ verify(mockStatement).execute(repositorySQL.getCreateTableSQL());
}
@Test
- void assertGet() throws Exception {
+ void assertGet() throws SQLException {
String key = "key";
String value = "value";
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(true);
when(mockResultSet.getString("value")).thenReturn(value);
@@ -109,8 +110,8 @@ class JDBCRepositoryTest {
}
@Test
- void assertGetFailure() throws Exception {
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
+ void assertGetFailure() throws SQLException {
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(false);
String actual = repository.getDirectly("key");
@@ -118,8 +119,8 @@ class JDBCRepositoryTest {
}
@Test
- void assertPersistAndGetChildrenKeys() throws Exception {
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByParentKeySQL())).thenReturn(mockPreparedStatement);
+ void assertPersistAndGetChildrenKeys() throws SQLException {
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByParentKeySQL())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(true, true, true, false);
when(mockResultSet.getString("key")).thenReturn("parent1/test1", "parent1/test2", "");
@@ -129,8 +130,8 @@ class JDBCRepositoryTest {
}
@Test
- void assertPersistAndGetChildrenKeysFailure() throws Exception {
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByParentKeySQL())).thenReturn(mockPreparedStatement);
+ void assertPersistAndGetChildrenKeysFailure() throws SQLException {
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByParentKeySQL())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(false);
List<String> actual = repository.getChildrenKeys("key");
@@ -138,11 +139,11 @@ class JDBCRepositoryTest {
}
@Test
- void assertPersistWithUpdateForSimpleKeys() throws Exception {
+ void assertPersistWithUpdateForSimpleKeys() throws SQLException {
final String key = "key";
final String value = "value";
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
- when(mockJdbcConnection.prepareStatement(fixture.getUpdateSQL())).thenReturn(mockPreparedStatementForPersist);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getUpdateSQL())).thenReturn(mockPreparedStatementForPersist);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(true);
when(mockResultSet.getString("value")).thenReturn("oldValue");
@@ -155,11 +156,11 @@ class JDBCRepositoryTest {
}
@Test
- void assertPersistForDirectory() throws Exception {
+ void assertPersistForDirectory() throws SQLException {
final String key = "/parent/child/test1";
final String value = "test1_content";
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
- when(mockJdbcConnection.prepareStatement(fixture.getInsertSQL())).thenReturn(mockPreparedStatementForPersist);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getInsertSQL())).thenReturn(mockPreparedStatementForPersist);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
repository.persist(key, value);
int depthOfDirectory = (int) key.chars().filter(ch -> ch == '/').count();
@@ -192,23 +193,23 @@ class JDBCRepositoryTest {
}
@Test
- void assertPersistFailureDuringUpdate() throws Exception {
+ void assertPersistFailureDuringUpdate() throws SQLException {
final String key = "key";
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(true);
when(mockResultSet.getString("value")).thenReturn("oldValue");
- when(mockJdbcConnection.prepareStatement(fixture.getUpdateSQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getUpdateSQL())).thenReturn(mockPreparedStatement);
repository.persist(key, "value");
verify(mockPreparedStatementForPersist, times(0)).executeUpdate();
}
@Test
- void assertPersistWithInsertForSimpleKeys() throws Exception {
+ void assertPersistWithInsertForSimpleKeys() throws SQLException {
final String key = "key";
final String value = "value";
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
- when(mockJdbcConnection.prepareStatement(fixture.getInsertSQL())).thenReturn(mockPreparedStatementForPersist);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getInsertSQL())).thenReturn(mockPreparedStatementForPersist);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(false);
repository.persist(key, value);
@@ -221,28 +222,28 @@ class JDBCRepositoryTest {
}
@Test
- void assertPersistFailureDuringInsert() throws Exception {
- when(mockJdbcConnection.prepareStatement(fixture.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
+ void assertPersistFailureDuringInsert() throws SQLException {
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getSelectByKeySQL())).thenReturn(mockPreparedStatement);
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
when(mockResultSet.next()).thenReturn(false);
- when(mockJdbcConnection.prepareStatement(fixture.getInsertSQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getInsertSQL())).thenReturn(mockPreparedStatement);
repository.persist("key", "value");
verify(mockPreparedStatementForPersist, times(0)).executeUpdate();
}
@Test
- void assertDelete() throws Exception {
+ void assertDelete() throws SQLException {
String key = "key";
- when(mockJdbcConnection.prepareStatement(fixture.getDeleteSQL())).thenReturn(mockPreparedStatement);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getDeleteSQL())).thenReturn(mockPreparedStatement);
repository.delete(key);
verify(mockPreparedStatement).setString(1, key);
verify(mockPreparedStatement).executeUpdate();
}
@Test
- void assertDeleteFailure() throws Exception {
+ void assertDeleteFailure() throws SQLException {
String key = "key";
- when(mockJdbcConnection.prepareStatement(fixture.getDeleteSQL())).thenReturn(mockPreparedStatementForPersist);
+ when(mockJdbcConnection.prepareStatement(repositorySQL.getDeleteSQL())).thenReturn(mockPreparedStatementForPersist);
repository.delete(key);
verify(mockPreparedStatement, times(0)).executeUpdate();
}
diff --git a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/H2JDBCRepositoryTest.java b/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/H2JDBCRepositoryTest.java
deleted file mode 100644
index 3e1b562787f..00000000000
--- a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/H2JDBCRepositoryTest.java
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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.mode.repository.standalone.jdbc.provider;
-
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import java.util.List;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-
-class H2JDBCRepositoryTest {
-
- private final JDBCRepository repository = new JDBCRepository();
-
- @BeforeEach
- void setUp() {
- Properties props = PropertiesBuilder.build(
- new Property("jdbc_url", "jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"),
- new Property("username", "sa"),
- new Property("password", ""),
- new Property("provider", "H2"));
- repository.init(props);
- }
-
- @AfterEach
- void stop() {
- repository.close();
- }
-
- @Test
- void assertPersistAndGet() {
- repository.persist("/testPath/test1", "test1_content");
- assertThat(repository.getDirectly("/testPath/test1"), is("test1_content"));
- repository.persist("/testPath/test1", "modify_content");
- assertThat(repository.getDirectly("/testPath/test1"), is("modify_content"));
- }
-
- @Test
- void assertPersistAndGetChildrenKeys() {
- repository.persist("/testPath/test1", "test1_content");
- repository.persist("/testPath/test2", "test2_content");
- List<String> childrenKeys = repository.getChildrenKeys("/testPath");
- assertThat(childrenKeys.get(0), is("test1"));
- assertThat(childrenKeys.get(1), is("test2"));
- }
-
- @Test
- void assertDelete() {
- repository.delete("/testPath");
- assertThat(repository.getDirectly("/testPath"), is(""));
- }
-}
diff --git a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/MySQLJDBCRepositoryProviderTest.java b/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/MySQLJDBCRepositoryProviderTest.java
deleted file mode 100644
index 8e5b490d4c6..00000000000
--- a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/MySQLJDBCRepositoryProviderTest.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- * 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.mode.repository.standalone.jdbc.provider;
-
-import com.zaxxer.hikari.HikariDataSource;
-import org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository;
-import org.apache.shardingsphere.test.util.PropertiesBuilder;
-import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.AfterEach;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Mock;
-import org.mockito.MockedConstruction;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockConstruction;
-import static org.mockito.Mockito.when;
-
-@ExtendWith(MockitoExtension.class)
-class MySQLJDBCRepositoryProviderTest {
-
- @Mock
- private Connection connection;
-
- @Mock
- private PreparedStatement preparedStatement;
-
- @Mock
- private ResultSet resultSet;
-
- private MockedConstruction<HikariDataSource> construction;
-
- private final JDBCRepositoryProvider provider = JDBCRepositoryProviderLoader.load("MySQL");
-
- private final JDBCRepository repository = new JDBCRepository();
-
- @BeforeEach
- void setUp() throws SQLException {
- construction = mockConstruction(HikariDataSource.class, (mock, context) -> when(mock.getConnection()).thenReturn(connection));
- when(connection.createStatement()).thenReturn(mock(Statement.class));
- repository.init(PropertiesBuilder.build(new Property("jdbc_url", "jdbc:mysql://localhost:3306/config"),
- new Property("username", "sa"), new Property("password", ""), new Property("provider", "MySQL")));
- }
-
- @AfterEach
- void stop() {
- repository.close();
- construction.close();
- }
-
- @Test
- void assertPersistAndGet() throws SQLException {
- when(connection.prepareStatement(provider.getSelectByKeySQL())).thenReturn(preparedStatement);
- when(connection.prepareStatement(provider.getInsertSQL())).thenReturn(preparedStatement);
- when(connection.prepareStatement(provider.getUpdateSQL())).thenReturn(preparedStatement);
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
- when(resultSet.next()).thenReturn(false, true, true);
- when(resultSet.getString("value")).thenReturn("test1_content", "test1_content", "modify_content");
- repository.persist("/testPath/test1", "test1_content");
- assertThat(repository.getDirectly("/testPath/test1"), is("test1_content"));
- repository.persist("/testPath/test1", "modify_content");
- assertThat(repository.getDirectly("/testPath/test1"), is("modify_content"));
- }
-
- @Test
- void assertPersistAndGetChildrenKeys() throws SQLException {
- when(connection.prepareStatement(provider.getSelectByKeySQL())).thenReturn(preparedStatement);
- when(connection.prepareStatement(provider.getInsertSQL())).thenReturn(preparedStatement);
- when(connection.prepareStatement(provider.getSelectByParentKeySQL())).thenReturn(preparedStatement);
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
- when(resultSet.next()).thenReturn(false, false, false, false, true, true, false);
- when(resultSet.getString("key")).thenReturn("test1", "test2");
- repository.persist("/testPath/test1", "test1_content");
- repository.persist("/testPath/test2", "test2_content");
- List<String> childrenKeys = repository.getChildrenKeys("/testPath");
- assertThat(childrenKeys.get(0), is("test1"));
- assertThat(childrenKeys.get(1), is("test2"));
- }
-
- @Test
- void assertDelete() throws SQLException {
- when(connection.prepareStatement(provider.getSelectByKeySQL())).thenReturn(preparedStatement);
- when(connection.prepareStatement(provider.getDeleteSQL())).thenReturn(preparedStatement);
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
- when(resultSet.next()).thenReturn(false);
- repository.delete("/testPath");
- assertThat(repository.getDirectly("/testPath"), is(""));
- }
-}
diff --git a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProviderLoaderTest.java b/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoaderTest.java
similarity index 71%
rename from mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProviderLoaderTest.java
rename to mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoaderTest.java
index f1433bff6ed..8963241cc07 100644
--- a/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/provider/JDBCRepositoryProviderLoaderTest.java
+++ b/mode/type/standalone/repository/provider/jdbc/src/test/java/org/apache/shardingsphere/mode/repository/standalone/jdbc/sql/JDBCRepositorySQLLoaderTest.java
@@ -15,24 +15,22 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.mode.repository.standalone.jdbc.provider;
+package org.apache.shardingsphere.mode.repository.standalone.jdbc.sql;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-class JDBCRepositoryProviderLoaderTest {
+class JDBCRepositorySQLLoaderTest {
@Test
- void assertLoadFixtureProvider() {
- final JDBCRepositoryProvider fixture = JDBCRepositoryProviderLoader.load("FIXTURE");
- assertThat(fixture.getType(), is("FIXTURE"));
+ void assertLoad() {
+ assertThat(JDBCRepositorySQLLoader.load("MySQL").getType(), is("MySQL"));
}
@Test
- void assertLoadDefaultProvider() {
- final JDBCRepositoryProvider fixture = JDBCRepositoryProviderLoader.load("nonexistent");
- assertThat(fixture.getType(), is("H2"));
+ void assertLoadByDefault() {
+ assertThat(JDBCRepositorySQLLoader.load("nonexistent").getType(), is("H2"));
}
}
diff --git a/mode/type/standalone/repository/provider/jdbc/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider b/mode/type/standalone/repository/provider/jdbc/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider
deleted file mode 100644
index bfb8e45119f..00000000000
--- a/mode/type/standalone/repository/provider/jdbc/src/test/resources/META-INF/services/org.apache.shardingsphere.mode.repository.standalone.jdbc.provider.JDBCRepositoryProvider
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# 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.mode.repository.standalone.jdbc.fixture.JDBCRepositoryProviderFixture