Author: niallp
Date: Sat Nov 4 19:13:20 2006
New Revision: 471351
URL: http://svn.apache.org/viewvc?view=rev&rev=471351
Log:
BEANUTILS-242 - Add generic ArrayConverter
Added:
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
(with props)
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
(with props)
Added:
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java?view=auto&rev=471351
==============================================================================
---
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
(added)
+++
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
Sat Nov 4 19:13:20 2006
@@ -0,0 +1,352 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.beanutils.converters;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Collection;
+import java.io.StreamTokenizer;
+import java.io.StringReader;
+import java.io.IOException;
+import java.lang.reflect.Array;
+import org.apache.commons.beanutils.ConversionException;
+import org.apache.commons.beanutils.Converter;
+
+/**
+ * Generic [EMAIL PROTECTED] Converter} implementaion that handles conversion
+ * to and from <b>array</b> objects.
+ * <p>
+ * Can be configured to either return a <i>default value</i> or throw a
+ * <code>ConversionException</code> if a conversion error occurs.
+ * <p>
+ * The main features of this implementation are:
+ * <ul>
+ * <li><b>Element Conversion</b> - delegates to a [EMAIL PROTECTED]
Converter},
+ * appropriate for the type, to convert individual elements
+ * of the array. This leverages the power of existing converters
+ * without having to replicate their functionality for converting
+ * to the element type and removes the need to create a specifc
+ * array type converters.</li>
+ * <li><b>Arrays or Collections</b> - can convert from either arrays or
+ * Collections to an array, limited only by the capability
+ * of the delegate [EMAIL PROTECTED] Converter}.</li>
+ * <li><b>Delimited Lists</b> - can Convert <b>to</b> and <b>from</b> a
+ * delimited list in String format.</li>
+ * <li><b>Multi Dimensional Arrays</b> - its possible to convert to
+ * multi-dimensional arrays, by embedding [EMAIL PROTECTED]
ArrayConverter}
+ * within each other - see example below.</li>
+ * </ul>
+ *
+ * <h3>Parsing Delimited Lists</h3>
+ * This implementation can convert a delimited list in <code>String</code>
format
+ * into an array of the appropriate type. By default, it uses a comma as the
delimiter
+ * but the following methods can be used to configure parsing:
+ * <ul>
+ * <li><code>setDelimiter(char)</code> - allows the character used as
+ * the delimiter to be configured [default is a comma].</li>
+ * <li><code>setAllowedChars(char[])</code> - adds additional characters
+ * (to the default alphabetic/numeric) to those considered to be
+ * valid token characters.
+ * </ul>
+ *
+ * <h3>Multi Dimensional Arrays</h3>
+ * It is possible to convert to mulit-dimensional arrays by using
+ * [EMAIL PROTECTED] ArrayConverter} as the element [EMAIL PROTECTED]
Converter}
+ * within another [EMAIL PROTECTED] ArrayConverter}.
+ * <p>
+ * For example, the following code demonstrates how to construct a [EMAIL
PROTECTED] Converter}
+ * to convert a delimited <code>String</code> into a two dimensional integer
array:
+ * <p>
+ * <pre>
+ * // Construct an Integer Converter
+ * IntegerConverter integerConverter = new IntegerConverter();
+ *
+ * // Construct an array Converter for an integer array (i.e. int[]) using
+ * // an IntegerConverter as the element converter.
+ * // N.B. Uses the default comma (i.e. ",") as the delimiter between
individual numbers
+ * ArrayConverter arrayConverter = new ArrayConverter(int[].class,
integerConverter);
+ *
+ * // Construct a "Matrix" Converter which converts arrays of integer
arrays using
+ * // the pre-ceeding ArrayConverter as the element Converter.
+ * // N.B. Uses a semi-colon (i.e. ";") as the delimiter to separate the
different sets of numbers.
+ * // Also the delimiter used by the first ArrayConverter needs to be
added to the
+ * // "allowed characters" for this one.
+ * ArrayConverter matrixConverter = new ArrayConverter(int[][].class,
arrayConverter);
+ * matrixConverter.setDelimiter(';');
+ * matrixConverter.setAllowedChars(new char[] {','});
+ *
+ * // Do the Conversion
+ * String matrixString = "11,12,13 ; 21,22,23 ; 31,32,33 ; 41,42,43";
+ * int[][] result = (int[][])matrixConverter.convert(int[][].class,
matrixString);
+ * </pre>
+ *
+ * @version $Revision$ $Date$
+ * @since 1.8.0
+ */
+public class ArrayConverter extends AbstractConverter {
+
+ private Converter elementConverter;
+ private int defaultSize;
+ private char delimiter = ',';
+ private char[] allowedChars = new char[] {'.', '-'};
+
+ // ----------------------------------------------------------- Constructors
+
+ /**
+ * Construct an <b>array</b> <code>Converter</code> with the specified
+ * <b>component</b> <code>Converter</code> that throws a
+ * <code>ConversionException</code> if an error occurs.
+ *
+ * @param defaultType The default array type this
+ * <code>Converter</code> handles
+ * @param elementConverter Converter used to convert
+ * individual array elements.
+ */
+ public ArrayConverter(Class defaultType, Converter elementConverter) {
+ super(defaultType);
+ if (!defaultType.isArray()) {
+ throw new IllegalArgumentException("Default type must be an
array.");
+ }
+ if (elementConverter == null) {
+ throw new IllegalArgumentException("Component Converter is
missing.");
+ }
+ this.elementConverter = elementConverter;
+ }
+
+ /**
+ * Construct an <b>array</b> <code>Converter</code> with the specified
+ * <b>component</b> <code>Converter</code> that returns a default
+ * array of the specified size if an error occurs.
+ *
+ * @param defaultType The default array type this
+ * <code>Converter</code> handles
+ * @param elementConverter Converter used to convert
+ * individual array elements.
+ * @param defaultSize Specifies the size of the default array value or if
less
+ * than zero indicates that a <code>null</code> default value should be
used.
+ */
+ public ArrayConverter(Class defaultType, Converter elementConverter, int
defaultSize) {
+ this(defaultType, elementConverter);
+ this.defaultSize = defaultSize;
+ Object defaultValue = null;
+ if (defaultSize >= 0) {
+ defaultValue = Array.newInstance(defaultType.getComponentType(),
defaultSize);
+ }
+ setDefaultValue(defaultValue);
+ }
+
+ /**
+ * Set the delimiter to be used for parsing a delimited String.
+ *
+ * @param delimiter The delimiter [default ',']
+ */
+ public void setDelimiter(char delimiter) {
+ this.delimiter = delimiter;
+ }
+
+ /**
+ * Set the allowed characters to be used for parsing a delimited String.
+ *
+ * @param allowedChars Characters which are to be considered as part of
+ * the tokens when parsing a delimited String [default is '.' and '-']
+ */
+ public void setAllowedChars(char[] allowedChars) {
+ this.allowedChars = allowedChars;
+ }
+
+ /**
+ * Convert the input object into a String.
+ *
+ * @param value The value to be converted.
+ * @return the converted String value.
+ */
+ protected String convertToString(Object value) {
+
+ Class type = value.getClass();
+ if (!type.isArray()) {
+ throw new ConversionException("Not an array: " + toString(type));
+ }
+
+ int size = Array.getLength(value);
+ if (size == 0) {
+ return (String)getDefault(String.class);
+ }
+
+ StringBuffer buffer = new StringBuffer();
+ for (int i = 0; i < size; i++) {
+ if (i > 0) {
+ buffer.append(delimiter);
+ }
+ Object element = Array.get(value, i);
+ element = elementConverter.convert(String.class, element);
+ if (element != null) {
+ buffer.append(element);
+ }
+ }
+
+ return buffer.toString();
+
+ }
+
+ /**
+ * Convert the input object into an array of the
+ * specified type.
+ *
+ * @param type The type to which this value should be converted.
+ * @param value The input value to be converted.
+ * @return The converted value.
+ * @throws Exception if conversion cannot be performed successfully
+ */
+ protected Object convertToType(Class type, Object value) throws Exception {
+
+ Class sourceType = value.getClass();
+ Class componentType = type.getComponentType();
+
+ Collection collection = null;
+ int size = 0;
+
+ if (sourceType.isArray()) {
+ size = Array.getLength(value);
+ } else if (value instanceof Collection) {
+ collection = (Collection)value;
+ size = collection.size();
+ } else {
+ collection = parseElements(value.toString().trim());
+ size = collection.size();
+ }
+
+ // Allocate a new Array
+ Object newArray = Array.newInstance(componentType, size);
+
+ // Convert and set each element in the new Array
+ Iterator iterator = collection == null ? null : collection.iterator();
+ for (int i = 0; i < size; i++) {
+ Object element = iterator == null ? Array.get(value, i) :
iterator.next();
+ element = elementConverter.convert(componentType, element);
+ Array.set(newArray, i, element);
+ }
+
+ return newArray;
+ }
+
+ /**
+ * Return the default value for conversions to the specified
+ * type.
+ * @param type Data type to which this value should be converted.
+ * @return The default value for the specified type.
+ */
+ protected Object getDefault(Class type) {
+ if (type.equals(String.class)) {
+ return null;
+ }
+
+ Object defaultValue = super.getDefault(type);
+ if (defaultValue == null) {
+ return null;
+ }
+
+ if (defaultValue.getClass().equals(type)) {
+ return defaultValue;
+ } else {
+ return Array.newInstance(type.getComponentType(), defaultSize);
+ }
+
+ }
+
+ /**
+ * <p>Parse an incoming String of the form similar to an array initializer
+ * in the Java language into a <code>List</code> individual Strings
+ * for each element, according to the following rules.</p>
+ * <ul>
+ * <li>The string is expected to be a comma-separated list of values.</li>
+ * <li>The string may optionally have matching '{' and '}' delimiters
+ * around the list.</li>
+ * <li>Whitespace before and after each element is stripped.</li>
+ * <li>Elements in the list may be delimited by single or double quotes.
+ * Within a quoted elements, the normal Java escape sequences are
valid.</li>
+ * </ul>
+ *
+ * @param value String value to be parsed
+ * @return List of parsed elements.
+ *
+ * @throws ConversionException if the syntax of <code>svalue</code>
+ * is not syntactically valid
+ * @throws NullPointerException if <code>svalue</code>
+ * is <code>null</code>
+ */
+ private List parseElements(String value) {
+
+ if (log().isDebugEnabled()) {
+ log().debug("Parsing elements, delimiter=[" + delimiter + "],
value=[" + value + "]");
+ }
+
+ // Trim any matching '{' and '}' delimiters
+ value = value.trim();
+ if (value.startsWith("{") && value.endsWith("}")) {
+ value = value.substring(1, value.length() - 1);
+ }
+
+ try {
+
+ // Set up a StreamTokenizer on the characters in this String
+ StreamTokenizer st = new StreamTokenizer(new StringReader(value));
+ st.whitespaceChars(delimiter , delimiter); // Set the delimiters
+ st.ordinaryChars('0', '9'); // Needed to turn off numeric flag
+ st.wordChars('0', '9'); // Needed to make part of tokens
+ for (int i = 0; i < allowedChars.length; i++) {
+ st.ordinaryChars(allowedChars[i], allowedChars[i]);
+ st.wordChars(allowedChars[i], allowedChars[i]);
+ }
+
+ // Split comma-delimited tokens into a List
+ List list = null;
+ while (true) {
+ int ttype = st.nextToken();
+ if ((ttype == StreamTokenizer.TT_WORD) || (ttype > 0)) {
+ if (list == null) {
+ list = new ArrayList();
+ }
+ list.add(st.sval);
+ } else if (ttype == StreamTokenizer.TT_EOF) {
+ break;
+ } else {
+ throw new ConversionException
+ ("Encountered token of type " + ttype);
+ }
+ }
+
+ if (list == null) {
+ list = Collections.EMPTY_LIST;
+ }
+ if (log().isDebugEnabled()) {
+ log().debug(list.size() + " elements parsed");
+ }
+
+ // Return the completed list
+ return (list);
+
+ } catch (IOException e) {
+
+ throw new ConversionException("Error parsing elements: " +
e.getMessage(), e);
+
+ }
+
+ }
+
+}
Propchange:
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java?view=auto&rev=471351
==============================================================================
---
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
(added)
+++
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
Sat Nov 4 19:13:20 2006
@@ -0,0 +1,302 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.beanutils.converters;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Locale;
+
+import org.apache.commons.beanutils.ConversionException;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Test Case for the ArrayConverter class.
+ *
+ * @version $Revision$ $Date$
+ */
+public class ArrayConverterTestCase extends TestCase {
+
+ /**
+ * Construct a new Array Converter test case.
+ * @param name Test Name
+ */
+ public ArrayConverterTestCase(String name) {
+ super(name);
+ }
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Create Test Suite
+ * @return test suite
+ */
+ public static TestSuite suite() {
+ return new TestSuite(ArrayConverterTestCase.class);
+ }
+
+ /** Set Up */
+ public void setUp() throws Exception {
+ }
+
+ /** Tear Down */
+ public void tearDown() throws Exception {
+ }
+
+
+ // ------------------------------------------------------------------------
+
+ /**
+ * Test Converting using the IntegerConverter as the component Converter
+ */
+ public void testComponentIntegerConverter() {
+
+ IntegerConverter intConverter = new IntegerConverter(new Integer(0));
+ intConverter.setPattern("#,###");
+ intConverter.setLocale(Locale.US);
+ ArrayConverter arrayConverter = new ArrayConverter(int[].class,
intConverter, 0);
+ arrayConverter.setAllowedChars(new char[] {',', '-'});
+ arrayConverter.setDelimiter(';');
+
+ // Expected results
+ int[] intArray = new int[] {1111, 2222, 3333, 4444};
+ String stringA = "1,111; 2,222; 3,333; 4,444";
+ String stringB = intArray[0]+ ";" + intArray[1] + ";" +
intArray[2] + ";" +intArray[3];
+ String[] strArray = new String[] {""+intArray[0], ""+intArray[1],
""+intArray[2], ""+intArray[3]};
+ long[] longArray = new long[] {intArray[0], intArray[1],
intArray[2], intArray[3]};
+ Long[] LONGArray = new Long[] {new Long(intArray[0]), new
Long(intArray[1]), new Long(intArray[2]), new Long(intArray[3])};
+ Integer[] IntegerArray = new Integer[] {new Integer(intArray[0]), new
Integer(intArray[1]), new Integer(intArray[2]), new Integer(intArray[3])};
+ ArrayList strList = new ArrayList();
+ ArrayList longList = new ArrayList();
+ for (int i = 0; i < strArray.length; i++) {
+ strList.add(strArray[i]);
+ longList.add(LONGArray[i]);
+ }
+
+
+ String msg = null;
+
+ // String --> int[]
+ try {
+ msg = "String --> int[]";
+ checkArray(msg, intArray, arrayConverter.convert(int[].class,
stringA));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // String --> int[] (with braces)
+ try {
+ msg = "String --> Integer[] (with braces)";
+ checkArray(msg, IntegerArray,
arrayConverter.convert(Integer[].class, "{" + stringA + "}"));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // String[] --> int[]
+ try {
+ msg = "String[] --> int[]";
+ checkArray(msg, intArray, arrayConverter.convert(int[].class,
strArray));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // String[] --> Integer[]
+ try {
+ msg = "String[] --> Integer[]";
+ checkArray(msg, IntegerArray,
arrayConverter.convert(Integer[].class, strArray));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // long[] --> int[]
+ try {
+ msg = "long[] --> int[]";
+ checkArray(msg, intArray, arrayConverter.convert(int[].class,
longArray));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // LONG[] --> int[]
+ try {
+ msg = "LONG[] --> int[]";
+ checkArray(msg, intArray, arrayConverter.convert(int[].class,
LONGArray));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // LONG[] --> String
+ try {
+ msg = "LONG[] --> String";
+ assertEquals(msg, stringB, arrayConverter.convert(String.class,
LONGArray));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // LONG[] --> String[]
+ try {
+ msg = "long[] --> String[]";
+ checkArray(msg, strArray, arrayConverter.convert(String[].class,
LONGArray));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // Collection of String --> Integer[]
+ try {
+ msg = "Collection of String --> Integer[]";
+ checkArray(msg, IntegerArray,
arrayConverter.convert(Integer[].class, strList));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+
+ // Collection of Long --> int[]
+ try {
+ msg = "Collection of Long --> int[]";
+ checkArray(msg, intArray, arrayConverter.convert(int[].class,
longList));
+ } catch (Exception e) {
+ fail(msg + " failed " + e);
+ }
+ }
+
+ /**
+ * Test the Matrix!!!! (parses a String into a 2 dimensional integer array
or matrix)
+ */
+ public void testTheMatrix() {
+
+ // Test Date - create the Matrix!!
+ // Following String uses two delimiter:
+ // - comma (",") to separate individual numbers
+ // - semi-colon (";") to separate lists of numbers
+ String matrixString = "11,12,13 ; 21,22,23 ; 31,32,33 ; 41,42,43";
+ int[][] expected = new int[][] {new int[] {11, 12, 13},
+ new int[] {21, 22, 23},
+ new int[] {31, 32, 33},
+ new int[] {41, 42, 43}};
+
+ // Construct an Integer Converter
+ IntegerConverter integerConverter = new IntegerConverter();
+
+ // Construct an array Converter for an integer array (i.e. int[]) using
+ // an IntegerConverter as the element converter.
+ // N.B. Uses the default comma (i.e. ",") as the delimiter between
individual numbers
+ ArrayConverter arrayConverter = new ArrayConverter(int[].class,
integerConverter);
+
+ // Construct a "Matrix" Converter which converts arrays of integer
arrays using
+ // the first (int[]) Converter as the element Converter.
+ // N.B. Uses a semi-colon (i.e. ";") as the delimiter to separate the
different sets of numbers.
+ // Also the delimiter for the above array Converter needs to be
added to this
+ // array Converter's "allowed characters"
+ ArrayConverter matrixConverter = new ArrayConverter(int[][].class,
arrayConverter);
+ matrixConverter.setDelimiter(';');
+ matrixConverter.setAllowedChars(new char[] {','});
+
+ try {
+ // Do the Conversion
+ Object result = matrixConverter.convert(int[][].class,
matrixString);
+
+ // Check it actually worked OK
+ assertEquals("Check int[][].class", int[][].class,
result.getClass());
+ int[][] matrix = (int[][])result;
+ assertEquals("Check int[][] length", expected.length,
matrix.length);
+ for (int i = 0; i < expected.length; i++) {
+ assertEquals("Check int[" + i + "] length",
expected[i].length, matrix[i].length);
+ for (int j = 0; j < expected[i].length; j++) {
+ String label = "Matrix int[" + i + "," + j + "] element";
+ assertEquals(label, expected[i][j], matrix[i][j]);
+ // System.out.println(label + " = " + matrix[i][j]);
+ }
+ }
+ } catch (Exception e) {
+ fail("Matrix Conversion threw " + e);
+ }
+ }
+
+ /**
+ * Test Converting using the IntegerConverter as the component Converter
+ */
+ public void testInvalidWithDefault() {
+ int[] zeroArray = new int[0];
+ int[] oneArray = new int[1];
+ 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() {
+ int[] zeroArray = new int[0];
+ 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 (IllegalArgumentException e) {
+ // expected result
+ }
+ try {
+ new ArrayConverter(Boolean.class, new DateConverter());
+ fail("Default Type not an array - expected
IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected result
+ }
+ try {
+ new ArrayConverter(int[].class, null);
+ fail("Component Converter missing - expected
IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected result
+ }
+ try {
+ new ArrayConverter(int[].class, new
IntegerConverter()).convert(String.class, "ABC");
+ fail("Non-array to String, expected ConversionException");
+ } catch (ConversionException e) {
+ // expected result
+ }
+ }
+
+ /**
+ * 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(String msg, Object expected, 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());
+ 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++) {
+ Object expectElement = Array.get(expected, i);
+ Object resultElement = Array.get(result, i);
+ assertEquals(msg + " Element " + i, expectElement, resultElement);
+ }
+ }
+}
Propchange:
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]