1
0

[HUDI-1254] TypedProperties can not get values by initializing an existing properties (#2059)

This commit is contained in:
linshan-ma
2020-09-09 23:42:41 +08:00
committed by GitHub
parent fec7cd3c97
commit 063a98fc2b
2 changed files with 100 additions and 7 deletions

View File

@@ -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;
}
}

View File

@@ -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));
}
}