Skip to content

Commit 9bfe675

Browse files
philwebbcbeams
authored andcommitted
Additional GenericConversionService Tests
1 parent d8469d1 commit 9bfe675

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,12 @@
1616

1717
package org.springframework.core.convert.support;
1818

19-
import static org.junit.Assert.assertTrue;
2019
import static org.junit.Assert.assertEquals;
2120
import static org.junit.Assert.assertFalse;
2221
import static org.junit.Assert.assertNotNull;
2322
import static org.junit.Assert.assertNull;
2423
import static org.junit.Assert.assertSame;
24+
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
2626

2727
import java.awt.Color;
@@ -52,6 +52,7 @@
5252
/**
5353
* @author Keith Donald
5454
* @author Juergen Hoeller
55+
* @author Phillip Webb
5556
*/
5657
public class GenericConversionServiceTests {
5758

@@ -599,4 +600,48 @@ public void testConvertiblePairDifferentEqualsAndHash() throws Exception {
599600
assertFalse(pair.hashCode() == pairOpposite.hashCode());
600601
}
601602

603+
@Test
604+
public void convertPrimitiveArray() throws Exception {
605+
GenericConversionService conversionService = new DefaultConversionService();
606+
byte[] byteArray = new byte[] { 1, 2, 3 };
607+
Byte[] converted = conversionService.convert(byteArray, Byte[].class);
608+
assertTrue(Arrays.equals(converted, new Byte[] { 1, 2, 3 }));
609+
}
610+
611+
@Test
612+
public void canConvertIllegalArgumentNullTargetTypeFromClass() {
613+
try {
614+
conversionService.canConvert(String.class, null);
615+
fail("Did not thow IllegalArgumentException");
616+
} catch(IllegalArgumentException e) {
617+
}
618+
}
619+
620+
@Test
621+
public void canConvertIllegalArgumentNullTargetTypeFromTypeDescriptor() {
622+
try {
623+
conversionService.canConvert(TypeDescriptor.valueOf(String.class), null);
624+
fail("Did not thow IllegalArgumentException");
625+
} catch(IllegalArgumentException e) {
626+
}
627+
}
628+
629+
@Test
630+
@SuppressWarnings({ "rawtypes" })
631+
public void convertHashMapValuesToList() throws Exception {
632+
GenericConversionService conversionService = new DefaultConversionService();
633+
Map<String, Integer> hashMap = new LinkedHashMap<String, Integer>();
634+
hashMap.put("1", 1);
635+
hashMap.put("2", 2);
636+
List converted = conversionService.convert(hashMap.values(), List.class);
637+
assertEquals(Arrays.asList(1, 2), converted);
638+
}
639+
640+
@Test
641+
public void removeConvertible() throws Exception {
642+
conversionService.addConverter(new ColorConverter());
643+
assertTrue(conversionService.canConvert(String.class, Color.class));
644+
conversionService.removeConvertible(String.class, Color.class);
645+
assertFalse(conversionService.canConvert(String.class, Color.class));
646+
}
602647
}

0 commit comments

Comments
 (0)