1   /*
2    * Copyright 2004 Ronald Blaschke.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.rblasch.convert;
17  
18  import junit.framework.TestCase;
19  import org.rblasch.convert.converters.primitives.StringToIntegerConverter;
20  import org.rblasch.convert.type.Types;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /***
26   * @author Ronald Blaschke
27   */
28  public class PConverterTest extends TestCase {
29      public void testList() throws Exception {
30          final SptConverter ci = SptConverter.createDefault();
31          final List/*<Integer>*/ iList = new ArrayList();
32          iList.add(new Integer(42));
33  
34          final List/*<String>*/ sList = new ArrayList();
35          sList.add("42");
36  
37          final Object l = ci.convert(iList, Types.findPTypeByType(Types.findTypeByClass(List.class), Types.findTypeByClass(Integer.class)), Types.findPTypeByType(Types.findTypeByClass(List.class), Types.findTypeByClass(String.class)));
38          assertEquals(sList, l);
39      }
40  
41      public void testParameterized() throws Exception {
42          final SptConverter ci = SptConverter.createDefault();
43  
44          final Integer[] array = new Integer[1];
45          array[0] = new Integer(42);
46  
47          final Object l = ci.convert(array, Types.findPTypeByType(Types.findTypeByClass(List.class), Types.findTypeByClass(String.class)));
48          assertTrue(l instanceof List);
49          assertEquals("42", ((List) l).get(0));
50      }
51  
52      public void testStringListToInteger() throws Exception {
53          final SptConverter ci = SptConverter.createDefault();
54          ci.addConverter(new StringToIntegerConverter());
55          final List/*<String>*/ sList = new ArrayList();
56          sList.add("42");
57  
58          final List/*<Integer>*/ iList = new ArrayList();
59          iList.add(new Integer(42));
60  
61          final Object l = ci.convert(sList, Types.findPTypeByType(Types.findTypeByClass(List.class), Types.findTypeByClass(String.class)), Types.findPTypeByType(Types.findTypeByClass(List.class), Types.findTypeByClass(Integer.class)));
62          assertEquals(iList, l);
63      }
64  }