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.StringToStringListConverter;
20  import org.rblasch.convert.converters.collections.ArrayConverterFactory;
21  import org.rblasch.convert.converters.lang.JlConverterFactory;
22  import org.rblasch.convert.converters.primitives.StringToIntegerConverter;
23  import org.rblasch.convert.type.Types;
24  
25  import java.util.List;
26  
27  /***
28   * @author Ronald Blaschke
29   */
30  public class ArrayTest extends TestCase {
31      public void testPrimitive() throws Exception {
32          final SptConverter ci = SptConverter.createEmpty();
33          ci.addFactory(new JlConverterFactory());
34          ci.addFactory(new ArrayConverterFactory());
35          final byte[] bytes = new byte[] {1, 2, 3};
36  
37          final Object o = ci.convert(bytes, Types.findTypeByName("array<int>"));
38          assertEquals(int[].class, o.getClass());
39      }
40  
41      public void testStringToStringList2() {
42          final SptConverter ci = SptConverter.createDefault();
43          ci.addConverter(new StringToStringListConverter());
44          ci.addConverter(new StringToIntegerConverter());
45  
46          ci.convert("10", Types.findTypeByClass(Integer.class));
47          final List/*<Integer>*/ l = (List) ci.convert("1,42,3", Types.findPTypeByClass(List.class, Integer.class));
48          assertEquals(3, l.size());
49          assertEquals(new Integer(1), l.get(0));
50          assertEquals(new Integer(42), l.get(1));
51          assertEquals(new Integer(3), l.get(2));
52          //System.out.println(ci.dumpGraph());
53      }
54  }