001 /*******************************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *******************************************************************************/
019 package org.apache.commons.convert;
020
021 import java.util.Collection;
022 import java.util.LinkedList;
023 import java.util.List;
024 import java.util.Set;
025 import java.util.TreeSet;
026
027 import junit.framework.TestCase;
028
029 public class TestCollectionConverters extends TestCase {
030
031 @SuppressWarnings("unchecked")
032 public static <S> void assertArrayToCollection(String label, S source, Class<? extends Collection> targetClass, int resultSize) throws Exception {
033 Class<?> sourceClass = source.getClass();
034 Converter<S, Collection<?>> converter = (Converter<S, Collection<?>>) Converters.getConverter(sourceClass, targetClass);
035 assertTrue(label + " can convert", converter.canConvert(sourceClass, targetClass));
036 Collection<?> result = converter.convert(source);
037 assertTrue(label + " converted", targetClass.isAssignableFrom(result.getClass()));
038 assertEquals(label + " result size", resultSize, result.size());
039 try {
040 Converter<Collection<?>, S> reflectiveConverter = (Converter<Collection<?>, S>) Converters.getConverter(targetClass, sourceClass);
041 assertEquals(label + " reflection converted", sourceClass, reflectiveConverter.convert(result).getClass());
042 assertTrue(label + " can convert", reflectiveConverter.canConvert(targetClass, sourceClass));
043 } catch (ClassNotFoundException e) {
044 System.out.println(converter.getClass() + " not reflective");
045 }
046 }
047
048 public TestCollectionConverters(String name) {
049 super(name);
050 }
051
052 public void testCollectionConverters() throws Exception {
053 ConverterLoader loader = new CollectionConverters();
054 loader.loadConverters();
055 int[] intArray = {0, 1, 2, 3, 3};
056 assertArrayToCollection("int[] to List", intArray, List.class, intArray.length);
057 assertArrayToCollection("int[] to LinkedList", intArray, LinkedList.class, intArray.length);
058 assertArrayToCollection("int[] to Set", intArray, Set.class, intArray.length - 1);
059 assertArrayToCollection("int[] to TreeSet", intArray, TreeSet.class, intArray.length - 1);
060 boolean[] booleanArray = {true, false};
061 assertArrayToCollection("boolean[] to List", booleanArray, List.class, booleanArray.length);
062 byte[] byteArray = {0, 1, 2, 3};
063 assertArrayToCollection("byte[] to List", byteArray, List.class, byteArray.length);
064 char[] charArray = {'a', 'b', 'c'};
065 assertArrayToCollection("char[] to List", charArray, List.class, charArray.length);
066 double[] doubleArray = {0, 1, 2, 3};
067 assertArrayToCollection("double[] to List", doubleArray, List.class, doubleArray.length);
068 float[] floatArray = {0, 1, 2, 3};
069 assertArrayToCollection("float[] to List", floatArray, List.class, floatArray.length);
070 long[] longArray = {0, 1, 2, 3};
071 assertArrayToCollection("long[] to List", longArray, List.class, longArray.length);
072 short[] shortArray = {0, 1, 2, 3};
073 assertArrayToCollection("short[] to List", shortArray, List.class, shortArray.length);
074 String[] stringArray = {"a", "b", "c"};
075 assertArrayToCollection("String[] to List", stringArray, List.class, stringArray.length);
076 }
077 }