geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject [geode] branch feature/GEODE-4009 updated: renamed ConnectionManager to DataSourceManager introduced JdbcDataSourceFactory to all unit tests to tell DataSourceManager how to create a data source.
Date Sat, 30 Dec 2017 01:20:56 GMT
This is an automated email from the ASF dual-hosted git repository.

dschneider pushed a commit to branch feature/GEODE-4009
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/feature/GEODE-4009 by this push:
     new a78a904  renamed ConnectionManager to DataSourceManager introduced JdbcDataSourceFactory
to all unit tests to tell DataSourceManager how to create a data source.
a78a904 is described below

commit a78a90434e327dda27b8fb33759a3181e33bb8ca
Author: Darrel Schneider <dschneider@pivotal.io>
AuthorDate: Fri Dec 29 17:19:57 2017 -0800

    renamed ConnectionManager to DataSourceManager
    introduced JdbcDataSourceFactory to all unit tests to tell DataSourceManager
    how to create a data source.
---
 .../jdbc/internal/AbstractJdbcCallback.java        |   4 +-
 ...nnectionManager.java => DataSourceManager.java} |  32 +----
 ...ilder.java => HikariJdbcDataSourceFactory.java} |  13 +--
 .../jdbc/internal/JdbcDataSourceFactory.java}      |  13 ++-
 .../geode/connectors/jdbc/internal/SqlHandler.java |  41 +++++--
 .../jdbc/JdbcAsyncWriterIntegrationTest.java       |   3 +-
 .../connectors/jdbc/JdbcLoaderIntegrationTest.java |   3 +-
 .../connectors/jdbc/JdbcWriterIntegrationTest.java |  11 +-
 .../jdbc/internal/ConnectionManagerUnitTest.java   | 129 ---------------------
 .../jdbc/internal/DataSourceManagerUnitTest.java   | 115 ++++++++++++++++++
 .../connectors/jdbc/internal/SqlHandlerTest.java   |  37 ++++--
 .../jdbc/internal/TestableConnectionManager.java   |   6 +-
 12 files changed, 198 insertions(+), 209 deletions(-)

diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/AbstractJdbcCallback.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/AbstractJdbcCallback.java
index 64d370b..aaba82b 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/AbstractJdbcCallback.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/AbstractJdbcCallback.java
@@ -58,8 +58,8 @@ public abstract class AbstractJdbcCallback implements CacheCallback {
   private synchronized void initialize(InternalCache cache) {
     if (sqlHandler == null) {
       InternalJdbcConnectorService service = cache.getService(InternalJdbcConnectorService.class);
-      ConnectionManager manager = new ConnectionManager(service);
-      sqlHandler = new SqlHandler(manager);
+      DataSourceManager manager = new DataSourceManager(new HikariJdbcDataSourceFactory());
+      sqlHandler = new SqlHandler(manager, service);
     }
   }
 }
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionManager.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/DataSourceManager.java
similarity index 59%
rename from geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionManager.java
rename to geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/DataSourceManager.java
index ce62587..1d66cf6 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/ConnectionManager.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/DataSourceManager.java
@@ -19,45 +19,25 @@ import java.sql.SQLException;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
-class ConnectionManager {
+class DataSourceManager {
 
-  private final InternalJdbcConnectorService configService;
+  private final JdbcDataSourceFactory jdbcDataSourceFactory;
   private final Map<String, JdbcDataSource> dataSourceMap = new ConcurrentHashMap<>();
 
-  ConnectionManager(InternalJdbcConnectorService configService) {
-    this.configService = configService;
+  DataSourceManager(JdbcDataSourceFactory jdbcDataSourceFactory) {
+    this.jdbcDataSourceFactory = jdbcDataSourceFactory;
   }
 
-  RegionMapping getMappingForRegion(String regionName) {
-    return configService.getMappingForRegion(regionName);
-  }
-
-  JdbcDataSource buildJdbcDataSource(ConnectionConfiguration config) {
-    return new JdbcDataSourceBuilder(config).create();
-  }
-
-  private JdbcDataSource getDataSource(ConnectionConfiguration config) {
+  JdbcDataSource getDataSource(ConnectionConfiguration config) {
     return dataSourceMap.computeIfAbsent(config.getName(), k -> {
-      return buildJdbcDataSource(config);
+      return this.jdbcDataSourceFactory.create(config);
     });
   }
 
-  Connection getConnection(ConnectionConfiguration config) {
-    try {
-      return getDataSource(config).getConnection();
-    } catch (SQLException e) {
-      throw new IllegalStateException("Could not connect to " + config.getUrl(), e);
-    }
-  }
-
   void close() {
     dataSourceMap.values().forEach(this::close);
   }
 
-  ConnectionConfiguration getConnectionConfig(String connectionConfigName) {
-    return configService.getConnectionConfig(connectionConfigName);
-  }
-
   private void close(JdbcDataSource dataSource) {
     try {
       dataSource.close();
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceBuilder.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/HikariJdbcDataSourceFactory.java
similarity index 74%
rename from geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceBuilder.java
rename to geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/HikariJdbcDataSourceFactory.java
index 4663a89..2bbd626 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceBuilder.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/HikariJdbcDataSourceFactory.java
@@ -14,15 +14,8 @@
  */
 package org.apache.geode.connectors.jdbc.internal;
 
-public class JdbcDataSourceBuilder {
-  private final ConnectionConfiguration configuration;
-
-  public JdbcDataSourceBuilder(ConnectionConfiguration configuration) {
-    this.configuration = configuration;
-  }
-
-  public JdbcDataSource create() {
-    return new HikariJdbcDataSource(this.configuration);
+public class HikariJdbcDataSourceFactory implements JdbcDataSourceFactory {
+  public JdbcDataSource create(ConnectionConfiguration configuration) {
+    return new HikariJdbcDataSource(configuration);
   }
-
 }
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TestableConnectionManager.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceFactory.java
similarity index 77%
copy from geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TestableConnectionManager.java
copy to geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceFactory.java
index 01f73f8..2dd5bbf 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TestableConnectionManager.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JdbcDataSourceFactory.java
@@ -14,9 +14,12 @@
  */
 package org.apache.geode.connectors.jdbc.internal;
 
-public class TestableConnectionManager extends ConnectionManager {
-
-  public TestableConnectionManager(InternalJdbcConnectorService configService) {
-    super(configService);
-  }
+/**
+ * Given a ConnnectionConfiguration create a JdbcDataSource.
+ */
+interface JdbcDataSourceFactory {
+  /**
+   * Given a ConnnectionConfiguration create and return a JdbcDataSource
+   */
+  JdbcDataSource create(ConnectionConfiguration configuration);
 }
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/SqlHandler.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/SqlHandler.java
index 4408212..8479217 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/SqlHandler.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/SqlHandler.java
@@ -33,34 +33,44 @@ import org.apache.geode.pdx.internal.PdxInstanceImpl;
 
 @Experimental
 public class SqlHandler {
-  private final ConnectionManager manager;
+  private final InternalJdbcConnectorService configService;
+  private final DataSourceManager manager;
   private final TableKeyColumnManager tableKeyColumnManager;
 
-  public SqlHandler(ConnectionManager manager) {
-    this(manager, new TableKeyColumnManager());
+  public SqlHandler(DataSourceManager manager, InternalJdbcConnectorService configService)
{
+    this(manager, new TableKeyColumnManager(), configService);
   }
 
-  SqlHandler(ConnectionManager manager, TableKeyColumnManager tableKeyColumnManager) {
+  SqlHandler(DataSourceManager manager, TableKeyColumnManager tableKeyColumnManager,
+      InternalJdbcConnectorService configService) {
     this.manager = manager;
     this.tableKeyColumnManager = tableKeyColumnManager;
+    this.configService = configService;
   }
 
   public void close() {
     manager.close();
   }
 
+  Connection getConnection(ConnectionConfiguration config) {
+    try {
+      return manager.getDataSource(config).getConnection();
+    } catch (SQLException e) {
+      throw new IllegalStateException("Could not connect to " + config.getUrl(), e);
+    }
+  }
+
   public <K, V> PdxInstance read(Region<K, V> region, K key) {
     if (key == null) {
       throw new IllegalArgumentException("Key for query cannot be null");
     }
 
-    RegionMapping regionMapping = manager.getMappingForRegion(region.getName());
+    RegionMapping regionMapping = getMappingForRegion(region.getName());
     ConnectionConfiguration connectionConfig =
-        manager.getConnectionConfig(regionMapping.getConnectionConfigName());
-
+        getConnectionConfig(regionMapping.getConnectionConfigName());
     String tableName = regionMapping.getRegionToTableName();
     PdxInstance result = null;
-    try (Connection connection = manager.getConnection(connectionConfig)) {
+    try (Connection connection = getConnection(connectionConfig)) {
       List<ColumnValue> columnList =
           getColumnToValueList(connection, regionMapping, key, null, Operation.GET);
       try (PreparedStatement statement =
@@ -75,6 +85,14 @@ public class SqlHandler {
     return result;
   }
 
+  private RegionMapping getMappingForRegion(String regionName) {
+    return this.configService.getMappingForRegion(regionName);
+  }
+
+  private ConnectionConfiguration getConnectionConfig(String connectionConfigName) {
+    return this.configService.getConnectionConfig(connectionConfigName);
+  }
+
   private String getKeyColumnName(Connection connection, String tableName) {
     return this.tableKeyColumnManager.getKeyColumnName(connection, tableName);
   }
@@ -141,15 +159,14 @@ public class SqlHandler {
     if (value == null && operation != Operation.DESTROY) {
       throw new IllegalArgumentException("PdxInstance cannot be null for non-destroy operations");
     }
-    RegionMapping regionMapping = manager.getMappingForRegion(region.getName());
-
+    RegionMapping regionMapping = getMappingForRegion(region.getName());
     if (regionMapping == null) {
       throw new IllegalStateException(
           "JDBC write failed. JDBC mapping for region " + region.getFullPath()
               + " not found. Create the mapping with the gfsh command 'create jdbc-mapping'.");
     }
     ConnectionConfiguration connectionConfig =
-        manager.getConnectionConfig(regionMapping.getConnectionConfigName());
+        getConnectionConfig(regionMapping.getConnectionConfigName());
     if (connectionConfig == null) {
       throw new IllegalStateException(
           "JDBC write failed. JDBC connection with name " + regionMapping.getConnectionConfigName()
@@ -159,7 +176,7 @@ public class SqlHandler {
     String tableName = regionMapping.getRegionToTableName();
     int pdxTypeId = value == null ? 0 : ((PdxInstanceImpl) value).getPdxType().getTypeId();
 
-    try (Connection connection = manager.getConnection(connectionConfig)) {
+    try (Connection connection = getConnection(connectionConfig)) {
       List<ColumnValue> columnList =
           getColumnToValueList(connection, regionMapping, key, value, operation);
       int updateCount = 0;
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcAsyncWriterIntegrationTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcAsyncWriterIntegrationTest.java
index 2aacfbe..e8c7e63 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcAsyncWriterIntegrationTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcAsyncWriterIntegrationTest.java
@@ -253,7 +253,8 @@ public class JdbcAsyncWriterIntegrationTest {
 
   private SqlHandler createSqlHandler()
       throws ConnectionConfigExistsException, RegionMappingExistsException {
-    return new SqlHandler(new TestableConnectionManager(TestConfigService.getTestConfigService()));
+    return new SqlHandler(new TestableConnectionManager(),
+        TestConfigService.getTestConfigService());
   }
 
 }
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcLoaderIntegrationTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcLoaderIntegrationTest.java
index 87d26cd..f162c3d 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcLoaderIntegrationTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcLoaderIntegrationTest.java
@@ -96,7 +96,8 @@ public class JdbcLoaderIntegrationTest {
 
   private SqlHandler createSqlHandler()
       throws ConnectionConfigExistsException, RegionMappingExistsException {
-    return new SqlHandler(new TestableConnectionManager(TestConfigService.getTestConfigService()));
+    return new SqlHandler(new TestableConnectionManager(),
+        TestConfigService.getTestConfigService());
   }
 
   private Region<String, PdxInstance> createRegionWithJDBCLoader(String regionName)
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcWriterIntegrationTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcWriterIntegrationTest.java
index 07f8d6c..86851b9 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcWriterIntegrationTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JdbcWriterIntegrationTest.java
@@ -27,7 +27,6 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
-import com.zaxxer.hikari.HikariDataSource;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -69,12 +68,7 @@ public class JdbcWriterIntegrationTest {
         .setPdxReadSerialized(false).create();
     employees = createRegionWithJDBCSynchronousWriter(REGION_TABLE_NAME);
 
-    HikariDataSource ds = new HikariDataSource();
-    ds.setJdbcUrl(CONNECTION_URL);
-    // ds.setDataSourceClassName("org.apache.derby.jdbc.ClientDataSource");
-    connection = ds.getConnection();
-
-    // connection = DriverManager.getConnection(CONNECTION_URL);
+    connection = DriverManager.getConnection(CONNECTION_URL);
     statement = connection.createStatement();
     statement.execute("Create Table " + REGION_TABLE_NAME
         + " (id varchar(10) primary key not null, name varchar(10), age int)");
@@ -233,7 +227,8 @@ public class JdbcWriterIntegrationTest {
 
   private SqlHandler createSqlHandler()
       throws ConnectionConfigExistsException, RegionMappingExistsException {
-    return new SqlHandler(new TestableConnectionManager(TestConfigService.getTestConfigService()));
+    return new SqlHandler(new TestableConnectionManager(),
+        TestConfigService.getTestConfigService());
   }
 
   private void assertRecordMatchesEmployee(ResultSet resultSet, String key, Employee employee)
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/ConnectionManagerUnitTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/ConnectionManagerUnitTest.java
deleted file mode 100644
index 9c3f6eb..0000000
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/ConnectionManagerUnitTest.java
+++ /dev/null
@@ -1,129 +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.geode.connectors.jdbc.internal;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.api.Assertions.assertThatThrownBy;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.List;
-
-import javax.sql.DataSource;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import org.apache.geode.cache.Operation;
-import org.apache.geode.pdx.PdxInstance;
-import org.apache.geode.test.junit.categories.UnitTest;
-
-@Category(UnitTest.class)
-public class ConnectionManagerUnitTest {
-
-  private static final String REGION_NAME = "testRegion";
-  private static final String CONFIG_NAME = "configName";
-  private JdbcConnectorService configService;
-  private JdbcDataSource dataSource;
-  private ConnectionManager manager;
-  private Connection connection;
-
-  private ConnectionConfiguration connectionConfig;
-
-
-  @Before
-  public void setup() throws Exception {
-    dataSource = mock(JdbcDataSource.class);
-    configService = mock(JdbcConnectorService.class);
-    manager = spy(new ConnectionManager(configService));
-    connection = mock(Connection.class);
-
-    connectionConfig = new ConnectionConfiguration("name", "url", null, null, null);
-
-    doReturn(dataSource).when(manager).buildJdbcDataSource(connectionConfig);
-    doReturn(connection).when(dataSource).getConnection();
-  }
-
-  @Test
-  public void getsCorrectMapping() {
-    manager.getMappingForRegion(REGION_NAME);
-
-    verify(configService).getMappingForRegion(REGION_NAME);
-  }
-
-  @Test
-  public void getsCorrectConnectionConfig() {
-    manager.getConnectionConfig(CONFIG_NAME);
-
-    verify(configService).getConnectionConfig(CONFIG_NAME);
-  }
-
-  @Test
-  public void retrievesANewConnection() throws Exception {
-    Connection returnedConnection = manager.getConnection(connectionConfig);
-
-    assertThat(returnedConnection).isNotNull().isSameAs(connection);
-  }
-
-  @Test
-  public void retrievesSameConnectionForSameConnectionConfig() throws Exception {
-    Connection returnedConnection = manager.getConnection(connectionConfig);
-    Connection secondReturnedConnection = manager.getConnection(connectionConfig);
-
-    assertThat(returnedConnection).isNotNull().isSameAs(connection);
-    assertThat(secondReturnedConnection).isNotNull().isSameAs(connection);
-  }
-
-  @Test
-  public void retrievesDifferentConnectionForEachConfig() throws Exception {
-    Connection secondConnection = mock(Connection.class);
-    JdbcDataSource dataSource2 = mock(JdbcDataSource.class);
-    ConnectionConfiguration secondConnectionConfig =
-        new ConnectionConfiguration("newName", "url", null, null, null);
-    doReturn(dataSource2).when(manager).buildJdbcDataSource(secondConnectionConfig);
-    doReturn(secondConnection).when(dataSource2).getConnection();
-
-    Connection returnedConnection = manager.getConnection(connectionConfig);
-    Connection secondReturnedConnection = manager.getConnection(secondConnectionConfig);
-
-    assertThat(returnedConnection).isNotNull().isSameAs(connection);
-    assertThat(secondReturnedConnection).isNotNull().isSameAs(secondConnection);
-    assertThat(returnedConnection).isNotSameAs(secondReturnedConnection);
-  }
-
-  @Test
-  public void closesAllDataSources() throws Exception {
-    JdbcDataSource dataSource2 = mock(JdbcDataSource.class);
-    ConnectionConfiguration secondConnectionConfig =
-        new ConnectionConfiguration("newName", "url", null, null, null);
-    doReturn(dataSource2).when(manager).buildJdbcDataSource(secondConnectionConfig);
-    manager.getConnection(connectionConfig);
-    manager.getConnection(secondConnectionConfig);
-    manager.close();
-    verify(dataSource).close();
-    verify(dataSource2).close();
-  }
-}
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/DataSourceManagerUnitTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/DataSourceManagerUnitTest.java
new file mode 100644
index 0000000..4f30bb2
--- /dev/null
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/DataSourceManagerUnitTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.geode.connectors.jdbc.internal;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class DataSourceManagerUnitTest {
+
+  private DataSourceManager manager;
+
+  private JdbcDataSource dataSource;
+  private JdbcDataSource dataSource2;
+
+  private ConnectionConfiguration connectionConfig;
+  private ConnectionConfiguration connectionConfig2;
+
+  @Before
+  public void setup() throws Exception {
+    dataSource = mock(JdbcDataSource.class);
+    JdbcDataSourceFactory dataSourceFactory = new JdbcDataSourceFactory() {
+      @Override
+      public JdbcDataSource create(ConnectionConfiguration configuration) {
+        return dataSource;
+      }
+
+    };
+    manager = new DataSourceManager(dataSourceFactory);
+    connectionConfig = new ConnectionConfiguration("dataSource1", "url", null, null, null);
+    connectionConfig2 = new ConnectionConfiguration("dataSource2", "url", null, null, null);
+
+    dataSource2 = mock(JdbcDataSource.class);
+    doThrow(new Exception()).when(dataSource2).close();
+  }
+
+  @Test
+  public void retrievesANewDataSource() throws Exception {
+    JdbcDataSource returnedDataSource = manager.getDataSource(connectionConfig);
+
+    assertThat(returnedDataSource).isNotNull().isSameAs(dataSource);
+  }
+
+  @Test
+  public void retrievesSameDataSourceForSameConnectionConfig() throws Exception {
+    JdbcDataSource returnedDataSource = manager.getDataSource(connectionConfig);
+    JdbcDataSource secondReturnedDataSource = manager.getDataSource(connectionConfig);
+
+    assertThat(returnedDataSource).isNotNull().isSameAs(dataSource);
+    assertThat(secondReturnedDataSource).isNotNull().isSameAs(dataSource);
+  }
+
+  private void registerTwoDataSourceFactory() {
+    JdbcDataSourceFactory dataSourceFactory = new JdbcDataSourceFactory() {
+      @Override
+      public JdbcDataSource create(ConnectionConfiguration configuration) {
+        if (configuration.getName().equals("dataSource1")) {
+          return dataSource;
+        } else if (configuration.getName().equals("dataSource2")) {
+          return dataSource2;
+        } else {
+          throw new IllegalStateException("unexpected " + configuration);
+        }
+      }
+    };
+    manager = new DataSourceManager(dataSourceFactory);
+  }
+
+  @Test
+  public void retrievesDifferentConnectionForEachConfig() throws Exception {
+    registerTwoDataSourceFactory();
+    JdbcDataSource returnedDataSource = manager.getDataSource(connectionConfig);
+    JdbcDataSource secondReturnedDataSource = manager.getDataSource(connectionConfig2);
+
+    assertThat(returnedDataSource).isNotNull().isSameAs(dataSource);
+    assertThat(secondReturnedDataSource).isNotNull().isSameAs(dataSource2);
+    assertThat(returnedDataSource).isNotSameAs(secondReturnedDataSource);
+  }
+
+  @Test
+  public void closesAllDataSources() throws Exception {
+    registerTwoDataSourceFactory();
+    manager.getDataSource(connectionConfig);
+    manager.getDataSource(connectionConfig2);
+    manager.close();
+
+    verify(dataSource).close();
+    verify(dataSource2).close();
+  }
+}
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/SqlHandlerTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/SqlHandlerTest.java
index e464585..bd1462f 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/SqlHandlerTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/SqlHandlerTest.java
@@ -20,6 +20,7 @@ import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyBoolean;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
@@ -62,7 +63,10 @@ public class SqlHandlerTest {
   @Rule
   public ExpectedException thrown = ExpectedException.none();
 
-  private ConnectionManager manager;
+  private DataSourceManager manager;
+  private JdbcDataSource dataSource;
+  private ConnectionConfiguration connectionConfig;
+  private InternalJdbcConnectorService connectorService;
   private TableKeyColumnManager tableKeyColumnManager;
   private Connection connection;
   private Region region;
@@ -75,33 +79,33 @@ public class SqlHandlerTest {
 
   @Before
   public void setup() throws Exception {
-    manager = mock(ConnectionManager.class);
+    manager = mock(DataSourceManager.class);
+    dataSource = mock(JdbcDataSource.class);
+    connectionConfig = mock(ConnectionConfiguration.class);
+    when(connectionConfig.getUrl()).thenReturn("fake:url");
     region = mock(Region.class);
     cache = mock(InternalCache.class);
     connection = mock(Connection.class);
     when(region.getRegionService()).thenReturn(cache);
     tableKeyColumnManager = mock(TableKeyColumnManager.class);
     when(tableKeyColumnManager.getKeyColumnName(connection, TABLE_NAME)).thenReturn(KEY_COLUMN);
-    handler = new SqlHandler(manager, tableKeyColumnManager);
-    key = new Object();
+    connectorService = mock(InternalJdbcConnectorService.class);
+    handler = new SqlHandler(manager, tableKeyColumnManager, connectorService);
+    key = "key";
     value = mock(PdxInstanceImpl.class);
     when(value.getPdxType()).thenReturn(mock(PdxType.class));
 
-    ConnectionConfiguration connectionConfig = mock(ConnectionConfiguration.class);
-    when(manager.getConnectionConfig(any())).thenReturn(connectionConfig);
+    when(connectorService.getConnectionConfig(any())).thenReturn(connectionConfig);
 
     regionMapping = mock(RegionMapping.class);
     when(regionMapping.getRegionName()).thenReturn(REGION_NAME);
     when(regionMapping.getTableName()).thenReturn(TABLE_NAME);
     when(regionMapping.getRegionToTableName()).thenReturn(TABLE_NAME);
-    when(manager.getMappingForRegion(any())).thenReturn(regionMapping);
+    when(connectorService.getMappingForRegion(any())).thenReturn(regionMapping);
 
-    // List<ColumnValue> columnList = new ArrayList<>();
-    // columnList.add(new ColumnValue(true, COLUMN_NAME_1, COLUMN_VALUE_1));
-    // columnList.add(new ColumnValue(true, COLUMN_NAME_2, COLUMN_VALUE_2));
-    // when(manager.getColumnToValueList(any(), any(), any(), any(), any())).thenReturn(columnList);
 
-    when(manager.getConnection(any())).thenReturn(this.connection);
+    when(manager.getDataSource(any())).thenReturn(this.dataSource);
+    when(dataSource.getConnection()).thenReturn(this.connection);
 
     statement = mock(PreparedStatement.class);
     when(this.connection.prepareStatement(any())).thenReturn(statement);
@@ -430,4 +434,13 @@ public class SqlHandlerTest {
 
     return primaryKeys;
   }
+
+  @Test
+  public void handlesSQLExceptionFromGetConnection() throws Exception {
+    doThrow(new SQLException("test exception")).when(dataSource).getConnection();
+
+    assertThatThrownBy(() -> handler.getConnection(connectionConfig))
+        .isInstanceOf(IllegalStateException.class).hasMessage("Could not connect to fake:url");
+  }
+
 }
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TestableConnectionManager.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TestableConnectionManager.java
index 01f73f8..4436f41 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TestableConnectionManager.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TestableConnectionManager.java
@@ -14,9 +14,9 @@
  */
 package org.apache.geode.connectors.jdbc.internal;
 
-public class TestableConnectionManager extends ConnectionManager {
+public class TestableConnectionManager extends DataSourceManager {
 
-  public TestableConnectionManager(InternalJdbcConnectorService configService) {
-    super(configService);
+  public TestableConnectionManager() {
+    super(new HikariJdbcDataSourceFactory());
   }
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <commits@geode.apache.org>'].

Mime
View raw message