You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/04/11 15:52:27 UTC
[shardingsphere] branch master updated: Remove H2 dep from infra-common module (#25119)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 bd52fffccda Remove H2 dep from infra-common module (#25119)
bd52fffccda is described below
commit bd52fffccda6b39cbaab83b2dd8c24f710bebb06
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Apr 11 23:52:20 2023 +0800
Remove H2 dep from infra-common module (#25119)
* Remove maven-resources-plugin on parser-it module
* Remove maven-resources-plugin on example
* Add todo for remove maven-resources-plugin
* Remove H2 dep from DataSourcePropertiesValidatorTest
* Remove H2 dep from DataSourcePoolDestroyerTest
* Remove H2 dep from HikariDataSourcePoolActiveDetectorTest
* Remove H2 dep from infra-common module
---
agent/plugins/metrics/core/pom.xml | 6 +++
infra/common/pom.xml | 5 ---
.../destroyer/DataSourcePoolDestroyerTest.java | 7 ++--
.../HikariDataSourcePoolActiveDetectorTest.java | 7 ++--
.../props/DataSourcePropertiesValidatorTest.java | 43 ++++++++++++----------
.../test/fixture/jdbc/MockedDriver.java | 15 +++++++-
6 files changed, 49 insertions(+), 34 deletions(-)
diff --git a/agent/plugins/metrics/core/pom.xml b/agent/plugins/metrics/core/pom.xml
index 3ecea430b01..5642a27c94c 100644
--- a/agent/plugins/metrics/core/pom.xml
+++ b/agent/plugins/metrics/core/pom.xml
@@ -99,6 +99,12 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/infra/common/pom.xml b/infra/common/pom.xml
index 957eadef17f..392fdab8774 100644
--- a/infra/common/pom.xml
+++ b/infra/common/pom.xml
@@ -58,11 +58,6 @@
<scope>test</scope>
</dependency>
- <dependency>
- <groupId>com.h2database</groupId>
- <artifactId>h2</artifactId>
- </dependency>
-
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/DataSourcePoolDestroyerTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/DataSourcePoolDestroyerTest.java
index c030be9aea8..6308637cb14 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/DataSourcePoolDestroyerTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/DataSourcePoolDestroyerTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.infra.datasource.pool.destroyer;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDriver;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
@@ -56,10 +57,8 @@ class DataSourcePoolDestroyerTest {
private HikariDataSource createHikariDataSource() {
HikariConfig config = new HikariConfig();
- config.setDriverClassName("org.h2.Driver");
- config.setJdbcUrl("jdbc:h2:mem:foo_ds;DB_CLOSE_DELAY=-1");
- config.setUsername("root");
- config.setPassword("root");
+ config.setDriverClassName(MockedDriver.class.getName());
+ config.setJdbcUrl("mock:jdbc");
return new HikariDataSource(config);
}
}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/detector/type/HikariDataSourcePoolActiveDetectorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/detector/type/HikariDataSourcePoolActiveDetectorTest.java
index 2981c0b08e8..735ade829fd 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/detector/type/HikariDataSourcePoolActiveDetectorTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/destroyer/detector/type/HikariDataSourcePoolActiveDetectorTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.datasource.pool.destroyer.detector.type;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDriver;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
@@ -50,10 +51,8 @@ class HikariDataSourcePoolActiveDetectorTest {
private HikariDataSource createHikariDataSource() {
HikariConfig config = new HikariConfig();
- config.setDriverClassName("org.h2.Driver");
- config.setJdbcUrl("jdbc:h2:mem:foo_ds;DB_CLOSE_DELAY=-1");
- config.setUsername("root");
- config.setPassword("root");
+ config.setDriverClassName(MockedDriver.class.getName());
+ config.setJdbcUrl("mock:jdbc");
return new HikariDataSource(config);
}
}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java
index d4a65079ca3..6e2b392711b 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesValidatorTest.java
@@ -18,43 +18,46 @@
package org.apache.shardingsphere.infra.datasource.props;
import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDriver;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.StaticMockSettings;
+import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import java.util.Collection;
import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(DatabaseTypeEngine.class)
class DataSourcePropertiesValidatorTest {
- @Test
- void assertValidateSuccess() {
- assertTrue(new DataSourcePropertiesValidator().validate(Collections.singletonMap("name", new DataSourceProperties(HikariDataSource.class.getName(), createValidProperties()))).isEmpty());
+ @BeforeAll
+ static void setUp() throws ClassNotFoundException {
+ Class.forName(MockedDriver.class.getName());
}
- private Map<String, Object> createValidProperties() {
- Map<String, Object> result = new HashMap<>();
- result.put("driverClassName", "org.h2.Driver");
- result.put("jdbcUrl", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
- result.put("username", "root");
- result.put("password", "root");
- return result;
+ @Test
+ void assertValidateSuccess() {
+ when(DatabaseTypeEngine.getDatabaseType("mock:jdbc").getDataSourceMetaData("mock:jdbc", null)).thenReturn(mock(DataSourceMetaData.class, RETURNS_DEEP_STUBS));
+ assertTrue(new DataSourcePropertiesValidator().validate(
+ Collections.singletonMap("name", new DataSourceProperties(HikariDataSource.class.getName(), Collections.singletonMap("jdbcUrl", "mock:jdbc")))).isEmpty());
}
@Test
void assertValidateFailed() {
+ when(DatabaseTypeEngine.getDatabaseType("mock:jdbc:invalid").getDataSourceMetaData("mock:jdbc:invalid", null)).thenReturn(mock(DataSourceMetaData.class, RETURNS_DEEP_STUBS));
Collection<String> actual = new DataSourcePropertiesValidator().validate(
- Collections.singletonMap("name", new DataSourceProperties(HikariDataSource.class.getName(), createInvalidProperties())));
- assertThat(actual, is(Collections.singletonList("Invalid data source `name`, error message is: The URL `InvalidJdbcUrl` is not recognized, please refer to the pattern `jdbc:.*`.")));
- }
-
- private Map<String, Object> createInvalidProperties() {
- Map<String, Object> result = new HashMap<>();
- result.put("jdbcUrl", "InvalidJdbcUrl");
- return result;
+ Collections.singletonMap("name", new DataSourceProperties(HikariDataSource.class.getName(), Collections.singletonMap("jdbcUrl", "mock:jdbc:invalid"))));
+ assertThat(actual, is(Collections.singletonList("Invalid data source `name`, error message is: Invalid URL.")));
}
}
diff --git a/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java b/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java
index 10927e5abc2..4e179c0293a 100644
--- a/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java
+++ b/test/fixture/jdbc/src/main/java/org/apache/shardingsphere/test/fixture/jdbc/MockedDriver.java
@@ -19,7 +19,9 @@ package org.apache.shardingsphere.test.fixture.jdbc;
import java.sql.Connection;
import java.sql.Driver;
+import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
+import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;
@@ -31,8 +33,19 @@ import static org.mockito.Mockito.mock;
*/
public final class MockedDriver implements Driver {
+ static {
+ try {
+ DriverManager.registerDriver(new MockedDriver());
+ } catch (final SQLException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+
@Override
- public Connection connect(final String url, final Properties info) {
+ public Connection connect(final String url, final Properties info) throws SQLException {
+ if (url.contains("invalid")) {
+ throw new SQLException("Invalid URL.");
+ }
return mock(Connection.class, RETURNS_DEEP_STUBS);
}