You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2017/10/27 22:12:14 UTC

[geode] branch feature/GEODE-3781 updated: started unit test for JDBCManager

This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/feature/GEODE-3781 by this push:
     new 6c6ea56  started unit test for JDBCManager
6c6ea56 is described below

commit 6c6ea56bfa9fc1987e1ca87d93143e0acd2a77df
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Fri Oct 27 15:11:53 2017 -0700

    started unit test for JDBCManager
---
 .../apache/geode/connectors/jdbc/JDBCManager.java  |   6 +-
 .../connectors/jdbc/JDBCManagerUnitTestTest.java   | 111 +++++++++++++++++++++
 .../java/org/apache/geode/test/fake/Fakes.java     |   5 +
 3 files changed, 121 insertions(+), 1 deletion(-)

diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java
index afc59b2..0b2fc70 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/JDBCManager.java
@@ -191,7 +191,7 @@ public class JDBCManager {
       }
     }
     try {
-      result = DriverManager.getConnection(this.config.getURL());
+      result = createConnection(this.config.getURL());
     } catch (SQLException e) {
       // TODO: consider a different exception
       throw new IllegalStateException("Could not connect to " + this.config.getURL(), e);
@@ -200,6 +200,10 @@ public class JDBCManager {
     return result;
   }
 
+  protected Connection createConnection(String url) throws SQLException {
+    return DriverManager.getConnection(url);
+  }
+
   private static class StatementKey {
     private final int pdxTypeId;
     private final Operation operation;
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCManagerUnitTestTest.java b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCManagerUnitTestTest.java
new file mode 100644
index 0000000..4098cb6
--- /dev/null
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/JDBCManagerUnitTestTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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;
+
+import static org.assertj.core.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Arrays;
+import java.util.Properties;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import org.apache.geode.cache.Operation;
+import org.apache.geode.cache.Region;
+import org.apache.geode.internal.cache.GemFireCacheImpl;
+import org.apache.geode.pdx.PdxInstance;
+import org.apache.geode.pdx.PdxInstanceFactory;
+import org.apache.geode.pdx.internal.PdxInstanceImpl;
+import org.apache.geode.pdx.internal.PdxType;
+import org.apache.geode.test.fake.Fakes;
+import org.apache.geode.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class JDBCManagerUnitTestTest {
+
+  private JDBCManager mgr;
+  private String regionName = "jdbcRegion";
+
+  public class TestableJDBCManager extends JDBCManager {
+
+    TestableJDBCManager(JDBCConfiguration config) {
+      super(config);
+    }
+
+    @Override
+    protected Connection createConnection(String url) throws SQLException {
+      ResultSet rsKeys = mock(ResultSet.class);
+      when(rsKeys.next()).thenReturn(true, false);
+      when(rsKeys.getString("COLUMN_NAME")).thenReturn("ID");
+
+      ResultSet rs = mock(ResultSet.class);
+      when(rs.next()).thenReturn(true, false);
+      when(rs.getString("TABLE_NAME")).thenReturn(regionName.toUpperCase());
+
+      DatabaseMetaData metaData = mock(DatabaseMetaData.class);
+      when(metaData.getPrimaryKeys(null, null, regionName.toUpperCase())).thenReturn(rsKeys);
+      when(metaData.getTables(any(), any(), any(), any())).thenReturn(rs);
+
+      PreparedStatement pstmt = mock(PreparedStatement.class);
+      when(pstmt.getUpdateCount()).thenReturn(1);
+
+      Connection conn = mock(Connection.class);
+      when(conn.getMetaData()).thenReturn(metaData);
+      when(conn.prepareStatement(any())).thenReturn(pstmt, null);
+
+      return conn;
+    }
+
+  }
+
+  @Before
+  public void setUp() throws Exception {
+    Properties props = new Properties();
+    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
+    String connectionURL = "jdbc:derby:memory:DerbyTestDB;create=true";
+    props.setProperty("url", connectionURL);
+    props.setProperty("driver", driver);
+    JDBCConfiguration config = new JDBCConfiguration(props);
+    this.mgr = new TestableJDBCManager(config);
+  }
+
+  @After
+  public void tearDown() throws Exception {}
+
+  @Test
+  public void testWritingCreate() {
+    GemFireCacheImpl cache = Fakes.cache();
+    Region region = Fakes.region(regionName, cache);
+    PdxInstanceImpl pdx1 = mock(PdxInstanceImpl.class);
+    when(pdx1.getFieldNames()).thenReturn(Arrays.asList("name", "age"));
+    when(pdx1.getField("name")).thenReturn("Emp1");
+    when(pdx1.getField("age")).thenReturn(21);
+    PdxType pdxType = mock(PdxType.class);
+    when(pdxType.getTypeId()).thenReturn(1);
+    when(pdx1.getPdxType()).thenReturn(pdxType);
+    this.mgr.write(region, Operation.CREATE, "1", pdx1);
+  }
+
+}
diff --git a/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java b/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java
index 5a86fa7..5988bc3 100644
--- a/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java
+++ b/geode-core/src/test/java/org/apache/geode/test/fake/Fakes.java
@@ -35,6 +35,7 @@ import org.apache.geode.distributed.internal.membership.InternalDistributedMembe
 import org.apache.geode.internal.cache.CachePerfStats;
 import org.apache.geode.internal.cache.GemFireCacheImpl;
 import org.apache.geode.internal.security.SecurityService;
+import org.apache.geode.pdx.PdxInstanceFactory;
 
 /**
  * Factory methods for fake objects for use in test.
@@ -62,6 +63,7 @@ public class Fakes {
     InternalDistributedSystem system = mock(InternalDistributedSystem.class);
     DistributionConfig config = mock(DistributionConfig.class);
     DistributionManager distributionManager = mock(DistributionManager.class);
+    PdxInstanceFactory pdxInstanceFactory = mock(PdxInstanceFactory.class);
     CancelCriterion systemCancelCriterion = mock(CancelCriterion.class);
     DSClock clock = mock(DSClock.class);
     LogWriter logger = mock(LogWriter.class);
@@ -81,6 +83,7 @@ public class Fakes {
     when(cache.getCancelCriterion()).thenReturn(systemCancelCriterion);
     when(cache.getCachePerfStats()).thenReturn(mock(CachePerfStats.class));
     when(cache.getSecurityService()).thenReturn(mock(SecurityService.class));
+    when(cache.createPdxInstanceFactory(any())).thenReturn(pdxInstanceFactory);
 
     when(system.getDistributedMember()).thenReturn(member);
     when(system.getConfig()).thenReturn(config);
@@ -120,6 +123,8 @@ public class Fakes {
     when(attributes.getDataPolicy()).thenReturn(policy);
     when(region.getCache()).thenReturn(cache);
     when(region.getRegionService()).thenReturn(cache);
+    when(region.getName()).thenReturn(name);
+    when(region.getFullPath()).thenReturn("/" + name);
     return region;
   }
 

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