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 23:16:14 UTC

[geode] branch feature/GEODE-3781 updated: code coverage tests

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 9094ced  code coverage tests
9094ced is described below

commit 9094ced978f7b221cfff01ec1b5953ee61fdff59
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Mon Oct 30 16:15:59 2017 -0700

    code coverage tests
---
 .../apache/geode/connectors/jdbc/JDBCManager.java  |   5 +-
 .../connectors/jdbc/JDBCManagerUnitTestTest.java   | 220 ++++++++++++++++++++-
 2 files changed, 221 insertions(+), 4 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 4e743bc..991dd47 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
@@ -67,7 +67,8 @@ public class JDBCManager {
     List<ColumnValue> columnList = getColumnToValueList(tableName, key, value, operation);
     int updateCount = executeWrite(columnList, tableName, operation, pdxTypeId, false);
     if (operation.isDestroy()) {
-      // TODO: should we check updateCount here?
+      // TODO: should we check updateCount here? Probably not. It is possible we have nothing in the
+      // table to destroy.
       return;
     }
     if (updateCount <= 0) {
@@ -305,7 +306,7 @@ public class JDBCManager {
     });
   }
 
-  private String computeKeyColumnName(String tableName) {
+  String computeKeyColumnName(String tableName) {
     // TODO: check config for key column
     Connection con = getConnection();
     try {
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 aeca590..0f3b101 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
@@ -86,8 +86,16 @@ public class JDBCManagerUnitTestTest {
 
   public class TestableUpsertJDBCManager extends JDBCManager {
 
+    final int upsertReturn;
+
     TestableUpsertJDBCManager(JDBCConfiguration config) {
       super(config);
+      upsertReturn = 1;
+    }
+
+    TestableUpsertJDBCManager(JDBCConfiguration config, int upsertReturn) {
+      super(config);
+      this.upsertReturn = upsertReturn;
     }
 
     @Override
@@ -107,7 +115,7 @@ public class JDBCManagerUnitTestTest {
       preparedStatement = mock(PreparedStatement.class);
       preparedStatement2 = mock(PreparedStatement.class);
       when(preparedStatement.getUpdateCount()).thenReturn(0);
-      when(preparedStatement2.getUpdateCount()).thenReturn(1);
+      when(preparedStatement2.getUpdateCount()).thenReturn(this.upsertReturn);
 
       connection = mock(Connection.class);
       when(connection.getMetaData()).thenReturn(metaData);
@@ -117,6 +125,48 @@ public class JDBCManagerUnitTestTest {
     }
   }
 
+  public class TestableJDBCManagerWithResultSets extends JDBCManager {
+
+    private ResultSet tableResults;
+    private ResultSet primaryKeyResults;
+
+    TestableJDBCManagerWithResultSets(JDBCConfiguration config, ResultSet tableResults,
+        ResultSet primaryKeyResults) {
+      super(config);
+      this.tableResults = tableResults;
+      this.primaryKeyResults = primaryKeyResults;
+    }
+
+    @Override
+    protected Connection createConnection(String url) throws SQLException {
+      if (primaryKeyResults == null) {
+        primaryKeyResults = mock(ResultSet.class);
+        when(primaryKeyResults.next()).thenReturn(true, false);
+        when(primaryKeyResults.getString("COLUMN_NAME")).thenReturn(ID_COLUMN_NAME);
+      }
+
+      if (tableResults == null) {
+        tableResults = mock(ResultSet.class);
+        when(tableResults.next()).thenReturn(true, false);
+        when(tableResults.getString("TABLE_NAME")).thenReturn(regionName.toUpperCase());
+      }
+
+      DatabaseMetaData metaData = mock(DatabaseMetaData.class);
+      when(metaData.getPrimaryKeys(null, null, regionName.toUpperCase()))
+          .thenReturn(primaryKeyResults);
+      when(metaData.getTables(any(), any(), any(), any())).thenReturn(tableResults);
+
+      preparedStatement = mock(PreparedStatement.class);
+      when(preparedStatement.getUpdateCount()).thenReturn(1);
+
+      connection = mock(Connection.class);
+      when(connection.getMetaData()).thenReturn(metaData);
+      when(connection.prepareStatement(any())).thenReturn(preparedStatement);
+
+      return connection;
+    }
+  }
+
   @Before
   public void setUp() throws Exception {}
 
@@ -135,6 +185,16 @@ public class JDBCManagerUnitTestTest {
     this.mgr = new TestableUpsertJDBCManager(createConfiguration("java.lang.String", "fakeURL"));
   }
 
+  private void createUpsertManager(int upsertReturn) {
+    this.mgr = new TestableUpsertJDBCManager(createConfiguration("java.lang.String", "fakeURL"),
+        upsertReturn);
+  }
+
+  private void createManager(ResultSet tableNames, ResultSet primaryKeys) {
+    this.mgr = new TestableJDBCManagerWithResultSets(
+        createConfiguration("java.lang.String", "fakeURL"), tableNames, primaryKeys);
+  }
+
   private JDBCConfiguration createConfiguration(String driver, String url) {
     Properties props = new Properties();
     props.setProperty("url", url);
@@ -154,6 +214,32 @@ public class JDBCManagerUnitTestTest {
     verify(this.connection).prepareStatement(sqlCaptor.capture());
     assertThat(sqlCaptor.getValue()).isEqualTo("INSERT INTO " + regionName + "(" + NAME_COLUMN_NAME
         + ", " + AGE_COLUMN_NAME + ", " + ID_COLUMN_NAME + ") VALUES (?,?,?)");
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(3)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
+  }
+
+  @Test
+  public void verifySimpleCreateWithIdField() throws SQLException {
+    createDefaultManager();
+    GemFireCacheImpl cache = Fakes.cache();
+    Region region = Fakes.region(regionName, cache);
+    PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21, true);
+    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 (?,?,?)");
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(3)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
   }
 
   @Test
@@ -168,6 +254,12 @@ public class JDBCManagerUnitTestTest {
     verify(this.connection).prepareStatement(sqlCaptor.capture());
     assertThat(sqlCaptor.getValue()).isEqualTo("UPDATE " + regionName + " SET " + NAME_COLUMN_NAME
         + " = ?, " + AGE_COLUMN_NAME + " = ? WHERE " + ID_COLUMN_NAME + " = ?");
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(3)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
   }
 
   @Test
@@ -181,6 +273,10 @@ public class JDBCManagerUnitTestTest {
     verify(this.connection).prepareStatement(sqlCaptor.capture());
     assertThat(sqlCaptor.getValue())
         .isEqualTo("DELETE FROM " + regionName + " WHERE " + ID_COLUMN_NAME + " = ?");
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(1)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("1");
   }
 
   @Test
@@ -194,11 +290,31 @@ public class JDBCManagerUnitTestTest {
     this.mgr.write(region, Operation.CREATE, "2", pdx2);
     verify(this.preparedStatement, times(2)).execute();
     verify(this.connection).prepareStatement(any());
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(6)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
+    assertThat(allObjects.get(3)).isEqualTo("Emp2");
+    assertThat(allObjects.get(4)).isEqualTo(55);
+    assertThat(allObjects.get(5)).isEqualTo("2");
   }
 
   private PdxInstanceImpl mockPdxInstance(String name, int age) {
+    return mockPdxInstance(name, age, false);
+  }
+
+  private PdxInstanceImpl mockPdxInstance(String name, int age, boolean addId) {
     PdxInstanceImpl pdxInstance = mock(PdxInstanceImpl.class);
-    when(pdxInstance.getFieldNames()).thenReturn(Arrays.asList(NAME_COLUMN_NAME, AGE_COLUMN_NAME));
+    if (addId) {
+      when(pdxInstance.getFieldNames())
+          .thenReturn(Arrays.asList(NAME_COLUMN_NAME, AGE_COLUMN_NAME, ID_COLUMN_NAME));
+      when(pdxInstance.getField(ID_COLUMN_NAME)).thenReturn("bogusId");
+    } else {
+      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);
@@ -228,6 +344,17 @@ public class JDBCManagerUnitTestTest {
   }
 
   @Test
+  public void verifyNoTableForRegion() throws SQLException {
+    createDefaultManager();
+    GemFireCacheImpl cache = Fakes.cache();
+    Region region = Fakes.region("badRegion", cache);
+    catchException(this.mgr).write(region, Operation.DESTROY, "1", null);
+    assertThat((Exception) caughtException()).isInstanceOf(IllegalStateException.class);
+    assertThat(caughtException().getMessage())
+        .isEqualTo("no table was found that matches badRegion");
+  }
+
+  @Test
   public void verifyInsertUpdate() throws SQLException {
     createUpsertManager();
     GemFireCacheImpl cache = Fakes.cache();
@@ -243,6 +370,17 @@ public class JDBCManagerUnitTestTest {
         + AGE_COLUMN_NAME + ", " + ID_COLUMN_NAME + ") VALUES (?,?,?)");
     assertThat(allArgs.get(1)).isEqualTo("UPDATE " + regionName + " SET " + NAME_COLUMN_NAME
         + " = ?, " + AGE_COLUMN_NAME + " = ? WHERE " + ID_COLUMN_NAME + " = ?");
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(3)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
+    verify(this.preparedStatement2, times(3)).setObject(anyInt(), objectCaptor.capture());
+    allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
   }
 
   @Test
@@ -261,5 +399,83 @@ public class JDBCManagerUnitTestTest {
         + " = ?, " + AGE_COLUMN_NAME + " = ? WHERE " + ID_COLUMN_NAME + " = ?");
     assertThat(allArgs.get(1)).isEqualTo("INSERT INTO " + regionName + "(" + NAME_COLUMN_NAME + ", "
         + AGE_COLUMN_NAME + ", " + ID_COLUMN_NAME + ") VALUES (?,?,?)");
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(3)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
+    verify(this.preparedStatement2, times(3)).setObject(anyInt(), objectCaptor.capture());
+    allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
+  }
+
+  @Test
+  public void verifyInsertUpdateThatUpdatesNothing() throws SQLException {
+    createUpsertManager(0);
+    GemFireCacheImpl cache = Fakes.cache();
+    Region region = Fakes.region(regionName, cache);
+    PdxInstanceImpl pdx1 = mockPdxInstance("Emp1", 21);
+    catchException(this.mgr).write(region, Operation.CREATE, "1", pdx1);
+    assertThat((Exception) caughtException()).isInstanceOf(IllegalStateException.class);
+    assertThat(caughtException().getMessage()).isEqualTo("Unexpected updateCount 0");
+    verify(this.preparedStatement).execute();
+    verify(this.preparedStatement2).execute();
+    ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
+    verify(this.connection, times(2)).prepareStatement(sqlCaptor.capture());
+    List<String> allArgs = sqlCaptor.getAllValues();
+    assertThat(allArgs.get(0)).isEqualTo("INSERT INTO " + regionName + "(" + NAME_COLUMN_NAME + ", "
+        + AGE_COLUMN_NAME + ", " + ID_COLUMN_NAME + ") VALUES (?,?,?)");
+    assertThat(allArgs.get(1)).isEqualTo("UPDATE " + regionName + " SET " + NAME_COLUMN_NAME
+        + " = ?, " + AGE_COLUMN_NAME + " = ? WHERE " + ID_COLUMN_NAME + " = ?");
+    ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class);
+    verify(this.preparedStatement, times(3)).setObject(anyInt(), objectCaptor.capture());
+    List<Object> allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
+    verify(this.preparedStatement2, times(3)).setObject(anyInt(), objectCaptor.capture());
+    allObjects = objectCaptor.getAllValues();
+    assertThat(allObjects.get(0)).isEqualTo("Emp1");
+    assertThat(allObjects.get(1)).isEqualTo(21);
+    assertThat(allObjects.get(2)).isEqualTo("1");
+  }
+
+  @Test
+  public void twoTablesOfSameName() throws SQLException {
+    ResultSet primaryKeys = null;
+    ResultSet tables = mock(ResultSet.class);
+    when(tables.next()).thenReturn(true, true, false);
+    when(tables.getString("TABLE_NAME")).thenReturn(regionName.toUpperCase());
+    createManager(tables, primaryKeys);
+    catchException(this.mgr).computeKeyColumnName(regionName);
+    assertThat((Exception) caughtException()).isInstanceOf(IllegalStateException.class);
+    assertThat(caughtException().getMessage()).isEqualTo("Duplicate tables that match region name");
+  }
+
+  @Test
+  public void noPrimaryKeyOnTable() throws SQLException {
+    ResultSet tables = null;
+    ResultSet primaryKeys = mock(ResultSet.class);
+    when(primaryKeys.next()).thenReturn(false);
+    createManager(tables, primaryKeys);
+    catchException(this.mgr).computeKeyColumnName(regionName);
+    assertThat((Exception) caughtException()).isInstanceOf(IllegalStateException.class);
+    assertThat(caughtException().getMessage())
+        .isEqualTo("The table " + regionName + " does not have a primary key column.");
+  }
+
+  @Test
+  public void twoPrimaryKeysOnTable() throws SQLException {
+    ResultSet tables = null;
+    ResultSet primaryKeys = mock(ResultSet.class);
+    when(primaryKeys.next()).thenReturn(true, true, false);
+    createManager(tables, primaryKeys);
+    catchException(this.mgr).computeKeyColumnName(regionName);
+    assertThat((Exception) caughtException()).isInstanceOf(IllegalStateException.class);
+    assertThat(caughtException().getMessage())
+        .isEqualTo("The table " + regionName + " has more than one primary key column.");
   }
 }

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