View Javadoc

1   /*******************************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   *******************************************************************************/
19  package org.apache.commons.convert;
20  
21  import java.util.Collection;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Set;
25  import java.util.TreeSet;
26  
27  import junit.framework.TestCase;
28  
29  public class TestCollectionConverters extends TestCase {
30  
31      @SuppressWarnings("unchecked")
32      public static <S> void assertArrayToCollection(String label, S source, Class<? extends Collection> targetClass, int resultSize) throws Exception {
33          Class<?> sourceClass = source.getClass();
34          Converter<S, Collection<?>> converter = (Converter<S, Collection<?>>) Converters.getConverter(sourceClass, targetClass);
35          assertTrue(label + " can convert", converter.canConvert(sourceClass, targetClass));
36          Collection<?> result = converter.convert(source);
37          assertTrue(label + " converted", targetClass.isAssignableFrom(result.getClass()));
38          assertEquals(label + " result size", resultSize, result.size());
39          try {
40              Converter<Collection<?>, S> reflectiveConverter = (Converter<Collection<?>, S>) Converters.getConverter(targetClass, sourceClass);
41              assertEquals(label + " reflection converted", sourceClass, reflectiveConverter.convert(result).getClass());
42              assertTrue(label + " can convert", reflectiveConverter.canConvert(targetClass, sourceClass));
43          } catch (ClassNotFoundException e) {
44              System.out.println(converter.getClass() + " not reflective");
45          }
46      }
47  
48      public TestCollectionConverters(String name) {
49          super(name);
50      }
51  
52      public void testCollectionConverters() throws Exception {
53          ConverterLoader loader = new CollectionConverters();
54          loader.loadConverters();
55          int[] intArray = {0, 1, 2, 3, 3};
56          assertArrayToCollection("int[] to List", intArray, List.class, intArray.length);
57          assertArrayToCollection("int[] to LinkedList", intArray, LinkedList.class, intArray.length);
58          assertArrayToCollection("int[] to Set", intArray, Set.class, intArray.length - 1);
59          assertArrayToCollection("int[] to TreeSet", intArray, TreeSet.class, intArray.length - 1);
60          boolean[] booleanArray = {true, false};
61          assertArrayToCollection("boolean[] to List", booleanArray, List.class, booleanArray.length);
62          byte[] byteArray = {0, 1, 2, 3};
63          assertArrayToCollection("byte[] to List", byteArray, List.class, byteArray.length);
64          char[] charArray = {'a', 'b', 'c'};
65          assertArrayToCollection("char[] to List", charArray, List.class, charArray.length);
66          double[] doubleArray = {0, 1, 2, 3};
67          assertArrayToCollection("double[] to List", doubleArray, List.class, doubleArray.length);
68          float[] floatArray = {0, 1, 2, 3};
69          assertArrayToCollection("float[] to List", floatArray, List.class, floatArray.length);
70          long[] longArray = {0, 1, 2, 3};
71          assertArrayToCollection("long[] to List", longArray, List.class, longArray.length);
72          short[] shortArray = {0, 1, 2, 3};
73          assertArrayToCollection("short[] to List", shortArray, List.class, shortArray.length);
74          String[] stringArray = {"a", "b", "c"};
75          assertArrayToCollection("String[] to List", stringArray, List.class, stringArray.length);
76      }
77  }