@Override @Nullable public <T> T convertIfNecessary(@Nullable Object value, @Nullable Class<T> requiredType, @Nullable MethodParameter methodParam) throws TypeMismatchException { return convertIfNecessary(value, requiredType, (methodParam != null ? new TypeDescriptor(methodParam) : TypeDescriptor.valueOf(requiredType))); }
@Test public void shouldFailToConvertIfNoStream() throws NoSuchFieldException { thrown.expect(IllegalStateException.class); this.streamConverter.convert(new Object(), new TypeDescriptor(Types.class.getField("listOfStrings")), new TypeDescriptor(Types.class.getField("arrayOfLongs"))); }
@Test public void convertCollectionToObjectAssignableTarget() throws Exception { Collection<String> source = new ArrayList<>(); source.add("foo"); Object result = conversionService.convert(source, new TypeDescriptor(getClass().getField("assignableTarget"))); assertEquals(source, result); }
@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 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 convertArrayToCollectionGenericTypeConversion() throws Exception { @SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) conversionService.convert(new String[] {"1", "2", "3"}, TypeDescriptor .valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList"))); assertEquals(Integer.valueOf(1), result.get(0)); assertEquals(Integer.valueOf(2), result.get(1)); assertEquals(Integer.valueOf(3), result.get(2)); }
@Test public void propertyGenericType() throws Exception { GenericType<Integer> genericBean = new IntegerType(); Property property = new Property(getClass(), genericBean.getClass().getMethod("getProperty"), genericBean.getClass().getMethod("setProperty", Integer.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(Integer.class, desc.getType()); }
@Test public void propertyTypeCovariance() throws Exception { GenericType<Number> genericBean = new NumberType(); Property property = new Property(getClass(), genericBean.getClass().getMethod("getProperty"), genericBean.getClass().getMethod("setProperty", Number.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(Integer.class, desc.getType()); }
private void assertAnnotationFoundOnMethod(Class<? extends Annotation> annotationType, String methodName) throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(new MethodParameter(getClass().getMethod(methodName), -1)); assertNotNull("Should have found @" + annotationType.getSimpleName() + " on " + methodName + ".", typeDescriptor.getAnnotation(annotationType)); }
@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 public void getSource() throws Exception { Field field = getClass().getField("fieldScalar"); MethodParameter methodParameter = new MethodParameter(getClass().getMethod("testParameterPrimitive", int.class), 0); assertThat(new TypeDescriptor(field).getSource(), equalTo((Object) field)); assertThat(new TypeDescriptor(methodParameter).getSource(), equalTo((Object) methodParameter)); assertThat(TypeDescriptor.valueOf(Integer.class).getSource(), equalTo((Object) Integer.class)); }
@Test @SuppressWarnings("rawtypes") public void convertStringToCollectionWithElementConversion() throws Exception { List result = (List) conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericList"))); assertEquals(3, result.size()); assertEquals(1, result.get(0)); assertEquals(2, result.get(1)); assertEquals(3, result.get(2)); }
@Test @SuppressWarnings("unchecked") public void convertObjectToOptional() { Method method = ClassUtils.getMethod(TestEntity.class, "handleOptionalValue", Optional.class); MethodParameter parameter = new MethodParameter(method, 0); TypeDescriptor descriptor = new TypeDescriptor(parameter); Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor); assertEquals(Optional.class, actual.getClass()); assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get()); }
@Test public void propertyComplex() throws Exception { Property property = new Property(getClass(), getClass().getMethod("getComplexProperty"), getClass().getMethod("setComplexProperty", Map.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(String.class, desc.getMapKeyTypeDescriptor().getType()); assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getElementTypeDescriptor().getType()); }
@Test public void propertyGenericClassList() throws Exception { IntegerClass genericBean = new IntegerClass(); Property property = new Property(genericBean.getClass(), genericBean.getClass().getMethod("getListProperty"), genericBean.getClass().getMethod("setListProperty", List.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(List.class, desc.getType()); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(MethodAnnotation1.class)); assertTrue(desc.hasAnnotation(MethodAnnotation1.class)); }
@Test public void mapKeyTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("mapPreserveContext")); assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.getMapKeyTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
@Test public void mapValueTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("mapPreserveContext")); assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.getMapValueTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
@Test public void fieldComplexTypeDescriptor() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString")); assertTrue(typeDescriptor.isArray()); assertEquals(List.class,typeDescriptor.getElementTypeDescriptor().getType()); assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals("java.util.List<java.lang.String>[]",typeDescriptor.toString()); }
@Test public void fieldListOfListUnknown() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfListOfUnknown")); assertFalse(typeDescriptor.isArray()); assertEquals(List.class, typeDescriptor.getType()); assertEquals(List.class, typeDescriptor.getElementTypeDescriptor().getType()); assertNull(typeDescriptor.getElementTypeDescriptor().getElementTypeDescriptor()); assertEquals("java.util.List<java.util.List<?>>", typeDescriptor.toString()); }
@Test public void parameterPrimitive() throws Exception { TypeDescriptor desc = new TypeDescriptor(new MethodParameter(getClass().getMethod("testParameterPrimitive", int.class), 0)); assertEquals(int.class, desc.getType()); assertEquals(Integer.class, desc.getObjectType()); assertEquals("int", desc.getName()); assertEquals("int", desc.toString()); assertTrue(desc.isPrimitive()); assertEquals(0, desc.getAnnotations().length); assertFalse(desc.isCollection()); assertFalse(desc.isMap()); }