org.springframework.core.convert.ConverterNotFoundException Java Examples

The following examples show how to use org.springframework.core.convert.ConverterNotFoundException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomDailyStatisticRepositoryTest.java    From microcks with Apache License 2.0 6 votes vote down vote up
@Test
public void testAggregateDailyStatistics(){
   try {
      DailyStatistic stat = repository.aggregateDailyStatistics("20140930");
   } catch (ConverterNotFoundException cvnfe){
      // For now, mapReduce in Fongo is experimental. MapReduce execution is working
      // but SpringData cannot convert Fongo Rhino result into Java object
      // ("No converter found capable of converting from type org.mozilla.javascript.UniqueTag to type java.lang.Integer")
   } catch (UncategorizedMongoDbException ume){
      // For now, mapReduce in Fongo is experimental. MapReduce execution is not working
      // ("org.mozilla.javascript.EcmaError: TypeError: Cannot read property "0" from undefined")
   } catch (RuntimeException re){
      // For now, mapReduce in Fongo is experimental. MapReduce execution is working
      // but SpringData cannot convert Fongo Rhino result into Java object
      // ("json can't serialize type : class org.mozilla.javascript.UniqueTag")
   }
}
 
Example #2
Source File: MapToMapConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void collectionMapSourceTarget() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertFalse(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
		fail("Should have failed");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
Example #3
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0).intValue());
	assertEquals(37, result.get(1).intValue());
}
 
Example #4
Source File: MapToMapConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void collectionMap() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
Example #5
Source File: MapToMapConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void scalarMapNotGenericSourceField() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example #6
Source File: MapToMapConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example #7
Source File: GenericConversionServiceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
	conversionService.addConverter(new MyStringToStringCollectionConverter());

	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));

	try {
		conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
		fail("Should have thrown ConverterNotFoundException");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
}
 
Example #8
Source File: SpringTypeConverterTest.java    From camel-spring-boot with Apache License 2.0 6 votes vote down vote up
@Test
public void testConversionService() {
    Collection<?> source = Arrays.asList(new Person("Name", 30));

    Assert.assertFalse(conversionService.canConvert(Person.class, String.class));
    Assert.assertTrue(conversionService.canConvert(source.getClass(), String.class));

    try {
        conversionService.convert(source, String.class);
    } catch (ConversionFailedException e) {
        // Expected as Person can't be converted to a string according to
        // Spring's FallbackObjectToStringConverter, see javadoc for:
        //
        //   org.springframework.core.convert.support.FallbackObjectToStringConverter
        //
        Assert.assertTrue(e.getCause() instanceof ConverterNotFoundException);
    }


    Assert.assertNull(converter.convertTo(String.class, source));
}
 
Example #9
Source File: CollectionToCollectionConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0).intValue());
	assertEquals(37, result.get(1).intValue());
}
 
Example #10
Source File: MapToMapConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example #11
Source File: MapToMapConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void collectionMap() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
Example #12
Source File: GenericConversionServiceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
	conversionService.addConverter(new MyStringToStringCollectionConverter());

	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));

	try {
		conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
		fail("Should have thrown ConverterNotFoundException");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
}
 
Example #13
Source File: Binder.java    From spring-cloud-gray with Apache License 2.0 6 votes vote down vote up
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target,
                              BindHandler handler, Context context, boolean allowRecursiveBinding) {
    ConfigurationProperty property = findProperty(name, context);
    if (property == null && containsNoDescendantOf(context.getSources(), name)) {
        return null;
    }
    AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);
    if (aggregateBinder != null) {
        return bindAggregate(name, target, handler, context, aggregateBinder);
    }
    if (property != null) {
        try {
            return bindProperty(target, context, property);
        } catch (ConverterNotFoundException ex) {
            // We might still be able to bind it as a bean
            Object bean = bindBean(name, target, handler, context,
                    allowRecursiveBinding);
            if (bean != null) {
                return bean;
            }
            throw ex;
        }
    }
    return bindBean(name, target, handler, context, allowRecursiveBinding);
}
 
Example #14
Source File: CollectionToCollectionConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<String>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0));
	assertEquals(37, result.get(1));
}
 
Example #15
Source File: MapToMapConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<String, String>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example #16
Source File: MapToMapConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void collectionMapSourceTarget() throws Exception {
	Map<String, List<String>> map = new HashMap<>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));

	assertFalse(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
		fail("Should have failed");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}

	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
Example #17
Source File: MapToMapConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void scalarMapNotGenericSourceField() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example #18
Source File: GenericConversionServiceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void adaptedCollectionTypesFromSameSourceType() throws Exception {
	conversionService.addConverter(new MyStringToStringCollectionConverter());

	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection"))));
	assertEquals(Collections.singleton("testX"),
			conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection"))));

	try {
		conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")));
		fail("Should have thrown ConverterNotFoundException");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
}
 
Example #19
Source File: MapToMapConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void scalarMapNotGenericSourceField() throws Exception {
	Map<String, String> map = new HashMap<String, String>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example #20
Source File: MapToMapConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void collectionMap() throws Exception {
	Map<String, List<String>> map = new HashMap<String, List<String>>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
Example #21
Source File: MapToMapConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void collectionMapSourceTarget() throws Exception {
	Map<String, List<String>> map = new HashMap<String, List<String>>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("sourceCollectionMapTarget"));
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
	assertFalse(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
		fail("Should have failed");
	}
	catch (ConverterNotFoundException ex) {
		// expected
	}
	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
Example #22
Source File: ConvertingEncoderDecoderSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void encodeToTextCannotConvert() throws Exception {
	setup(NoConvertersConfig.class);
	thown.expect(EncodeException.class);
	thown.expectCause(isA(ConverterNotFoundException.class));
	new MyTextEncoder().encode(myType);
}
 
Example #23
Source File: ConvertingEncoderDecoderSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void decodeFromTextCannotConvert() throws Exception {
	setup(NoConvertersConfig.class);
	Decoder.Text<MyType> decoder = new MyTextDecoder();
	assertThat(decoder.willDecode(CONVERTED_TEXT), is(false));
	thown.expect(DecodeException.class);
	thown.expectCause(isA(ConverterNotFoundException.class));
	decoder.decode(CONVERTED_TEXT);
}
 
Example #24
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Object handleConverterNotFound(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		assertNotPrimitiveTargetType(sourceType, targetType);
		return null;
	}
	if (sourceType.isAssignableTo(targetType) && targetType.getObjectType().isInstance(source)) {
		return source;
	}
	throw new ConverterNotFoundException(sourceType, targetType);
}
 
Example #25
Source File: GenericConversionService.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object handleConverterNotFound(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		assertNotPrimitiveTargetType(sourceType, targetType);
		return null;
	}
	if (sourceType.isAssignableTo(targetType) && targetType.getObjectType().isInstance(source)) {
		return source;
	}
	throw new ConverterNotFoundException(sourceType, targetType);
}
 
Example #26
Source File: IgnoreTopLevelConverterNotFoundBindHandler.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
@Override
public Object onFailure(ConfigurationPropertyName name, Bindable<?> target,
                        BindContext context, Exception error) throws Exception {
    if (context.getDepth() == 0 && error instanceof ConverterNotFoundException) {
        return null;
    }
    throw error;
}
 
Example #27
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test(expected = ConverterNotFoundException.class)
public void elementTypesNotConvertible() throws Exception {
	List<String> resources = new ArrayList<>();
	resources.add(null);
	resources.add(null);
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings"));
	assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
 
Example #28
Source File: ConvertingEncoderDecoderSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void decodeFromBinaryCannotConvert() throws Exception {
	setup(NoConvertersConfig.class);
	Decoder.Binary<MyType> decoder = new MyBinaryDecoder();
	assertThat(decoder.willDecode(CONVERTED_BYTES), is(false));
	thown.expect(DecodeException.class);
	thown.expectCause(isA(ConverterNotFoundException.class));
	decoder.decode(CONVERTED_BYTES);
}
 
Example #29
Source File: ConvertingEncoderDecoderSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void decodeFromTextCannotConvert() throws Exception {
	setup(NoConvertersConfig.class);
	Decoder.Text<MyType> decoder = new MyTextDecoder();
	assertThat(decoder.willDecode(CONVERTED_TEXT), is(false));
	thown.expect(DecodeException.class);
	thown.expectCause(isA(ConverterNotFoundException.class));
	decoder.decode(CONVERTED_TEXT);
}
 
Example #30
Source File: CollectionToCollectionConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test(expected = ConverterNotFoundException.class)
public void elementTypesNotConvertible() throws Exception {
	List<String> resources = new ArrayList<String>();
	resources.add(null);
	resources.add(null);
	TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("strings"));
	assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}