@Test public void testListToObjectConversion() { List<Object> raw = new ArrayList<>(); raw.add("one"); raw.add("two"); Object converted = conversionService.convert(raw, Object.class); assertSame(raw, converted); }
@Test public void testMapToObjectConversion() { Map<Object, Object> raw = new HashMap<>(); raw.put("key", "value"); Object converted = conversionService.convert(raw, Object.class); assertSame(raw, converted); }
@Test(expected = ConversionFailedException.class) public void nothingInCommon() throws Exception { List<Object> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(3); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Test public void differentImpls() throws Exception { List<Resource> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(new FileSystemResource("test")); resources.add(new TestResource()); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Test public void allNulls() throws Exception { List<Resource> resources = new ArrayList<>(); resources.add(null); resources.add(null); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Test public void byteBufferToByteBuffer() throws Exception { byte[] bytes = new byte[] { 1, 2, 3 }; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); ByteBuffer convert = this.conversionService.convert(byteBuffer, ByteBuffer.class); assertThat(convert, not(sameInstance(byteBuffer.rewind()))); assertThat(convert, equalTo(byteBuffer.rewind())); assertThat(convert, equalTo(ByteBuffer.wrap(bytes))); assertThat(convert.array(), equalTo(bytes)); }
@Test public void convertNullAnnotatedStringToString() throws Exception { String source = null; TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("annotatedString")); TypeDescriptor targetType = TypeDescriptor.valueOf(String.class); conversionService.convert(source, sourceType, targetType); }
@Test public void byteArrayToByteBuffer() throws Exception { byte[] bytes = new byte[] { 1, 2, 3 }; ByteBuffer convert = this.conversionService.convert(bytes, ByteBuffer.class); assertThat(convert.array(), not(sameInstance(bytes))); assertThat(convert.array(), equalTo(bytes)); }
@Test public void scalarMapNotGenericTarget() throws Exception { Map<String, String> map = new HashMap<>(); map.put("1", "9"); map.put("2", "37"); assertTrue(conversionService.canConvert(Map.class, Map.class)); assertSame(map, conversionService.convert(map, Map.class)); }
@Test public void byteBufferToByteArray() throws Exception { byte[] bytes = new byte[] { 1, 2, 3 }; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byte[] convert = this.conversionService.convert(byteBuffer, byte[].class); assertThat(convert, not(sameInstance(bytes))); assertThat(convert, equalTo(bytes)); }
@Test public void testStringToEnumSet() throws Exception { conversionService.addConverterFactory(new StringToEnumConverterFactory()); List<String> list = new ArrayList<>(); list.add("A"); list.add("C"); assertEquals(EnumSet.of(MyEnum.A, MyEnum.C), conversionService.convert(list, TypeDescriptor.forObject(list), new TypeDescriptor(getClass().getField("enumSet")))); }
@Test(expected = ConversionFailedException.class) public void convertWrongTypeArgument() { conversionService.addConverterFactory(new StringToNumberConverterFactory()); conversionService.convert("BOGUS", Integer.class); }
@Test public void testIgnoreCopyConstructor() { WithCopyConstructor value = new WithCopyConstructor(); Object result = conversionService.convert(value, WithCopyConstructor.class); assertSame(value, result); }
@Test public void conditionalConversionForAllTypes() { MyConditionalGenericConverter converter = new MyConditionalGenericConverter(); conversionService.addConverter(converter); assertEquals((Integer) 3, conversionService.convert(3, Integer.class)); assertThat(converter.getSourceTypes().size(), greaterThan(2)); assertTrue(converter.getSourceTypes().stream().allMatch(td -> Integer.class.equals(td.getType()))); }
@Test public void testStringArrayToIntegerArray() { conversionService.addConverter(new MyStringArrayToIntegerArrayConverter()); Integer[] converted = conversionService.convert(new String[] {"x1", "z3"}, Integer[].class); assertArrayEquals(new Integer[] { 1, 3 }, converted); }
@Test public void testEnumWithInterfaceToStringConversion() { // SPR-9692 conversionService.addConverter(new EnumToStringConverter(conversionService)); conversionService.addConverter(new MyEnumInterfaceToStringConverter<MyEnum>()); assertEquals("1", conversionService.convert(MyEnum.A, String.class)); }
@Test public void testObjectArrayToStringArray() { conversionService.addConverter(new MyBaseInterfaceToStringConverter()); conversionService.addConverter(new ArrayToArrayConverter(conversionService)); String[] converted = conversionService.convert(new MyInterfaceImplementer[] {new MyInterfaceImplementer()}, String[].class); assertEquals("RESULT", converted[0]); }
@Test public void emptyListToObject() { conversionService.addConverter(new CollectionToObjectConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); List<String> list = new ArrayList<>(); TypeDescriptor sourceType = TypeDescriptor.forObject(list); TypeDescriptor targetType = TypeDescriptor.valueOf(Integer.class); assertTrue(conversionService.canConvert(sourceType, targetType)); assertNull(conversionService.convert(list, sourceType, targetType)); }
@Test public void conditionalConverter() { MyConditionalConverter converter = new MyConditionalConverter(); conversionService.addConverter(new ColorConverter()); conversionService.addConverter(converter); assertEquals(Color.BLACK, conversionService.convert("#000000", Color.class)); assertTrue(converter.getMatchAttempts() > 0); }
@Test public void conditionalConverterFactory() { MyConditionalConverterFactory converter = new MyConditionalConverterFactory(); conversionService.addConverter(new ColorConverter()); conversionService.addConverterFactory(converter); assertEquals(Color.BLACK, conversionService.convert("#000000", Color.class)); assertTrue(converter.getMatchAttempts() > 0); assertTrue(converter.getNestedMatchAttempts() > 0); }