mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2026-04-30 00:00:01 -04:00
Clean-up and minor changes in core classes.
Adapted new code style. No or few functional changes.
This commit is contained in:
+22
-1
@@ -1,5 +1,8 @@
|
||||
package com.twelvemonkeys.util.convert;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* DefaultConverterTestCase
|
||||
* <p/>
|
||||
@@ -20,7 +23,9 @@ public class DefaultConverterTestCase extends PropertyConverterAbstractTestCase
|
||||
new Conversion("true", Boolean.TRUE),
|
||||
new Conversion("TRUE", Boolean.TRUE, null, "true"),
|
||||
new Conversion("false", Boolean.FALSE),
|
||||
new Conversion("FALSE", new Boolean(false), null, "false"),
|
||||
new Conversion("FALSE", false, null, "false"),
|
||||
|
||||
new Conversion("2", 2),
|
||||
|
||||
// Stupid but valid
|
||||
new Conversion("fooBar", "fooBar"),
|
||||
@@ -29,6 +34,22 @@ public class DefaultConverterTestCase extends PropertyConverterAbstractTestCase
|
||||
// Stupid test class that reveres chars
|
||||
new Conversion("fooBar", new FooBar("fooBar")),
|
||||
|
||||
// String array tests
|
||||
new Conversion("foo, bar, baz", new String[] {"foo", "bar", "baz"}),
|
||||
new Conversion("foo", new String[] {"foo"}),
|
||||
new Conversion("foo;bar; baz", new String[] {"foo", "bar", "baz"}, "; ", "foo; bar; baz"),
|
||||
|
||||
// Native array tests
|
||||
new Conversion("1, 2, 3", new int[] {1, 2, 3}),
|
||||
new Conversion("-1, 42, 0", new long[] {-1, 42, 0}),
|
||||
new Conversion("true, true, false", new boolean[] {true, true, false}),
|
||||
new Conversion(".3, 4E7, .97", new float[] {.3f, 4e7f, .97f}, ", ", "0.3, 4.0E7, 0.97"),
|
||||
|
||||
// Object array test
|
||||
new Conversion("foo, bar", new FooBar[] {new FooBar("foo"), new FooBar("bar")}),
|
||||
new Conversion("/temp, /usr/local/bin", new File[] {new File("/temp"), new File("/usr/local/bin")}),
|
||||
new Conversion("file:/temp, http://java.net/", new URI[] {URI.create("file:/temp"), URI.create("http://java.net/")}),
|
||||
|
||||
// TODO: More tests
|
||||
};
|
||||
}
|
||||
|
||||
+52
-5
@@ -2,6 +2,8 @@ package com.twelvemonkeys.util.convert;
|
||||
|
||||
import com.twelvemonkeys.lang.ObjectAbstractTestCase;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* PropertyConverterAbstractTestCase
|
||||
* <p/>
|
||||
@@ -30,16 +32,28 @@ public abstract class PropertyConverterAbstractTestCase extends ObjectAbstractTe
|
||||
Object obj;
|
||||
try {
|
||||
obj = converter.toObject(test.original(), test.type(), test.format());
|
||||
assertEquals("'" + test.original() + "' convtered to incorrect type", test.type(), obj.getClass());
|
||||
assertEquals("'" + test.original() + "' not converted", test.value(), obj);
|
||||
|
||||
assertEquals("'" + test.original() + "' converted to incorrect type", test.type(), obj.getClass());
|
||||
if (test.type().isArray()) {
|
||||
assertTrue("'" + test.original() + "' not converted", arrayEquals(test.value(), obj));
|
||||
}
|
||||
else {
|
||||
assertEquals("'" + test.original() + "' not converted", test.value(), obj);
|
||||
}
|
||||
|
||||
String result = converter.toString(test.value(), test.format());
|
||||
|
||||
assertEquals("'" + test.converted() + "' does not macth", test.converted(), result);
|
||||
assertEquals("'" + test.converted() + "' does not match", test.converted(), result);
|
||||
|
||||
obj = converter.toObject(result, test.type(), test.format());
|
||||
assertEquals("'" + test.original() + "' convtered to incorrect type", test.type(), obj.getClass());
|
||||
assertEquals("'" + test.original() + "' did not survive roundrip conversion", test.value(), obj);
|
||||
assertEquals("'" + test.original() + "' converted to incorrect type", test.type(), obj.getClass());
|
||||
|
||||
if (test.type().isArray()) {
|
||||
assertTrue("'" + test.original() + "' did not survive round trip conversion", arrayEquals(test.value(), obj));
|
||||
}
|
||||
else {
|
||||
assertEquals("'" + test.original() + "' did not survive round trip conversion", test.value(), obj);
|
||||
}
|
||||
}
|
||||
catch (ConversionException e) {
|
||||
e.printStackTrace();
|
||||
@@ -48,6 +62,39 @@ public abstract class PropertyConverterAbstractTestCase extends ObjectAbstractTe
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Util method?
|
||||
private boolean arrayEquals(final Object left, final Object right) {
|
||||
if (left.getClass().getComponentType().isPrimitive()) {
|
||||
if (int.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((int[]) left, (int[]) right);
|
||||
}
|
||||
if (short.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((short[]) left, (short[]) right);
|
||||
}
|
||||
if (long.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((long[]) left, (long[]) right);
|
||||
}
|
||||
if (float.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((float[]) left, (float[]) right);
|
||||
}
|
||||
if (double.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((double[]) left, (double[]) right);
|
||||
}
|
||||
if (boolean.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((boolean[]) left, (boolean[]) right);
|
||||
}
|
||||
if (byte.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((byte[]) left, (byte[]) right);
|
||||
}
|
||||
if (char.class == left.getClass().getComponentType()) {
|
||||
return Arrays.equals((char[]) left, (char[]) right);
|
||||
}
|
||||
// Else blow up below...
|
||||
}
|
||||
|
||||
return Arrays.equals((Object[]) left, (Object[]) right);
|
||||
}
|
||||
|
||||
public static final class Conversion {
|
||||
private final String mStrVal;
|
||||
private final Object mObjVal;
|
||||
|
||||
Reference in New Issue
Block a user