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/11/03 23:45:57 UTC

[geode] branch feature/GEODE-3781 updated: parser now detects empty fields around the jdbc separator

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 5b9698d  parser now detects empty fields around the jdbc separator
5b9698d is described below

commit 5b9698d98a02d1f28f88d18b9d7e7d0ae5f41a6d
Author: Darrel Schneider <ds...@pivotal.io>
AuthorDate: Fri Nov 3 16:45:31 2017 -0700

    parser now detects empty fields around the jdbc separator
---
 .../jdbc/internal/JDBCConfiguration.java           | 79 ++++++++++++++--------
 .../jdbc/internal/JDBCConfigurationUnitTest.java   | 69 ++++++++++++++++++-
 2 files changed, 118 insertions(+), 30 deletions(-)

diff --git a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JDBCConfiguration.java b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JDBCConfiguration.java
index d66303b..8694b41 100644
--- a/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JDBCConfiguration.java
+++ b/geode-connectors/src/main/java/org/apache/geode/connectors/jdbc/internal/JDBCConfiguration.java
@@ -24,7 +24,6 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.function.Function;
-import java.util.function.IntPredicate;
 
 public class JDBCConfiguration {
   private static final String URL = "url";
@@ -86,17 +85,17 @@ public class JDBCConfiguration {
     this.user = configProps.getProperty(USER);
     this.password = configProps.getProperty(PASSWORD);
     String valueClassNameProp = configProps.getProperty(VALUE_CLASS_NAME);
-    this.valueClassNameDefault = computeDefaultValueClassName(valueClassNameProp);
-    this.regionToClassMap = computeRegionToClassMap(valueClassNameProp);
+    this.regionToClassMap = parseRegionToClassMap(valueClassNameProp);
+    this.valueClassNameDefault = parseDefaultValueClassName(valueClassNameProp);
     String keyPartOfValueProp = configProps.getProperty(IS_KEY_PART_OF_VALUE);
-    this.keyPartOfValueDefault = computeDefaultKeyPartOfValue(keyPartOfValueProp);
-    this.keyPartOfValueMap = computeKeyPartOfValueMap(keyPartOfValueProp);
-    this.regionToTableMap = computeRegionToTableMap(configProps.getProperty(REGION_TO_TABLE));
-    this.fieldToColumnMap = computeFieldToColumnMap(configProps.getProperty(FIELD_TO_COLUMN));
-    this.columnToFieldMap = computeColumnToFieldMap(this.fieldToColumnMap);
+    this.keyPartOfValueMap = parseKeyPartOfValueMap(keyPartOfValueProp);
+    this.keyPartOfValueDefault = parseDefaultKeyPartOfValue(keyPartOfValueProp);
+    this.regionToTableMap = parseRegionToTableMap(configProps.getProperty(REGION_TO_TABLE));
+    this.fieldToColumnMap = parseFieldToColumnMap(configProps.getProperty(FIELD_TO_COLUMN));
+    this.columnToFieldMap = parseColumnToFieldMap(this.fieldToColumnMap);
   }
 
-  private Map<RegionAndName, String> computeColumnToFieldMap(Map<RegionAndName, String> map) {
+  private Map<RegionAndName, String> parseColumnToFieldMap(Map<RegionAndName, String> map) {
     if (map == null) {
       return null;
     }
@@ -111,7 +110,7 @@ public class JDBCConfiguration {
         if (outputKey.getRegionName() == null) {
           columnString = outputKey.getName();
         } else {
-          columnString = outputKey.getRegionName() + getjdbcSeparator() + outputKey.getName();
+          columnString = outputKey.getRegionName() + getJdbcSeparator() + outputKey.getName();
         }
         throw new IllegalArgumentException(
             " The column " + columnString + " can not be mapped to two different fields.");
@@ -173,16 +172,25 @@ public class JDBCConfiguration {
     }
   }
 
-  private Map<RegionAndName, String> computeFieldToColumnMap(String prop) {
+  private Map<RegionAndName, String> parseFieldToColumnMap(String prop) {
     Function<String, RegionAndName> regionFieldParser = new Function<String, RegionAndName>() {
       @Override
       public RegionAndName apply(String item) {
         String regionName = null;
         String fieldName;
-        int idx = item.indexOf(getjdbcSeparator());
+        int idx = item.indexOf(getJdbcSeparator());
         if (idx != -1) {
           regionName = item.substring(0, idx);
-          fieldName = item.substring(idx + getjdbcSeparator().length());
+          fieldName = item.substring(idx + getJdbcSeparator().length());
+          if (regionName.length() == 0 || fieldName.length() == 0) {
+            throw new IllegalArgumentException("Empty string found while splitting " + item
+                + " on the " + getJdbcSeparator() + " separator");
+          }
+
+          if (fieldName.contains(getJdbcSeparator())) {
+            throw new IllegalArgumentException(
+                "Too many " + getJdbcSeparator() + " separators in " + fieldName);
+          }
         } else {
           fieldName = item;
         }
@@ -192,24 +200,32 @@ public class JDBCConfiguration {
     return parseMap(prop, regionFieldParser, v -> v, true);
   }
 
-  private Map<String, String> computeRegionToTableMap(String prop) {
-    return parseMap(prop, String::toLowerCase, v -> v, true);
+  private Map<String, String> parseRegionToTableMap(String prop) {
+    return parseMap(prop, k -> parseRegionKey(k), v -> v, true);
   }
 
-  private String computeDefaultValueClassName(String valueClassNameProp) {
+  private String parseRegionKey(String key) {
+    if (key.contains(getJdbcSeparator())) {
+      throw new IllegalArgumentException(
+          "Too many " + getJdbcSeparator() + " separators in " + key);
+    }
+    return key.toLowerCase();
+  }
+
+  private String parseDefaultValueClassName(String valueClassNameProp) {
     return parseDefault(VALUE_CLASS_NAME, valueClassNameProp, v -> v, null);
   }
 
-  private Map<String, String> computeRegionToClassMap(String valueClassNameProp) {
-    return parseMap(valueClassNameProp, String::toLowerCase, v -> v, false);
+  private Map<String, String> parseRegionToClassMap(String valueClassNameProp) {
+    return parseMap(valueClassNameProp, k -> parseRegionKey(k), v -> v, false);
   }
 
-  private boolean computeDefaultKeyPartOfValue(String keyPartOfValueProp) {
+  private boolean parseDefaultKeyPartOfValue(String keyPartOfValueProp) {
     return parseDefault(IS_KEY_PART_OF_VALUE, keyPartOfValueProp, Boolean::parseBoolean, false);
   }
 
-  private Map<String, Boolean> computeKeyPartOfValueMap(String keyPartOfValueProp) {
-    return parseMap(keyPartOfValueProp, String::toLowerCase, Boolean::parseBoolean, false);
+  private Map<String, Boolean> parseKeyPartOfValueMap(String keyPartOfValueProp) {
+    return parseMap(keyPartOfValueProp, k -> parseRegionKey(k), Boolean::parseBoolean, false);
   }
 
   private <K, V> Map<K, V> parseMap(String propertyValue, Function<String, K> keyParser,
@@ -220,15 +236,19 @@ public class JDBCConfiguration {
     Map<K, V> result = new HashMap<>();
     List<String> items = Arrays.asList(propertyValue.split("\\s*,\\s*"));
     for (String item : items) {
-      int idx = item.lastIndexOf(getjdbcSeparator());
+      int idx = item.lastIndexOf(getJdbcSeparator());
       if (idx == -1) {
         if (failOnNoSeparator) {
-          throw new IllegalArgumentException(item + " does not contain " + getjdbcSeparator());
+          throw new IllegalArgumentException(item + " does not contain " + getJdbcSeparator());
         }
         continue;
       }
       String keyString = item.substring(0, idx);
-      String valueString = item.substring(idx + getjdbcSeparator().length());
+      String valueString = item.substring(idx + getJdbcSeparator().length());
+      if (keyString.length() == 0 || valueString.length() == 0) {
+        throw new IllegalArgumentException("Empty string found while splitting " + item + " on the "
+            + getJdbcSeparator() + " separator");
+      }
       K key = keyParser.apply(keyString);
       if (result.containsKey(key)) {
         throw new IllegalArgumentException("Duplicate item " + key + " is not allowed.");
@@ -246,13 +266,13 @@ public class JDBCConfiguration {
     V result = null;
     List<String> items = Arrays.asList(propertyValue.split("\\s*,\\s*"));
     for (String item : items) {
-      int idx = item.indexOf(getjdbcSeparator());
+      int idx = item.indexOf(getJdbcSeparator());
       if (idx != -1) {
         continue;
       }
       if (result != null) {
         throw new IllegalArgumentException(propertyName
-            + " can have at most one item that does not have a " + getjdbcSeparator() + " in it.");
+            + " can have at most one item that does not have a " + getJdbcSeparator() + " in it.");
       }
       result = parser.apply(item);
     }
@@ -314,7 +334,7 @@ public class JDBCConfiguration {
     return result;
   }
 
-  protected String getjdbcSeparator() {
+  protected String getJdbcSeparator() {
     return JDBC_SEPARATOR;
   }
 
@@ -346,7 +366,7 @@ public class JDBCConfiguration {
   }
 
   public String getFieldForRegionColumn(String regionName, String columnName) {
-    String result = columnName.toLowerCase();
+    String result = null;
     if (this.columnToFieldMap != null) {
       RegionAndName key = new RegionAndName(regionName, columnName);
       String mapValue = this.columnToFieldMap.get(key);
@@ -358,6 +378,9 @@ public class JDBCConfiguration {
         result = mapValue;
       }
     }
+    if (result == null) {
+      result = columnName.toLowerCase();
+    }
     return result;
   }
 }
diff --git a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/JDBCConfigurationUnitTest.java b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/JDBCConfigurationUnitTest.java
index 188bfa4..4ad9b24 100644
--- a/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/JDBCConfigurationUnitTest.java
+++ b/geode-connectors/src/test/java/org/apache/geode/connectors/jdbc/internal/JDBCConfigurationUnitTest.java
@@ -115,6 +115,14 @@ public class JDBCConfigurationUnitTest {
     new JDBCConfiguration(props);
   }
 
+  @Test(expected = IllegalArgumentException.class)
+  public void verifyValueClassNameWithTooManySeparatorsThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("valueClassName", "reg1:myClass1:extra");
+    new JDBCConfiguration(props);
+  }
+
   @Test
   public void testValueClassNameWithRegionNames() {
     Properties props = new Properties();
@@ -144,6 +152,14 @@ public class JDBCConfigurationUnitTest {
     new JDBCConfiguration(props);
   }
 
+  @Test(expected = IllegalArgumentException.class)
+  public void verifyKeyPartOfValueWithExtraSeparatorThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("isKeyPartOfValue", "reg1:true:true");
+    new JDBCConfiguration(props);
+  }
+
   @Test
   public void testIsKeyPartOfValueWithRegionNames() {
     Properties props = new Properties();
@@ -186,7 +202,7 @@ public class JDBCConfigurationUnitTest {
     }
 
     @Override
-    protected String getjdbcSeparator() {
+    protected String getJdbcSeparator() {
       return "->";
     }
   }
@@ -229,6 +245,14 @@ public class JDBCConfigurationUnitTest {
   }
 
   @Test(expected = IllegalArgumentException.class)
+  public void verifyRegionToTableWithTooManySeparatorsThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("regionToTable", "reg1:table1:2");
+    new JDBCConfiguration(props);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
   public void verifyDuplicateRegionToTableThrows() {
     Properties props = new Properties();
     props.setProperty("url", "");
@@ -314,6 +338,15 @@ public class JDBCConfigurationUnitTest {
   }
 
   @Test(expected = IllegalArgumentException.class)
+  public void verifyFieldToColumnTooManySeparatorsThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("fieldToColumn", "reg1:field1:column1:extra");
+    new JDBCConfiguration(props);
+  }
+
+
+  @Test(expected = IllegalArgumentException.class)
   public void verifyDuplicateRegionFieldThrows() {
     Properties props = new Properties();
     props.setProperty("url", "");
@@ -346,10 +379,42 @@ public class JDBCConfigurationUnitTest {
   }
 
   @Test(expected = IllegalArgumentException.class)
+  public void verifyEmptyRegionThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("fieldToColumn", ":field1:column1");
+    new JDBCConfiguration(props);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void verifyEmptyFieldThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("fieldToColumn", "reg1::column1");
+    new JDBCConfiguration(props);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void verifyEmptyColumnThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("fieldToColumn", "reg1:field1:");
+    new JDBCConfiguration(props);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void verifyEmptyAllThrows() {
+    Properties props = new Properties();
+    props.setProperty("url", "");
+    props.setProperty("fieldToColumn", ":");
+    new JDBCConfiguration(props);
+  }
+
+  @Test(expected = IllegalArgumentException.class)
   public void verifyFieldToColumnRequiresSeparator() {
     Properties props = new Properties();
     props.setProperty("url", "");
-    props.setProperty("regionToTable", "reg1:table1, reg2:table2, noSeparator");
+    props.setProperty("fieldToColumn", "reg1:table1, reg2:table2, noSeparator");
     new JDBCConfiguration(props);
   }
 

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