Skip to content

Commit

Permalink
Trim configuration value string which contains blank prefix or suffix…
Browse files Browse the repository at this point in the history
… string (#13984)
  • Loading branch information
hangc0276 committed Jan 27, 2022
1 parent 3c2e879 commit fa47a9f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Expand Up @@ -141,7 +141,7 @@ public static <T> void update(Map<String, String> properties, T obj) throws Ille
f.setAccessible(true);
String v = properties.get(f.getName());
if (!StringUtils.isBlank(v)) {
f.set(obj, value(v, f));
f.set(obj, value(trim(v), f));
} else {
setEmptyValue(v, f, obj);
}
Expand Down Expand Up @@ -316,7 +316,7 @@ public static Float stringToFloat(String val) {
public static <T> List<T> stringToList(String val, Class<T> type) {
String[] tokens = trim(val).split(",");
return Arrays.stream(tokens).map(t -> {
return convert(t, type);
return convert(trim(t), type);
}).collect(Collectors.toList());
}

Expand All @@ -332,7 +332,7 @@ public static <T> List<T> stringToList(String val, Class<T> type) {
public static <T> Set<T> stringToSet(String val, Class<T> type) {
String[] tokens = trim(val).split(",");
return Arrays.stream(tokens).map(t -> {
return convert(t, type);
return convert(trim(t), type);
}).collect(Collectors.toSet());
}

Expand All @@ -343,7 +343,7 @@ private static <K, V> Map<K, V> stringToMap(String strValue, Class<K> keyType, C
String[] keyValue = trim(token).split("=");
checkArgument(keyValue.length == 2,
strValue + " map-value is not in correct format key1=value,key2=value2");
map.put(convert(keyValue[0], keyType), convert(keyValue[1], valueType));
map.put(convert(trim(keyValue[0]), keyType), convert(trim(keyValue[1]), valueType));
}
return map;
}
Expand Down
Expand Up @@ -19,10 +19,13 @@
package org.apache.pulsar.common.util;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.Set;
import org.testng.annotations.Test;

public class FieldParserTest {
Expand All @@ -34,6 +37,7 @@ public void testMap() {
properties.put("stringStringMap", "key1=value1,key2=value2");
properties.put("stringIntMap", "key1=1,key2=2");
properties.put("longStringMap", "1=value1,2=value2");

MyConfig config = new MyConfig();
FieldParser.update(properties, config);
assertEquals(config.name, "config");
Expand All @@ -48,11 +52,46 @@ public void testMap() {

}

@Test
public void testWithBlankVallueConfig() {
Map<String, String> properties = new HashMap<>();
properties.put("name", " config ");
properties.put("stringStringMap", "key1=value1 , key2= value2 ");
properties.put("stringIntMap", "key1 = 1, key2 = 2 ");
properties.put("longStringMap", " 1 =value1 ,2 =value2 ");
properties.put("longList", " 1, 3, 8 , 0 ,9 ");
properties.put("stringList", " aa, bb , cc, ee ");
properties.put("longSet", " 1, 3, 8 , 0 , 3, 1 ,9 ");
properties.put("stringSet", " aa, bb , cc, ee , bb, aa ");

MyConfig config = new MyConfig();
FieldParser.update(properties, config);
assertEquals(config.name, "config");
assertEquals(config.stringStringMap.get("key1"), "value1");
assertEquals(config.stringStringMap.get("key2"), "value2");

assertEquals((int) config.stringIntMap.get("key1"), 1);
assertEquals((int) config.stringIntMap.get("key2"), 2);

assertEquals(config.longStringMap.get(1L), "value1");
assertEquals(config.longStringMap.get(2L), "value2");

assertEquals((long)config.longList.get(2), 8);
assertEquals(config.stringList.get(1), "bb");

assertTrue(config.longSet.contains(3L));
assertTrue(config.stringSet.contains("bb"));
}

public static class MyConfig {
public String name;
public Map<String, String> stringStringMap;
public Map<String, Integer> stringIntMap;
public Map<Long, String> longStringMap;
public List<Long> longList;
public List<String> stringList;
public Set<Long> longSet;
public Set<String> stringSet;
}

}

0 comments on commit fa47a9f

Please sign in to comment.