@@ -63,7 +63,7 @@ public class CollectionFactoryTests {
63
63
* actually contains elements of type {@code E}.
64
64
*/
65
65
@ Test
66
- public void createApproximateCollectionIsNotTypeSafe () {
66
+ public void createApproximateCollectionIsNotTypeSafeForEnumSet () {
67
67
Collection <Integer > ints = createApproximateCollection (EnumSet .of (Color .BLUE ), 3 );
68
68
69
69
// Use a try-catch block to ensure that the exception is thrown as a result of the
@@ -80,6 +80,24 @@ public void createApproximateCollectionIsNotTypeSafe() {
80
80
}
81
81
}
82
82
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
+
83
101
/**
84
102
* The test demonstrates that the generics-based API for
85
103
* {@link CollectionFactory#createApproximateMap(Object, int)}
@@ -88,7 +106,7 @@ public void createApproximateCollectionIsNotTypeSafe() {
88
106
* {@link #createApproximateCollectionIsNotTypeSafe()}.
89
107
*/
90
108
@ Test
91
- public void createApproximateMapIsNotTypeSafe () {
109
+ public void createApproximateMapIsNotTypeSafeForEnumMap () {
92
110
EnumMap <Color , Integer > enumMap = new EnumMap <>(Color .class );
93
111
enumMap .put (Color .RED , 1 );
94
112
enumMap .put (Color .BLUE , 2 );
@@ -97,8 +115,26 @@ public void createApproximateMapIsNotTypeSafe() {
97
115
// Use a try-catch block to ensure that the exception is thrown as a result of the
98
116
// next line and not as a result of the previous line.
99
117
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
102
138
// Color.
103
139
map .put ("foo" , 1 );
104
140
fail ("Should have thrown a ClassCastException" );
@@ -108,6 +144,24 @@ public void createApproximateMapIsNotTypeSafe() {
108
144
}
109
145
}
110
146
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
+
111
165
@ Test
112
166
public void createApproximateCollectionFromEmptyHashSet () {
113
167
Collection <String > set = createApproximateCollection (new HashSet <String >(), 2 );
0 commit comments