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

leesf pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hudi.git


The following commit(s) were added to refs/heads/master by this push:
     new 063a98f  [HUDI-1254] TypedProperties can not get values by 
initializing an existing properties (#2059)
063a98f is described below

commit 063a98fc2b76beac28a4797884973abd2911c887
Author: linshan-ma <mabin194...@163.com>
AuthorDate: Wed Sep 9 23:42:41 2020 +0800

    [HUDI-1254] TypedProperties can not get values by initializing an existing 
properties (#2059)
---
 .../apache/hudi/common/config/TypedProperties.java | 23 ++++--
 .../common/properties/TestTypedProperties.java     | 84 ++++++++++++++++++++++
 2 files changed, 100 insertions(+), 7 deletions(-)

diff --git 
a/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java 
b/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java
index 295598c..c780ded 100644
--- 
a/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java
+++ 
b/hudi-common/src/main/java/org/apache/hudi/common/config/TypedProperties.java
@@ -22,6 +22,7 @@ import java.io.Serializable;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Properties;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -38,22 +39,30 @@ public class TypedProperties extends Properties implements 
Serializable {
   }
 
   private void checkKey(String property) {
-    if (!containsKey(property)) {
+    if (!keyExists(property)) {
       throw new IllegalArgumentException("Property " + property + " not 
found");
     }
   }
 
+  private boolean keyExists(String property) {
+    Set<String> keys = super.stringPropertyNames();
+    if (keys.contains(property)) {
+      return true;
+    }
+    return false;
+  }
+
   public String getString(String property) {
     checkKey(property);
     return getProperty(property);
   }
 
   public String getString(String property, String defaultValue) {
-    return containsKey(property) ? getProperty(property) : defaultValue;
+    return keyExists(property) ? getProperty(property) : defaultValue;
   }
 
   public List<String> getStringList(String property, String delimiter, 
List<String> defaultVal) {
-    if (!containsKey(property)) {
+    if (!keyExists(property)) {
       return defaultVal;
     }
     return 
Arrays.stream(getProperty(property).split(delimiter)).map(String::trim).collect(Collectors.toList());
@@ -65,7 +74,7 @@ public class TypedProperties extends Properties implements 
Serializable {
   }
 
   public int getInteger(String property, int defaultValue) {
-    return containsKey(property) ? Integer.parseInt(getProperty(property)) : 
defaultValue;
+    return keyExists(property) ? Integer.parseInt(getProperty(property)) : 
defaultValue;
   }
 
   public long getLong(String property) {
@@ -74,7 +83,7 @@ public class TypedProperties extends Properties implements 
Serializable {
   }
 
   public long getLong(String property, long defaultValue) {
-    return containsKey(property) ? Long.parseLong(getProperty(property)) : 
defaultValue;
+    return keyExists(property) ? Long.parseLong(getProperty(property)) : 
defaultValue;
   }
 
   public boolean getBoolean(String property) {
@@ -83,7 +92,7 @@ public class TypedProperties extends Properties implements 
Serializable {
   }
 
   public boolean getBoolean(String property, boolean defaultValue) {
-    return containsKey(property) ? Boolean.parseBoolean(getProperty(property)) 
: defaultValue;
+    return keyExists(property) ? Boolean.parseBoolean(getProperty(property)) : 
defaultValue;
   }
 
   public double getDouble(String property) {
@@ -92,6 +101,6 @@ public class TypedProperties extends Properties implements 
Serializable {
   }
 
   public double getDouble(String property, double defaultValue) {
-    return containsKey(property) ? Double.parseDouble(getProperty(property)) : 
defaultValue;
+    return keyExists(property) ? Double.parseDouble(getProperty(property)) : 
defaultValue;
   }
 }
diff --git 
a/hudi-common/src/test/java/org/apache/hudi/common/properties/TestTypedProperties.java
 
b/hudi-common/src/test/java/org/apache/hudi/common/properties/TestTypedProperties.java
new file mode 100644
index 0000000..95955d4
--- /dev/null
+++ 
b/hudi-common/src/test/java/org/apache/hudi/common/properties/TestTypedProperties.java
@@ -0,0 +1,84 @@
+/*
+ * 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.hudi.common.properties;
+
+import org.apache.hudi.common.config.TypedProperties;
+import org.junit.jupiter.api.Test;
+
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class TestTypedProperties {
+  @Test
+  public void testGetString() {
+    Properties properties = new Properties();
+    properties.put("key1", "value1");
+
+    TypedProperties typedProperties = new TypedProperties(properties);
+    assertEquals("value1", typedProperties.getString("key1"));
+    assertEquals("value1", typedProperties.getString("key1", "default"));
+    assertEquals("default", typedProperties.getString("key2", "default"));
+  }
+
+  @Test
+  public void testGetInteger() {
+    Properties properties = new Properties();
+    properties.put("key1", "123");
+
+    TypedProperties typedProperties = new TypedProperties(properties);
+    assertEquals(123, typedProperties.getInteger("key1"));
+    assertEquals(123, typedProperties.getInteger("key1", 456));
+    assertEquals(456, typedProperties.getInteger("key2", 456));
+
+  }
+
+  @Test
+  public void testGetDouble() {
+    Properties properties = new Properties();
+    properties.put("key1", "123.4");
+
+    TypedProperties typedProperties = new TypedProperties(properties);
+    assertEquals(123.4, typedProperties.getDouble("key1"));
+    assertEquals(123.4, typedProperties.getDouble("key1", 0.001D));
+    assertEquals(0.001D, typedProperties.getDouble("key2", 0.001D));
+  }
+
+  @Test
+  public void testGetLong() {
+    Properties properties = new Properties();
+    properties.put("key1", "1354354354");
+
+    TypedProperties typedProperties = new TypedProperties(properties);
+    assertEquals(1354354354, typedProperties.getLong("key1"));
+    assertEquals(1354354354, typedProperties.getLong("key1", 8578494434L));
+    assertEquals(8578494434L, typedProperties.getLong("key2", 8578494434L));
+  }
+
+  @Test
+  public void testGetBoolean() {
+    Properties properties = new Properties();
+    properties.put("key1", "true");
+
+    TypedProperties typedProperties = new TypedProperties(properties);
+    assertEquals(true, typedProperties.getBoolean("key1"));
+    assertEquals(true, typedProperties.getBoolean("key1", false));
+    assertEquals(false, typedProperties.getBoolean("key2", false));
+  }
+}

Reply via email to