Skip to content

Commit

Permalink
修改原Properties读取方式,使其支持UTF-8格式,及换行符,增加测试类
Browse files Browse the repository at this point in the history
  • Loading branch information
凡梦星尘 committed Sep 3, 2015
1 parent bcc1d62 commit 635a23e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/org/nutz/ioc/impl/PropertiesProxy.java
@@ -1,5 +1,6 @@
package org.nutz.ioc.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
Expand Down Expand Up @@ -48,6 +49,11 @@ public class PropertiesProxy {
public PropertiesProxy() {
this(true);
}

public PropertiesProxy(boolean utf8, String... paths) {
this(utf8);
this.setPaths(paths);
}

public PropertiesProxy(boolean utf8) {
this.utf8 = utf8;
Expand Down Expand Up @@ -111,12 +117,13 @@ public void setPaths(String... paths) {
else {
Properties p = new Properties();
for (NutResource nr : list) {
InputStream in = nr.getInputStream();
// 用字符流来读取文件
BufferedReader bf = new BufferedReader(new InputStreamReader(nr.getInputStream()));
try {
p.load(nr.getInputStream());
p.load(bf);
}
finally {
Streams.safeClose(in);
Streams.safeClose(bf);
}
}
mp.putAll(p);
Expand Down
6 changes: 6 additions & 0 deletions test/config/conf-utf8.properties
@@ -0,0 +1,6 @@
# 用UTF-8编辑保存配置

charset=UTF-8
longstr=Nutz \
超级棒的Framework!\
谁用谁喜欢,嘻嘻。
6 changes: 6 additions & 0 deletions test/config/conf.properties
@@ -0,0 +1,6 @@
# default charset store

str=Nutz
number=153
bool=1
chinese=\u575A\u679C
4 changes: 3 additions & 1 deletion test/org/nutz/ioc/AllIoc.java
Expand Up @@ -3,6 +3,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.nutz.ioc.aop.config.impl.AllAopConfigration;
import org.nutz.ioc.impl.PropertiesProxyTest;
import org.nutz.ioc.java.ChainParsingTest;
import org.nutz.ioc.json.AllJsonIoc;
import org.nutz.ioc.loader.AllLoader;
Expand All @@ -14,5 +15,6 @@
AllLoader.class,
AllVal.class,
AllAopConfigration.class,
SimpleIocTest.class})
SimpleIocTest.class,
PropertiesProxyTest.class})
public class AllIoc {}
61 changes: 61 additions & 0 deletions test/org/nutz/ioc/impl/PropertiesProxyTest.java
@@ -0,0 +1,61 @@
package org.nutz.ioc.impl;

import java.io.UnsupportedEncodingException;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class PropertiesProxyTest {

private PropertiesProxy pp;

private static final String UTF8_CHARSET = "UTF-8";
private static final String CHINESE_STR = "Nutz 超级棒的Framework!谁用谁喜欢,嘻嘻。";

private static final String CHARSET_KEY = "charset";
private static final String LONG_STR = "longstr";

@Before
public void init() {
pp = new PropertiesProxy(false, "/config/conf.properties");
}

@Test
public void testUTF8Properties() {
PropertiesProxy i18nPP = new PropertiesProxy(false, "/config/conf-utf8.properties");
i18nPP.setIgnoreResourceNotFound(true);

Assert.assertEquals(UTF8_CHARSET, i18nPP.get(CHARSET_KEY));
Assert.assertEquals(CHINESE_STR, i18nPP.get(LONG_STR));
}

@Test
public void testString() throws UnsupportedEncodingException {
Assert.assertEquals("Nutz ", pp.get("str"));
Assert.assertEquals("Nutz", pp.getTrim("str"));
Assert.assertEquals("坚果", new String(pp.getTrim("chinese")));
}

@Test
public void testNumber() {
Assert.assertEquals(153, pp.getLong("number"));
Assert.assertEquals(153, pp.getInt("number"));
}

@Test
public void testBoolean() {
Assert.assertEquals(true, pp.getBoolean("bool"));
}

@Test
public void testHas() {
Assert.assertTrue(pp.has("str"));
}

@Test
public void testSize() {
Assert.assertEquals(pp.getKeys().size(), 4);
Assert.assertEquals(pp.getValues().size(), 4);
}
}

0 comments on commit 635a23e

Please sign in to comment.