1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
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.apache.commons.jxpath.util;
17  
18  import java.lang.reflect.Array;
19  import java.math.BigDecimal;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.Date;
24  import java.util.List;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.commons.jxpath.NodeSet;
29  import org.apache.commons.jxpath.Pointer;
30  
31  /***
32   * Tests BasicTypeConverter
33   * 
34   * @author Dmitri Plotnikov
35   * @version $Revision: 1.7 $ $Date: 2004/06/29 21:50:02 $
36   */
37  
38  public class BasicTypeConverterTest extends TestCase {
39      /***
40       * Construct a new instance of this test case.
41       *
42       * @param name Name of the test case
43       */
44      public BasicTypeConverterTest(String name) {
45          super(name);
46      }
47  
48      public void testPrimitiveToString() {
49          assertConversion(new Integer(1), String.class, "1");
50      }
51  
52      public void testArrayToList() {
53          assertConversion(
54              new int[] { 1, 2 },
55              List.class,
56              Arrays.asList(new Object[] { new Integer(1), new Integer(2)}));
57      }
58  
59      public void testArrayToArray() {
60          assertConversion(
61              new int[] { 1, 2 },
62              String[].class,
63              Arrays.asList(new String[] { "1", "2" }));
64      }
65  
66      public void testListToArray() {
67          assertConversion(
68              Arrays.asList(new Integer[] { new Integer(1), new Integer(2)}),
69              String[].class,
70              Arrays.asList(new String[] { "1", "2" }));
71  
72          assertConversion(
73              Arrays.asList(new String[] { "1", "2" }),
74              int[].class,
75              Arrays.asList(new Integer[] { new Integer(1), new Integer(2)}));
76      }
77  
78      public void testInvalidConversion() {
79          boolean exception = false;
80          try {
81              TypeUtils.convert("'foo'", Date.class);
82          }
83          catch (Throwable ex) {
84              exception = true;
85          }
86          assertTrue("Type conversion exception", exception);
87      }
88  
89      public void assertConversion(Object from, Class toType, Object expected) {
90          boolean can = TypeUtils.canConvert(from, toType);
91          assertTrue("Can convert: " + from.getClass() + " to " + toType, can);
92          Object result = TypeUtils.convert(from, toType);
93          if (result.getClass().isArray()) {
94              ArrayList list = new ArrayList();
95              for (int j = 0; j < Array.getLength(result); j++) {
96                  list.add(Array.get(result, j));
97              }
98              result = list;
99          }
100         assertEquals(
101             "Convert: " + from.getClass() + " to " + toType,
102             expected,
103             result);
104     }
105     
106     public void testSingletonCollectionToString() {
107         assertConversion(Collections.singleton("Earth"), String.class, "Earth");
108     }
109 
110     public void testSingletonArrayToString() {
111         assertConversion(new String[] { "Earth" }, String.class, "Earth");
112     }
113 
114     public void testPointerToString() {
115         assertConversion(new Pointer() {
116             public Object getValue() {
117                 return "value";
118             }
119             public Object getNode() {
120                 return null;
121             }
122             public void setValue(Object value) {
123             }
124             public Object getRootNode() {
125                 return null;
126             }
127             public String asPath() {
128                 return null;
129             }
130             public Object clone() {
131                 return null;
132             }
133             public int compareTo(Object o) {
134                 return 0;
135             }
136         }, String.class, "value");
137     }
138 
139     public void testNodeSetToString() {
140         assertConversion(new NodeSet() {
141             public List getNodes() {
142                 return null;
143             }
144             public List getPointers() {
145                 return null;
146             }
147             public List getValues() {
148                 List list = new ArrayList();
149                 list.add("hello");
150                 list.add("goodbye");
151                 return Collections.singletonList(list);
152             }
153         }, String.class, "hello");
154     }
155 
156     // succeeds in current version
157     public void testNodeSetToInteger() {
158         assertConversion(new NodeSet() {
159             public List getNodes() {
160                 return null;
161             }
162             public List getPointers() {
163                 return null;
164             }
165             public List getValues() {
166                 return Collections.singletonList("9");
167             }
168         }, Integer.class, new Integer(9));
169     }    
170     
171     public void testBeanUtilsConverter() {
172         assertConversion("12", BigDecimal.class, new BigDecimal(12));
173     }
174 }