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/30 21:19:39 UTC

[geode] branch feature/GEODE-3781 updated (83566d4 -> 2bb4416)

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

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


    from 83566d4  minor cleanup
     new 7dc90f9  more mocking
     new 2bb4416  added test for bad driver

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/geode/connectors/jdbc/JDBCManager.java  |   4 +-
 .../connectors/jdbc/JDBCManagerUnitTestTest.java   | 112 +++++++++++++++++----
 2 files changed, 93 insertions(+), 23 deletions(-)

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

[geode] 01/02: more mocking

Posted by ds...@apache.org.
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

commit 7dc90f992829495ecb86ce60d14bc470e5cee0f0
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Mon Oct 30 14:02:48 2017 -0700

    more mocking
---
 .../connectors/jdbc/JDBCManagerUnitTestTest.java   | 80 ++++++++++++++++++----
 1 file changed, 65 insertions(+), 15 deletions(-)

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
index 4098cb6..d067898 100644
--- 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
@@ -31,6 +31,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
+import org.mockito.ArgumentCaptor;
 
 import org.apache.geode.cache.Operation;
 import org.apache.geode.cache.Region;
@@ -47,6 +48,12 @@ public class JDBCManagerUnitTestTest {
 
   private JDBCManager mgr;
   private String regionName = "jdbcRegion";
+  Connection connection;
+  PreparedStatement preparedStatement;
+  
+  private static final String ID_COLUMN_NAME = "ID";
+  private static final String NAME_COLUMN_NAME = "name";
+  private static final String AGE_COLUMN_NAME = "age";
 
   public class TestableJDBCManager extends JDBCManager {
 
@@ -58,7 +65,7 @@ public class JDBCManagerUnitTestTest {
     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");
+      when(rsKeys.getString("COLUMN_NAME")).thenReturn(ID_COLUMN_NAME);
 
       ResultSet rs = mock(ResultSet.class);
       when(rs.next()).thenReturn(true, false);
@@ -68,14 +75,14 @@ public class JDBCManagerUnitTestTest {
       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);
+      preparedStatement = mock(PreparedStatement.class);
+      when(preparedStatement.getUpdateCount()).thenReturn(1);
 
-      Connection conn = mock(Connection.class);
-      when(conn.getMetaData()).thenReturn(metaData);
-      when(conn.prepareStatement(any())).thenReturn(pstmt, null);
+      connection = mock(Connection.class);
+      when(connection.getMetaData()).thenReturn(metaData);
+      when(connection.prepareStatement(any())).thenReturn(preparedStatement, null);
 
-      return conn;
+      return connection;
     }
 
   }
@@ -95,17 +102,60 @@ public class JDBCManagerUnitTestTest {
   public void tearDown() throws Exception {}
 
   @Test
-  public void testWritingCreate() {
+  public void verifySimpleCreateCallsExecute() throws SQLException {
     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);
+    PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21);
     this.mgr.write(region, Operation.CREATE, "1", pdx1);
+    verify(this.preparedStatement).execute();
+    ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
+    verify(this.connection).prepareStatement(sqlCaptor.capture());
+    assertThat(sqlCaptor.getValue()).isEqualTo("INSERT INTO " + regionName + "(" + NAME_COLUMN_NAME + ", " + AGE_COLUMN_NAME + ", " + ID_COLUMN_NAME + ") VALUES (?,?,?)");
   }
 
+  @Test
+  public void verifySimpleUpdateCallsExecute() throws SQLException {
+    GemFireCacheImpl cache = Fakes.cache();
+    Region region = Fakes.region(regionName, cache);
+    PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21);
+    this.mgr.write(region, Operation.UPDATE, "1", pdx1);
+    verify(this.preparedStatement).execute();
+    ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
+    verify(this.connection).prepareStatement(sqlCaptor.capture());
+    assertThat(sqlCaptor.getValue()).isEqualTo("UPDATE " + regionName + " SET " + NAME_COLUMN_NAME + " = ?, " + AGE_COLUMN_NAME + " = ? WHERE " + ID_COLUMN_NAME + " = ?");
+  }
+  
+  @Test
+  public void verifySimpleDestroyCallsExecute() throws SQLException {
+    GemFireCacheImpl cache = Fakes.cache();
+    Region region = Fakes.region(regionName, cache);
+    this.mgr.write(region, Operation.DESTROY, "1", null);
+    verify(this.preparedStatement).execute();
+    ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
+    verify(this.connection).prepareStatement(sqlCaptor.capture());
+    assertThat(sqlCaptor.getValue()).isEqualTo("DELETE FROM " + regionName + " WHERE " + ID_COLUMN_NAME + " = ?");
+  }
+  
+  @Test
+  public void verifyTwoCreatesReuseSameStatement() throws SQLException {
+    GemFireCacheImpl cache = Fakes.cache();
+    Region region = Fakes.region(regionName, cache);
+    PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21);
+    PdxInstanceImpl pdx2 = mockPdxInstance("Emp2", 55);
+    this.mgr.write(region, Operation.CREATE, "1", pdx1);
+    this.mgr.write(region, Operation.CREATE, "2", pdx2);
+    verify(this.preparedStatement, times(2)).execute();
+    verify(this.connection).prepareStatement(any());
+  }
+
+  private PdxInstanceImpl mockPdxInstance(String name, int age) {
+    PdxInstanceImpl pdxInstance = mock(PdxInstanceImpl.class);
+    when(pdxInstance.getFieldNames()).thenReturn(Arrays.asList(NAME_COLUMN_NAME, AGE_COLUMN_NAME));
+    when(pdxInstance.getField(NAME_COLUMN_NAME)).thenReturn(name);
+    when(pdxInstance.getField(AGE_COLUMN_NAME)).thenReturn(age);
+    PdxType pdxType = mock(PdxType.class);
+    when(pdxType.getTypeId()).thenReturn(1);
+    when(pdxInstance.getPdxType()).thenReturn(pdxType);
+    return pdxInstance;
+  }
 }

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

[geode] 02/02: added test for bad driver

Posted by ds...@apache.org.
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

commit 2bb441648d30065b29eaba916cda0f45c964f8a1
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Mon Oct 30 14:19:23 2017 -0700

    added test for bad driver
---
 .../apache/geode/connectors/jdbc/JDBCManager.java  |  4 +--
 .../connectors/jdbc/JDBCManagerUnitTestTest.java   | 34 +++++++++++++++++-----
 2 files changed, 29 insertions(+), 9 deletions(-)

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 69d0cdb..4160887 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
@@ -173,7 +173,7 @@ public class JDBCManager {
     return columnNames.append(columnValues).toString();
   }
 
-  private Connection getConnection() {
+  Connection getConnection() {
     Connection result = this.conn;
     try {
       if (result != null && !result.isClosed()) {
@@ -188,7 +188,7 @@ public class JDBCManager {
         Class.forName(this.config.getDriver());
       } catch (ClassNotFoundException e) {
         // TODO: consider a different exception
-        throw new IllegalStateException("Driver class " + this.config.getDriver() + " not found",
+        throw new IllegalStateException("Driver class \"" + this.config.getDriver() + "\" not found",
             e);
       }
     }
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
index d067898..661d1b6 100644
--- 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
@@ -16,6 +16,8 @@ package org.apache.geode.connectors.jdbc;
 
 import static org.assertj.core.api.Assertions.*;
 import static org.mockito.Mockito.*;
+import static com.googlecode.catchexception.CatchException.*;
+import static com.googlecode.catchexception.CatchException.caughtException;
 
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
@@ -89,20 +91,27 @@ public class JDBCManagerUnitTestTest {
 
   @Before
   public void setUp() throws Exception {
+  }
+
+  @After
+  public void tearDown() throws Exception {
+  }
+
+  private void createManager(String driver, String url) {
     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("url", url);
     props.setProperty("driver", driver);
     JDBCConfiguration config = new JDBCConfiguration(props);
     this.mgr = new TestableJDBCManager(config);
   }
-
-  @After
-  public void tearDown() throws Exception {}
-
+  
+  private void createDefaultManager() {
+    createManager("java.lang.String", "fakeURL");
+  }
+  
   @Test
   public void verifySimpleCreateCallsExecute() throws SQLException {
+    createDefaultManager();
     GemFireCacheImpl cache = Fakes.cache();
     Region region = Fakes.region(regionName, cache);
     PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21);
@@ -115,6 +124,7 @@ public class JDBCManagerUnitTestTest {
 
   @Test
   public void verifySimpleUpdateCallsExecute() throws SQLException {
+    createDefaultManager();
     GemFireCacheImpl cache = Fakes.cache();
     Region region = Fakes.region(regionName, cache);
     PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21);
@@ -127,6 +137,7 @@ public class JDBCManagerUnitTestTest {
   
   @Test
   public void verifySimpleDestroyCallsExecute() throws SQLException {
+    createDefaultManager();
     GemFireCacheImpl cache = Fakes.cache();
     Region region = Fakes.region(regionName, cache);
     this.mgr.write(region, Operation.DESTROY, "1", null);
@@ -138,6 +149,7 @@ public class JDBCManagerUnitTestTest {
   
   @Test
   public void verifyTwoCreatesReuseSameStatement() throws SQLException {
+    createDefaultManager();
     GemFireCacheImpl cache = Fakes.cache();
     Region region = Fakes.region(regionName, cache);
     PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21);
@@ -158,4 +170,12 @@ public class JDBCManagerUnitTestTest {
     when(pdxInstance.getPdxType()).thenReturn(pdxType);
     return pdxInstance;
   }
+  
+  @Test
+  public void verifyMissingDriverClass() {
+    createManager("non existent driver", "fakeURL");
+    catchException(this.mgr).getConnection();
+    assertThat((Exception)caughtException()).isInstanceOf(IllegalStateException.class);
+    assertThat(caughtException().getMessage()).isEqualTo("Driver class \"non existent driver\" not found");
+  }
 }

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