You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/10/22 21:08:55 UTC
[commons-beanutils] branch master updated: Sort methods.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-beanutils.git
The following commit(s) were added to refs/heads/master by this push:
new 893b769 Sort methods.
893b769 is described below
commit 893b769d4136282ba26dfff92a1c67e443de8f7f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Oct 22 17:08:51 2019 -0400
Sort methods.
---
.../converters/ArrayConverterTestCase.java | 156 +++----
.../converters/BigDecimalConverterTestCase.java | 54 +--
.../converters/BigIntegerConverterTestCase.java | 40 +-
.../converters/BooleanConverterTestCase.java | 80 ++--
.../converters/ByteConverterTestCase.java | 106 ++---
.../converters/CalendarConverterTestCase.java | 30 +-
.../converters/CharacterConverterTestCase.java | 56 +--
.../converters/ClassConverterTestCase.java | 44 +-
.../converters/ClassReloaderTestCase.java | 6 +-
.../converters/DateConverterTestBase.java | 500 ++++++++++-----------
.../converters/DateConverterTestCase.java | 46 +-
.../converters/DoubleConverterTestCase.java | 40 +-
.../converters/FileConverterTestCase.java | 28 +-
.../converters/FloatConverterTestCase.java | 78 ++--
.../converters/IntegerConverterTestCase.java | 128 +++---
.../converters/LocalDateConverterTestCase.java | 46 +-
.../converters/LocalDateTimeConverterTestCase.java | 46 +-
.../converters/LongConverterTestCase.java | 40 +-
.../beanutils2/converters/MemoryTestCase.java | 260 +++++------
.../converters/NumberConverterTestBase.java | 388 ++++++++--------
.../OffsetDateTimeConverterTestCase.java | 46 +-
.../converters/PathConverterTestCase.java | 28 +-
.../converters/ShortConverterTestCase.java | 104 ++---
.../converters/SqlDateConverterTestCase.java | 66 +--
.../converters/SqlTimeConverterTestCase.java | 106 ++---
.../converters/SqlTimestampConverterTestCase.java | 112 ++---
.../converters/StringConverterTestCase.java | 14 +-
.../converters/URIConverterTestCase.java | 28 +-
.../converters/URLConverterTestCase.java | 28 +-
.../converters/UUIDConverterTestCase.java | 28 +-
.../converters/ZonedDateTimeConverterTestCase.java | 46 +-
31 files changed, 1389 insertions(+), 1389 deletions(-)
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/ArrayConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/ArrayConverterTestCase.java
index 353b4f4..80a8b6f 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/ArrayConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/ArrayConverterTestCase.java
@@ -30,6 +30,16 @@ import junit.framework.TestSuite;
public class ArrayConverterTestCase extends TestCase {
/**
+ * Create Test Suite
+ * @return test suite
+ */
+ public static TestSuite suite() {
+ return new TestSuite(ArrayConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
* Construct a new Array Converter test case.
* @param name Test Name
*/
@@ -37,14 +47,25 @@ public class ArrayConverterTestCase extends TestCase {
super(name);
}
- // ------------------------------------------------------------------------
-
/**
- * Create Test Suite
- * @return test suite
+ * Check that two arrays are the same.
+ * @param msg Test prefix msg
+ * @param expected Expected Array value
+ * @param result Result array value
*/
- public static TestSuite suite() {
- return new TestSuite(ArrayConverterTestCase.class);
+ private void checkArray(final String msg, final Object expected, final Object result) {
+ assertNotNull(msg + " Expected Null", expected);
+ assertNotNull(msg + " Result Null", result);
+ assertTrue(msg + " Result not array", result.getClass().isArray());
+ assertTrue(msg + " Expected not array", expected.getClass().isArray());
+ final int resultLth = Array.getLength(result);
+ assertEquals(msg + " Size", Array.getLength(expected), resultLth);
+ assertEquals(msg + " Type", expected.getClass(), result.getClass());
+ for (int i = 0; i < resultLth; i++) {
+ final Object expectElement = Array.get(expected, i);
+ final Object resultElement = Array.get(result, i);
+ assertEquals(msg + " Element " + i, expectElement, resultElement);
+ }
}
/** Set Up */
@@ -52,14 +73,14 @@ public class ArrayConverterTestCase extends TestCase {
public void setUp() throws Exception {
}
+
+ // ------------------------------------------------------------------------
+
/** Tear Down */
@Override
public void tearDown() throws Exception {
}
-
- // ------------------------------------------------------------------------
-
/**
* Test Converting using the IntegerConverter as the component Converter
*/
@@ -205,6 +226,54 @@ public class ArrayConverterTestCase extends TestCase {
}
/**
+ * Test Empty String
+ */
+ public void testEmptyString() {
+ final int[] zeroArray = new int[0];
+ final IntegerConverter intConverter = new IntegerConverter();
+
+ checkArray("Empty String", zeroArray, new ArrayConverter(int[].class, intConverter, -1).convert(int[].class, ""));
+ assertEquals("Default String", null, new ArrayConverter(int[].class, intConverter).convert(String.class, null));
+ }
+
+ /**
+ * Test Errors creating the converter
+ */
+ public void testErrors() {
+ try {
+ new ArrayConverter(null, new DateConverter());
+ fail("Default Type missing - expected IllegalArgumentException");
+ } catch (final IllegalArgumentException e) {
+ // expected result
+ }
+ try {
+ new ArrayConverter(Boolean.class, new DateConverter());
+ fail("Default Type not an array - expected IllegalArgumentException");
+ } catch (final IllegalArgumentException e) {
+ // expected result
+ }
+ try {
+ new ArrayConverter(int[].class, null);
+ fail("Component Converter missing - expected IllegalArgumentException");
+ } catch (final IllegalArgumentException e) {
+ // expected result
+ }
+ }
+
+ /**
+ * Test Converting using the IntegerConverter as the component Converter
+ */
+ public void testInvalidWithDefault() {
+ final int[] zeroArray = new int[0];
+ final int[] oneArray = new int[1];
+ final IntegerConverter intConverter = new IntegerConverter();
+
+ assertEquals("Null Default", null, new ArrayConverter(int[].class, intConverter, -1).convert(int[].class, null));
+ checkArray("Zero Length", zeroArray, new ArrayConverter(int[].class, intConverter, 0).convert(int[].class, null));
+ checkArray("One Length", oneArray, new ArrayConverter(Integer[].class, intConverter, 1).convert(int[].class, null));
+ }
+
+ /**
* Test Converting a String[] to integer array (with leading/trailing whitespace)
*/
public void testStringArrayToNumber() {
@@ -312,54 +381,6 @@ public class ArrayConverterTestCase extends TestCase {
}
/**
- * Test Converting using the IntegerConverter as the component Converter
- */
- public void testInvalidWithDefault() {
- final int[] zeroArray = new int[0];
- final int[] oneArray = new int[1];
- final IntegerConverter intConverter = new IntegerConverter();
-
- assertEquals("Null Default", null, new ArrayConverter(int[].class, intConverter, -1).convert(int[].class, null));
- checkArray("Zero Length", zeroArray, new ArrayConverter(int[].class, intConverter, 0).convert(int[].class, null));
- checkArray("One Length", oneArray, new ArrayConverter(Integer[].class, intConverter, 1).convert(int[].class, null));
- }
-
- /**
- * Test Empty String
- */
- public void testEmptyString() {
- final int[] zeroArray = new int[0];
- final IntegerConverter intConverter = new IntegerConverter();
-
- checkArray("Empty String", zeroArray, new ArrayConverter(int[].class, intConverter, -1).convert(int[].class, ""));
- assertEquals("Default String", null, new ArrayConverter(int[].class, intConverter).convert(String.class, null));
- }
-
- /**
- * Test Errors creating the converter
- */
- public void testErrors() {
- try {
- new ArrayConverter(null, new DateConverter());
- fail("Default Type missing - expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected result
- }
- try {
- new ArrayConverter(Boolean.class, new DateConverter());
- fail("Default Type not an array - expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected result
- }
- try {
- new ArrayConverter(int[].class, null);
- fail("Component Converter missing - expected IllegalArgumentException");
- } catch (final IllegalArgumentException e) {
- // expected result
- }
- }
-
- /**
* Test for BEANUTILS-302 - throwing a NPE when underscore used
*/
public void testUnderscore_BEANUTILS_302() {
@@ -385,25 +406,4 @@ public class ArrayConverterTestCase extends TestCase {
assertEquals("result[0]", "first_value", result[0]);
assertEquals("result[1]", "second_value", result[1]);
}
-
- /**
- * Check that two arrays are the same.
- * @param msg Test prefix msg
- * @param expected Expected Array value
- * @param result Result array value
- */
- private void checkArray(final String msg, final Object expected, final Object result) {
- assertNotNull(msg + " Expected Null", expected);
- assertNotNull(msg + " Result Null", result);
- assertTrue(msg + " Result not array", result.getClass().isArray());
- assertTrue(msg + " Expected not array", expected.getClass().isArray());
- final int resultLth = Array.getLength(result);
- assertEquals(msg + " Size", Array.getLength(expected), resultLth);
- assertEquals(msg + " Type", expected.getClass(), result.getClass());
- for (int i = 0; i < resultLth; i++) {
- final Object expectElement = Array.get(expected, i);
- final Object resultElement = Array.get(result, i);
- assertEquals(msg + " Element " + i, expectElement, resultElement);
- }
- }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalConverterTestCase.java
index 7a4164f..93a0de6 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/BigDecimalConverterTestCase.java
@@ -31,32 +31,33 @@ import junit.framework.TestSuite;
public class BigDecimalConverterTestCase extends NumberConverterTestBase {
- private Converter converter = null;
+ /**
+ * A class derived from {@code BigDecimal} used for testing whether
+ * derived number classes are handled correctly.
+ */
+ private class ExtendingBigDecimal extends BigDecimal {
+ private ExtendingBigDecimal(final String val) {
+ super(val);
+ }
+ }
// ------------------------------------------------------------------------
- public BigDecimalConverterTestCase(final String name) {
- super(name);
+ public static TestSuite suite() {
+ return new TestSuite(BigDecimalConverterTestCase.class);
}
// ------------------------------------------------------------------------
- @Override
- public void setUp() throws Exception {
- converter = makeConverter();
- numbers[0] = new BigDecimal("-12");
- numbers[1] = new BigDecimal("13");
- numbers[2] = new BigDecimal("-22");
- numbers[3] = new BigDecimal("23");
- }
+ private Converter converter = null;
- public static TestSuite suite() {
- return new TestSuite(BigDecimalConverterTestCase.class);
+ public BigDecimalConverterTestCase(final String name) {
+ super(name);
}
@Override
- public void tearDown() throws Exception {
- converter = null;
+ protected Class<?> getExpectedType() {
+ return BigDecimal.class;
}
// ------------------------------------------------------------------------
@@ -72,12 +73,21 @@ public class BigDecimalConverterTestCase extends NumberConverterTestBase {
}
@Override
- protected Class<?> getExpectedType() {
- return BigDecimal.class;
+ public void setUp() throws Exception {
+ converter = makeConverter();
+ numbers[0] = new BigDecimal("-12");
+ numbers[1] = new BigDecimal("13");
+ numbers[2] = new BigDecimal("-22");
+ numbers[3] = new BigDecimal("23");
}
// ------------------------------------------------------------------------
+ @Override
+ public void tearDown() throws Exception {
+ converter = null;
+ }
+
public void testSimpleConversion() throws Exception {
final String[] message= {
"from String",
@@ -138,15 +148,5 @@ public class BigDecimalConverterTestCase extends NumberConverterTestBase {
converter.convert(null,input[i]));
}
}
-
- /**
- * A class derived from {@code BigDecimal} used for testing whether
- * derived number classes are handled correctly.
- */
- private class ExtendingBigDecimal extends BigDecimal {
- private ExtendingBigDecimal(final String val) {
- super(val);
- }
- }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerConverterTestCase.java
index 9676d45..559d9e6 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/BigIntegerConverterTestCase.java
@@ -30,6 +30,12 @@ import junit.framework.TestSuite;
*/
public class BigIntegerConverterTestCase extends NumberConverterTestBase {
+ public static TestSuite suite() {
+ return new TestSuite(BigIntegerConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -38,41 +44,35 @@ public class BigIntegerConverterTestCase extends NumberConverterTestBase {
super(name);
}
- // ------------------------------------------------------------------------
-
@Override
- public void setUp() throws Exception {
- converter = makeConverter();
- numbers[0] = new BigInteger("-12");
- numbers[1] = new BigInteger("13");
- numbers[2] = new BigInteger("-22");
- numbers[3] = new BigInteger("23");
- }
-
- public static TestSuite suite() {
- return new TestSuite(BigIntegerConverterTestCase.class);
+ protected Class<?> getExpectedType() {
+ return BigInteger.class;
}
@Override
- public void tearDown() throws Exception {
- converter = null;
+ protected NumberConverter makeConverter() {
+ return new BigIntegerConverter();
}
// ------------------------------------------------------------------------
@Override
- protected NumberConverter makeConverter() {
- return new BigIntegerConverter();
+ protected NumberConverter makeConverter(final Object defaultValue) {
+ return new BigIntegerConverter(defaultValue);
}
@Override
- protected NumberConverter makeConverter(final Object defaultValue) {
- return new BigIntegerConverter(defaultValue);
+ public void setUp() throws Exception {
+ converter = makeConverter();
+ numbers[0] = new BigInteger("-12");
+ numbers[1] = new BigInteger("13");
+ numbers[2] = new BigInteger("-22");
+ numbers[3] = new BigInteger("23");
}
@Override
- protected Class<?> getExpectedType() {
- return BigInteger.class;
+ public void tearDown() throws Exception {
+ converter = null;
}
// ------------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java
index e57617b..0bafcec 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/BooleanConverterTestCase.java
@@ -38,38 +38,6 @@ public class BooleanConverterTestCase extends TestCase {
super(name);
}
- public void testStandardValues() {
- final BooleanConverter converter = new BooleanConverter();
- testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
- }
-
- public void testCaseInsensitivity() {
- final BooleanConverter converter = new BooleanConverter();
- testConversionValues(
- converter,
- new String[] {"Yes", "TRUE"},
- new String[] {"NO", "fAlSe"});
- }
-
-
- public void testInvalidString() {
- final BooleanConverter converter = new BooleanConverter();
- try {
- converter.convert(Boolean.class, "bogus");
- fail("Converting invalid string should have generated an exception");
- } catch (final ConversionException expected) {
- // Exception is successful test
- }
- }
-
- public void testDefaultValue() {
- final Object defaultValue = Boolean.TRUE;
- final BooleanConverter converter = new BooleanConverter(defaultValue);
-
- assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
- testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
- }
-
public void testAdditionalStrings() {
final String[] trueStrings = {"sure"};
final String[] falseStrings = {"nope"};
@@ -94,6 +62,15 @@ public class BooleanConverterTestCase extends TestCase {
}
}
+ public void testCaseInsensitivity() {
+ final BooleanConverter converter = new BooleanConverter();
+ testConversionValues(
+ converter,
+ new String[] {"Yes", "TRUE"},
+ new String[] {"NO", "fAlSe"});
+ }
+
+
/**
* Tests a conversion to another target type. This should not be possible.
*/
@@ -107,14 +84,6 @@ public class BooleanConverterTestCase extends TestCase {
}
}
- /**
- * Tests whether a conversion to a primitive boolean is possible.
- */
- public void testPrimitiveTargetClass() {
- final BooleanConverter converter = new BooleanConverter();
- assertTrue("Wrong result", converter.convert(Boolean.TYPE, STANDARD_TRUES[0]));
- }
-
protected void testConversionValues(final BooleanConverter converter,
final String[] trueValues, final String[] falseValues) {
@@ -126,4 +95,35 @@ public class BooleanConverterTestCase extends TestCase {
}
}
+ public void testDefaultValue() {
+ final Object defaultValue = Boolean.TRUE;
+ final BooleanConverter converter = new BooleanConverter(defaultValue);
+
+ assertSame(defaultValue, converter.convert(Boolean.class, "bogus"));
+ testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
+ }
+
+ public void testInvalidString() {
+ final BooleanConverter converter = new BooleanConverter();
+ try {
+ converter.convert(Boolean.class, "bogus");
+ fail("Converting invalid string should have generated an exception");
+ } catch (final ConversionException expected) {
+ // Exception is successful test
+ }
+ }
+
+ /**
+ * Tests whether a conversion to a primitive boolean is possible.
+ */
+ public void testPrimitiveTargetClass() {
+ final BooleanConverter converter = new BooleanConverter();
+ assertTrue("Wrong result", converter.convert(Boolean.TYPE, STANDARD_TRUES[0]));
+ }
+
+ public void testStandardValues() {
+ final BooleanConverter converter = new BooleanConverter();
+ testConversionValues(converter, STANDARD_TRUES, STANDARD_FALSES);
+ }
+
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/ByteConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/ByteConverterTestCase.java
index 3de4742..9bbaa81 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/ByteConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/ByteConverterTestCase.java
@@ -29,6 +29,12 @@ import junit.framework.TestSuite;
public class ByteConverterTestCase extends NumberConverterTestBase {
+ public static TestSuite suite() {
+ return new TestSuite(ByteConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -37,9 +43,24 @@ public class ByteConverterTestCase extends NumberConverterTestBase {
super(name);
}
+ @Override
+ protected Class<?> getExpectedType() {
+ return Byte.class;
+ }
+
+ @Override
+ protected NumberConverter makeConverter() {
+ return new ByteConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
+ protected NumberConverter makeConverter(final Object defaultValue) {
+ return new ByteConverter(defaultValue);
+ }
+
+ @Override
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = new Byte("-12");
@@ -47,11 +68,6 @@ public class ByteConverterTestCase extends NumberConverterTestBase {
numbers[2] = new Byte("-22");
numbers[3] = new Byte("23");
}
-
- public static TestSuite suite() {
- return new TestSuite(ByteConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -59,21 +75,40 @@ public class ByteConverterTestCase extends NumberConverterTestBase {
// ------------------------------------------------------------------------
- @Override
- protected NumberConverter makeConverter() {
- return new ByteConverter();
- }
+ /**
+ * Test Invalid Amounts (too big/small)
+ */
+ public void testInvalidAmount() {
+ final Converter converter = makeConverter();
+ final Class<?> clazz = Byte.class;
- @Override
- protected NumberConverter makeConverter(final Object defaultValue) {
- return new ByteConverter(defaultValue);
- }
- @Override
- protected Class<?> getExpectedType() {
- return Byte.class;
- }
+ final Long min = new Long(Byte.MIN_VALUE);
+ final Long max = new Long(Byte.MAX_VALUE);
+ final Long minMinusOne = new Long(min.longValue() - 1);
+ final Long maxPlusOne = new Long(max.longValue() + 1);
- // ------------------------------------------------------------------------
+ // Minimum
+ assertEquals("Minimum", new Byte(Byte.MIN_VALUE), converter.convert(clazz, min));
+
+ // Maximum
+ assertEquals("Maximum", new Byte(Byte.MAX_VALUE), converter.convert(clazz, max));
+
+ // Too Small
+ try {
+ assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
+ fail("Less than minimum, expected ConversionException");
+ } catch (final Exception e) {
+ // expected result
+ }
+
+ // Too Large
+ try {
+ assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
+ fail("More than maximum, expected ConversionException");
+ } catch (final Exception e) {
+ // expected result
+ }
+ }
public void testSimpleConversion() throws Exception {
final String[] message= {
@@ -131,40 +166,5 @@ public class ByteConverterTestCase extends NumberConverterTestBase {
}
}
- /**
- * Test Invalid Amounts (too big/small)
- */
- public void testInvalidAmount() {
- final Converter converter = makeConverter();
- final Class<?> clazz = Byte.class;
-
- final Long min = new Long(Byte.MIN_VALUE);
- final Long max = new Long(Byte.MAX_VALUE);
- final Long minMinusOne = new Long(min.longValue() - 1);
- final Long maxPlusOne = new Long(max.longValue() + 1);
-
- // Minimum
- assertEquals("Minimum", new Byte(Byte.MIN_VALUE), converter.convert(clazz, min));
-
- // Maximum
- assertEquals("Maximum", new Byte(Byte.MAX_VALUE), converter.convert(clazz, max));
-
- // Too Small
- try {
- assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
- fail("Less than minimum, expected ConversionException");
- } catch (final Exception e) {
- // expected result
- }
-
- // Too Large
- try {
- assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
- fail("More than maximum, expected ConversionException");
- } catch (final Exception e) {
- // expected result
- }
- }
-
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/CalendarConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/CalendarConverterTestCase.java
index fe6f7de..6f2b4c3 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/CalendarConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/CalendarConverterTestCase.java
@@ -27,6 +27,16 @@ import junit.framework.TestSuite;
public class CalendarConverterTestCase extends DateConverterTestBase {
/**
+ * Create Test Suite
+ * @return test suite
+ */
+ public static TestSuite suite() {
+ return new TestSuite(CalendarConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
* Construct a new Calendar test case.
* @param name Test Name
*/
@@ -34,14 +44,13 @@ public class CalendarConverterTestCase extends DateConverterTestBase {
super(name);
}
- // ------------------------------------------------------------------------
-
/**
- * Create Test Suite
- * @return test suite
+ * Return the expected type
+ * @return The expected type
*/
- public static TestSuite suite() {
- return new TestSuite(CalendarConverterTestCase.class);
+ @Override
+ protected Class<?> getExpectedType() {
+ return Calendar.class;
}
// ------------------------------------------------------------------------
@@ -65,15 +74,6 @@ public class CalendarConverterTestCase extends DateConverterTestBase {
}
/**
- * Return the expected type
- * @return The expected type
- */
- @Override
- protected Class<?> getExpectedType() {
- return Calendar.class;
- }
-
- /**
* Convert from a java.util.Date to the Converter's type.
*
* @param value The Date value to convert
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/CharacterConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/CharacterConverterTestCase.java
index 23dca55..1da2b12 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/CharacterConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/CharacterConverterTestCase.java
@@ -29,21 +29,21 @@ import junit.framework.TestSuite;
public class CharacterConverterTestCase extends TestCase {
/**
- * Construct a new Character Converter test case.
- * @param name Test Name
+ * Create Test Suite
+ * @return test suite
*/
- public CharacterConverterTestCase(final String name) {
- super(name);
+ public static TestSuite suite() {
+ return new TestSuite(CharacterConverterTestCase.class);
}
// ------------------------------------------------------------------------
/**
- * Create Test Suite
- * @return test suite
+ * Construct a new Character Converter test case.
+ * @param name Test Name
*/
- public static TestSuite suite() {
- return new TestSuite(CharacterConverterTestCase.class);
+ public CharacterConverterTestCase(final String name) {
+ super(name);
}
/** Set Up */
@@ -60,16 +60,11 @@ public class CharacterConverterTestCase extends TestCase {
// ------------------------------------------------------------------------
/**
- * Test Conversion to String
+ * Tests whether the primitive char class can be passed as target type.
*/
- public void testConvertToString() {
-
+ public void testConvertToChar() {
final Converter converter = new CharacterConverter();
-
- assertEquals("Character Test", "N", converter.convert(String.class, new Character('N')));
- assertEquals("String Test", "F", converter.convert(String.class, "FOO"));
- assertEquals("Integer Test", "3", converter.convert(String.class, new Integer(321)));
- assertEquals("Null Test", null, converter.convert(String.class, null));
+ assertEquals("Wrong result", new Character('F'), converter.convert(Character.TYPE, "FOO"));
}
/**
@@ -83,14 +78,6 @@ public class CharacterConverterTestCase extends TestCase {
}
/**
- * Tests whether the primitive char class can be passed as target type.
- */
- public void testConvertToChar() {
- final Converter converter = new CharacterConverter();
- assertEquals("Wrong result", new Character('F'), converter.convert(Character.TYPE, "FOO"));
- }
-
- /**
* Tests a conversion to character for null input if no default value is
* provided.
*/
@@ -105,11 +92,16 @@ public class CharacterConverterTestCase extends TestCase {
}
/**
- * Test Conversion to Character (with default)
+ * Test Conversion to String
*/
- public void testDefault() {
- final Converter converter = new CharacterConverter("C");
- assertEquals("Default Test", new Character('C'), converter.convert(Character.class, null));
+ public void testConvertToString() {
+
+ final Converter converter = new CharacterConverter();
+
+ assertEquals("Character Test", "N", converter.convert(String.class, new Character('N')));
+ assertEquals("String Test", "F", converter.convert(String.class, "FOO"));
+ assertEquals("Integer Test", "3", converter.convert(String.class, new Integer(321)));
+ assertEquals("Null Test", null, converter.convert(String.class, null));
}
/**
@@ -124,4 +116,12 @@ public class CharacterConverterTestCase extends TestCase {
// expected result
}
}
+
+ /**
+ * Test Conversion to Character (with default)
+ */
+ public void testDefault() {
+ final Converter converter = new CharacterConverter("C");
+ assertEquals("Default Test", new Character('C'), converter.convert(Character.class, null));
+ }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/ClassConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/ClassConverterTestCase.java
index cadb4b4..e68f0a9 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/ClassConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/ClassConverterTestCase.java
@@ -29,21 +29,21 @@ import junit.framework.TestSuite;
public class ClassConverterTestCase extends TestCase {
/**
- * Construct a new Class Converter test case.
- * @param name Test Name
+ * Create Test Suite
+ * @return test suite
*/
- public ClassConverterTestCase(final String name) {
- super(name);
+ public static TestSuite suite() {
+ return new TestSuite(ClassConverterTestCase.class);
}
// ------------------------------------------------------------------------
/**
- * Create Test Suite
- * @return test suite
+ * Construct a new Class Converter test case.
+ * @param name Test Name
*/
- public static TestSuite suite() {
- return new TestSuite(ClassConverterTestCase.class);
+ public ClassConverterTestCase(final String name) {
+ super(name);
}
/** Set Up */
@@ -60,15 +60,17 @@ public class ClassConverterTestCase extends TestCase {
// ------------------------------------------------------------------------
/**
- * Test Conversion to String
+ * Test Array Conversion
*/
- public void testConvertToString() {
+ public void testArray() {
final Converter converter = new ClassConverter();
- assertEquals("Class Test", "java.lang.Integer", converter.convert(String.class, Integer.class));
- assertEquals("Value Test", "foo", converter.convert(String.class, "foo"));
- assertEquals("Value Test", "bar", converter.convert(String.class, new StringBuilder("bar")));
- assertEquals("Null Test", null, converter.convert(String.class, null));
+ // Test Array Class to String
+ assertEquals("Array to String", "[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class));
+
+ // *** N.B. for some reason the following works on m1, but not m2
+ // Test String to Array Class
+ // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
}
/**
@@ -121,17 +123,15 @@ public class ClassConverterTestCase extends TestCase {
}
/**
- * Test Array Conversion
+ * Test Conversion to String
*/
- public void testArray() {
+ public void testConvertToString() {
final Converter converter = new ClassConverter();
- // Test Array Class to String
- assertEquals("Array to String", "[Ljava.lang.Boolean;", converter.convert(String.class, Boolean[].class));
-
- // *** N.B. for some reason the following works on m1, but not m2
- // Test String to Array Class
- // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
+ assertEquals("Class Test", "java.lang.Integer", converter.convert(String.class, Integer.class));
+ assertEquals("Value Test", "foo", converter.convert(String.class, "foo"));
+ assertEquals("Value Test", "bar", converter.convert(String.class, new StringBuilder("bar")));
+ assertEquals("Null Test", null, converter.convert(String.class, null));
}
/**
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/ClassReloaderTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/ClassReloaderTestCase.java
index a104cbd..c3dd0bd 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/ClassReloaderTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/ClassReloaderTestCase.java
@@ -29,8 +29,7 @@ public class ClassReloaderTestCase extends TestCase {
// ------------------------------------------------------------------------
- public ClassReloaderTestCase(final String name) {
- super(name);
+ public static class DummyClass {
}
@@ -40,7 +39,8 @@ public class ClassReloaderTestCase extends TestCase {
// ------------------------------------------------------------------------
- public static class DummyClass {
+ public ClassReloaderTestCase(final String name) {
+ super(name);
}
/**
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestBase.java b/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestBase.java
index b04e085..33e10e8 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestBase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestBase.java
@@ -55,43 +55,99 @@ public abstract class DateConverterTestBase extends TestCase {
// ------------------------------------------------------------------------
/**
- * Create the Converter with no default value.
- * @return A new Converter
+ * Return the expected type
+ * @return The expected type
*/
- protected abstract DateTimeConverter makeConverter();
+ protected abstract Class<?> getExpectedType();
/**
- * Create the Converter with a default value.
- * @param defaultValue The default value
- * @return A new Converter
+ * Convert a Date or Calendar objects to the time in millisconds
+ * @param date The date or calendar object
+ * @return The time in milliseconds
*/
- protected abstract DateTimeConverter makeConverter(Object defaultValue);
+ long getTimeInMillis(final Object date) {
+
+ if (date instanceof java.sql.Timestamp) {
+ // ---------------------- JDK 1.3 Fix ----------------------
+ // N.B. Prior to JDK 1.4 the Timestamp's getTime() method
+ // didn't include the milliseconds. The following code
+ // ensures it works consistently accross JDK versions
+ final java.sql.Timestamp timestamp = (java.sql.Timestamp)date;
+ long timeInMillis = timestamp.getTime() / 1000 * 1000;
+ timeInMillis += timestamp.getNanos() / 1000000;
+ return timeInMillis;
+ }
+
+ if (date instanceof LocalDate) {
+ return ((LocalDate)date).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
+ }
+
+ if (date instanceof LocalDateTime) {
+ return ((LocalDateTime)date).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
+ }
+
+ if (date instanceof ZonedDateTime) {
+ return ((ZonedDateTime)date).toInstant().toEpochMilli();
+ }
+
+ if (date instanceof OffsetDateTime) {
+ return ((OffsetDateTime)date).toInstant().toEpochMilli();
+ }
+
+ if (date instanceof Calendar) {
+ return ((Calendar)date).getTime().getTime();
+ }
+ return ((Date)date).getTime();
+ }
/**
- * Return the expected type
- * @return The expected type
+ * Test Conversion Error
+ * @param converter The converter to use
+ * @param value The value to convert
*/
- protected abstract Class<?> getExpectedType();
+ void invalidConversion(final Converter converter, final Object value) {
+ final String valueType = value == null ? "null" : value.getClass().getName();
+ final String msg = "Converting '" + valueType + "' value '" + value + "'";
+ try {
+ final Object result = converter.convert(getExpectedType(), value);
+ fail(msg + ", expected ConversionException, but result = '" + result + "'");
+ } catch (final ConversionException ex) {
+ // Expected Result
+ }
+ }
/**
- * Convert from a Calendar to the appropriate Date type
- *
- * @param value The Calendar value to convert
- * @return The converted value
+ * Create the Converter with no default value.
+ * @return A new Converter
*/
- protected abstract Object toType(Calendar value);
+ protected abstract DateTimeConverter makeConverter();
// ------------------------------------------------------------------------
/**
- * Assumes ConversionException in response to covert(getExpectedType(), null).
+ * Create the Converter with a default value.
+ * @param defaultValue The default value
+ * @return A new Converter
*/
- public void testConvertNull() {
+ protected abstract DateTimeConverter makeConverter(Object defaultValue);
+
+ /**
+ * Test Conversion to String
+ * @param converter The converter to use
+ * @param expected The expected result
+ * @param value The value to convert
+ */
+ void stringConversion(final Converter converter, final String expected, final Object value) {
+ final String valueType = value == null ? "null" : value.getClass().getName();
+ final String msg = "Converting '" + valueType + "' value '" + value + "' to String";
try {
- makeConverter().convert(getExpectedType(), null);
- fail("Expected ConversionException");
- } catch(final ConversionException e) {
- // expected
+ final Object result = converter.convert(String.class, value);
+ final Class<?> resultType = result == null ? null : result.getClass();
+ final Class<?> expectType = expected == null ? null : expected.getClass();
+ assertEquals("TYPE " + msg, expectType, resultType);
+ assertEquals("VALUE " + msg, expected, result);
+ } catch (final Exception ex) {
+ fail(msg + " threw " + ex.toString());
}
}
@@ -146,6 +202,38 @@ public abstract class DateConverterTestBase extends TestCase {
}
/**
+ * Assumes ConversionException in response to covert(getExpectedType(), null).
+ */
+ public void testConvertNull() {
+ try {
+ makeConverter().convert(getExpectedType(), null);
+ fail("Expected ConversionException");
+ } catch(final ConversionException e) {
+ // expected
+ }
+ }
+
+ /**
+ * Test default String to type conversion
+ *
+ * N.B. This method is overridden by test case
+ * implementations for java.sql.Date/Time/Timestamp
+ */
+ public void testDefaultStringToTypeConvert() {
+
+ // Create & Configure the Converter
+ final DateTimeConverter converter = makeConverter();
+ converter.setUseLocaleFormat(false);
+ try {
+ converter.convert(getExpectedType(), "2006-10-23");
+ fail("Expected Conversion exception");
+ } catch (final ConversionException e) {
+ // expected result
+ }
+
+ }
+
+ /**
* Test Default Type conversion (i.e. don't specify target type)
*/
public void testDefaultType() {
@@ -170,63 +258,108 @@ public abstract class DateConverterTestBase extends TestCase {
}
/**
- * Test default String to type conversion
- *
- * N.B. This method is overridden by test case
- * implementations for java.sql.Date/Time/Timestamp
+ * Test Converter with types it can't handle
*/
- public void testDefaultStringToTypeConvert() {
+ public void testInvalidType() {
// Create & Configure the Converter
final DateTimeConverter converter = makeConverter();
- converter.setUseLocaleFormat(false);
+
+ // Invalid Class Type
try {
- converter.convert(getExpectedType(), "2006-10-23");
- fail("Expected Conversion exception");
+ converter.convert(Character.class, new Date());
+ fail("Requested Character.class conversion, expected ConversionException");
} catch (final ConversionException e) {
- // expected result
+ // Expected result
}
-
}
/**
- * Test Conversion to String
+ * Test Date Converter with no default value
*/
- public void testStringConversion() {
+ public void testLocale() {
- final String pattern = "yyyy-MM-dd";
+ // Re-set the default Locale to Locale.US
+ final Locale defaultLocale = Locale.getDefault();
+ Locale.setDefault(Locale.US);
+
+ final String pattern = "M/d/yy"; // SHORT style date format for US Locale
// Create & Configure the Converter
final DateTimeConverter converter = makeConverter();
- converter.setPattern(pattern);
+ converter.setUseLocaleFormat(true);
- // Create Values
- final String expected = "2006-10-29";
- final Calendar calendar = toCalendar(expected, pattern, null);
+ // Valid String --> Type Conversion
+ final String testString = "10/28/06";
+ final Object expected = toType(testString, pattern, null);
+ validConversion(converter, expected, testString);
- // Type --> String Conversion
- stringConversion(converter, expected, toType(calendar));
+ // Invalid Conversions
+ invalidConversion(converter, null);
+ invalidConversion(converter, "");
+ invalidConversion(converter, "2006-10-2X");
+ invalidConversion(converter, "10.28.06");
+ invalidConversion(converter, "10-28-06");
+ invalidConversion(converter, new Integer(2));
- // Calendar --> String Conversion
- stringConversion(converter, expected, calendar);
+ // Restore the default Locale
+ Locale.setDefault(defaultLocale);
- // java.util.Date --> String Conversion
- stringConversion(converter, expected, toDate(calendar));
+ }
- // java.sql.Date --> String Conversion
- stringConversion(converter, expected, toSqlDate(calendar));
+ /**
+ * Test Converter with multiple patterns
+ */
+ public void testMultiplePatterns() {
+ String testString = null;
+ Object expected = null;
- // java.sql.Timestamp --> String Conversion
- stringConversion(converter, expected, toSqlTimestamp(calendar));
+ // Create & Configure the Converter
+ final String[] patterns = new String[] {"yyyy-MM-dd", "yyyy/MM/dd"};
+ final DateTimeConverter converter = makeConverter();
+ converter.setPatterns(patterns);
- // java.sql.Time --> String Conversion
- stringConversion(converter, expected, toSqlTime(calendar));
+ // First Pattern
+ testString = "2006-10-28";
+ expected = toType(testString, patterns[0], null);
+ validConversion(converter, expected, testString);
- // java.time.LocalDateTime --> String Conversion
- stringConversion(converter, expected, toLocalDateTime(calendar));
+ // Second pattern
+ testString = "2006/10/18";
+ expected = toType(testString, patterns[1], null);
+ validConversion(converter, expected, testString);
- stringConversion(converter, null, null);
- stringConversion(converter, "", "");
+ // Invalid Conversion
+ invalidConversion(converter, "17/03/2006");
+ invalidConversion(converter, "17.03.2006");
+
+ }
+
+ /**
+ * Test Converter with no default value
+ */
+ public void testPatternDefault() {
+
+ final String pattern = "yyyy-MM-dd";
+
+ // Create & Configure the Converter
+ final Object defaultValue = toType("2000-01-01", pattern, null);
+ assertNotNull("Check default date", defaultValue);
+ final DateTimeConverter converter = makeConverter(defaultValue);
+ converter.setPattern(pattern);
+
+ // Valid String --> Type Conversion
+ final String testString = "2006-10-29";
+ final Object expected = toType(testString, pattern, null);
+ validConversion(converter, expected, testString);
+
+ // Invalid Values, expect default value
+ validConversion(converter, defaultValue, null);
+ validConversion(converter, defaultValue, "");
+ validConversion(converter, defaultValue, "2006-10-2X");
+ validConversion(converter, defaultValue, "2006/10/01");
+ validConversion(converter, defaultValue, "02/10/06");
+ validConversion(converter, defaultValue, new Integer(2));
}
@@ -276,34 +409,6 @@ public abstract class DateConverterTestBase extends TestCase {
/**
* Test Converter with no default value
*/
- public void testPatternDefault() {
-
- final String pattern = "yyyy-MM-dd";
-
- // Create & Configure the Converter
- final Object defaultValue = toType("2000-01-01", pattern, null);
- assertNotNull("Check default date", defaultValue);
- final DateTimeConverter converter = makeConverter(defaultValue);
- converter.setPattern(pattern);
-
- // Valid String --> Type Conversion
- final String testString = "2006-10-29";
- final Object expected = toType(testString, pattern, null);
- validConversion(converter, expected, testString);
-
- // Invalid Values, expect default value
- validConversion(converter, defaultValue, null);
- validConversion(converter, defaultValue, "");
- validConversion(converter, defaultValue, "2006-10-2X");
- validConversion(converter, defaultValue, "2006/10/01");
- validConversion(converter, defaultValue, "02/10/06");
- validConversion(converter, defaultValue, new Integer(2));
-
- }
-
- /**
- * Test Converter with no default value
- */
public void testPatternNullDefault() {
final String pattern = "yyyy-MM-dd";
@@ -329,149 +434,44 @@ public abstract class DateConverterTestBase extends TestCase {
}
/**
- * Test Converter with multiple patterns
- */
- public void testMultiplePatterns() {
- String testString = null;
- Object expected = null;
-
- // Create & Configure the Converter
- final String[] patterns = new String[] {"yyyy-MM-dd", "yyyy/MM/dd"};
- final DateTimeConverter converter = makeConverter();
- converter.setPatterns(patterns);
-
- // First Pattern
- testString = "2006-10-28";
- expected = toType(testString, patterns[0], null);
- validConversion(converter, expected, testString);
-
- // Second pattern
- testString = "2006/10/18";
- expected = toType(testString, patterns[1], null);
- validConversion(converter, expected, testString);
-
- // Invalid Conversion
- invalidConversion(converter, "17/03/2006");
- invalidConversion(converter, "17.03.2006");
-
- }
-
- /**
- * Test Date Converter with no default value
+ * Test Conversion to String
*/
- public void testLocale() {
-
- // Re-set the default Locale to Locale.US
- final Locale defaultLocale = Locale.getDefault();
- Locale.setDefault(Locale.US);
+ public void testStringConversion() {
- final String pattern = "M/d/yy"; // SHORT style date format for US Locale
+ final String pattern = "yyyy-MM-dd";
// Create & Configure the Converter
final DateTimeConverter converter = makeConverter();
- converter.setUseLocaleFormat(true);
-
- // Valid String --> Type Conversion
- final String testString = "10/28/06";
- final Object expected = toType(testString, pattern, null);
- validConversion(converter, expected, testString);
+ converter.setPattern(pattern);
- // Invalid Conversions
- invalidConversion(converter, null);
- invalidConversion(converter, "");
- invalidConversion(converter, "2006-10-2X");
- invalidConversion(converter, "10.28.06");
- invalidConversion(converter, "10-28-06");
- invalidConversion(converter, new Integer(2));
+ // Create Values
+ final String expected = "2006-10-29";
+ final Calendar calendar = toCalendar(expected, pattern, null);
- // Restore the default Locale
- Locale.setDefault(defaultLocale);
+ // Type --> String Conversion
+ stringConversion(converter, expected, toType(calendar));
- }
+ // Calendar --> String Conversion
+ stringConversion(converter, expected, calendar);
- /**
- * Test Converter with types it can't handle
- */
- public void testInvalidType() {
+ // java.util.Date --> String Conversion
+ stringConversion(converter, expected, toDate(calendar));
- // Create & Configure the Converter
- final DateTimeConverter converter = makeConverter();
+ // java.sql.Date --> String Conversion
+ stringConversion(converter, expected, toSqlDate(calendar));
- // Invalid Class Type
- try {
- converter.convert(Character.class, new Date());
- fail("Requested Character.class conversion, expected ConversionException");
- } catch (final ConversionException e) {
- // Expected result
- }
- }
+ // java.sql.Timestamp --> String Conversion
+ stringConversion(converter, expected, toSqlTimestamp(calendar));
- /**
- * Test Conversion to the required type
- * @param converter The converter to use
- * @param expected The expected result
- * @param value The value to convert
- */
- void validConversion(final Converter converter, final Object expected, final Object value) {
- final String valueType = value == null ? "null" : value.getClass().getName();
- final String msg = "Converting '" + valueType + "' value '" + value + "'";
- try {
- final Object result = converter.convert(getExpectedType(), value);
- final Class<?> resultType = result == null ? null : result.getClass();
- final Class<?> expectType = expected == null ? null : expected.getClass();
- assertEquals("TYPE " + msg, expectType, resultType);
- assertEquals("VALUE " + msg, expected, result);
- } catch (final Exception ex) {
- fail(msg + " threw " + ex.toString());
- }
- }
+ // java.sql.Time --> String Conversion
+ stringConversion(converter, expected, toSqlTime(calendar));
- /**
- * Test Conversion to String
- * @param converter The converter to use
- * @param expected The expected result
- * @param value The value to convert
- */
- void stringConversion(final Converter converter, final String expected, final Object value) {
- final String valueType = value == null ? "null" : value.getClass().getName();
- final String msg = "Converting '" + valueType + "' value '" + value + "' to String";
- try {
- final Object result = converter.convert(String.class, value);
- final Class<?> resultType = result == null ? null : result.getClass();
- final Class<?> expectType = expected == null ? null : expected.getClass();
- assertEquals("TYPE " + msg, expectType, resultType);
- assertEquals("VALUE " + msg, expected, result);
- } catch (final Exception ex) {
- fail(msg + " threw " + ex.toString());
- }
- }
+ // java.time.LocalDateTime --> String Conversion
+ stringConversion(converter, expected, toLocalDateTime(calendar));
- /**
- * Test Conversion Error
- * @param converter The converter to use
- * @param value The value to convert
- */
- void invalidConversion(final Converter converter, final Object value) {
- final String valueType = value == null ? "null" : value.getClass().getName();
- final String msg = "Converting '" + valueType + "' value '" + value + "'";
- try {
- final Object result = converter.convert(getExpectedType(), value);
- fail(msg + ", expected ConversionException, but result = '" + result + "'");
- } catch (final ConversionException ex) {
- // Expected Result
- }
- }
+ stringConversion(converter, null, null);
+ stringConversion(converter, "", "");
- /**
- * Parse a String value to the required type
- * @param value The String value to parse
- * @param pattern The date pattern
- * @param locale The locale to use (or null)
- * @return parsed Calendar value
- */
- Object toType(final String value, final String pattern, final Locale locale) {
- final Calendar calendar = toCalendar(value, pattern, locale);
- return toType(calendar);
}
/**
@@ -507,6 +507,15 @@ public abstract class DateConverterTestBase extends TestCase {
}
/**
+ * Convert a Calendar to a java.time.LocalDateTime
+ * @param calendar The calendar object to convert
+ * @return The converted java.time.LocalDate
+ */
+ LocalDateTime toLocalDateTime(final Calendar calendar) {
+ return Instant.ofEpochMilli(calendar.getTimeInMillis()).atZone(ZoneId.systemDefault()).toLocalDateTime();
+ }
+
+ /**
* Convert a Calendar to a java.sql.Date
* @param calendar The calendar object to convert
* @return The converted java.sql.Date
@@ -534,51 +543,42 @@ public abstract class DateConverterTestBase extends TestCase {
}
/**
- * Convert a Calendar to a java.time.LocalDateTime
- * @param calendar The calendar object to convert
- * @return The converted java.time.LocalDate
+ * Convert from a Calendar to the appropriate Date type
+ *
+ * @param value The Calendar value to convert
+ * @return The converted value
*/
- LocalDateTime toLocalDateTime(final Calendar calendar) {
- return Instant.ofEpochMilli(calendar.getTimeInMillis()).atZone(ZoneId.systemDefault()).toLocalDateTime();
- }
+ protected abstract Object toType(Calendar value);
/**
- * Convert a Date or Calendar objects to the time in millisconds
- * @param date The date or calendar object
- * @return The time in milliseconds
+ * Parse a String value to the required type
+ * @param value The String value to parse
+ * @param pattern The date pattern
+ * @param locale The locale to use (or null)
+ * @return parsed Calendar value
*/
- long getTimeInMillis(final Object date) {
-
- if (date instanceof java.sql.Timestamp) {
- // ---------------------- JDK 1.3 Fix ----------------------
- // N.B. Prior to JDK 1.4 the Timestamp's getTime() method
- // didn't include the milliseconds. The following code
- // ensures it works consistently accross JDK versions
- final java.sql.Timestamp timestamp = (java.sql.Timestamp)date;
- long timeInMillis = timestamp.getTime() / 1000 * 1000;
- timeInMillis += timestamp.getNanos() / 1000000;
- return timeInMillis;
- }
-
- if (date instanceof LocalDate) {
- return ((LocalDate)date).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
-
- if (date instanceof LocalDateTime) {
- return ((LocalDateTime)date).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
- }
-
- if (date instanceof ZonedDateTime) {
- return ((ZonedDateTime)date).toInstant().toEpochMilli();
- }
-
- if (date instanceof OffsetDateTime) {
- return ((OffsetDateTime)date).toInstant().toEpochMilli();
- }
+ Object toType(final String value, final String pattern, final Locale locale) {
+ final Calendar calendar = toCalendar(value, pattern, locale);
+ return toType(calendar);
+ }
- if (date instanceof Calendar) {
- return ((Calendar)date).getTime().getTime();
+ /**
+ * Test Conversion to the required type
+ * @param converter The converter to use
+ * @param expected The expected result
+ * @param value The value to convert
+ */
+ void validConversion(final Converter converter, final Object expected, final Object value) {
+ final String valueType = value == null ? "null" : value.getClass().getName();
+ final String msg = "Converting '" + valueType + "' value '" + value + "'";
+ try {
+ final Object result = converter.convert(getExpectedType(), value);
+ final Class<?> resultType = result == null ? null : result.getClass();
+ final Class<?> expectType = expected == null ? null : expected.getClass();
+ assertEquals("TYPE " + msg, expectType, resultType);
+ assertEquals("VALUE " + msg, expected, result);
+ } catch (final Exception ex) {
+ fail(msg + " threw " + ex.toString());
}
- return ((Date)date).getTime();
}
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestCase.java
index b1fe62f..71cd21d 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/DateConverterTestCase.java
@@ -28,16 +28,6 @@ import junit.framework.TestSuite;
public class DateConverterTestCase extends DateConverterTestBase {
/**
- * Construct a new Date test case.
- * @param name Test Name
- */
- public DateConverterTestCase(final String name) {
- super(name);
- }
-
- // ------------------------------------------------------------------------
-
- /**
* Create Test Suite
* @return test suite
*/
@@ -45,18 +35,25 @@ public class DateConverterTestCase extends DateConverterTestBase {
return new TestSuite(DateConverterTestCase.class);
}
- /** Set Up */
- @Override
- public void setUp() throws Exception {
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a new Date test case.
+ * @param name Test Name
+ */
+ public DateConverterTestCase(final String name) {
+ super(name);
}
- /** Tear Down */
+ /**
+ * Return the expected type
+ * @return The expected type
+ */
@Override
- public void tearDown() throws Exception {
+ protected Class<?> getExpectedType() {
+ return Date.class;
}
- // ------------------------------------------------------------------------
-
/**
* Create the Converter with no default value.
* @return A new Converter
@@ -66,6 +63,8 @@ public class DateConverterTestCase extends DateConverterTestBase {
return new DateConverter();
}
+ // ------------------------------------------------------------------------
+
/**
* Create the Converter with a default value.
* @param defaultValue The default value
@@ -76,13 +75,14 @@ public class DateConverterTestCase extends DateConverterTestBase {
return new DateConverter(defaultValue);
}
- /**
- * Return the expected type
- * @return The expected type
- */
+ /** Set Up */
@Override
- protected Class<?> getExpectedType() {
- return Date.class;
+ public void setUp() throws Exception {
+ }
+
+ /** Tear Down */
+ @Override
+ public void tearDown() throws Exception {
}
/**
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/DoubleConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/DoubleConverterTestCase.java
index 1299ba1..20e07eb 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/DoubleConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/DoubleConverterTestCase.java
@@ -29,6 +29,12 @@ import junit.framework.TestSuite;
public class DoubleConverterTestCase extends NumberConverterTestBase {
+ public static TestSuite suite() {
+ return new TestSuite(DoubleConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -37,41 +43,35 @@ public class DoubleConverterTestCase extends NumberConverterTestBase {
super(name);
}
- // ------------------------------------------------------------------------
-
@Override
- public void setUp() throws Exception {
- converter = makeConverter();
- numbers[0] = new Double("-12");
- numbers[1] = new Double("13");
- numbers[2] = new Double("-22");
- numbers[3] = new Double("23");
- }
-
- public static TestSuite suite() {
- return new TestSuite(DoubleConverterTestCase.class);
+ protected Class<?> getExpectedType() {
+ return Double.class;
}
@Override
- public void tearDown() throws Exception {
- converter = null;
+ protected NumberConverter makeConverter() {
+ return new DoubleConverter();
}
// ------------------------------------------------------------------------
@Override
- protected NumberConverter makeConverter() {
- return new DoubleConverter();
+ protected NumberConverter makeConverter(final Object defaultValue) {
+ return new DoubleConverter(defaultValue);
}
@Override
- protected NumberConverter makeConverter(final Object defaultValue) {
- return new DoubleConverter(defaultValue);
+ public void setUp() throws Exception {
+ converter = makeConverter();
+ numbers[0] = new Double("-12");
+ numbers[1] = new Double("13");
+ numbers[2] = new Double("-22");
+ numbers[3] = new Double("23");
}
@Override
- protected Class<?> getExpectedType() {
- return Double.class;
+ public void tearDown() throws Exception {
+ converter = null;
}
// ------------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/FileConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/FileConverterTestCase.java
index 9a52c0a..8bb8c2e 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/FileConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/FileConverterTestCase.java
@@ -33,6 +33,12 @@ import junit.framework.TestSuite;
public class FileConverterTestCase extends TestCase {
+ public static TestSuite suite() {
+ return new TestSuite(FileConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -41,6 +47,14 @@ public class FileConverterTestCase extends TestCase {
super(name);
}
+ protected Class<?> getExpectedType() {
+ return File.class;
+ }
+
+ protected Converter makeConverter() {
+ return new FileConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
@@ -48,10 +62,6 @@ public class FileConverterTestCase extends TestCase {
converter = makeConverter();
}
- public static TestSuite suite() {
- return new TestSuite(FileConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -59,16 +69,6 @@ public class FileConverterTestCase extends TestCase {
// ------------------------------------------------------------------------
- protected Converter makeConverter() {
- return new FileConverter();
- }
-
- protected Class<?> getExpectedType() {
- return File.class;
- }
-
- // ------------------------------------------------------------------------
-
public void testSimpleConversion() throws Exception {
final String[] message= {
"from String",
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/FloatConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/FloatConverterTestCase.java
index b6cfa6e..03b5176 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/FloatConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/FloatConverterTestCase.java
@@ -29,6 +29,12 @@ import junit.framework.TestSuite;
public class FloatConverterTestCase extends NumberConverterTestBase {
+ public static TestSuite suite() {
+ return new TestSuite(FloatConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -37,9 +43,24 @@ public class FloatConverterTestCase extends NumberConverterTestBase {
super(name);
}
+ @Override
+ protected Class<?> getExpectedType() {
+ return Float.class;
+ }
+
+ @Override
+ protected NumberConverter makeConverter() {
+ return new FloatConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
+ protected NumberConverter makeConverter(final Object defaultValue) {
+ return new FloatConverter(defaultValue);
+ }
+
+ @Override
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = new Float("-12");
@@ -48,10 +69,6 @@ public class FloatConverterTestCase extends NumberConverterTestBase {
numbers[3] = new Float("23");
}
- public static TestSuite suite() {
- return new TestSuite(FloatConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -59,22 +76,28 @@ public class FloatConverterTestCase extends NumberConverterTestBase {
// ------------------------------------------------------------------------
- @Override
- protected NumberConverter makeConverter() {
- return new FloatConverter();
- }
+ /**
+ * Test Invalid Amounts (too big/small)
+ */
+ public void testInvalidAmount() {
+ final Converter converter = makeConverter();
+ final Class<?> clazz = Float.class;
- @Override
- protected NumberConverter makeConverter(final Object defaultValue) {
- return new FloatConverter(defaultValue);
- }
+ final Double max = new Double(Float.MAX_VALUE);
+ final Double tooBig = new Double(Double.MAX_VALUE);
- @Override
- protected Class<?> getExpectedType() {
- return Float.class;
+ // Maximum
+ assertEquals("Maximum", new Float(Float.MAX_VALUE), converter.convert(clazz, max));
+
+ // Too Large
+ try {
+ assertEquals("Too Big", null, converter.convert(clazz, tooBig));
+ fail("More than maximum, expected ConversionException");
+ } catch (final Exception e) {
+ // expected result
+ }
}
- // ------------------------------------------------------------------------
public void testSimpleConversion() throws Exception {
final String[] message= {
@@ -143,28 +166,5 @@ public class FloatConverterTestCase extends NumberConverterTestBase {
0.00001);
}
}
-
-
- /**
- * Test Invalid Amounts (too big/small)
- */
- public void testInvalidAmount() {
- final Converter converter = makeConverter();
- final Class<?> clazz = Float.class;
-
- final Double max = new Double(Float.MAX_VALUE);
- final Double tooBig = new Double(Double.MAX_VALUE);
-
- // Maximum
- assertEquals("Maximum", new Float(Float.MAX_VALUE), converter.convert(clazz, max));
-
- // Too Large
- try {
- assertEquals("Too Big", null, converter.convert(clazz, tooBig));
- fail("More than maximum, expected ConversionException");
- } catch (final Exception e) {
- // expected result
- }
- }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/IntegerConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/IntegerConverterTestCase.java
index 201fc63..71d4641 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/IntegerConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/IntegerConverterTestCase.java
@@ -30,6 +30,12 @@ import junit.framework.TestSuite;
public class IntegerConverterTestCase extends NumberConverterTestBase {
+ public static TestSuite suite() {
+ return new TestSuite(IntegerConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -38,9 +44,24 @@ public class IntegerConverterTestCase extends NumberConverterTestBase {
super(name);
}
+ @Override
+ protected Class<?> getExpectedType() {
+ return Integer.class;
+ }
+
+ @Override
+ protected NumberConverter makeConverter() {
+ return new IntegerConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
+ protected NumberConverter makeConverter(final Object defaultValue) {
+ return new IntegerConverter(defaultValue);
+ }
+
+ @Override
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = new Integer("-12");
@@ -49,10 +70,6 @@ public class IntegerConverterTestCase extends NumberConverterTestBase {
numbers[3] = new Integer("23");
}
- public static TestSuite suite() {
- return new TestSuite(IntegerConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -60,22 +77,53 @@ public class IntegerConverterTestCase extends NumberConverterTestBase {
// ------------------------------------------------------------------------
- @Override
- protected NumberConverter makeConverter() {
- return new IntegerConverter();
- }
+ /**
+ * Test Invalid Amounts (too big/small)
+ */
+ public void testInvalidAmount() {
+ final Converter converter = makeConverter();
+ final Class<?> clazz = Integer.class;
- @Override
- protected NumberConverter makeConverter(final Object defaultValue) {
- return new IntegerConverter(defaultValue);
- }
+ final Long min = new Long(Integer.MIN_VALUE);
+ final Long max = new Long(Integer.MAX_VALUE);
+ final Long minMinusOne = new Long(min.longValue() - 1);
+ final Long maxPlusOne = new Long(max.longValue() + 1);
- @Override
- protected Class<?> getExpectedType() {
- return Integer.class;
+ // Minimum
+ assertEquals("Minimum", new Integer(Integer.MIN_VALUE), converter.convert(clazz, min));
+
+ // Maximum
+ assertEquals("Maximum", new Integer(Integer.MAX_VALUE), converter.convert(clazz, max));
+
+ // Too Small
+ try {
+ assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
+ fail("Less than minimum, expected ConversionException");
+ } catch (final Exception e) {
+ // expected result
+ }
+
+ // Too Large
+ try {
+ assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
+ fail("More than maximum, expected ConversionException");
+ } catch (final Exception e) {
+ // expected result
+ }
}
- // ------------------------------------------------------------------------
+ /**
+ * Tests whether an invalid default object causes an exception.
+ */
+ public void testInvalidDefaultObject() {
+ final NumberConverter converter = makeConverter();
+ try {
+ converter.setDefaultValue("notANumber");
+ fail("Invalid default value not detected!");
+ } catch (final ConversionException cex) {
+ // expected result
+ }
+ }
public void testSimpleConversion() throws Exception {
final String[] message= {
@@ -132,53 +180,5 @@ public class IntegerConverterTestCase extends NumberConverterTestBase {
assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
}
}
-
- /**
- * Test Invalid Amounts (too big/small)
- */
- public void testInvalidAmount() {
- final Converter converter = makeConverter();
- final Class<?> clazz = Integer.class;
-
- final Long min = new Long(Integer.MIN_VALUE);
- final Long max = new Long(Integer.MAX_VALUE);
- final Long minMinusOne = new Long(min.longValue() - 1);
- final Long maxPlusOne = new Long(max.longValue() + 1);
-
- // Minimum
- assertEquals("Minimum", new Integer(Integer.MIN_VALUE), converter.convert(clazz, min));
-
- // Maximum
- assertEquals("Maximum", new Integer(Integer.MAX_VALUE), converter.convert(clazz, max));
-
- // Too Small
- try {
- assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
- fail("Less than minimum, expected ConversionException");
- } catch (final Exception e) {
- // expected result
- }
-
- // Too Large
- try {
- assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
- fail("More than maximum, expected ConversionException");
- } catch (final Exception e) {
- // expected result
- }
- }
-
- /**
- * Tests whether an invalid default object causes an exception.
- */
- public void testInvalidDefaultObject() {
- final NumberConverter converter = makeConverter();
- try {
- converter.setDefaultValue("notANumber");
- fail("Invalid default value not detected!");
- } catch (final ConversionException cex) {
- // expected result
- }
- }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/LocalDateConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/LocalDateConverterTestCase.java
index 277e82f..3f05e24 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/LocalDateConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/LocalDateConverterTestCase.java
@@ -30,16 +30,6 @@ import junit.framework.TestSuite;
public class LocalDateConverterTestCase extends DateConverterTestBase {
/**
- * Construct a new Date test case.
- * @param name Test Name
- */
- public LocalDateConverterTestCase(final String name) {
- super(name);
- }
-
- // ------------------------------------------------------------------------
-
- /**
* Create Test Suite
* @return test suite
*/
@@ -47,18 +37,25 @@ public class LocalDateConverterTestCase extends DateConverterTestBase {
return new TestSuite(LocalDateConverterTestCase.class);
}
- /** Set Up */
- @Override
- public void setUp() throws Exception {
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a new Date test case.
+ * @param name Test Name
+ */
+ public LocalDateConverterTestCase(final String name) {
+ super(name);
}
- /** Tear Down */
+ /**
+ * Return the expected type
+ * @return The expected type
+ */
@Override
- public void tearDown() throws Exception {
+ protected Class<?> getExpectedType() {
+ return LocalDate.class;
}
- // ------------------------------------------------------------------------
-
/**
* Create the Converter with no default value.
* @return A new Converter
@@ -68,6 +65,8 @@ public class LocalDateConverterTestCase extends DateConverterTestBase {
return new LocalDateConverter();
}
+ // ------------------------------------------------------------------------
+
/**
* Create the Converter with a default value.
* @param defaultValue The default value
@@ -78,13 +77,14 @@ public class LocalDateConverterTestCase extends DateConverterTestBase {
return new LocalDateConverter(defaultValue);
}
- /**
- * Return the expected type
- * @return The expected type
- */
+ /** Set Up */
@Override
- protected Class<?> getExpectedType() {
- return LocalDate.class;
+ public void setUp() throws Exception {
+ }
+
+ /** Tear Down */
+ @Override
+ public void tearDown() throws Exception {
}
/**
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/LocalDateTimeConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/LocalDateTimeConverterTestCase.java
index ad63e35..37c5833 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/LocalDateTimeConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/LocalDateTimeConverterTestCase.java
@@ -30,16 +30,6 @@ import junit.framework.TestSuite;
public class LocalDateTimeConverterTestCase extends DateConverterTestBase {
/**
- * Construct a new Date test case.
- * @param name Test Name
- */
- public LocalDateTimeConverterTestCase(final String name) {
- super(name);
- }
-
- // ------------------------------------------------------------------------
-
- /**
* Create Test Suite
* @return test suite
*/
@@ -47,18 +37,25 @@ public class LocalDateTimeConverterTestCase extends DateConverterTestBase {
return new TestSuite(LocalDateTimeConverterTestCase.class);
}
- /** Set Up */
- @Override
- public void setUp() throws Exception {
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a new Date test case.
+ * @param name Test Name
+ */
+ public LocalDateTimeConverterTestCase(final String name) {
+ super(name);
}
- /** Tear Down */
+ /**
+ * Return the expected type
+ * @return The expected type
+ */
@Override
- public void tearDown() throws Exception {
+ protected Class<?> getExpectedType() {
+ return LocalDateTime.class;
}
- // ------------------------------------------------------------------------
-
/**
* Create the Converter with no default value.
* @return A new Converter
@@ -68,6 +65,8 @@ public class LocalDateTimeConverterTestCase extends DateConverterTestBase {
return new LocalDateTimeConverter();
}
+ // ------------------------------------------------------------------------
+
/**
* Create the Converter with a default value.
* @param defaultValue The default value
@@ -78,13 +77,14 @@ public class LocalDateTimeConverterTestCase extends DateConverterTestBase {
return new LocalDateTimeConverter(defaultValue);
}
- /**
- * Return the expected type
- * @return The expected type
- */
+ /** Set Up */
@Override
- protected Class<?> getExpectedType() {
- return LocalDateTime.class;
+ public void setUp() throws Exception {
+ }
+
+ /** Tear Down */
+ @Override
+ public void tearDown() throws Exception {
}
/**
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/LongConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/LongConverterTestCase.java
index fbdb33c..31f24e1 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/LongConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/LongConverterTestCase.java
@@ -29,6 +29,12 @@ import junit.framework.TestSuite;
public class LongConverterTestCase extends NumberConverterTestBase {
+ public static TestSuite suite() {
+ return new TestSuite(LongConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -37,41 +43,35 @@ public class LongConverterTestCase extends NumberConverterTestBase {
super(name);
}
- // ------------------------------------------------------------------------
-
@Override
- public void setUp() throws Exception {
- converter = makeConverter();
- numbers[0] = new Long("-12");
- numbers[1] = new Long("13");
- numbers[2] = new Long("-22");
- numbers[3] = new Long("23");
- }
-
- public static TestSuite suite() {
- return new TestSuite(LongConverterTestCase.class);
+ protected Class<?> getExpectedType() {
+ return Long.class;
}
@Override
- public void tearDown() throws Exception {
- converter = null;
+ protected NumberConverter makeConverter() {
+ return new LongConverter();
}
// ------------------------------------------------------------------------
@Override
- protected NumberConverter makeConverter() {
- return new LongConverter();
+ protected NumberConverter makeConverter(final Object defaultValue) {
+ return new LongConverter(defaultValue);
}
@Override
- protected NumberConverter makeConverter(final Object defaultValue) {
- return new LongConverter(defaultValue);
+ public void setUp() throws Exception {
+ converter = makeConverter();
+ numbers[0] = new Long("-12");
+ numbers[1] = new Long("13");
+ numbers[2] = new Long("-22");
+ numbers[3] = new Long("23");
}
@Override
- protected Class<?> getExpectedType() {
- return Long.class;
+ public void tearDown() throws Exception {
+ converter = null;
}
// ------------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/MemoryTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/MemoryTestCase.java
index 15e8480..60ae0e7 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/MemoryTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/MemoryTestCase.java
@@ -35,20 +35,134 @@ import org.junit.Test;
*/
public class MemoryTestCase {
+ /**
+ * Attempt to force garbage collection of the specified target.
+ *
+ * <p>Unfortunately there is no way to force a JVM to perform
+ * garbage collection; all we can do is <i>hint</i> to it that
+ * garbage-collection would be a good idea, and to consume
+ * memory in order to trigger it.</p>
+ *
+ * <p>On return, target.get() will return null if the target has
+ * been garbage collected.</p>
+ *
+ * <p>If target.get() still returns non-null after this method has returned,
+ * then either there is some reference still being held to the target, or
+ * else we were not able to trigger garbage collection; there is no way
+ * to tell these scenarios apart.</p>
+ */
+ private void forceGarbageCollection(final WeakReference<?> target) {
+ int bytes = 2;
+
+ while(target.get() != null) {
+ System.gc();
+
+ // Create increasingly-large amounts of non-referenced memory
+ // in order to persuade the JVM to collect it. We are hoping
+ // here that the JVM is dumb enough to run a full gc pass over
+ // all data (including the target) rather than simply collecting
+ // this easily-reclaimable memory!
+ try {
+ @SuppressWarnings("unused")
+ final
+ byte[] b = new byte[bytes];
+ bytes = bytes * 2;
+ } catch(final OutOfMemoryError e) {
+ // well that sure should have forced a garbage collection
+ // run to occur!
+ break;
+ }
+ }
+
+ // and let's do one more just to clean up any garbage we might have
+ // created on the last pass..
+ System.gc();
+ }
+
+ /**
+ * Test whether registering a custom Converter subclass while
+ * a custom context classloader is set causes a memory leak.
+ *
+ * <p>This test emulates a j2ee container where BeanUtils has been
+ * loaded from a "common" lib location that is shared across all
+ * components running within the container. The "component" registers
+ * a converter object, whose class was loaded via the component-specific
+ * classloader. The registered converter:
+ * <ul>
+ * <li>should not be visible to other components; and</li>
+ * <li>should not prevent the component-specific classloader from being
+ * garbage-collected when the container sets its reference to null.
+ * </ul>
+ *
+ */
@Test
- public void testWeakReference() throws Exception {
+ public void testComponentRegistersCustomConverter() throws Exception {
+
final ClassLoader origContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
- ClassReloader componentLoader = new ClassReloader(origContextClassLoader);
+ // sanity check; who's paranoid?? :-)
+ assertEquals(origContextClassLoader, ConvertUtils.class.getClassLoader());
- Thread.currentThread().setContextClassLoader(componentLoader);
- Thread.currentThread().setContextClassLoader(origContextClassLoader);
+ // create a custom classloader for a "component"
+ // just like a container would.
+ ClassReloader componentLoader = new ClassReloader(origContextClassLoader);
- final WeakReference<ClassLoader> ref = new WeakReference<>(componentLoader);
- componentLoader = null;
+ // Load a custom Converter via component loader. This emulates what
+ // would happen if a user wrote their own FloatConverter subclass
+ // and deployed it via the component-specific classpath.
+ Thread.currentThread().setContextClassLoader(componentLoader);
+ {
+ // Here we pretend we're running inside the component, and that
+ // a class FloatConverter has been loaded from the component's
+ // private classpath.
+ final Class<?> newFloatConverterClass = componentLoader.reload(FloatConverter.class);
+ Object newFloatConverter = newFloatConverterClass.newInstance();
+ assertTrue(newFloatConverter.getClass().getClassLoader() == componentLoader);
- forceGarbageCollection(ref);
- assertNull(ref.get());
+ // verify that this new object does implement the Converter type
+ // despite being loaded via a classloader different from the one
+ // that loaded the Converter class.
+ assertTrue(
+ "Converter loader via child does not implement parent type",
+ Converter.class.isInstance(newFloatConverter));
+
+ // this converter registration will only apply to the
+ // componentLoader classloader...
+ ConvertUtils.register((Converter)newFloatConverter, Float.TYPE);
+
+ // After registering a custom converter, lookup should return
+ // it back to us. We'll try this lookup again with a different
+ // context-classloader set, and shouldn't see it
+ final Converter componentConverter = ConvertUtils.lookup(Float.TYPE);
+ assertTrue(componentConverter.getClass().getClassLoader() == componentLoader);
+
+ newFloatConverter = null;
+ }
+ Thread.currentThread().setContextClassLoader(origContextClassLoader);
+
+ // Because the context classloader has been reset, we shouldn't
+ // see the custom registered converter here...
+ final Converter sharedConverter = ConvertUtils.lookup(Float.TYPE);
+ assertFalse(sharedConverter.getClass().getClassLoader() == componentLoader);
+
+ // and here we should see it again
+ Thread.currentThread().setContextClassLoader(componentLoader);
+ {
+ final Converter componentConverter = ConvertUtils.lookup(Float.TYPE);
+ assertTrue(componentConverter.getClass().getClassLoader() == componentLoader);
+ }
+ Thread.currentThread().setContextClassLoader(origContextClassLoader);
+ // Emulate a container "undeploying" the component. This should
+ // make component loader available for garbage collection (we hope)
+ final WeakReference<ClassLoader> weakRefToComponent = new WeakReference<>(componentLoader);
+ componentLoader = null;
+
+ // force garbage collection and verify that the componentLoader
+ // has been garbage-collected
+ forceGarbageCollection(weakRefToComponent);
+ assertNull(
+ "Component classloader did not release properly; memory leak present",
+ weakRefToComponent.get());
} finally {
// Restore context classloader that was present before this
// test started. It is expected to be the same as the system
@@ -150,90 +264,20 @@ public class MemoryTestCase {
}
}
- /**
- * Test whether registering a custom Converter subclass while
- * a custom context classloader is set causes a memory leak.
- *
- * <p>This test emulates a j2ee container where BeanUtils has been
- * loaded from a "common" lib location that is shared across all
- * components running within the container. The "component" registers
- * a converter object, whose class was loaded via the component-specific
- * classloader. The registered converter:
- * <ul>
- * <li>should not be visible to other components; and</li>
- * <li>should not prevent the component-specific classloader from being
- * garbage-collected when the container sets its reference to null.
- * </ul>
- *
- */
@Test
- public void testComponentRegistersCustomConverter() throws Exception {
-
+ public void testWeakReference() throws Exception {
final ClassLoader origContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
- // sanity check; who's paranoid?? :-)
- assertEquals(origContextClassLoader, ConvertUtils.class.getClassLoader());
-
- // create a custom classloader for a "component"
- // just like a container would.
- ClassReloader componentLoader = new ClassReloader(origContextClassLoader);
-
- // Load a custom Converter via component loader. This emulates what
- // would happen if a user wrote their own FloatConverter subclass
- // and deployed it via the component-specific classpath.
- Thread.currentThread().setContextClassLoader(componentLoader);
- {
- // Here we pretend we're running inside the component, and that
- // a class FloatConverter has been loaded from the component's
- // private classpath.
- final Class<?> newFloatConverterClass = componentLoader.reload(FloatConverter.class);
- Object newFloatConverter = newFloatConverterClass.newInstance();
- assertTrue(newFloatConverter.getClass().getClassLoader() == componentLoader);
-
- // verify that this new object does implement the Converter type
- // despite being loaded via a classloader different from the one
- // that loaded the Converter class.
- assertTrue(
- "Converter loader via child does not implement parent type",
- Converter.class.isInstance(newFloatConverter));
-
- // this converter registration will only apply to the
- // componentLoader classloader...
- ConvertUtils.register((Converter)newFloatConverter, Float.TYPE);
-
- // After registering a custom converter, lookup should return
- // it back to us. We'll try this lookup again with a different
- // context-classloader set, and shouldn't see it
- final Converter componentConverter = ConvertUtils.lookup(Float.TYPE);
- assertTrue(componentConverter.getClass().getClassLoader() == componentLoader);
-
- newFloatConverter = null;
- }
- Thread.currentThread().setContextClassLoader(origContextClassLoader);
+ ClassReloader componentLoader = new ClassReloader(origContextClassLoader);
- // Because the context classloader has been reset, we shouldn't
- // see the custom registered converter here...
- final Converter sharedConverter = ConvertUtils.lookup(Float.TYPE);
- assertFalse(sharedConverter.getClass().getClassLoader() == componentLoader);
+ Thread.currentThread().setContextClassLoader(componentLoader);
+ Thread.currentThread().setContextClassLoader(origContextClassLoader);
- // and here we should see it again
- Thread.currentThread().setContextClassLoader(componentLoader);
- {
- final Converter componentConverter = ConvertUtils.lookup(Float.TYPE);
- assertTrue(componentConverter.getClass().getClassLoader() == componentLoader);
- }
- Thread.currentThread().setContextClassLoader(origContextClassLoader);
- // Emulate a container "undeploying" the component. This should
- // make component loader available for garbage collection (we hope)
- final WeakReference<ClassLoader> weakRefToComponent = new WeakReference<>(componentLoader);
- componentLoader = null;
+ final WeakReference<ClassLoader> ref = new WeakReference<>(componentLoader);
+ componentLoader = null;
- // force garbage collection and verify that the componentLoader
- // has been garbage-collected
- forceGarbageCollection(weakRefToComponent);
- assertNull(
- "Component classloader did not release properly; memory leak present",
- weakRefToComponent.get());
+ forceGarbageCollection(ref);
+ assertNull(ref.get());
} finally {
// Restore context classloader that was present before this
// test started. It is expected to be the same as the system
@@ -244,48 +288,4 @@ public class MemoryTestCase {
ConvertUtils.deregister();
}
}
-
- /**
- * Attempt to force garbage collection of the specified target.
- *
- * <p>Unfortunately there is no way to force a JVM to perform
- * garbage collection; all we can do is <i>hint</i> to it that
- * garbage-collection would be a good idea, and to consume
- * memory in order to trigger it.</p>
- *
- * <p>On return, target.get() will return null if the target has
- * been garbage collected.</p>
- *
- * <p>If target.get() still returns non-null after this method has returned,
- * then either there is some reference still being held to the target, or
- * else we were not able to trigger garbage collection; there is no way
- * to tell these scenarios apart.</p>
- */
- private void forceGarbageCollection(final WeakReference<?> target) {
- int bytes = 2;
-
- while(target.get() != null) {
- System.gc();
-
- // Create increasingly-large amounts of non-referenced memory
- // in order to persuade the JVM to collect it. We are hoping
- // here that the JVM is dumb enough to run a full gc pass over
- // all data (including the target) rather than simply collecting
- // this easily-reclaimable memory!
- try {
- @SuppressWarnings("unused")
- final
- byte[] b = new byte[bytes];
- bytes = bytes * 2;
- } catch(final OutOfMemoryError e) {
- // well that sure should have forced a garbage collection
- // run to occur!
- break;
- }
- }
-
- // and let's do one more just to clean up any garbage we might have
- // created on the last pass..
- System.gc();
- }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/NumberConverterTestBase.java b/src/test/java/org/apache/commons/beanutils2/converters/NumberConverterTestBase.java
index b7bbaa4..e42a202 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/NumberConverterTestBase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/NumberConverterTestBase.java
@@ -46,13 +46,48 @@ public abstract class NumberConverterTestBase extends TestCase {
// ------------------------------------------------------------------------
+ protected abstract Class<?> getExpectedType();
protected abstract NumberConverter makeConverter();
protected abstract NumberConverter makeConverter(Object defaultValue);
- protected abstract Class<?> getExpectedType();
// ------------------------------------------------------------------------
/**
+ * Convert Boolean --> Number (default conversion)
+ */
+ public void testBooleanToNumberDefault() {
+
+ final NumberConverter converter = makeConverter();
+
+ // Other type --> String conversion
+ assertEquals("Boolean.FALSE to Number ", 0, ((Number)converter.convert(getExpectedType(), Boolean.FALSE)).intValue());
+ assertEquals("Boolean.TRUE to Number ", 1, ((Number)converter.convert(getExpectedType(), Boolean.TRUE)).intValue());
+
+ }
+
+ /**
+ * Convert Calendar --> Long
+ */
+ public void testCalendarToNumber() {
+
+ final NumberConverter converter = makeConverter();
+
+ final Calendar calendarValue = Calendar.getInstance();
+ final long longValue = calendarValue.getTime().getTime();
+
+ // Calendar --> Long conversion
+ assertEquals("Calendar to Long", new Long(longValue), converter.convert(Long.class, calendarValue));
+
+ // Calendar --> Integer
+ try {
+ converter.convert(Integer.class, calendarValue);
+ fail("Calendar to Integer - expected a ConversionException");
+ } catch (final ConversionException e) {
+ // expected result - too large for Integer
+ }
+
+ }
+ /**
* Assumes ConversionException in response to covert(getExpectedType(),null).
*/
public void testConvertNull() {
@@ -101,69 +136,92 @@ public abstract class NumberConverterTestBase extends TestCase {
getExpectedType().isInstance(val));
}
}
+
/**
- * Convert Number --> String (using a Pattern, with default and specified Locales)
+ * Convert Date --> Long
*/
- public void testNumberToStringPattern() {
-
- // Re-set the default Locale to Locale.US
- final Locale defaultLocale = Locale.getDefault();
- Locale.setDefault(Locale.US);
+ public void testDateToNumber() {
final NumberConverter converter = makeConverter();
- converter.setPattern("[0,0.0];(0,0.0)");
- // Default Locale
- assertEquals("Default Locale " + numbers[0], "(12.0)", converter.convert(String.class, numbers[0]));
- assertEquals("Default Locale " + numbers[1], "[13.0]", converter.convert(String.class, numbers[1]));
+ final Date dateValue = new Date();
+ final long longValue = dateValue.getTime();
- // Locale.GERMAN
- converter.setLocale(Locale.GERMAN);
- assertEquals("Locale.GERMAN " + numbers[2], "(22,0)", converter.convert(String.class, numbers[2]));
- assertEquals("Locale.GERMAN " + numbers[3], "[23,0]", converter.convert(String.class, numbers[3]));
+ // Date --> Long conversion
+ assertEquals("Date to Long", new Long(longValue), converter.convert(Long.class, dateValue));
+
+ // Date --> Integer
+ try {
+ converter.convert(Integer.class, dateValue);
+ fail("Date to Integer - expected a ConversionException");
+ } catch (final ConversionException e) {
+ // expected result - too large for Integer
+ }
- // Restore the default Locale
- Locale.setDefault(defaultLocale);
}
/**
* Convert Number --> String (using default and specified Locales)
*/
- public void testNumberToStringLocale() {
+ public void testInvalidDefault() {
- // Re-set the default Locale to Locale.US
- final Locale defaultLocale = Locale.getDefault();
- Locale.setDefault(Locale.US);
+ final Object defaultvalue = numbers[0];
+ final NumberConverter converter = makeConverter(defaultvalue);
+
+ // Default String --> Number conversion
+ assertEquals("Invalid null ", defaultvalue, converter.convert(getExpectedType(), null));
+ assertEquals("Default XXXX ", defaultvalue, converter.convert(getExpectedType(), "XXXX"));
+ }
+
+ /**
+ * Convert Number --> String (using default and specified Locales)
+ */
+ public void testInvalidException() {
final NumberConverter converter = makeConverter();
- converter.setUseLocaleFormat(true);
- // Default Locale
- assertEquals("Default Locale " + numbers[0], "-12", converter.convert(String.class, numbers[0]));
- assertEquals("Default Locale " + numbers[1], "13", converter.convert(String.class, numbers[1]));
+ try {
+ converter.convert(getExpectedType(), null);
+ fail("Null test, expected ConversionException");
+ } catch (final ConversionException e) {
+ // expected result
+ }
+ try {
+ converter.convert(getExpectedType(), "XXXX");
+ fail("Invalid test, expected ConversionException");
+ } catch (final ConversionException e) {
+ // expected result
+ }
+ }
- // Locale.GERMAN
- converter.setLocale(Locale.GERMAN);
- assertEquals("Locale.GERMAN " + numbers[2], "-22", converter.convert(String.class, numbers[2]));
- assertEquals("Locale.GERMAN " + numbers[3], "23", converter.convert(String.class, numbers[3]));
+ /**
+ * Test specifying an invalid type.
+ */
+ public void testInvalidType() {
- // Restore the default Locale
- Locale.setDefault(defaultLocale);
+ final NumberConverter converter = makeConverter();
+
+ try {
+ converter.convert(Object.class, numbers[0]);
+ fail("Invalid type test, expected ConversionException");
+ } catch (final ConversionException e) {
+ // expected result
+ }
}
/**
- * Convert Array --> Number
+ * Tests a conversion to an unsupported type if a default value is set.
*/
- public void testStringArrayToInteger() {
+ public void testInvalidTypeWithDefault() {
- final Integer defaultValue = new Integer(-1);
- final NumberConverter converter = makeConverter(defaultValue);
+ final NumberConverter converter = makeConverter(42);
- // Default Locale
- assertEquals("Valid First", new Integer(5), converter.convert(Integer.class, new String[] {"5", "4", "3"}));
- assertEquals("Invalid First", defaultValue, converter.convert(Integer.class, new String[] {"FOO", "1", "2"}));
- assertEquals("Null First", defaultValue, converter.convert(Integer.class, new String[] {null, "1", "2"}));
- assertEquals("Long Array", new Integer(9), converter.convert(Integer.class, new long[] {9, 2, 6}));
+ try {
+ converter.convert(Object.class, numbers[0]);
+ fail("Invalid type with default test, expected ConversionException");
+ } catch(final ConversionException e) {
+ // expected result
+ }
}
/**
@@ -180,94 +238,83 @@ public abstract class NumberConverterTestBase extends TestCase {
}
/**
- * Convert String --> Number (using a Pattern, with default and specified Locales)
+ * Convert Number --> String (using default and specified Locales)
*/
- public void testStringToNumberPattern() {
+ public void testNumberToStringLocale() {
// Re-set the default Locale to Locale.US
final Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
final NumberConverter converter = makeConverter();
- converter.setPattern("[0,0];(0,0)");
+ converter.setUseLocaleFormat(true);
// Default Locale
- assertEquals("Default Locale " + numbers[0], numbers[0], converter.convert(getExpectedType(), "(1,2)"));
- assertEquals("Default Locale " + numbers[1], numbers[1], converter.convert(getExpectedType(), "[1,3]"));
+ assertEquals("Default Locale " + numbers[0], "-12", converter.convert(String.class, numbers[0]));
+ assertEquals("Default Locale " + numbers[1], "13", converter.convert(String.class, numbers[1]));
// Locale.GERMAN
converter.setLocale(Locale.GERMAN);
- assertEquals("Locale.GERMAN " + numbers[2], numbers[2], converter.convert(getExpectedType(), "(2.2)"));
- assertEquals("Locale.GERMAN " + numbers[3], numbers[3], converter.convert(getExpectedType(), "[2.3]"));
-
- // Invalid Value
- try {
- converter.convert(getExpectedType(), "1,2");
- fail("Expected invalid value to cause ConversionException");
- } catch (final Exception e) {
- // expected result
- }
-
- // Invalid Type (will try via String)
- final Object obj = new Object() {
- @Override
- public String toString() {
- return "dsdgsdsdg";
- }
- };
- try {
- converter.convert(getExpectedType(), obj);
- fail("Expected invalid value to cause ConversionException");
- } catch (final Exception e) {
- // expected result
- }
+ assertEquals("Locale.GERMAN " + numbers[2], "-22", converter.convert(String.class, numbers[2]));
+ assertEquals("Locale.GERMAN " + numbers[3], "23", converter.convert(String.class, numbers[3]));
// Restore the default Locale
Locale.setDefault(defaultLocale);
}
/**
- * Convert String --> Number (using default and specified Locales)
+ * Convert Number --> String (using a Pattern, with default and specified Locales)
*/
- public void testStringToNumberLocale() {
+ public void testNumberToStringPattern() {
// Re-set the default Locale to Locale.US
final Locale defaultLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
final NumberConverter converter = makeConverter();
- converter.setUseLocaleFormat(true);
+ converter.setPattern("[0,0.0];(0,0.0)");
// Default Locale
- assertEquals("Default Locale " + numbers[0], numbers[0], converter.convert(getExpectedType(), "-0,012"));
- assertEquals("Default Locale " + numbers[1], numbers[1], converter.convert(getExpectedType(), "0,013"));
-
- // Invalid Value
- try {
- converter.convert(getExpectedType(), "0,02x");
- fail("Expected invalid value to cause ConversionException");
- } catch (final Exception e) {
- // expected result
- }
+ assertEquals("Default Locale " + numbers[0], "(12.0)", converter.convert(String.class, numbers[0]));
+ assertEquals("Default Locale " + numbers[1], "[13.0]", converter.convert(String.class, numbers[1]));
// Locale.GERMAN
converter.setLocale(Locale.GERMAN);
- assertEquals("Locale.GERMAN " + numbers[2], numbers[2], converter.convert(getExpectedType(), "-0.022"));
- assertEquals("Locale.GERMAN " + numbers[3], numbers[3], converter.convert(getExpectedType(), "0.023"));
-
- // Invalid Value
- try {
- converter.convert(getExpectedType(), "0.02x");
- fail("Expected invalid value to cause ConversionException");
- } catch (final Exception e) {
- // expected result
- }
+ assertEquals("Locale.GERMAN " + numbers[2], "(22,0)", converter.convert(String.class, numbers[2]));
+ assertEquals("Locale.GERMAN " + numbers[3], "[23,0]", converter.convert(String.class, numbers[3]));
// Restore the default Locale
Locale.setDefault(defaultLocale);
}
/**
+ * Convert Other --> String (default conversion)
+ */
+ public void testOtherToStringDefault() {
+
+ final NumberConverter converter = makeConverter();
+
+ // Other type --> String conversion
+ assertEquals("Default Convert ", "ABC", converter.convert(String.class, new StringBuilder("ABC")));
+
+ }
+
+ /**
+ * Convert Array --> Number
+ */
+ public void testStringArrayToInteger() {
+
+ final Integer defaultValue = new Integer(-1);
+ final NumberConverter converter = makeConverter(defaultValue);
+
+ // Default Locale
+ assertEquals("Valid First", new Integer(5), converter.convert(Integer.class, new String[] {"5", "4", "3"}));
+ assertEquals("Invalid First", defaultValue, converter.convert(Integer.class, new String[] {"FOO", "1", "2"}));
+ assertEquals("Null First", defaultValue, converter.convert(Integer.class, new String[] {null, "1", "2"}));
+ assertEquals("Long Array", new Integer(9), converter.convert(Integer.class, new long[] {9, 2, 6}));
+ }
+
+ /**
* Convert String --> Number (default conversion)
*/
public void testStringToNumberDefault() {
@@ -299,138 +346,91 @@ public abstract class NumberConverterTestBase extends TestCase {
}
/**
- * Convert Boolean --> Number (default conversion)
+ * Convert String --> Number (using default and specified Locales)
*/
- public void testBooleanToNumberDefault() {
-
- final NumberConverter converter = makeConverter();
-
- // Other type --> String conversion
- assertEquals("Boolean.FALSE to Number ", 0, ((Number)converter.convert(getExpectedType(), Boolean.FALSE)).intValue());
- assertEquals("Boolean.TRUE to Number ", 1, ((Number)converter.convert(getExpectedType(), Boolean.TRUE)).intValue());
-
- }
+ public void testStringToNumberLocale() {
- /**
- * Convert Date --> Long
- */
- public void testDateToNumber() {
+ // Re-set the default Locale to Locale.US
+ final Locale defaultLocale = Locale.getDefault();
+ Locale.setDefault(Locale.US);
final NumberConverter converter = makeConverter();
+ converter.setUseLocaleFormat(true);
- final Date dateValue = new Date();
- final long longValue = dateValue.getTime();
-
- // Date --> Long conversion
- assertEquals("Date to Long", new Long(longValue), converter.convert(Long.class, dateValue));
+ // Default Locale
+ assertEquals("Default Locale " + numbers[0], numbers[0], converter.convert(getExpectedType(), "-0,012"));
+ assertEquals("Default Locale " + numbers[1], numbers[1], converter.convert(getExpectedType(), "0,013"));
- // Date --> Integer
+ // Invalid Value
try {
- converter.convert(Integer.class, dateValue);
- fail("Date to Integer - expected a ConversionException");
- } catch (final ConversionException e) {
- // expected result - too large for Integer
+ converter.convert(getExpectedType(), "0,02x");
+ fail("Expected invalid value to cause ConversionException");
+ } catch (final Exception e) {
+ // expected result
}
- }
-
- /**
- * Convert Calendar --> Long
- */
- public void testCalendarToNumber() {
-
- final NumberConverter converter = makeConverter();
-
- final Calendar calendarValue = Calendar.getInstance();
- final long longValue = calendarValue.getTime().getTime();
-
- // Calendar --> Long conversion
- assertEquals("Calendar to Long", new Long(longValue), converter.convert(Long.class, calendarValue));
+ // Locale.GERMAN
+ converter.setLocale(Locale.GERMAN);
+ assertEquals("Locale.GERMAN " + numbers[2], numbers[2], converter.convert(getExpectedType(), "-0.022"));
+ assertEquals("Locale.GERMAN " + numbers[3], numbers[3], converter.convert(getExpectedType(), "0.023"));
- // Calendar --> Integer
+ // Invalid Value
try {
- converter.convert(Integer.class, calendarValue);
- fail("Calendar to Integer - expected a ConversionException");
- } catch (final ConversionException e) {
- // expected result - too large for Integer
+ converter.convert(getExpectedType(), "0.02x");
+ fail("Expected invalid value to cause ConversionException");
+ } catch (final Exception e) {
+ // expected result
}
+ // Restore the default Locale
+ Locale.setDefault(defaultLocale);
}
/**
- * Convert Other --> String (default conversion)
- */
- public void testOtherToStringDefault() {
-
- final NumberConverter converter = makeConverter();
-
- // Other type --> String conversion
- assertEquals("Default Convert ", "ABC", converter.convert(String.class, new StringBuilder("ABC")));
-
- }
-
- /**
- * Convert Number --> String (using default and specified Locales)
+ * Convert String --> Number (using a Pattern, with default and specified Locales)
*/
- public void testInvalidDefault() {
-
- final Object defaultvalue = numbers[0];
- final NumberConverter converter = makeConverter(defaultvalue);
-
- // Default String --> Number conversion
- assertEquals("Invalid null ", defaultvalue, converter.convert(getExpectedType(), null));
- assertEquals("Default XXXX ", defaultvalue, converter.convert(getExpectedType(), "XXXX"));
- }
+ public void testStringToNumberPattern() {
- /**
- * Convert Number --> String (using default and specified Locales)
- */
- public void testInvalidException() {
+ // Re-set the default Locale to Locale.US
+ final Locale defaultLocale = Locale.getDefault();
+ Locale.setDefault(Locale.US);
final NumberConverter converter = makeConverter();
+ converter.setPattern("[0,0];(0,0)");
- try {
- converter.convert(getExpectedType(), null);
- fail("Null test, expected ConversionException");
- } catch (final ConversionException e) {
- // expected result
- }
- try {
- converter.convert(getExpectedType(), "XXXX");
- fail("Invalid test, expected ConversionException");
- } catch (final ConversionException e) {
- // expected result
- }
- }
-
- /**
- * Test specifying an invalid type.
- */
- public void testInvalidType() {
+ // Default Locale
+ assertEquals("Default Locale " + numbers[0], numbers[0], converter.convert(getExpectedType(), "(1,2)"));
+ assertEquals("Default Locale " + numbers[1], numbers[1], converter.convert(getExpectedType(), "[1,3]"));
- final NumberConverter converter = makeConverter();
+ // Locale.GERMAN
+ converter.setLocale(Locale.GERMAN);
+ assertEquals("Locale.GERMAN " + numbers[2], numbers[2], converter.convert(getExpectedType(), "(2.2)"));
+ assertEquals("Locale.GERMAN " + numbers[3], numbers[3], converter.convert(getExpectedType(), "[2.3]"));
+ // Invalid Value
try {
- converter.convert(Object.class, numbers[0]);
- fail("Invalid type test, expected ConversionException");
- } catch (final ConversionException e) {
+ converter.convert(getExpectedType(), "1,2");
+ fail("Expected invalid value to cause ConversionException");
+ } catch (final Exception e) {
// expected result
}
- }
-
- /**
- * Tests a conversion to an unsupported type if a default value is set.
- */
- public void testInvalidTypeWithDefault() {
-
- final NumberConverter converter = makeConverter(42);
+ // Invalid Type (will try via String)
+ final Object obj = new Object() {
+ @Override
+ public String toString() {
+ return "dsdgsdsdg";
+ }
+ };
try {
- converter.convert(Object.class, numbers[0]);
- fail("Invalid type with default test, expected ConversionException");
- } catch(final ConversionException e) {
+ converter.convert(getExpectedType(), obj);
+ fail("Expected invalid value to cause ConversionException");
+ } catch (final Exception e) {
// expected result
}
+
+ // Restore the default Locale
+ Locale.setDefault(defaultLocale);
}
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/OffsetDateTimeConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/OffsetDateTimeConverterTestCase.java
index c4d2f05..5014d6a 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/OffsetDateTimeConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/OffsetDateTimeConverterTestCase.java
@@ -30,16 +30,6 @@ import junit.framework.TestSuite;
public class OffsetDateTimeConverterTestCase extends DateConverterTestBase {
/**
- * Construct a new Date test case.
- * @param name Test Name
- */
- public OffsetDateTimeConverterTestCase(final String name) {
- super(name);
- }
-
- // ------------------------------------------------------------------------
-
- /**
* Create Test Suite
* @return test suite
*/
@@ -47,18 +37,25 @@ public class OffsetDateTimeConverterTestCase extends DateConverterTestBase {
return new TestSuite(OffsetDateTimeConverterTestCase.class);
}
- /** Set Up */
- @Override
- public void setUp() throws Exception {
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a new Date test case.
+ * @param name Test Name
+ */
+ public OffsetDateTimeConverterTestCase(final String name) {
+ super(name);
}
- /** Tear Down */
+ /**
+ * Return the expected type
+ * @return The expected type
+ */
@Override
- public void tearDown() throws Exception {
+ protected Class<?> getExpectedType() {
+ return OffsetDateTime.class;
}
- // ------------------------------------------------------------------------
-
/**
* Create the Converter with no default value.
* @return A new Converter
@@ -68,6 +65,8 @@ public class OffsetDateTimeConverterTestCase extends DateConverterTestBase {
return new OffsetDateTimeConverter();
}
+ // ------------------------------------------------------------------------
+
/**
* Create the Converter with a default value.
* @param defaultValue The default value
@@ -78,13 +77,14 @@ public class OffsetDateTimeConverterTestCase extends DateConverterTestBase {
return new OffsetDateTimeConverter(defaultValue);
}
- /**
- * Return the expected type
- * @return The expected type
- */
+ /** Set Up */
@Override
- protected Class<?> getExpectedType() {
- return OffsetDateTime.class;
+ public void setUp() throws Exception {
+ }
+
+ /** Tear Down */
+ @Override
+ public void tearDown() throws Exception {
}
/**
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/PathConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/PathConverterTestCase.java
index c169381..5723cfc 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/PathConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/PathConverterTestCase.java
@@ -36,6 +36,12 @@ import junit.framework.TestSuite;
*/
public class PathConverterTestCase extends TestCase {
+ public static TestSuite suite() {
+ return new TestSuite(PathConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -44,6 +50,14 @@ public class PathConverterTestCase extends TestCase {
super(name);
}
+ protected Class<?> getExpectedType() {
+ return Path.class;
+ }
+
+ protected Converter makeConverter() {
+ return new PathConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
@@ -51,10 +65,6 @@ public class PathConverterTestCase extends TestCase {
converter = makeConverter();
}
- public static TestSuite suite() {
- return new TestSuite(PathConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -62,16 +72,6 @@ public class PathConverterTestCase extends TestCase {
// ------------------------------------------------------------------------
- protected Converter makeConverter() {
- return new PathConverter();
- }
-
- protected Class<?> getExpectedType() {
- return Path.class;
- }
-
- // ------------------------------------------------------------------------
-
public void testSimpleConversion() throws Exception {
final String[] message= {
"from String",
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/ShortConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/ShortConverterTestCase.java
index b744acc..d1733e3 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/ShortConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/ShortConverterTestCase.java
@@ -29,6 +29,12 @@ import junit.framework.TestSuite;
public class ShortConverterTestCase extends NumberConverterTestBase {
+ public static TestSuite suite() {
+ return new TestSuite(ShortConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -37,9 +43,24 @@ public class ShortConverterTestCase extends NumberConverterTestBase {
super(name);
}
+ @Override
+ protected Class<?> getExpectedType() {
+ return Short.class;
+ }
+
+ @Override
+ protected NumberConverter makeConverter() {
+ return new ShortConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
+ protected NumberConverter makeConverter(final Object defaultValue) {
+ return new ShortConverter(defaultValue);
+ }
+
+ @Override
public void setUp() throws Exception {
converter = makeConverter();
numbers[0] = new Short("-12");
@@ -48,10 +69,6 @@ public class ShortConverterTestCase extends NumberConverterTestBase {
numbers[3] = new Short("23");
}
- public static TestSuite suite() {
- return new TestSuite(ShortConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -59,22 +76,40 @@ public class ShortConverterTestCase extends NumberConverterTestBase {
// ------------------------------------------------------------------------
- @Override
- protected NumberConverter makeConverter() {
- return new ShortConverter();
- }
+ /**
+ * Test Invalid Amounts (too big/small)
+ */
+ public void testInvalidAmount() {
+ final Converter converter = makeConverter();
+ final Class<?> clazz = Short.class;
- @Override
- protected NumberConverter makeConverter(final Object defaultValue) {
- return new ShortConverter(defaultValue);
- }
+ final Long min = new Long(Short.MIN_VALUE);
+ final Long max = new Long(Short.MAX_VALUE);
+ final Long minMinusOne = new Long(min.longValue() - 1);
+ final Long maxPlusOne = new Long(max.longValue() + 1);
- @Override
- protected Class<?> getExpectedType() {
- return Short.class;
- }
+ // Minimum
+ assertEquals("Minimum", new Short(Short.MIN_VALUE), converter.convert(clazz, min));
- // ------------------------------------------------------------------------
+ // Maximum
+ assertEquals("Maximum", new Short(Short.MAX_VALUE), converter.convert(clazz, max));
+
+ // Too Small
+ try {
+ assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
+ fail("Less than minimum, expected ConversionException");
+ } catch (final Exception e) {
+ // expected result
+ }
+
+ // Too Large
+ try {
+ assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
+ fail("More than maximum, expected ConversionException");
+ } catch (final Exception e) {
+ // expected result
+ }
+ }
public void testSimpleConversion() throws Exception {
final String[] message= {
@@ -131,40 +166,5 @@ public class ShortConverterTestCase extends NumberConverterTestBase {
assertEquals(message[i] + " to null type",expected[i],converter.convert(null,input[i]));
}
}
-
- /**
- * Test Invalid Amounts (too big/small)
- */
- public void testInvalidAmount() {
- final Converter converter = makeConverter();
- final Class<?> clazz = Short.class;
-
- final Long min = new Long(Short.MIN_VALUE);
- final Long max = new Long(Short.MAX_VALUE);
- final Long minMinusOne = new Long(min.longValue() - 1);
- final Long maxPlusOne = new Long(max.longValue() + 1);
-
- // Minimum
- assertEquals("Minimum", new Short(Short.MIN_VALUE), converter.convert(clazz, min));
-
- // Maximum
- assertEquals("Maximum", new Short(Short.MAX_VALUE), converter.convert(clazz, max));
-
- // Too Small
- try {
- assertEquals("Minimum - 1", null, converter.convert(clazz, minMinusOne));
- fail("Less than minimum, expected ConversionException");
- } catch (final Exception e) {
- // expected result
- }
-
- // Too Large
- try {
- assertEquals("Maximum + 1", null, converter.convert(clazz, maxPlusOne));
- fail("More than maximum, expected ConversionException");
- } catch (final Exception e) {
- // expected result
- }
- }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/SqlDateConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/SqlDateConverterTestCase.java
index face175..2b3ed93 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/SqlDateConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/SqlDateConverterTestCase.java
@@ -30,6 +30,16 @@ import junit.framework.TestSuite;
public class SqlDateConverterTestCase extends DateConverterTestBase {
/**
+ * Create Test Suite
+ * @return test suite
+ */
+ public static TestSuite suite() {
+ return new TestSuite(SqlDateConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
* Construct a new Date test case.
* @param name Test Name
*/
@@ -40,14 +50,32 @@ public class SqlDateConverterTestCase extends DateConverterTestBase {
// ------------------------------------------------------------------------
/**
- * Create Test Suite
- * @return test suite
+ * Return the expected type
+ * @return The expected type
*/
- public static TestSuite suite() {
- return new TestSuite(SqlDateConverterTestCase.class);
+ @Override
+ protected Class<?> getExpectedType() {
+ return Date.class;
}
- // ------------------------------------------------------------------------
+ /**
+ * Create the Converter with no default value.
+ * @return A new Converter
+ */
+ @Override
+ protected DateTimeConverter makeConverter() {
+ return new SqlDateConverter();
+ }
+
+ /**
+ * Create the Converter with a default value.
+ * @param defaultValue The default value
+ * @return A new Converter
+ */
+ @Override
+ protected DateTimeConverter makeConverter(final Object defaultValue) {
+ return new SqlDateConverter(defaultValue);
+ }
/**
* Test default String to java.sql.Date conversion
@@ -87,34 +115,6 @@ public class SqlDateConverterTestCase extends DateConverterTestBase {
}
/**
- * Create the Converter with no default value.
- * @return A new Converter
- */
- @Override
- protected DateTimeConverter makeConverter() {
- return new SqlDateConverter();
- }
-
- /**
- * Create the Converter with a default value.
- * @param defaultValue The default value
- * @return A new Converter
- */
- @Override
- protected DateTimeConverter makeConverter(final Object defaultValue) {
- return new SqlDateConverter(defaultValue);
- }
-
- /**
- * Return the expected type
- * @return The expected type
- */
- @Override
- protected Class<?> getExpectedType() {
- return Date.class;
- }
-
- /**
* Convert from a Calendar to the appropriate Date type
*
* @param value The Calendar value to convert
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/SqlTimeConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/SqlTimeConverterTestCase.java
index c1d3028..a3bae0e 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/SqlTimeConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/SqlTimeConverterTestCase.java
@@ -31,6 +31,16 @@ import junit.framework.TestSuite;
public class SqlTimeConverterTestCase extends DateConverterTestBase {
/**
+ * Create Test Suite
+ * @return test suite
+ */
+ public static TestSuite suite() {
+ return new TestSuite(SqlTimeConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
* Construct a new Date test case.
* @param name Test Name
*/
@@ -41,14 +51,52 @@ public class SqlTimeConverterTestCase extends DateConverterTestBase {
// ------------------------------------------------------------------------
/**
- * Create Test Suite
- * @return test suite
+ * Return the expected type
+ * @return The expected type
*/
- public static TestSuite suite() {
- return new TestSuite(SqlTimeConverterTestCase.class);
+ @Override
+ protected Class<?> getExpectedType() {
+ return Time.class;
}
- // ------------------------------------------------------------------------
+ /**
+ * Create the Converter with no default value.
+ * @return A new Converter
+ */
+ @Override
+ protected DateTimeConverter makeConverter() {
+ return new SqlTimeConverter();
+ }
+
+ /**
+ * Create the Converter with a default value.
+ * @param defaultValue The default value
+ * @return A new Converter
+ */
+ @Override
+ protected DateTimeConverter makeConverter(final Object defaultValue) {
+ return new SqlTimeConverter(defaultValue);
+ }
+
+ /**
+ * Test default String to java.sql.Time conversion
+ */
+ @Override
+ public void testDefaultStringToTypeConvert() {
+
+ // Create & Configure the Converter
+ final DateTimeConverter converter = makeConverter();
+ converter.setUseLocaleFormat(false);
+
+ // Valid String --> java.sql.Time Conversion
+ final String testString = "15:36:21";
+ final Object expected = toType(testString, "HH:mm:ss", null);
+ validConversion(converter, expected, testString);
+
+ // Invalid String --> java.sql.Time Conversion
+ invalidConversion(converter, "15:36");
+
+ }
/**
* Test Date Converter with no default value
@@ -90,54 +138,6 @@ public class SqlTimeConverterTestCase extends DateConverterTestBase {
}
/**
- * Test default String to java.sql.Time conversion
- */
- @Override
- public void testDefaultStringToTypeConvert() {
-
- // Create & Configure the Converter
- final DateTimeConverter converter = makeConverter();
- converter.setUseLocaleFormat(false);
-
- // Valid String --> java.sql.Time Conversion
- final String testString = "15:36:21";
- final Object expected = toType(testString, "HH:mm:ss", null);
- validConversion(converter, expected, testString);
-
- // Invalid String --> java.sql.Time Conversion
- invalidConversion(converter, "15:36");
-
- }
-
- /**
- * Create the Converter with no default value.
- * @return A new Converter
- */
- @Override
- protected DateTimeConverter makeConverter() {
- return new SqlTimeConverter();
- }
-
- /**
- * Create the Converter with a default value.
- * @param defaultValue The default value
- * @return A new Converter
- */
- @Override
- protected DateTimeConverter makeConverter(final Object defaultValue) {
- return new SqlTimeConverter(defaultValue);
- }
-
- /**
- * Return the expected type
- * @return The expected type
- */
- @Override
- protected Class<?> getExpectedType() {
- return Time.class;
- }
-
- /**
* Convert from a Calendar to the appropriate Date type
*
* @param value The Calendar value to convert
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/SqlTimestampConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/SqlTimestampConverterTestCase.java
index 817462c..c1e0d1f 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/SqlTimestampConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/SqlTimestampConverterTestCase.java
@@ -33,6 +33,16 @@ import junit.framework.TestSuite;
public class SqlTimestampConverterTestCase extends DateConverterTestBase {
/**
+ * Create Test Suite
+ * @return test suite
+ */
+ public static TestSuite suite() {
+ return new TestSuite(SqlTimestampConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
* Construct a new Date test case.
* @param name Test Name
*/
@@ -43,15 +53,14 @@ public class SqlTimestampConverterTestCase extends DateConverterTestBase {
// ------------------------------------------------------------------------
/**
- * Create Test Suite
- * @return test suite
+ * Return the expected type
+ * @return The expected type
*/
- public static TestSuite suite() {
- return new TestSuite(SqlTimestampConverterTestCase.class);
+ @Override
+ protected Class<?> getExpectedType() {
+ return Timestamp.class;
}
- // ------------------------------------------------------------------------
-
private boolean isUSFormatWithComma() {
// BEANUTILS-495 workaround - sometimes Java 9 expects "," in date even if
// the format is set to lenient
@@ -60,6 +69,47 @@ public class SqlTimestampConverterTestCase extends DateConverterTestBase {
}
/**
+ * Create the Converter with no default value.
+ * @return A new Converter
+ */
+ @Override
+ protected DateTimeConverter makeConverter() {
+ return new SqlTimestampConverter();
+ }
+
+ /**
+ * Create the Converter with a default value.
+ * @param defaultValue The default value
+ * @return A new Converter
+ */
+ @Override
+ protected DateTimeConverter makeConverter(final Object defaultValue) {
+ return new SqlTimestampConverter(defaultValue);
+ }
+
+ /**
+ * Test default String to java.sql.Timestamp conversion
+ */
+ @Override
+ public void testDefaultStringToTypeConvert() {
+
+ // Create & Configure the Converter
+ final DateTimeConverter converter = makeConverter();
+ converter.setUseLocaleFormat(false);
+
+ // Valid String --> java.sql.Timestamp Conversion
+ final String testString = "2006-10-23 15:36:01.0";
+ final Object expected = toType(testString, "yyyy-MM-dd HH:mm:ss.S", null);
+ validConversion(converter, expected, testString);
+
+ // Invalid String --> java.sql.Timestamp Conversion
+ invalidConversion(converter, "2006/09/21 15:36:01.0");
+ invalidConversion(converter, "2006-10-22");
+ invalidConversion(converter, "15:36:01");
+
+ }
+
+ /**
* Test Date Converter with no default value
*/
@Override
@@ -105,56 +155,6 @@ public class SqlTimestampConverterTestCase extends DateConverterTestBase {
}
/**
- * Test default String to java.sql.Timestamp conversion
- */
- @Override
- public void testDefaultStringToTypeConvert() {
-
- // Create & Configure the Converter
- final DateTimeConverter converter = makeConverter();
- converter.setUseLocaleFormat(false);
-
- // Valid String --> java.sql.Timestamp Conversion
- final String testString = "2006-10-23 15:36:01.0";
- final Object expected = toType(testString, "yyyy-MM-dd HH:mm:ss.S", null);
- validConversion(converter, expected, testString);
-
- // Invalid String --> java.sql.Timestamp Conversion
- invalidConversion(converter, "2006/09/21 15:36:01.0");
- invalidConversion(converter, "2006-10-22");
- invalidConversion(converter, "15:36:01");
-
- }
-
- /**
- * Create the Converter with no default value.
- * @return A new Converter
- */
- @Override
- protected DateTimeConverter makeConverter() {
- return new SqlTimestampConverter();
- }
-
- /**
- * Create the Converter with a default value.
- * @param defaultValue The default value
- * @return A new Converter
- */
- @Override
- protected DateTimeConverter makeConverter(final Object defaultValue) {
- return new SqlTimestampConverter(defaultValue);
- }
-
- /**
- * Return the expected type
- * @return The expected type
- */
- @Override
- protected Class<?> getExpectedType() {
- return Timestamp.class;
- }
-
- /**
* Convert from a Calendar to the appropriate Date type
*
* @param value The Calendar value to convert
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/StringConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/StringConverterTestCase.java
index d1ee3a5..91beae7 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/StringConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/StringConverterTestCase.java
@@ -35,13 +35,6 @@ public class StringConverterTestCase extends TestCase {
}
/**
- * Tests whether the correct default type is returned.
- */
- public void testDefaultType() {
- assertEquals("Wrong default type", String.class, converter.getDefaultType());
- }
-
- /**
* Tests a conversion to a string type.
*/
public void testConvertToTypeString() {
@@ -61,4 +54,11 @@ public class StringConverterTestCase extends TestCase {
// expected result
}
}
+
+ /**
+ * Tests whether the correct default type is returned.
+ */
+ public void testDefaultType() {
+ assertEquals("Wrong default type", String.class, converter.getDefaultType());
+ }
}
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/URIConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/URIConverterTestCase.java
index faaef5a..faad7e0 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/URIConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/URIConverterTestCase.java
@@ -32,6 +32,12 @@ import junit.framework.TestSuite;
*/
public class URIConverterTestCase extends TestCase {
+ public static TestSuite suite() {
+ return new TestSuite(URIConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -40,6 +46,14 @@ public class URIConverterTestCase extends TestCase {
super(name);
}
+ protected Class<?> getExpectedType() {
+ return URI.class;
+ }
+
+ protected Converter makeConverter() {
+ return new URIConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
@@ -47,10 +61,6 @@ public class URIConverterTestCase extends TestCase {
converter = makeConverter();
}
- public static TestSuite suite() {
- return new TestSuite(URIConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -58,16 +68,6 @@ public class URIConverterTestCase extends TestCase {
// ------------------------------------------------------------------------
- protected Converter makeConverter() {
- return new URIConverter();
- }
-
- protected Class<?> getExpectedType() {
- return URI.class;
- }
-
- // ------------------------------------------------------------------------
-
public void testSimpleConversion() throws Exception {
final String[] message= {
"from String",
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/URLConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/URLConverterTestCase.java
index 8c9f195..05241cd 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/URLConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/URLConverterTestCase.java
@@ -33,6 +33,12 @@ import junit.framework.TestSuite;
public class URLConverterTestCase extends TestCase {
+ public static TestSuite suite() {
+ return new TestSuite(URLConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -41,6 +47,14 @@ public class URLConverterTestCase extends TestCase {
super(name);
}
+ protected Class<?> getExpectedType() {
+ return URL.class;
+ }
+
+ protected Converter makeConverter() {
+ return new URLConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
@@ -48,10 +62,6 @@ public class URLConverterTestCase extends TestCase {
converter = makeConverter();
}
- public static TestSuite suite() {
- return new TestSuite(URLConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -59,16 +69,6 @@ public class URLConverterTestCase extends TestCase {
// ------------------------------------------------------------------------
- protected Converter makeConverter() {
- return new URLConverter();
- }
-
- protected Class<?> getExpectedType() {
- return URL.class;
- }
-
- // ------------------------------------------------------------------------
-
public void testSimpleConversion() throws Exception {
final String[] message= {
"from String",
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/UUIDConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/UUIDConverterTestCase.java
index 019b2fe..bb08b8a 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/UUIDConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/UUIDConverterTestCase.java
@@ -33,6 +33,12 @@ import junit.framework.TestSuite;
*/
public class UUIDConverterTestCase extends TestCase {
+ public static TestSuite suite() {
+ return new TestSuite(UUIDConverterTestCase.class);
+ }
+
+ // ------------------------------------------------------------------------
+
private Converter converter = null;
// ------------------------------------------------------------------------
@@ -41,6 +47,14 @@ public class UUIDConverterTestCase extends TestCase {
super(name);
}
+ protected Class<?> getExpectedType() {
+ return UUID.class;
+ }
+
+ protected Converter makeConverter() {
+ return new UUIDConverter();
+ }
+
// ------------------------------------------------------------------------
@Override
@@ -48,10 +62,6 @@ public class UUIDConverterTestCase extends TestCase {
converter = makeConverter();
}
- public static TestSuite suite() {
- return new TestSuite(UUIDConverterTestCase.class);
- }
-
@Override
public void tearDown() throws Exception {
converter = null;
@@ -59,16 +69,6 @@ public class UUIDConverterTestCase extends TestCase {
// ------------------------------------------------------------------------
- protected Converter makeConverter() {
- return new UUIDConverter();
- }
-
- protected Class<?> getExpectedType() {
- return UUID.class;
- }
-
- // ------------------------------------------------------------------------
-
public void testSimpleConversion() throws Exception {
final String[] message= {
"from String",
diff --git a/src/test/java/org/apache/commons/beanutils2/converters/ZonedDateTimeConverterTestCase.java b/src/test/java/org/apache/commons/beanutils2/converters/ZonedDateTimeConverterTestCase.java
index 25e8247..c6ccb8e 100644
--- a/src/test/java/org/apache/commons/beanutils2/converters/ZonedDateTimeConverterTestCase.java
+++ b/src/test/java/org/apache/commons/beanutils2/converters/ZonedDateTimeConverterTestCase.java
@@ -30,16 +30,6 @@ import junit.framework.TestSuite;
public class ZonedDateTimeConverterTestCase extends DateConverterTestBase {
/**
- * Construct a new Date test case.
- * @param name Test Name
- */
- public ZonedDateTimeConverterTestCase(final String name) {
- super(name);
- }
-
- // ------------------------------------------------------------------------
-
- /**
* Create Test Suite
* @return test suite
*/
@@ -47,18 +37,25 @@ public class ZonedDateTimeConverterTestCase extends DateConverterTestBase {
return new TestSuite(ZonedDateTimeConverterTestCase.class);
}
- /** Set Up */
- @Override
- public void setUp() throws Exception {
+ // ------------------------------------------------------------------------
+
+ /**
+ * Construct a new Date test case.
+ * @param name Test Name
+ */
+ public ZonedDateTimeConverterTestCase(final String name) {
+ super(name);
}
- /** Tear Down */
+ /**
+ * Return the expected type
+ * @return The expected type
+ */
@Override
- public void tearDown() throws Exception {
+ protected Class<?> getExpectedType() {
+ return ZonedDateTime.class;
}
- // ------------------------------------------------------------------------
-
/**
* Create the Converter with no default value.
* @return A new Converter
@@ -68,6 +65,8 @@ public class ZonedDateTimeConverterTestCase extends DateConverterTestBase {
return new ZonedDateTimeConverter();
}
+ // ------------------------------------------------------------------------
+
/**
* Create the Converter with a default value.
* @param defaultValue The default value
@@ -78,13 +77,14 @@ public class ZonedDateTimeConverterTestCase extends DateConverterTestBase {
return new ZonedDateTimeConverter(defaultValue);
}
- /**
- * Return the expected type
- * @return The expected type
- */
+ /** Set Up */
@Override
- protected Class<?> getExpectedType() {
- return ZonedDateTime.class;
+ public void setUp() throws Exception {
+ }
+
+ /** Tear Down */
+ @Override
+ public void tearDown() throws Exception {
}
/**