From 96efd8ffe2411207d95f40ac1ad85d9d32105301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=87=E6=AD=A6?= Date: Thu, 26 Apr 2018 15:00:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Properties#stringPropertyName?= =?UTF-8?q?s=E5=A4=84=E7=90=86=EF=BC=8Cjdk9=E4=BB=A5=E4=B8=8B=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=AD=A4=E6=96=B9=E6=B3=95cpu=E6=B6=88=E8=80=97?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E8=BF=87=E5=A4=A7.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apollo/internals/DefaultConfig.java | 16 ++++++++- .../apollo/internals/DefaultConfigTest.java | 35 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java index 0d28ad012f4..f6cdd8afddf 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/DefaultConfig.java @@ -8,6 +8,7 @@ import java.util.Objects; import java.util.Properties; import java.util.Set; +import java.util.HashMap; import java.util.concurrent.atomic.AtomicReference; import org.slf4j.Logger; @@ -101,7 +102,20 @@ public Set getPropertyNames() { return Collections.emptySet(); } - return properties.stringPropertyNames(); + return stringPropertyNames(properties); + } + + private Set stringPropertyNames(Properties properties) { + //jdk9以下版本Properties#enumerateStringProperties方法存在性能问题,keys() + get(k) 重复迭代, jdk9之后改为entrySet遍历. + Map h = new HashMap<>(); + for (Map.Entry e : properties.entrySet()) { + Object k = e.getKey(); + Object v = e.getValue(); + if (k instanceof String && v instanceof String) { + h.put((String) k, (String) v); + } + } + return h.keySet(); } @Override diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java index a82a04fc892..19b7ab75ca3 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java @@ -11,6 +11,8 @@ import java.util.Calendar; import java.util.Date; import java.util.Properties; +import java.util.Set; +import java.util.Collections; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -645,6 +647,39 @@ public void onChange(ConfigChangeEvent changeEvent) { assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType()); } + @Test + public void testGetPropertyNames() { + String someKeyPrefix = "someKey"; + String someValuePrefix = "someValue"; + + //set up config repo + someProperties = new Properties(); + for (int i = 0; i < 10; i++) { + someProperties.setProperty(someKeyPrefix + i, someValuePrefix + i); + } + + when(configRepository.getConfig()).thenReturn(someProperties); + + DefaultConfig defaultConfig = + new DefaultConfig(someNamespace, configRepository); + + Set propertyNames = defaultConfig.getPropertyNames(); + + assertEquals(10, propertyNames.size()); + assertEquals(someProperties.stringPropertyNames(), propertyNames); + } + + @Test + public void testGetPropertyNamesWithNullProp() { + when(configRepository.getConfig()).thenReturn(null); + + DefaultConfig defaultConfig = + new DefaultConfig(someNamespace, configRepository); + + Set propertyNames = defaultConfig.getPropertyNames(); + assertEquals(Collections.emptySet(), propertyNames); + } + private void checkDatePropertyWithFormat(Config config, Date expected, String propertyName, String format, Date defaultValue) { assertEquals(expected, config.getDateProperty(propertyName, format, defaultValue));