TMC-LANG: Code clean-up, added tests and fixed issues.

This commit is contained in:
Harald Kuhr
2013-06-06 19:51:07 +02:00
parent f5b5e818c5
commit cdce0aebff
5 changed files with 182 additions and 97 deletions

View File

@@ -108,13 +108,13 @@ public class BeanUtilTestCase extends TestCase {
assertEquals(0.3, bean.getDoubleValue());
}
public void testConfigureAmbigious1() {
public void testConfigureAmbiguous1() {
TestBean bean = new TestBean();
Map<String, String> map = new HashMap<String, String>();
String value = "one";
map.put("ambigious", value);
map.put("ambiguous", value);
try {
BeanUtil.configure(bean, map);
@@ -123,20 +123,20 @@ public class BeanUtilTestCase extends TestCase {
fail(e.getMessage());
}
assertNotNull(bean.getAmbigious());
assertEquals("String converted rather than invoking setAmbigiouos(String), ordering not predictable",
"one", bean.getAmbigious());
assertSame("String converted rather than invoking setAmbigiouos(String), ordering not predictable",
value, bean.getAmbigious());
assertNotNull(bean.getAmbiguous());
assertEquals("String converted rather than invoking setAmbiguous(String), ordering not predictable",
"one", bean.getAmbiguous());
assertSame("String converted rather than invoking setAmbiguous(String), ordering not predictable",
value, bean.getAmbiguous());
}
public void testConfigureAmbigious2() {
public void testConfigureAmbiguous2() {
TestBean bean = new TestBean();
Map<String, Integer> map = new HashMap<String, Integer>();
Integer value = 2;
map.put("ambigious", value);
map.put("ambiguous", value);
try {
BeanUtil.configure(bean, map);
@@ -145,20 +145,20 @@ public class BeanUtilTestCase extends TestCase {
fail(e.getMessage());
}
assertNotNull(bean.getAmbigious());
assertEquals("Integer converted rather than invoking setAmbigiouos(Integer), ordering not predictable",
2, bean.getAmbigious());
assertSame("Integer converted rather than invoking setAmbigiouos(Integer), ordering not predictable",
value, bean.getAmbigious());
assertNotNull(bean.getAmbiguous());
assertEquals("Integer converted rather than invoking setAmbiguous(Integer), ordering not predictable",
2, bean.getAmbiguous());
assertSame("Integer converted rather than invoking setAmbiguous(Integer), ordering not predictable",
value, bean.getAmbiguous());
}
public void testConfigureAmbigious3() {
public void testConfigureAmbiguous3() {
TestBean bean = new TestBean();
Map<String, Double> map = new HashMap<String, Double>();
Double value = .3;
map.put("ambigious", value);
map.put("ambiguous", value);
try {
BeanUtil.configure(bean, map);
@@ -167,11 +167,11 @@ public class BeanUtilTestCase extends TestCase {
fail(e.getMessage());
}
assertNotNull(bean.getAmbigious());
assertEquals("Object converted rather than invoking setAmbigious(Object), ordering not predictable",
value.getClass(), bean.getAmbigious().getClass());
assertSame("Object converted rather than invoking setAmbigious(Object), ordering not predictable",
value, bean.getAmbigious());
assertNotNull(bean.getAmbiguous());
assertEquals("Object converted rather than invoking setAmbiguous(Object), ordering not predictable",
value.getClass(), bean.getAmbiguous().getClass());
assertSame("Object converted rather than invoking setAmbiguous(Object), ordering not predictable",
value, bean.getAmbiguous());
}
static class TestBean {
@@ -179,7 +179,7 @@ public class BeanUtilTestCase extends TestCase {
private int intVal;
private Double doubleVal;
private Object ambigious;
private Object ambiguous;
public Double getDoubleValue() {
return doubleVal;
@@ -193,36 +193,43 @@ public class BeanUtilTestCase extends TestCase {
return stringVal;
}
@SuppressWarnings("UnusedDeclaration")
public void setStringValue(String pString) {
stringVal = pString;
}
@SuppressWarnings("UnusedDeclaration")
public void setIntValue(int pInt) {
intVal = pInt;
}
@SuppressWarnings("UnusedDeclaration")
public void setDoubleValue(Double pDouble) {
doubleVal = pDouble;
}
public void setAmbigious(String pString) {
ambigious = pString;
@SuppressWarnings("UnusedDeclaration")
public void setAmbiguous(String pString) {
ambiguous = pString;
}
public void setAmbigious(Object pObject) {
ambigious = pObject;
@SuppressWarnings("UnusedDeclaration")
public void setAmbiguous(Object pObject) {
ambiguous = pObject;
}
public void setAmbigious(Integer pInteger) {
ambigious = pInteger;
@SuppressWarnings("UnusedDeclaration")
public void setAmbiguous(Integer pInteger) {
ambiguous = pInteger;
}
public void setAmbigious(int pInt) {
ambigious = (long) pInt; // Just to differentiate...
@SuppressWarnings("UnusedDeclaration")
public void setAmbiguous(int pInt) {
ambiguous = (long) pInt; // Just to differentiate...
}
public Object getAmbigious() {
return ambigious;
public Object getAmbiguous() {
return ambiguous;
}
}
}

View File

@@ -1,10 +1,14 @@
package com.twelvemonkeys.util.convert;
import com.twelvemonkeys.lang.Validate;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.net.URI;
import static org.junit.Assert.*;
/**
* DefaultConverterTestCase
* <p/>
@@ -56,7 +60,61 @@ public class DefaultConverterTestCase extends PropertyConverterAbstractTestCase
};
}
// TODO: Test boolean -> Boolean conversion
@Test
public void testConvertBooleanPrimitive() {
PropertyConverter converter = makePropertyConverter();
assertTrue((Boolean) converter.toObject("true", boolean.class, null));
assertFalse((Boolean) converter.toObject("FalsE", Boolean.TYPE, null));
}
@Test
public void testConvertShortPrimitive() {
PropertyConverter converter = makePropertyConverter();
assertEquals(1, (short) (Short) converter.toObject("1", short.class, null));
assertEquals(-2, (short) (Short) converter.toObject("-2", Short.TYPE, null));
}
@Test
public void testConvertIntPrimitive() {
PropertyConverter converter = makePropertyConverter();
assertEquals(1, (int) (Integer) converter.toObject("1", int.class, null));
assertEquals(-2, (int) (Integer) converter.toObject("-2", Integer.TYPE, null));
}
@Test
public void testConvertLongPrimitive() {
PropertyConverter converter = makePropertyConverter();
assertEquals(Long.MAX_VALUE, (long) (Long) converter.toObject("9223372036854775807", long.class, null));
assertEquals(-2, (long) (Long) converter.toObject("-2", Long.TYPE, null));
}
@Test
public void testConvertBytePrimitive() {
PropertyConverter converter = makePropertyConverter();
assertEquals(1, (byte) (Byte) converter.toObject("1", byte.class, null));
assertEquals(-2, (byte) (Byte) converter.toObject("-2", Byte.TYPE, null));
}
@Test
public void testConvertFloatPrimitive() {
PropertyConverter converter = makePropertyConverter();
assertEquals(1f, (Float) converter.toObject("1.0", float.class, null), 0);
assertEquals(-2.3456f, (Float) converter.toObject("-2.3456", Float.TYPE, null), 0);
}
@Test
public void testConvertDoublePrimitive() {
PropertyConverter converter = makePropertyConverter();
assertEquals(1d, (Double) converter.toObject("1.0", double.class, null), 0);
assertEquals(-2.3456, (Double) converter.toObject("-2.3456", Double.TYPE, null), 0);
}
@Ignore("Known issue. Why would anyone do something like this?")
@Test
public void testConvertCharPrimitive() {
PropertyConverter converter = makePropertyConverter();
assertEquals('A', (char) (Character) converter.toObject("A", char.class, null));
assertEquals('Z', (char) (Character) converter.toObject("Z", Character.TYPE, null));
}
public static class FooBar {
private final String bar;