KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jxpath > util > BasicTypeConverterTest


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 JavaDoc;
19 import java.math.BigDecimal JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.List JavaDoc;
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 JavaDoc name) {
45         super(name);
46     }
47
48     public void testPrimitiveToString() {
49         assertConversion(new Integer JavaDoc(1), String JavaDoc.class, "1");
50     }
51
52     public void testArrayToList() {
53         assertConversion(
54             new int[] { 1, 2 },
55             List JavaDoc.class,
56             Arrays.asList(new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2)}));
57     }
58
59     public void testArrayToArray() {
60         assertConversion(
61             new int[] { 1, 2 },
62             String JavaDoc[].class,
63             Arrays.asList(new String JavaDoc[] { "1", "2" }));
64     }
65
66     public void testListToArray() {
67         assertConversion(
68             Arrays.asList(new Integer JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2)}),
69             String JavaDoc[].class,
70             Arrays.asList(new String JavaDoc[] { "1", "2" }));
71
72         assertConversion(
73             Arrays.asList(new String JavaDoc[] { "1", "2" }),
74             int[].class,
75             Arrays.asList(new Integer JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2)}));
76     }
77
78     public void testInvalidConversion() {
79         boolean exception = false;
80         try {
81             TypeUtils.convert("'foo'", Date JavaDoc.class);
82         }
83         catch (Throwable JavaDoc ex) {
84             exception = true;
85         }
86         assertTrue("Type conversion exception", exception);
87     }
88
89     public void assertConversion(Object JavaDoc from, Class JavaDoc toType, Object JavaDoc expected) {
90         boolean can = TypeUtils.canConvert(from, toType);
91         assertTrue("Can convert: " + from.getClass() + " to " + toType, can);
92         Object JavaDoc result = TypeUtils.convert(from, toType);
93         if (result.getClass().isArray()) {
94             ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc.class, "Earth");
108     }
109
110     public void testSingletonArrayToString() {
111         assertConversion(new String JavaDoc[] { "Earth" }, String JavaDoc.class, "Earth");
112     }
113
114     public void testPointerToString() {
115         assertConversion(new Pointer() {
116             public Object JavaDoc getValue() {
117                 return "value";
118             }
119             public Object JavaDoc getNode() {
120                 return null;
121             }
122             public void setValue(Object JavaDoc value) {
123             }
124             public Object JavaDoc getRootNode() {
125                 return null;
126             }
127             public String JavaDoc asPath() {
128                 return null;
129             }
130             public Object JavaDoc clone() {
131                 return null;
132             }
133             public int compareTo(Object JavaDoc o) {
134                 return 0;
135             }
136         }, String JavaDoc.class, "value");
137     }
138
139     public void testNodeSetToString() {
140         assertConversion(new NodeSet() {
141             public List JavaDoc getNodes() {
142                 return null;
143             }
144             public List JavaDoc getPointers() {
145                 return null;
146             }
147             public List JavaDoc getValues() {
148                 List JavaDoc list = new ArrayList JavaDoc();
149                 list.add("hello");
150                 list.add("goodbye");
151                 return Collections.singletonList(list);
152             }
153         }, String JavaDoc.class, "hello");
154     }
155
156     // succeeds in current version
157
public void testNodeSetToInteger() {
158         assertConversion(new NodeSet() {
159             public List JavaDoc getNodes() {
160                 return null;
161             }
162             public List JavaDoc getPointers() {
163                 return null;
164             }
165             public List JavaDoc getValues() {
166                 return Collections.singletonList("9");
167             }
168         }, Integer JavaDoc.class, new Integer JavaDoc(9));
169     }
170     
171     public void testBeanUtilsConverter() {
172         assertConversion("12", BigDecimal JavaDoc.class, new BigDecimal JavaDoc(12));
173     }
174 }
Popular Tags