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.ClassMetaConverter;
20  import org.rblasch.convert.converters.lang.StringToClassConverter;
21  import org.rblasch.convert.converters.lang.NullToStringConverter;
22  import org.rblasch.convert.converters.primitives.ByteToBooleanConverter;
23  import org.rblasch.convert.converters.primitives.IntegerToByteConverter;
24  import org.rblasch.convert.converters.primitives.StringToBooleanConverter;
25  import org.rblasch.convert.converters.primitives.StringToIntegerConverter;
26  import org.rblasch.convert.type.Types;
27  
28  import java.util.Collections;
29  import java.util.HashSet;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.Set;
33  
34  /***
35   * @author Ronald Blaschke
36   */
37  public class ConverterTest extends TestCase {
38      public void testNoIdentity() throws Exception {
39          final SptConverter c = SptConverter.createEmpty();
40          try {
41              c.convert("ABC", Types.findTypeByClass(String.class));
42              fail("Should throw no such conversion exception");
43          } catch (final NoSuchConversionException ex) {
44              // expected
45          }
46      }
47  
48      public void testNull() throws Exception {
49          final SptConverter c = SptConverter.createEmpty();
50          c.addConverter(new NullToStringConverter());
51          assertEquals("null", c.convert(null, Types.findTypeByClass(String.class)));
52      }
53  
54      public void testSimple() throws Exception {
55          final Set/*<MetaConverter>*/ converters = new HashSet();
56          converters.add(new ClassMetaConverter(Byte.class, Integer.class,
57                  new Converter() {
58                      public Object convert(final Object obj) throws Exception {
59                          return new Integer(((Byte) obj).byteValue());
60                      }
61                  }, 10));
62  
63          final SptConverter ci = new SptConverter(converters);
64  
65          final Object i = ci.convert(new Byte((byte) 42), Types.findTypeByClass(Integer.class));
66          assertEquals(Integer.class, i.getClass());
67          assertEquals(42, ((Integer) i).intValue());
68      }
69  
70      public void testToString() throws Exception {
71          final SptConverter ci = SptConverter.createJl();
72  
73          final Object s = ci.convert(new Integer(42), Types.findTypeByClass(String.class));
74          assertEquals(String.class, s.getClass());
75          assertEquals("42", s);
76      }
77  
78      public void testInterface() throws Exception {
79          final SptConverter ci = SptConverter.createJl();
80          final LinkedList list = new LinkedList();
81          list.add(new Integer(42));
82  
83          final Object l = ci.convert(list, Types.findTypeByClass(List.class));
84          assertEquals(LinkedList.class, l.getClass());
85          assertEquals(new Integer(42), ((List) l).get(0));
86      }
87  
88      public void testStringToInteger() throws Exception {
89          final SptConverter ci = new SptConverter(Collections.singleton(new StringToIntegerConverter()));
90          final String s = "42";
91  
92          final Object i = ci.convert(s, Types.findTypeByClass(Integer.class));
93          assertEquals(new Integer(42), i);
94      }
95  
96      public void testStringToBoolean() throws Exception {
97          final Set/*<MetaConverter>*/ cs = new HashSet();
98          cs.add(new ByteToBooleanConverter());
99          cs.add(new IntegerToByteConverter());
100         cs.add(new StringToIntegerConverter());
101 
102         final SptConverter ci = new SptConverter(cs);
103         assertEquals(Boolean.TRUE, (Boolean) ci.convert("1", Types.findTypeByClass(Boolean.class)));
104         assertEquals(Boolean.FALSE, (Boolean) ci.convert("0", Types.findTypeByClass(Boolean.class)));
105     }
106 
107     public void testStringToBoolean2() throws Exception {
108         final Set/*<MetaConverter>*/ cs = new HashSet();
109         // possible conversion: String -> Integer -> Byte -> Boolean
110         cs.add(new ByteToBooleanConverter());
111         cs.add(new IntegerToByteConverter());
112         cs.add(new StringToIntegerConverter());
113 
114         // but this one is shorter, and will be used
115         cs.add(new StringToBooleanConverter());
116 
117         final SptConverter ci = new SptConverter(cs);
118         assertEquals(Boolean.TRUE, (Boolean) ci.convert("1", Types.findTypeByClass(Boolean.class)));
119         assertEquals(Boolean.FALSE, (Boolean) ci.convert("0", Types.findTypeByClass(Boolean.class)));
120     }
121 
122     public void testWidening() throws Exception {
123         final SptConverter ci = SptConverter.createJl();
124         assertEquals(new Long(10), ci.convert(new Byte((byte) 10), Types.findTypeByClass(Long.class)));
125     }
126 
127     public void testAddConverter() throws Exception {
128         final SptConverter ci = new SptConverter();
129     }
130 
131     public void testClass() {
132         final Set/*<MetaConverter>*/ cs = new HashSet();
133         cs.add(new StringToClassConverter());
134 
135         final SptConverter ci = new SptConverter(cs);
136         final Object o = ci.convert(A.class.getName(), Types.findTypeByClass(Class.class));
137         assertTrue(o instanceof Class);
138         assertEquals(A.class.getName(), ((Class) o).getName());
139     }
140 
141     private static class A {}
142 }