/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.convert.support;
import java.awt.Color;
import java.awt.SystemColor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.ConditionalConverter;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
import static java.util.Comparator.*;
import static java.util.stream.Collectors.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for the {@link GenericConversionService}.
*
*
For tests involving the {@link DefaultConversionService}, see
* {@link DefaultConversionServiceTests}.
*
* @author Keith Donald
* @author Juergen Hoeller
* @author Phillip Webb
* @author David Haraburda
* @author Sam Brannen
* @see DefaultConversionServiceTests
*/
public class GenericConversionServiceTests {
private final GenericConversionService conversionService = new GenericConversionService();
@Test
public void canConvert() {
assertFalse(conversionService.canConvert(String.class, Integer.class));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(String.class, Integer.class));
}
@Test
public void canConvertAssignable() {
assertTrue(conversionService.canConvert(String.class, String.class));
assertTrue(conversionService.canConvert(Integer.class, Number.class));
assertTrue(conversionService.canConvert(boolean.class, boolean.class));
assertTrue(conversionService.canConvert(boolean.class, Boolean.class));
}
@Test(expected = IllegalArgumentException.class)
public void canConvertFromClassSourceTypeToNullTargetType() {
conversionService.canConvert(String.class, null);
}
@Test(expected = IllegalArgumentException.class)
public void canConvertFromTypeDescriptorSourceTypeToNullTargetType() {
conversionService.canConvert(TypeDescriptor.valueOf(String.class), null);
}
@Test
public void canConvertNullSourceType() {
assertTrue(conversionService.canConvert(null, Integer.class));
assertTrue(conversionService.canConvert(null, TypeDescriptor.valueOf(Integer.class)));
}
@Test
public void convert() {
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertEquals(new Integer(3), conversionService.convert("3", Integer.class));
}
@Test
public void convertNullSource() {
assertEquals(null, conversionService.convert(null, Integer.class));
}
@Test(expected = ConversionFailedException.class)
public void convertNullSourcePrimitiveTarget() {
conversionService.convert(null, int.class);
}
@Test(expected = ConversionFailedException.class)
public void convertNullSourcePrimitiveTargetTypeDescriptor() {
conversionService.convert(null, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(int.class));
}
@Test(expected = IllegalArgumentException.class)
public void convertNotNullSourceNullSourceTypeDescriptor() {
conversionService.convert("3", null, TypeDescriptor.valueOf(int.class));
}
@Test
public void convertAssignableSource() {
assertEquals(Boolean.FALSE, conversionService.convert(false, boolean.class));
assertEquals(Boolean.FALSE, conversionService.convert(false, Boolean.class));
}
@Test(expected = ConverterNotFoundException.class)
public void converterNotFound() {
conversionService.convert("3", Integer.class);
}
@Test(expected = IllegalArgumentException.class)
public void addConverterNoSourceTargetClassInfoAvailable() {
conversionService.addConverter(new UntypedConverter());
}
@Test
public void sourceTypeIsVoid() {
assertFalse(conversionService.canConvert(void.class, String.class));
}
@Test
public void targetTypeIsVoid() {
assertFalse(conversionService.canConvert(String.class, void.class));
}
@Test
public void convertNull() {
assertNull(conversionService.convert(null, Integer.class));
}
@Test(expected = IllegalArgumentException.class)
public void convertToNullTargetClass() {
conversionService.convert("3", (Class>) null);
}
@Test(expected = IllegalArgumentException.class)
public void convertToNullTargetTypeDescriptor() {
conversionService.convert("3", TypeDescriptor.valueOf(String.class), null);
}
@Test(expected = IllegalArgumentException.class)
public void convertWrongSourceTypeDescriptor() {
conversionService.convert("3", TypeDescriptor.valueOf(Integer.class), TypeDescriptor.valueOf(Long.class));
}
@Test(expected = ConversionFailedException.class)
public void convertWrongTypeArgument() {
conversionService.addConverterFactory(new StringToNumberConverterFactory());
conversionService.convert("BOGUS", Integer.class);
}
@Test
public void convertSuperSourceType() {
conversionService.addConverter(new Converter() {
@Override
public Integer convert(CharSequence source) {
return Integer.valueOf(source.toString());
}
});
Integer result = conversionService.convert("3", Integer.class);
assertEquals(new Integer(3), result);
}
// SPR-8718
@Test(expected = ConverterNotFoundException.class)
public void convertSuperTarget() {
conversionService.addConverter(new ColorConverter());
conversionService.convert("#000000", SystemColor.class);
}
@Test
public void convertObjectToPrimitive() {
assertFalse(conversionService.canConvert(String.class, boolean.class));
conversionService.addConverter(new StringToBooleanConverter());
assertTrue(conversionService.canConvert(String.class, boolean.class));
Boolean b = conversionService.convert("true", boolean.class);
assertTrue(b);
assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class)));
b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(boolean.class));
assertTrue(b);
}
@Test
public void convertObjectToPrimitiveViaConverterFactory() {
assertFalse(conversionService.canConvert(String.class, int.class));
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertTrue(conversionService.canConvert(String.class, int.class));
Integer three = conversionService.convert("3", int.class);
assertEquals(3, three.intValue());
}
@Test(expected = ConverterNotFoundException.class)
public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
conversionService.addConverter(new ObjectToArrayConverter(conversionService));
assertFalse(conversionService.canConvert(String.class, Integer[].class));
conversionService.convert("3,4,5", Integer[].class);
}
@Test
public void testListToIterableConversion() {
List