View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.util;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.lang.reflect.Array;
25  import java.math.BigDecimal;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.Date;
30  import java.util.List;
31  
32  import org.apache.commons.jxpath.NodeSet;
33  import org.apache.commons.jxpath.Pointer;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests BasicTypeConverter
38   */
39  public class BasicTypeConverterTest {
40  
41      public void assertConversion(final Object from, final Class toType, final Object expected) {
42          final boolean can = TypeUtils.canConvert(from, toType);
43          assertTrue(can, "Can convert: " + from.getClass() + " to " + toType);
44          Object result = TypeUtils.convert(from, toType);
45          if (result.getClass().isArray()) {
46              final ArrayList list = new ArrayList();
47              for (int j = 0; j < Array.getLength(result); j++) {
48                  list.add(Array.get(result, j));
49              }
50              result = list;
51          }
52          assertEquals(expected, result, "Convert: " + from.getClass() + " to " + toType);
53      }
54  
55      @Test
56      public void testArrayToArray() {
57          assertConversion(new int[] { 1, 2 }, String[].class, Arrays.asList(new String[] { "1", "2" }));
58      }
59  
60      @Test
61      public void testArrayToList() {
62          assertConversion(new int[] { 1, 2 }, List.class, Arrays.asList(new Object[] { Integer.valueOf(1), Integer.valueOf(2) }));
63      }
64  
65      @Test
66      public void testBeanUtilsConverter() {
67          assertConversion("12", BigDecimal.class, new BigDecimal(12));
68      }
69  
70      @Test
71      public void testInvalidConversion() {
72          assertThrows(Exception.class, () -> TypeUtils.convert("'foo'", Date.class), "Type conversion exception");
73      }
74  
75      @Test
76      public void testListToArray() {
77          assertConversion(Arrays.asList(new Integer[] { Integer.valueOf(1), Integer.valueOf(2) }), String[].class, Arrays.asList(new String[] { "1", "2" }));
78          assertConversion(Arrays.asList(new String[] { "1", "2" }), int[].class, Arrays.asList(new Integer[] { Integer.valueOf(1), Integer.valueOf(2) }));
79      }
80  
81      // succeeds in current version
82      @Test
83      public void testNodeSetToInteger() {
84          assertConversion(new NodeSet() {
85  
86              @Override
87              public List getNodes() {
88                  return null;
89              }
90  
91              @Override
92              public List getPointers() {
93                  return null;
94              }
95  
96              @Override
97              public List getValues() {
98                  return Collections.singletonList("9");
99              }
100         }, Integer.class, Integer.valueOf(9));
101     }
102 
103     @Test
104     public void testNodeSetToString() {
105         assertConversion(new NodeSet() {
106 
107             @Override
108             public List getNodes() {
109                 return null;
110             }
111 
112             @Override
113             public List getPointers() {
114                 return null;
115             }
116 
117             @Override
118             public List getValues() {
119                 final List list = new ArrayList();
120                 list.add("hello");
121                 list.add("goodbye");
122                 return Collections.singletonList(list);
123             }
124         }, String.class, "hello");
125     }
126 
127     @Test
128     public void testPointerToString() {
129         assertConversion(new Pointer() {
130 
131             private static final long serialVersionUID = 1L;
132 
133             @Override
134             public String asPath() {
135                 return null;
136             }
137 
138             @Override
139             public Object clone() {
140                 return null;
141             }
142 
143             @Override
144             public int compareTo(final Object o) {
145                 return 0;
146             }
147 
148             @Override
149             public Object getNode() {
150                 return null;
151             }
152 
153             @Override
154             public Object getRootNode() {
155                 return null;
156             }
157 
158             @Override
159             public Object getValue() {
160                 return "value";
161             }
162 
163             @Override
164             public void setValue(final Object value) {
165             }
166         }, String.class, "value");
167     }
168 
169     @Test
170     public void testPrimitiveToString() {
171         assertConversion(Integer.valueOf(1), String.class, "1");
172     }
173 
174     @Test
175     public void testSingletonArrayToString() {
176         assertConversion(new String[] { "Earth" }, String.class, "Earth");
177     }
178 
179     @Test
180     public void testSingletonCollectionToString() {
181         assertConversion(Collections.singleton("Earth"), String.class, "Earth");
182     }
183 }