#14 开始整理Modules的单元测试,提高测试覆盖率,并顺手改进代码。

This commit is contained in:
Calvin
2012-05-05 23:39:31 +08:00
parent da2440535c
commit e819fbcefd
50 changed files with 271 additions and 451 deletions
@@ -42,6 +42,10 @@ public class BeanValidatorsTest extends SpringContextTestCase {
String result = StringUtils.join(BeanValidators.extractPropertyAndMessage(violations, " "), ",");
assertTrue(StringUtils.contains(result, "email not a well-formed email address"));
assertTrue(StringUtils.contains(result, "name may not be empty"));
result = StringUtils.join(BeanValidators.extractMessage(violations), ",");
assertTrue(StringUtils.contains(result, "not a well-formed email address"));
assertTrue(StringUtils.contains(result, "may not be empty"));
}
@Test
@@ -62,9 +66,9 @@ public class BeanValidatorsTest extends SpringContextTestCase {
private static class Customer {
String name;
private String name;
String email;
private String email;
@NotBlank
public String getName() {
@@ -51,17 +51,4 @@ public class EncodesTest {
assertEquals("1>2", result);
assertEquals(input, Encodes.unescapeHtml(result));
}
@Test
public void csv() {
String input = "haha,kaka";
String result = Encodes.escapeCsv(input);
System.out.println(result);
assertEquals(input, Encodes.unescapeCsv(result));
//
input = "\"ha\"ka";
result = Encodes.escapeCsv(input);
System.out.println(result);
assertEquals(input, Encodes.unescapeCsv(result));
}
}
@@ -0,0 +1,5 @@
package org.springside.modules.utils;
public class ExceptionsTest {
}
@@ -24,6 +24,21 @@ public class PropertiesLoaderTest {
public void notExistProperty() throws IOException {
Properties p = new PropertiesLoader("classpath:/notexist.properties").getProperties();
assertNull(p.getProperty("notexist"));
assertEquals("defaultValue", p.getProperty("notexist", "defaultValue"));
}
@Test
public void integerAndBooleanProperty() {
PropertiesLoader p = new PropertiesLoader("classpath:/test1.properties", "classpath:/test2.properties");
assertEquals(new Integer(1), p.getInteger("p1"));
assertEquals(new Integer(0), p.getInteger("notExist"));
assertEquals(new Integer(100), p.getInteger("notExist", 100));
assertEquals(new Boolean(true), p.getBoolean("p4"));
assertEquals(new Boolean(false), p.getBoolean("notExist"));
assertEquals(new Boolean(true), p.getBoolean("notExist", true));
}
@Test
@@ -31,5 +46,6 @@ public class PropertiesLoaderTest {
System.setProperty("p1", "sys");
PropertiesLoader pl = new PropertiesLoader("classpath:/test1.properties", "classpath:/test2.properties");
assertEquals("sys", pl.getProperty("p1"));
System.clearProperty("p1");
}
}
@@ -34,6 +34,10 @@ public class ReflectionsTest {
bean = new TestBean();
Reflections.invokeSetter(bean, "publicField", 10, int.class);
assertEquals(10 + 1, bean.inspectPublicField());
//非原始類型,可省略類型參數
Reflections.invokeSetter(bean, "name", "foo");
assertEquals("foo", bean.name);
}
@Test
@@ -88,14 +92,22 @@ public class ReflectionsTest {
/** 有getter/setter的field */
private int publicField = 1;
private String name;
//通過getter函數會比屬性值+1
public int getPublicField() {
return publicField + 1;
}
//通過setter函數會被比輸入值加1
public void setPublicField(int publicField) {
this.publicField = publicField + 1;
}
public void setName(String name) {
this.name = name;
}
public int inspectPrivateField() {
return privateField;
}
@@ -1,2 +1,3 @@
p1=1
p2=2
p2=2
p4=true