Skip to content

Commit c62fbea

Browse files
committed
Demonstrate that the CollectionFactory API is not type-safe
This commit introduces test methods in CollectionFactoryTests that demonstrate how the APIs for createCollection() and createMap() are not type-safe, specifically regarding the use of generics, raw types, and casting.
1 parent aec284a commit c62fbea

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

spring-core/src/test/java/org/springframework/core/CollectionFactoryTests.java

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class CollectionFactoryTests {
6363
* actually contains elements of type {@code E}.
6464
*/
6565
@Test
66-
public void createApproximateCollectionIsNotTypeSafe() {
66+
public void createApproximateCollectionIsNotTypeSafeForEnumSet() {
6767
Collection<Integer> ints = createApproximateCollection(EnumSet.of(Color.BLUE), 3);
6868

6969
// Use a try-catch block to ensure that the exception is thrown as a result of the
@@ -80,6 +80,24 @@ public void createApproximateCollectionIsNotTypeSafe() {
8080
}
8181
}
8282

83+
@Test
84+
public void createCollectionIsNotTypeSafeForEnumSet() {
85+
Collection<Integer> ints = createCollection(EnumSet.class, Color.class, 3);
86+
87+
// Use a try-catch block to ensure that the exception is thrown as a result of the
88+
// next line and not as a result of the previous line.
89+
try {
90+
// Note that ints is of type Collection<Integer>, but the collection returned
91+
// by createCollection() is of type Collection<Color>. Thus, 42 cannot be cast
92+
// to a Color.
93+
ints.add(42);
94+
fail("Should have thrown a ClassCastException");
95+
}
96+
catch (ClassCastException e) {
97+
/* expected */
98+
}
99+
}
100+
83101
/**
84102
* The test demonstrates that the generics-based API for
85103
* {@link CollectionFactory#createApproximateMap(Object, int)}
@@ -88,7 +106,7 @@ public void createApproximateCollectionIsNotTypeSafe() {
88106
* {@link #createApproximateCollectionIsNotTypeSafe()}.
89107
*/
90108
@Test
91-
public void createApproximateMapIsNotTypeSafe() {
109+
public void createApproximateMapIsNotTypeSafeForEnumMap() {
92110
EnumMap<Color, Integer> enumMap = new EnumMap<>(Color.class);
93111
enumMap.put(Color.RED, 1);
94112
enumMap.put(Color.BLUE, 2);
@@ -97,8 +115,26 @@ public void createApproximateMapIsNotTypeSafe() {
97115
// Use a try-catch block to ensure that the exception is thrown as a result of the
98116
// next line and not as a result of the previous line.
99117
try {
100-
// Note that the 'map' key is of type String, but the keys in the map returned
101-
// by createApproximateMap() are of type Color. Thus "foo" cannot be cast to a
118+
// Note that the 'map' key must be of type String, but the keys in the map
119+
// returned by createApproximateMap() are of type Color. Thus "foo" cannot be
120+
// cast to a Color.
121+
map.put("foo", 1);
122+
fail("Should have thrown a ClassCastException");
123+
}
124+
catch (ClassCastException e) {
125+
/* expected */
126+
}
127+
}
128+
129+
@Test
130+
public void createMapIsNotTypeSafeForEnumMap() {
131+
Map<String, Integer> map = createMap(EnumMap.class, Color.class, 3);
132+
133+
// Use a try-catch block to ensure that the exception is thrown as a result of the
134+
// next line and not as a result of the previous line.
135+
try {
136+
// Note that the 'map' key must be of type String, but the keys in the map
137+
// returned by createMap() are of type Color. Thus "foo" cannot be cast to a
102138
// Color.
103139
map.put("foo", 1);
104140
fail("Should have thrown a ClassCastException");
@@ -108,6 +144,24 @@ public void createApproximateMapIsNotTypeSafe() {
108144
}
109145
}
110146

147+
@Test
148+
public void createMapIsNotTypeSafeForLinkedMultiValueMap() {
149+
Map<String, Integer> map = createMap(MultiValueMap.class, null, 3);
150+
151+
// Use a try-catch block to ensure that the exception is thrown as a result of the
152+
// next line and not as a result of the previous line.
153+
try {
154+
// Note: 'map' values must be of type Integer, but the values in the map
155+
// returned by createMap() are of type java.util.List. Thus 1 cannot be
156+
// cast to a List.
157+
map.put("foo", 1);
158+
fail("Should have thrown a ClassCastException");
159+
}
160+
catch (ClassCastException e) {
161+
/* expected */
162+
}
163+
}
164+
111165
@Test
112166
public void createApproximateCollectionFromEmptyHashSet() {
113167
Collection<String> set = createApproximateCollection(new HashSet<String>(), 2);

0 commit comments

Comments
 (0)