geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject [geode] 02/02: added unit test for TableMetaData. Catalog and Schema now passed to TableMetaData and used to create the table path for sql statements.
Date Wed, 09 Jan 2019 22:43:56 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit f4f54fea6c17ff68a9ec4ab254832636124f2315
Author: Darrel Schneider <dschneider@pivotal.io>
AuthorDate: Wed Jan 9 14:42:55 2019 -0800

    added unit test for TableMetaData.
    Catalog and Schema now passed to TableMetaData and used
    to create the table path for sql statements.
---
 .../connectors/jdbc/internal/TableMetaData.java    |  24 ++-
 .../jdbc/internal/TableMetaDataManager.java        |  25 ++--
 .../jdbc/internal/TableMetaDataTest.java           | 162 +++++++++++++++++++++
 3 files changed, 192 insertions(+), 19 deletions(-)

diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaData.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaData.java
index affd8a6..71df7e1 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaData.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaData.java
@@ -27,14 +27,32 @@ public class TableMetaData implements TableMetaDataView {
   private final Map<String, Integer> columnNameToTypeMap;
   private final String identifierQuoteString;
 
-  public TableMetaData(String tableName, List<String> keyColumnNames, String quoteString,
-      Map<String, Integer> dataTypes) {
-    this.quotedTablePath = quoteString + tableName + quoteString;
+  public TableMetaData(String catalogName, String schemaName, String tableName,
+      List<String> keyColumnNames, String quoteString, Map<String, Integer> dataTypes)
{
+    if (quoteString == null) {
+      quoteString = "";
+    }
+    this.quotedTablePath = createQuotedTablePath(catalogName, schemaName, tableName, quoteString);
     this.keyColumnNames = keyColumnNames;
     this.columnNameToTypeMap = dataTypes;
     this.identifierQuoteString = quoteString;
   }
 
+  private static String createQuotedTablePath(String catalogName, String schemaName,
+      String tableName, String quote) {
+    StringBuilder builder = new StringBuilder();
+    appendPrefix(builder, catalogName, quote);
+    appendPrefix(builder, schemaName, quote);
+    builder.append(quote).append(tableName).append(quote);
+    return builder.toString();
+  }
+
+  private static void appendPrefix(StringBuilder builder, String prefix, String quote) {
+    if (prefix != null && !prefix.isEmpty()) {
+      builder.append(quote).append(prefix).append(quote).append('.');
+    }
+  }
+
   @Override
   public String getQuotedTablePath() {
     return quotedTablePath;
diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManager.java
b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManager.java
index d8ec81a..5b3ccf9 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManager.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataManager.java
@@ -64,29 +64,22 @@ public class TableMetaDataManager {
       String tableName, RegionMapping regionMapping) {
     try {
       DatabaseMetaData metaData = connection.getMetaData();
-      String catalogFilter = getCatalogNameFromMetaData(metaData, regionMapping);
-      String schemaFilter = getSchemaNameFromMetaData(metaData, regionMapping, catalogFilter);
+      String realCatalogName = getCatalogNameFromMetaData(metaData, regionMapping);
+      String realSchemaName = getSchemaNameFromMetaData(metaData, regionMapping, realCatalogName);
       String realTableName =
-          getTableNameFromMetaData(metaData, catalogFilter, schemaFilter, tableName);
-      List<String> keys = getPrimaryKeyColumnNamesFromMetaData(metaData, catalogFilter,
-          schemaFilter, realTableName, regionMapping.getIds());
-      String quoteString = getQuoteStringFromMetaData(metaData);
+          getTableNameFromMetaData(metaData, realCatalogName, realSchemaName, tableName);
+      List<String> keys = getPrimaryKeyColumnNamesFromMetaData(metaData, realCatalogName,
+          realSchemaName, realTableName, regionMapping.getIds());
+      String quoteString = metaData.getIdentifierQuoteString();
       Map<String, Integer> dataTypes =
-          getDataTypesFromMetaData(metaData, catalogFilter, schemaFilter, realTableName);
-      return new TableMetaData(realTableName, keys, quoteString, dataTypes);
+          getDataTypesFromMetaData(metaData, realCatalogName, realSchemaName, realTableName);
+      return new TableMetaData(realCatalogName, realSchemaName, realTableName, keys, quoteString,
+          dataTypes);
     } catch (SQLException e) {
       throw JdbcConnectorException.createException(e);
     }
   }
 
-  private String getQuoteStringFromMetaData(DatabaseMetaData metaData) throws SQLException
{
-    String quoteString = metaData.getIdentifierQuoteString();
-    if (quoteString == null) {
-      quoteString = "";
-    }
-    return quoteString;
-  }
-
   String getCatalogNameFromMetaData(DatabaseMetaData metaData, RegionMapping regionMapping)
       throws SQLException {
     String catalogFilter = regionMapping.getCatalog();
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataTest.java
b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataTest.java
new file mode 100644
index 0000000..26f1aa9
--- /dev/null
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/TableMetaDataTest.java
@@ -0,0 +1,162 @@
+/*
+ * 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 java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+
+
+public class TableMetaDataTest {
+  private static final String TABLE_NAME = "testTable";
+  private static final String KEY_COLUMN = "keyColumn";
+  private static final String KEY_COLUMN2 = "keyColumn2";
+
+  private String catalogName;
+  private String schemaName;
+  private String tableName;
+  private List<String> keyColumnNames;
+  private String quoteString;
+  private Map<String, Integer> dataTypes;
+
+  private TableMetaData tableMetaData;
+
+  private void createTableMetaData() {
+    tableMetaData = new TableMetaData(catalogName, schemaName, tableName, keyColumnNames,
+        quoteString, dataTypes);
+  }
+
+  @Test
+  public void verifyGetIdentifierQuoteString() {
+    quoteString = "MyQuote";
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getIdentifierQuoteString()).isEqualTo(quoteString);
+  }
+
+  @Test
+  public void verifyKeyColumnNames() {
+    keyColumnNames = Arrays.asList("c1", "c2");
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getKeyColumnNames()).isEqualTo(keyColumnNames);
+  }
+
+  @Test
+  public void verifyColumnNames() {
+    Map<String, Integer> map = new HashMap<>();
+    map.put("k1", 1);
+    map.put("k2", 2);
+    dataTypes = map;
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getColumnNames()).isEqualTo(dataTypes.keySet());
+  }
+
+  @Test
+  public void verifyColumnDataType() {
+    Map<String, Integer> map = new HashMap<>();
+    map.put("k1", 1);
+    map.put("k2", 2);
+    dataTypes = map;
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getColumnDataType("k1")).isEqualTo(1);
+    assertThat(tableMetaData.getColumnDataType("k2")).isEqualTo(2);
+    assertThat(tableMetaData.getColumnDataType("k3")).isEqualTo(0);
+  }
+
+  @Test
+  public void verifyTableWithQuoteAndNoCatalogOrSchema() {
+    quoteString = "+";
+    tableName = "myTable";
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getQuotedTablePath()).isEqualTo(quoteString + tableName + quoteString);
+  }
+
+  @Test
+  public void verifyTableWithQuoteAndEmptyCatalogAndSchema() {
+    quoteString = "+";
+    tableName = "myTable";
+    catalogName = "";
+    schemaName = "";
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getQuotedTablePath()).isEqualTo(quoteString + tableName + quoteString);
+  }
+
+  @Test
+  public void verifyTableWithQuoteAndSchemaAndNoCatalog() {
+    quoteString = "+";
+    tableName = "myTable";
+    schemaName = "mySchema";
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getQuotedTablePath()).isEqualTo(
+        quoteString + schemaName + quoteString + "." + quoteString + tableName + quoteString);
+  }
+
+  @Test
+  public void verifyTableWithQuoteAndCatalogAndNoSchema() {
+    quoteString = "+";
+    tableName = "myTable";
+    catalogName = "myCatalog";
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getQuotedTablePath()).isEqualTo(
+        quoteString + catalogName + quoteString + "." + quoteString + tableName + quoteString);
+  }
+
+  @Test
+  public void verifyTableWithQuoteAndSchemaCatalog() {
+    quoteString = "+";
+    tableName = "myTable";
+    schemaName = "mySchema";
+    catalogName = "myCatalog";
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getQuotedTablePath())
+        .isEqualTo(quoteString + catalogName + quoteString + "." + quoteString + schemaName
+            + quoteString + "." + quoteString + tableName + quoteString);
+  }
+
+  @Test
+  public void verifyTableWithSchemaCatalogAndNoQuote() {
+    tableName = "myTable";
+    schemaName = "mySchema";
+    catalogName = "myCatalog";
+
+    createTableMetaData();
+
+    assertThat(tableMetaData.getQuotedTablePath())
+        .isEqualTo(catalogName + "." + schemaName + "." + tableName);
+  }
+
+}


Mime
View raw message