Skip to content

Commit 07f985a

Browse files
author
Keith Donald
committed
more tests; several assertions for the programmer
1 parent 08180e9 commit 07f985a

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

org.springframework.core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public static TypeDescriptor valueOf(Class<?> type) {
135135
* @return the collection type descriptor
136136
*/
137137
public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor elementType) {
138+
if (!Collection.class.isAssignableFrom(collectionType)) {
139+
throw new IllegalArgumentException("collectionType must be a java.util.Collection");
140+
}
138141
return new TypeDescriptor(collectionType, elementType);
139142
}
140143

@@ -148,6 +151,9 @@ public static TypeDescriptor collection(Class<?> collectionType, TypeDescriptor
148151
* @return the map type descriptor
149152
*/
150153
public static TypeDescriptor map(Class<?> mapType, TypeDescriptor keyType, TypeDescriptor valueType) {
154+
if (!Map.class.isAssignableFrom(mapType)) {
155+
throw new IllegalArgumentException("mapType must be a java.util.Map");
156+
}
151157
return new TypeDescriptor(mapType, keyType, valueType);
152158
}
153159

org.springframework.core/src/test/java/org/springframework/core/convert/TypeDescriptorTests.java

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,85 @@ public void setTest4(List<Map<Integer, String>> test4) {
719719
}
720720

721721
@Test
722-
public void testEquals() throws Exception {
722+
public void collection() {
723+
TypeDescriptor desc = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Integer.class));
724+
assertEquals(List.class, desc.getType());
725+
assertEquals(List.class, desc.getObjectType());
726+
assertEquals("java.util.List", desc.getName());
727+
assertEquals("java.util.List<java.lang.Integer>", desc.toString());
728+
assertTrue(!desc.isPrimitive());
729+
assertEquals(0, desc.getAnnotations().length);
730+
assertTrue(desc.isCollection());
731+
assertFalse(desc.isArray());
732+
assertEquals(Integer.class, desc.getElementType());
733+
assertEquals(TypeDescriptor.valueOf(Integer.class), desc.getElementTypeDescriptor());
734+
assertFalse(desc.isMap());
735+
assertNull(desc.getMapKeyType());
736+
assertEquals(TypeDescriptor.NULL, desc.getMapKeyTypeDescriptor());
737+
assertNull(desc.getMapValueType());
738+
assertEquals(TypeDescriptor.NULL, desc.getMapValueTypeDescriptor());
739+
}
740+
741+
@Test
742+
public void collectionNested() {
743+
TypeDescriptor desc = TypeDescriptor.collection(List.class, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Integer.class)));
744+
assertEquals(List.class, desc.getType());
745+
assertEquals(List.class, desc.getObjectType());
746+
assertEquals("java.util.List", desc.getName());
747+
assertEquals("java.util.List<java.util.List<java.lang.Integer>>", desc.toString());
748+
assertTrue(!desc.isPrimitive());
749+
assertEquals(0, desc.getAnnotations().length);
750+
assertTrue(desc.isCollection());
751+
assertFalse(desc.isArray());
752+
assertEquals(List.class, desc.getElementType());
753+
assertEquals(TypeDescriptor.valueOf(Integer.class), desc.getElementTypeDescriptor().getElementTypeDescriptor());
754+
assertFalse(desc.isMap());
755+
assertNull(desc.getMapKeyType());
756+
assertEquals(TypeDescriptor.NULL, desc.getMapKeyTypeDescriptor());
757+
assertNull(desc.getMapValueType());
758+
assertEquals(TypeDescriptor.NULL, desc.getMapValueTypeDescriptor());
759+
}
760+
761+
@Test
762+
public void map() {
763+
TypeDescriptor desc = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class));
764+
assertEquals(Map.class, desc.getType());
765+
assertEquals(Map.class, desc.getObjectType());
766+
assertEquals("java.util.Map", desc.getName());
767+
assertEquals("java.util.Map<java.lang.String, java.lang.Integer>", desc.toString());
768+
assertTrue(!desc.isPrimitive());
769+
assertEquals(0, desc.getAnnotations().length);
770+
assertFalse(desc.isCollection());
771+
assertFalse(desc.isArray());
772+
assertNull(desc.getElementType());
773+
assertEquals(TypeDescriptor.NULL, desc.getElementTypeDescriptor());
774+
assertTrue(desc.isMap());
775+
assertEquals(String.class, desc.getMapKeyTypeDescriptor().getType());
776+
assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getType());
777+
}
778+
779+
@Test
780+
public void mapNested() {
781+
TypeDescriptor desc = TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class),
782+
TypeDescriptor.map(Map.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)));
783+
assertEquals(Map.class, desc.getType());
784+
assertEquals(Map.class, desc.getObjectType());
785+
assertEquals("java.util.Map", desc.getName());
786+
assertEquals("java.util.Map<java.lang.String, java.util.Map<java.lang.String, java.lang.Integer>>", desc.toString());
787+
assertTrue(!desc.isPrimitive());
788+
assertEquals(0, desc.getAnnotations().length);
789+
assertFalse(desc.isCollection());
790+
assertFalse(desc.isArray());
791+
assertNull(desc.getElementType());
792+
assertEquals(TypeDescriptor.NULL, desc.getElementTypeDescriptor());
793+
assertTrue(desc.isMap());
794+
assertEquals(String.class, desc.getMapKeyTypeDescriptor().getType());
795+
assertEquals(String.class, desc.getMapValueTypeDescriptor().getMapKeyTypeDescriptor().getType());
796+
assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getMapValueTypeDescriptor().getType());
797+
}
798+
799+
@Test
800+
public void equals() throws Exception {
723801
TypeDescriptor t1 = TypeDescriptor.valueOf(String.class);
724802
TypeDescriptor t2 = TypeDescriptor.valueOf(String.class);
725803
TypeDescriptor t3 = TypeDescriptor.valueOf(Date.class);

0 commit comments

Comments
 (0)