@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)); }
@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)); }
@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)); }
@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)); }
@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 } }
@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") } }
@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)); }
@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 e) { } 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)); }
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); }
@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")))); }
@Test public void convertFromStreamToArrayNoConverter() throws NoSuchFieldException { Stream<Integer> stream = Arrays.asList(1, 2, 3).stream(); TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs")); ; thrown.expect(ConversionFailedException.class); thrown.expectCause(is(instanceOf(ConverterNotFoundException.class))); this.conversionService.convert(stream, arrayOfLongs); }
@Test public void encodeToTextCannotConvert() throws Exception { setup(NoConvertersConfig.class); thown.expect(EncodeException.class); thown.expectCause(isA(ConverterNotFoundException.class)); new MyTextEncoder().encode(myType); }
@Test public void encodeToBinaryCannotConvert() throws Exception { setup(NoConvertersConfig.class); thown.expect(EncodeException.class); thown.expectCause(isA(ConverterNotFoundException.class)); new MyBinaryEncoder().encode(myType); }
@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); }
@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); }
private <T> T get(String propertyName, Class<T> type, Properties props) throws IllegalArgumentException { Validate.notEmpty(propertyName); Validate.notNull(type); String value = props.getProperty(propertyName); if(value == null) { throw new IllegalArgumentException(); } try { return converter.convert(value, type); } catch (IllegalArgumentException | ConversionFailedException | ConverterNotFoundException ex) { LOGGER.warn("Invalid type for property: {}, value: {}, reason: {}", propertyName, value, ex.getMessage()); throw new IllegalArgumentException(); } }
private Object handleConverterNotFound(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { assertNotPrimitiveTargetType(sourceType, targetType); return source; } if (sourceType.isAssignableTo(targetType) && targetType.getObjectType().isInstance(source)) { return source; } throw new ConverterNotFoundException(sourceType, targetType); }
@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")))); }
@Test public void converterNotFound() { try { conversionService.convert("3", Integer.class); fail("Should have thrown an exception"); } catch (ConverterNotFoundException e) { } }
@Test public void genericConverterDelegatingBackToConversionServiceConverterNotFound() { conversionService.addConverter(new ObjectToArrayConverter(conversionService)); assertFalse(conversionService.canConvert(String.class, Integer[].class)); try { conversionService.convert("3,4,5", Integer[].class); fail("should have failed"); } catch (ConverterNotFoundException ex) { } }
public static String toString(Object source) { if (source == null) return null; if (source instanceof String) return (String) source; if (source instanceof Entity) { Object labelValue = ((Entity) source).getLabelValue(); return labelValue != null ? labelValue.toString() : null; } if (source instanceof List) { StringBuilder sb = new StringBuilder(); for (Object obj : (List<?>) source) { if (sb.length() > 0) sb.append(","); sb.append(toString(obj)); } return sb.toString(); } if (getConversionService() == null) return source.toString(); try { return convert(source, String.class); } catch (ConverterNotFoundException e) { return source.toString(); } }
@Test(expected = ConverterNotFoundException.class) public void convertObjectToObjectNoValueOfMethodOrConstructor() { conversionService.convert(new Long(3), SSN.class); }
@Test(expected = ConverterNotFoundException.class) public void converterNotFound() { conversionService.convert("3", Integer.class); }
@Test(expected = ConverterNotFoundException.class) public void convertSuperTarget() { conversionService.addConverter(new ColorConverter()); conversionService.convert("#000000", SystemColor.class); }
@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(expected=ConverterNotFoundException.class) public void convertObjectToObjectNoValueOFMethodOrConstructor() { conversionService.convert(new Long(3), SSN.class); }
@Test(expected=ConverterNotFoundException.class) public void convertSuperTarget() { conversionService.addConverter(new ColorConverter()); conversionService.convert("#000000", SystemColor.class); }