Fixed DateConverter to not have locale-aware formats (thanks, Fredrik Gustafsson).

Rewrote test cases for better readability/new code style.
This commit is contained in:
Harald Kuhr 2011-11-01 13:11:11 +01:00
parent 8edc448bf9
commit 38b197f6c1
3 changed files with 70 additions and 63 deletions

View File

@ -38,7 +38,7 @@ import java.lang.reflect.InvocationTargetException;
* Converts strings to dates and back. * Converts strings to dates and back.
* <p/> * <p/>
* <small>This class has a static cache of {@code DateFormats}, to avoid * <small>This class has a static cache of {@code DateFormats}, to avoid
* creation and parsing of dateformats every time one is used.</small> * creation and parsing of date formats every time one is used.</small>
* *
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a> * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haku $ * @author last modified by $Author: haku $
@ -68,8 +68,7 @@ public class DateConverter extends NumberConverter {
* *
* @throws ConversionException * @throws ConversionException
*/ */
public Object toObject(String pString, Class pType, String pFormat) public Object toObject(String pString, Class pType, String pFormat) throws ConversionException {
throws ConversionException {
if (StringUtil.isEmpty(pString)) if (StringUtil.isEmpty(pString))
return null; return null;
@ -123,13 +122,13 @@ public class DateConverter extends NumberConverter {
* @see Date * @see Date
* @see java.text.DateFormat * @see java.text.DateFormat
*/ */
public String toString(Object pObject, String pFormat) public String toString(Object pObject, String pFormat) throws ConversionException {
throws ConversionException {
if (pObject == null) if (pObject == null)
return null; return null;
if (!(pObject instanceof Date)) if (!(pObject instanceof Date)) {
throw new TypeMismathException(pObject.getClass()); throw new TypeMismathException(pObject.getClass());
}
try { try {
// Convert to string, default way // Convert to string, default way
@ -139,6 +138,7 @@ public class DateConverter extends NumberConverter {
// Convert to string, using format // Convert to string, using format
DateFormat format = getDateFormat(pFormat); DateFormat format = getDateFormat(pFormat);
return format.format(pObject); return format.format(pObject);
} }
catch (RuntimeException rte) { catch (RuntimeException rte) {
@ -147,6 +147,6 @@ public class DateConverter extends NumberConverter {
} }
private DateFormat getDateFormat(String pFormat) { private DateFormat getDateFormat(String pFormat) {
return (DateFormat) getFormat(SimpleDateFormat.class, pFormat); return (DateFormat) getFormat(SimpleDateFormat.class, pFormat, Locale.US);
} }
} }

View File

@ -4,9 +4,7 @@ import com.twelvemonkeys.lang.DateUtil;
import org.junit.Test; import org.junit.Test;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.Date; import java.util.*;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/** /**
* DateConverterTestCase * DateConverterTestCase
@ -48,13 +46,16 @@ public class DateConverterTestCase extends PropertyConverterAbstractTestCase {
@Test @Test
@Override @Override
public void testConvert() { public void testConvert() {
TimeZone old = TimeZone.getDefault(); // Custom setup, to make test cases stable: Always use GMT
TimeZone oldTZ = TimeZone.getDefault();
try { try {
TimeZone.setDefault(TimeZone.getTimeZone("GMT")); TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
super.testConvert(); super.testConvert();
} }
finally { finally {
TimeZone.setDefault(old); // Restore
TimeZone.setDefault(oldTZ);
} }
} }
} }

View File

@ -1,6 +1,7 @@
package com.twelvemonkeys.util.convert; package com.twelvemonkeys.util.convert;
import com.twelvemonkeys.lang.ObjectAbstractTestCase; import com.twelvemonkeys.lang.ObjectAbstractTestCase;
import com.twelvemonkeys.lang.Validate;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
@ -35,114 +36,119 @@ public abstract class PropertyConverterAbstractTestCase extends ObjectAbstractTe
try { try {
obj = converter.toObject(test.original(), test.type(), test.format()); obj = converter.toObject(test.original(), test.type(), test.format());
assertEquals("'" + test.original() + "' converted to incorrect type", test.type(), obj.getClass()); assertEquals(String.format("'%s' converted to incorrect type", test.original()), test.type(), obj.getClass());
if (test.type().isArray()) { if (test.type().isArray()) {
assertTrue("'" + test.original() + "' not converted", arrayEquals(test.value(), obj)); assertArrayEquals0(String.format("'%s' not converted", test.original()), test.value(), obj);
} }
else { else {
assertEquals("'" + test.original() + "' not converted", test.value(), obj); assertEquals(String.format("'%s' not converted", test.original()), test.value(), obj);
} }
String result = converter.toString(test.value(), test.format()); String result = converter.toString(test.value(), test.format());
assertEquals("'" + test.converted() + "' does not match", test.converted(), result); assertEquals(String.format("'%s' does not match", test.converted()), test.converted(), result);
obj = converter.toObject(result, test.type(), test.format()); obj = converter.toObject(result, test.type(), test.format());
assertEquals("'" + test.original() + "' converted to incorrect type", test.type(), obj.getClass()); assertEquals(String.format("'%s' converted to incorrect type", test.original()), test.type(), obj.getClass());
if (test.type().isArray()) { if (test.type().isArray()) {
assertTrue("'" + test.original() + "' did not survive round trip conversion", arrayEquals(test.value(), obj)); assertArrayEquals0(String.format("'%s' did not survive round trip conversion", test.original()), test.value(), obj);
} }
else { else {
assertEquals("'" + test.original() + "' did not survive round trip conversion", test.value(), obj); assertEquals(String.format("'%s' did not survive round trip conversion", test.original()), test.value(), obj);
} }
} }
catch (ConversionException e) { catch (ConversionException e) {
e.printStackTrace(); failBecause(String.format("Converting '%s' to %s failed", test.original(), test.type()), e);
fail("Converting '" + test.original() + "' to " + test.type() + " failed: " + e.getMessage());
} }
} }
} }
// TODO: Util method? private static void assertArrayEquals0(final String message, final Object left, final Object right) {
private boolean arrayEquals(final Object left, final Object right) { Class<?> componentType = left.getClass().getComponentType();
if (left.getClass().getComponentType().isPrimitive()) { if (componentType.isPrimitive()) {
if (int.class == left.getClass().getComponentType()) { if (int.class == componentType) {
return Arrays.equals((int[]) left, (int[]) right); assertArrayEquals(message, (int[]) left, (int[]) right);
} }
if (short.class == left.getClass().getComponentType()) { else if (short.class == componentType) {
return Arrays.equals((short[]) left, (short[]) right); assertArrayEquals(message, (short[]) left, (short[]) right);
} }
if (long.class == left.getClass().getComponentType()) { else if (long.class == componentType) {
return Arrays.equals((long[]) left, (long[]) right); assertArrayEquals(message, (long[]) left, (long[]) right);
} }
if (float.class == left.getClass().getComponentType()) { else if (float.class == componentType) {
return Arrays.equals((float[]) left, (float[]) right); assertArrayEquals(message, (float[]) left, (float[]) right, 0f);
} }
if (double.class == left.getClass().getComponentType()) { else if (double.class == componentType) {
return Arrays.equals((double[]) left, (double[]) right); assertArrayEquals(message, (double[]) left, (double[]) right, 0d);
} }
if (boolean.class == left.getClass().getComponentType()) { else if (boolean.class == componentType) {
return Arrays.equals((boolean[]) left, (boolean[]) right); assertTrue(message, Arrays.equals((boolean[]) left, (boolean[]) right));
} }
if (byte.class == left.getClass().getComponentType()) { else if (byte.class == componentType) {
return Arrays.equals((byte[]) left, (byte[]) right); assertArrayEquals(message, (byte[]) left, (byte[]) right);
} }
if (char.class == left.getClass().getComponentType()) { else if (char.class == componentType) {
return Arrays.equals((char[]) left, (char[]) right); assertArrayEquals(message, (char[]) left, (char[]) right);
}
else {
fail(String.format("Unknown primitive type: %s", componentType));
} }
// Else blow up below...
} }
else {
assertArrayEquals(message, (Object[]) left, (Object[]) right);
}
}
return Arrays.equals((Object[]) left, (Object[]) right); private static void failBecause(String message, Throwable exception) {
AssertionError error = new AssertionError(message);
error.initCause(exception);
throw error;
} }
public static final class Conversion { public static final class Conversion {
private final String mStrVal; private final String strVal;
private final Object mObjVal; private final Object objVal;
private final String mFormat; private final String format;
private final String mConvertedStrVal; private final String convertedStrVal;
public Conversion(String pStrVal, Object pObjVal) { public Conversion(String pStrVal, Object pObjVal) {
this(pStrVal, pObjVal, null, null); this(pStrVal, pObjVal, null);
} }
public Conversion(String pStrVal, Object pObjVal, String pFormat) { public Conversion(String pStrVal, Object pObjVal, String pFormat) {
this(pStrVal, pObjVal, pFormat, null); this(pStrVal, pObjVal, pFormat, pStrVal);
} }
public Conversion(String pStrVal, Object pObjVal, String pFormat, String pConvertedStrVal) { public Conversion(String pStrVal, Object pObjVal, String pFormat, String pConvertedStrVal) {
if (pStrVal == null) { Validate.notNull(pStrVal, "strVal");
throw new IllegalArgumentException("pStrVal == null"); Validate.notNull(pObjVal, "objVal");
} Validate.notNull(pConvertedStrVal, "convertedStrVal");
if (pObjVal == null) {
throw new IllegalArgumentException("pObjVal == null");
}
mStrVal = pStrVal; strVal = pStrVal;
mObjVal = pObjVal; objVal = pObjVal;
mFormat = pFormat; format = pFormat;
mConvertedStrVal = pConvertedStrVal != null ? pConvertedStrVal : pStrVal; convertedStrVal = pConvertedStrVal;
} }
public String original() { public String original() {
return mStrVal; return strVal;
} }
public Object value() { public Object value() {
return mObjVal; return objVal;
} }
public Class type() { public Class type() {
return mObjVal.getClass(); return objVal.getClass();
} }
public String format() { public String format() {
return mFormat; return format;
} }
public String converted() { public String converted() {
return mConvertedStrVal; return convertedStrVal;
} }
} }
} }