From 3102f13714eb328d7886999de16cecde08367948 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Fri, 1 Dec 2017 10:57:52 +0800 Subject: [PATCH] fix issue #1361 --- src/org/nutz/ioc/impl/PropertiesProxy.java | 2 +- .../nutz/lang/util/MultiLineProperties.java | 18 ++++++++++++++---- test/config/conf.properties | 4 +++- .../org/nutz/ioc/impl/PropertiesProxyTest.java | 12 +++++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/org/nutz/ioc/impl/PropertiesProxy.java b/src/org/nutz/ioc/impl/PropertiesProxy.java index 191e100aa..84a5508a1 100644 --- a/src/org/nutz/ioc/impl/PropertiesProxy.java +++ b/src/org/nutz/ioc/impl/PropertiesProxy.java @@ -132,7 +132,7 @@ public void setPaths(String... paths) { Streams.safeClose(bf); } } - putAll(p); + putAllTrim(p); } } catch (IOException e) { diff --git a/src/org/nutz/lang/util/MultiLineProperties.java b/src/org/nutz/lang/util/MultiLineProperties.java index 03a0c2176..6bcf7cf67 100644 --- a/src/org/nutz/lang/util/MultiLineProperties.java +++ b/src/org/nutz/lang/util/MultiLineProperties.java @@ -23,8 +23,6 @@ */ public class MultiLineProperties implements Map { - - public MultiLineProperties(Reader reader) throws IOException { this(); load(reader); @@ -89,7 +87,7 @@ public synchronized void load(Reader reader, boolean clear) throws IOException { value = Strings.unicodeDecode(value); } value = value.replace("\\:", ":").replace("\\=", "="); - maps.put(Strings.trim(name), value); + putTrim(name, value); } else if (c == ':') { String name = s.substring(0, pos); StringBuffer sb = new StringBuffer(); @@ -100,7 +98,7 @@ public synchronized void load(Reader reader, boolean clear) throws IOException { break; sb.append("\r\n" + ss); } - maps.put(Strings.trim(name), sb.toString()); + putTrim(name, sb.toString()); if (null == ss) return; } else { @@ -108,6 +106,10 @@ public synchronized void load(Reader reader, boolean clear) throws IOException { } } } + + public void putTrim(String key, String value) { + maps.put(Strings.trim(key), Strings.trim(value)); + } public synchronized void clear() { maps.clear(); @@ -151,11 +153,19 @@ public synchronized String put(String key, String value) { return maps.put(key, value); } + @SuppressWarnings({"unchecked", "rawtypes"}) public synchronized void putAll(Map t) { maps.putAll(t); } + @SuppressWarnings({"rawtypes"}) + public synchronized void putAllTrim(Map t) { + for (Object key : t.keySet()) { + putTrim(Strings.sNull(key), Strings.sNull(t.get(key))); + } + } + public synchronized String remove(Object key) { return maps.remove(key); } diff --git a/test/config/conf.properties b/test/config/conf.properties index 6b1b0a95f..26ecfa62b 100644 --- a/test/config/conf.properties +++ b/test/config/conf.properties @@ -3,4 +3,6 @@ str=Nutz number=153 bool=1 -chinese=\u575A\u679C \ No newline at end of file +chinese=\u575A\u679C + + ABC = 123 \ No newline at end of file diff --git a/test/org/nutz/ioc/impl/PropertiesProxyTest.java b/test/org/nutz/ioc/impl/PropertiesProxyTest.java index ca4a66fcc..a15a0595d 100644 --- a/test/org/nutz/ioc/impl/PropertiesProxyTest.java +++ b/test/org/nutz/ioc/impl/PropertiesProxyTest.java @@ -32,7 +32,7 @@ public void testUTF8Properties() { @Test public void testString() throws UnsupportedEncodingException { - Assert.assertEquals("Nutz ", pp.get("str")); + Assert.assertEquals("Nutz", pp.get("str")); Assert.assertEquals("Nutz", pp.getTrim("str")); Assert.assertEquals("坚果", new String(pp.getTrim("chinese"))); } @@ -55,7 +55,13 @@ public void testHas() { @Test public void testSize() { - Assert.assertEquals(pp.getKeys().size(), 4); - Assert.assertEquals(pp.getValues().size(), 4); + Assert.assertEquals(pp.getKeys().size(), 5); + Assert.assertEquals(pp.getValues().size(), 5); + } + + @Test + public void testTrim() { + System.out.println(pp.get("ABC")); + Assert.assertEquals(123, pp.getInt("ABC")); } }