From 9bfe675761b5e13dcc533b4f4a82fe240c7a2595 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 26 Oct 2012 15:32:49 -0700 Subject: [PATCH] Additional GenericConversionService Tests --- .../GenericConversionServiceTests.java | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java index 27f15b76d0..9c87aed2dd 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 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. @@ -16,12 +16,12 @@ package org.springframework.core.convert.support; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.awt.Color; @@ -52,6 +52,7 @@ import org.springframework.util.StringUtils; /** * @author Keith Donald * @author Juergen Hoeller + * @author Phillip Webb */ public class GenericConversionServiceTests { @@ -599,4 +600,48 @@ public class GenericConversionServiceTests { assertFalse(pair.hashCode() == pairOpposite.hashCode()); } + @Test + public void convertPrimitiveArray() throws Exception { + GenericConversionService conversionService = new DefaultConversionService(); + byte[] byteArray = new byte[] { 1, 2, 3 }; + Byte[] converted = conversionService.convert(byteArray, Byte[].class); + assertTrue(Arrays.equals(converted, new Byte[] { 1, 2, 3 })); + } + + @Test + public void canConvertIllegalArgumentNullTargetTypeFromClass() { + try { + conversionService.canConvert(String.class, null); + fail("Did not thow IllegalArgumentException"); + } catch(IllegalArgumentException e) { + } + } + + @Test + public void canConvertIllegalArgumentNullTargetTypeFromTypeDescriptor() { + try { + conversionService.canConvert(TypeDescriptor.valueOf(String.class), null); + fail("Did not thow IllegalArgumentException"); + } catch(IllegalArgumentException e) { + } + } + + @Test + @SuppressWarnings({ "rawtypes" }) + public void convertHashMapValuesToList() throws Exception { + GenericConversionService conversionService = new DefaultConversionService(); + Map hashMap = new LinkedHashMap(); + hashMap.put("1", 1); + hashMap.put("2", 2); + List converted = conversionService.convert(hashMap.values(), List.class); + assertEquals(Arrays.asList(1, 2), converted); + } + + @Test + public void removeConvertible() throws Exception { + conversionService.addConverter(new ColorConverter()); + assertTrue(conversionService.canConvert(String.class, Color.class)); + conversionService.removeConvertible(String.class, Color.class); + assertFalse(conversionService.canConvert(String.class, Color.class)); + } }