|
29 | 29 |
|
30 | 30 | package org.scijava.util; |
31 | 31 |
|
| 32 | +import static org.junit.Assert.assertArrayEquals; |
32 | 33 | import static org.junit.Assert.assertEquals; |
33 | 34 | import static org.junit.Assert.assertNotNull; |
34 | 35 | import static org.junit.Assert.assertNull; |
|
43 | 44 | import java.util.Set; |
44 | 45 |
|
45 | 46 | import org.junit.Test; |
46 | | -import org.scijava.util.Types; |
47 | 47 |
|
48 | 48 | /** |
49 | 49 | * Tests {@link ConversionUtils}. |
@@ -254,23 +254,37 @@ class Struct { |
254 | 254 | * and a collection. |
255 | 255 | */ |
256 | 256 | @Test |
257 | | - public void testBadObjectElements() { |
| 257 | + public void testIncompatibleCollections() { |
258 | 258 | class Struct { |
259 | 259 |
|
260 | 260 | private Double[] doubleArray; |
261 | | - private List<String> stringList; |
262 | | - @SuppressWarnings("unused") |
263 | | - private Set<char[]> nestedArray; |
| 261 | + private List<Number> numberList; |
| 262 | + private Set<Integer[]> setOfIntegerArrays; |
264 | 263 | } |
265 | 264 | final Struct struct = new Struct(); |
266 | 265 |
|
267 | | - // Test abnormal behavior for an object array |
268 | | - setFieldValue(struct, "doubleArray", "not a double array"); |
269 | | - assertEquals(null, struct.doubleArray); |
| 266 | + // NB: DefaultConverter converts non-collection/array objects to |
| 267 | + // collection/array objects, even if some or all of the constituent elements |
| 268 | + // cannot be converted to the array/collection component/element type. |
270 | 269 |
|
271 | | - // Test abnormal behavior for a list |
272 | | - setFieldValue(struct, "nestedArray", "definitely not a set of char arrays"); |
273 | | - assertNull(struct.stringList); |
| 270 | + // Test object to incompatible array type |
| 271 | + setFieldValue(struct, "doubleArray", "not a double array"); |
| 272 | + assertArrayEquals(new Double[] {null}, struct.doubleArray); |
| 273 | + |
| 274 | + // Test object to incompatible List type |
| 275 | + setFieldValue(struct, "numberList", "not actually a list of numbers"); |
| 276 | + List<Number> expectedList = Arrays.asList((Number) null); |
| 277 | + assertEquals(expectedList, struct.numberList); |
| 278 | + |
| 279 | + // Test object to incompatible Set type |
| 280 | + setFieldValue(struct, "setOfIntegerArrays", // |
| 281 | + "definitely not a set of Integer[]"); |
| 282 | + assertNotNull(struct.setOfIntegerArrays); |
| 283 | + assertEquals(1, struct.setOfIntegerArrays.size()); |
| 284 | + Integer[] singleton = struct.setOfIntegerArrays.iterator().next(); |
| 285 | + assertNotNull(singleton); |
| 286 | + assertEquals(1, singleton.length); |
| 287 | + assertNull(singleton[0]); |
274 | 288 | } |
275 | 289 |
|
276 | 290 | /** |
|
0 commit comments