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