8
8
import org .junit .jupiter .api .Order ;
9
9
import org .junit .jupiter .api .Test ;
10
10
import org .junit .jupiter .api .TestMethodOrder ;
11
+ import org .mockito .Mockito ;
11
12
12
13
import java .lang .reflect .Method ;
13
- import java .lang .reflect .Type ;
14
+ import java .lang .reflect .ParameterizedType ;
14
15
import java .math .BigDecimal ;
15
16
import java .util .Arrays ;
16
17
import java .util .Comparator ;
23
24
@ TestMethodOrder (OrderAnnotation .class )
24
25
class HeterogeneousMaxHolderTest {
25
26
private HeterogeneousMaxHolder heterogeneousMaxHolder = new HeterogeneousMaxHolder ();
27
+ private HeterogeneousMaxHolder heterogeneousMaxHolderMock = Mockito .spy (HeterogeneousMaxHolder .class );
26
28
27
29
@ Test
28
30
@ Order (1 )
@@ -71,58 +73,53 @@ void putDeclaresOneTypeParam() {
71
73
var putMethod = getPutMethod ();
72
74
73
75
var methodTypeParameters = putMethod .getTypeParameters ();
74
-
75
76
assertThat (methodTypeParameters ).hasSize (1 );
77
+
78
+ var typeParam = putMethod .getTypeParameters ()[0 ];
79
+ assertThat (typeParam .getName ()).isEqualTo ("T" );
76
80
}
77
81
78
82
@ Test
79
83
@ Order (6 )
80
- @ DisplayName ("put method type parameter is called 'T' " )
81
- void putTypeParamIsCalledT () {
84
+ @ DisplayName ("put method accepts type-safe key " )
85
+ void putMethodAcceptsTypeSafeKeyParameter () {
82
86
var putMethod = getPutMethod ();
83
87
84
- var typeParam = putMethod .getTypeParameters ()[0 ];
88
+ var typeParam = (ParameterizedType ) putMethod .getGenericParameterTypes ()[0 ];
89
+ var typeArgument = typeParam .getActualTypeArguments ()[0 ];
85
90
86
- assertThat (typeParam .getName ()).isEqualTo ("T" );
91
+ assertThat (typeParam .getRawType ()).isEqualTo (Class .class );
92
+ assertThat (typeArgument .getTypeName ()).isEqualTo ("T" );
87
93
}
88
94
89
95
@ Test
90
96
@ Order (7 )
91
- @ DisplayName ("type parameter 'T' is declared as Comparable " )
92
- void typeParamIsComparable () {
97
+ @ DisplayName ("put method accepts comparable value " )
98
+ void putMethodAcceptsComparableValueParameter () {
93
99
var putMethod = getPutMethod ();
94
100
95
101
var typeParam = putMethod .getTypeParameters ()[0 ];
96
- var bound = typeParam .getBounds ()[0 ];
102
+ var boundType = ( ParameterizedType ) typeParam .getBounds ()[0 ];
97
103
98
- assertThat (bound . getTypeName ()).isEqualTo (Comparable .class . getTypeName () + "<? super T>" );
104
+ assertThat (boundType . getRawType ()).isEqualTo (Comparable .class );
99
105
}
100
106
101
107
@ Test
102
108
@ Order (8 )
103
- @ SneakyThrows
104
- @ DisplayName ("put method accepts type (class) and value (object) parameters" )
105
- void putHasKeyValueParameters () {
106
- HeterogeneousMaxHolder .class .getMethod ("put" , Class .class , Comparable .class );
107
- }
109
+ @ DisplayName ("put method supports value that has comparable super class" )
110
+ void putMethodAcceptsValueParameterWithComparableSuperClass () {
111
+ var putMethod = getPutMethod ();
108
112
109
- @ Test
110
- @ Order (9 )
111
- @ SneakyThrows
112
- @ DisplayName ("put method params specify type arguments" )
113
- void putParametersSpecifyTypeArguments () {
114
- var putMethod = HeterogeneousMaxHolder .class .getMethod ("put" , Class .class , Comparable .class );
115
- var genericParamTypeNames = Arrays .stream (putMethod .getGenericParameterTypes ())
116
- .map (Type ::getTypeName )
117
- .toList ();
113
+ var typeParam = putMethod .getTypeParameters ()[0 ];
114
+ var boundType = (ParameterizedType ) typeParam .getBounds ()[0 ];
115
+ var typeArgument = boundType .getActualTypeArguments ()[0 ].getTypeName ();
118
116
119
- assertThat (genericParamTypeNames )
120
- .contains (Class .class .getTypeName () + "<T>" )
121
- .contains ("T" );
117
+ assertThat (boundType .getRawType ()).isEqualTo (Comparable .class );
118
+ assertThat (typeArgument ).isEqualTo ("? super T" );
122
119
}
123
120
124
121
@ Test
125
- @ Order (10 )
122
+ @ Order (9 )
126
123
@ SneakyThrows
127
124
@ DisplayName ("put stores provided value when current max is null" )
128
125
void putStoresValueWhenCurrentMaxIsNull () {
@@ -133,7 +130,7 @@ void putStoresValueWhenCurrentMaxIsNull() {
133
130
}
134
131
135
132
@ Test
136
- @ Order (11 )
133
+ @ Order (10 )
137
134
@ SneakyThrows
138
135
@ DisplayName ("put returns null when current max is null" )
139
136
void putReturnsNullWhenCurrentMaxIsNull () {
@@ -143,7 +140,7 @@ void putReturnsNullWhenCurrentMaxIsNull() {
143
140
}
144
141
145
142
@ Test
146
- @ Order (12 )
143
+ @ Order (11 )
147
144
@ SneakyThrows
148
145
@ DisplayName ("put stores provided value when current max is smaller than it" )
149
146
void putStoresValueWhenCurrentMaxIsSmaller () {
@@ -156,7 +153,7 @@ void putStoresValueWhenCurrentMaxIsSmaller() {
156
153
}
157
154
158
155
@ Test
159
- @ Order (13 )
156
+ @ Order (12 )
160
157
@ SneakyThrows
161
158
@ DisplayName ("put returns old max value when the provided value is greater than it" )
162
159
void putReturnsOldMaxValue () {
@@ -168,7 +165,7 @@ void putReturnsOldMaxValue() {
168
165
}
169
166
170
167
@ Test
171
- @ Order (14 )
168
+ @ Order (13 )
172
169
@ SneakyThrows
173
170
@ DisplayName ("put ignores provided value when the current max is greater than it" )
174
171
void putIgnoresNewValueWhenCurrentMaxIsGreater () {
@@ -181,7 +178,7 @@ void putIgnoresNewValueWhenCurrentMaxIsGreater() {
181
178
}
182
179
183
180
@ Test
184
- @ Order (15 )
181
+ @ Order (14 )
185
182
@ SneakyThrows
186
183
@ DisplayName ("put returns provided value when the current max is greater than it" )
187
184
void putReturnsProvidedValueWhenCurrentMaxIsGreater () {
@@ -193,45 +190,67 @@ void putReturnsProvidedValueWhenCurrentMaxIsGreater() {
193
190
}
194
191
195
192
@ Test
196
- @ Order (16 )
193
+ @ Order (15 )
197
194
@ SneakyThrows
198
- @ DisplayName ("put method is overloaded with additional Comparator param " )
195
+ @ DisplayName ("put method is overloaded with additional Comparator parameter " )
199
196
void putIsOverloadedWithAdditionalComparatorParam () {
200
- var overloadedPutMethod = getOverloadedPutMethod ();
201
- var params = overloadedPutMethod .getParameters ();
197
+ HeterogeneousMaxHolder .class .getMethod ("put" , Class .class , Object .class , Comparator .class );
198
+ }
199
+
200
+ @ Test
201
+ @ Order (16 )
202
+ @ DisplayName ("Overloaded put method declares one type parameter T" )
203
+ void overloadedPutDeclaresOneTypeParam () {
204
+ var putMethod = getOverloadedPutMethod ();
202
205
203
- assertThat (params [2 ].getType ()).isEqualTo (Comparator .class );
206
+ var methodTypeParameters = putMethod .getTypeParameters ();
207
+ assertThat (methodTypeParameters ).hasSize (1 );
204
208
209
+ var typeParam = putMethod .getTypeParameters ()[0 ];
210
+ assertThat (typeParam .getName ()).isEqualTo ("T" );
205
211
}
206
212
207
213
@ Test
208
214
@ Order (17 )
209
- @ DisplayName ("Overloaded put has simple type param 'T'" )
210
- void overloadedPutHasSimpleTypeParameterT () {
211
- var overloadedPutMethod = getOverloadedPutMethod ();
215
+ @ DisplayName ("Overloaded put method accepts type-safe key" )
216
+ void overloadedPutMethodAcceptsTypeSafeKeyParameter () {
217
+ var putMethod = getOverloadedPutMethod ();
218
+
219
+ var typeParam = (ParameterizedType ) putMethod .getGenericParameterTypes ()[0 ];
220
+ var typeArgument = typeParam .getActualTypeArguments ()[0 ];
212
221
213
- assertThat (overloadedPutMethod . getTypeParameters ()).hasSize ( 1 );
214
- assertThat (overloadedPutMethod . getTypeParameters ()[ 0 ] .getTypeName ()).isEqualTo ("T" );
222
+ assertThat (typeParam . getRawType ()).isEqualTo ( Class . class );
223
+ assertThat (typeArgument .getTypeName ()).isEqualTo ("T" );
215
224
}
216
225
217
226
@ Test
218
227
@ Order (18 )
228
+ @ DisplayName ("Overloaded put method accepts value of arbitrary type T" )
229
+ void overloadedPutMethodAcceptsAnyValue () {
230
+ var putMethod = getOverloadedPutMethod ();
231
+
232
+ var genericValueTypeParam = putMethod .getGenericParameterTypes ()[1 ];
233
+ var actualValueTypeParm = putMethod .getParameterTypes ()[1 ];
234
+
235
+ assertThat (genericValueTypeParam .getTypeName ()).isEqualTo ("T" );
236
+ assertThat (actualValueTypeParm ).isEqualTo (Object .class );
237
+ }
238
+
239
+ @ Test
240
+ @ Order (19 )
219
241
@ SneakyThrows
220
- @ DisplayName ("Overloaded put method params specify type arguments " )
221
- void overloadedPutParametersSpecifyTypeArguments () {
242
+ @ DisplayName ("Overloaded put method supports comparator of a super type " )
243
+ void overloadedPutAcceptsComparatorOfSuperTypes () {
222
244
var putMethod = HeterogeneousMaxHolder .class .getMethod ("put" , Class .class , Object .class , Comparator .class );
223
- var genericParamTypeNames = Arrays .stream (putMethod .getGenericParameterTypes ())
224
- .map (Type ::getTypeName )
225
- .toList ();
226
245
227
- assertThat ( genericParamTypeNames )
228
- . contains ( Class . class . getTypeName () + "<T>" )
229
- . contains ( "T" )
230
- . contains ( Comparator . class . getTypeName () + "< ? super T> " );
246
+ var comparatorParam = ( ParameterizedType ) putMethod . getGenericParameterTypes ()[ 2 ];
247
+ var comparatorTypeArgument = comparatorParam . getActualTypeArguments ()[ 0 ];
248
+
249
+ assertThat ( comparatorTypeArgument . getTypeName ()). isEqualTo ( " ? super T" );
231
250
}
232
251
233
252
@ Test
234
- @ Order (19 )
253
+ @ Order (20 )
235
254
@ SneakyThrows
236
255
@ DisplayName ("Overloaded put stores provided value when current max is null" )
237
256
void overloadedPutStoresValueWhenCurrentMaxIsNull () {
@@ -243,7 +262,7 @@ void overloadedPutStoresValueWhenCurrentMaxIsNull() {
243
262
}
244
263
245
264
@ Test
246
- @ Order (20 )
265
+ @ Order (21 )
247
266
@ SneakyThrows
248
267
@ DisplayName ("Overloaded put returns null when current max is null" )
249
268
void overloadedPutReturnsNullWhenCurrentMaxIsNull () {
@@ -253,7 +272,7 @@ void overloadedPutReturnsNullWhenCurrentMaxIsNull() {
253
272
}
254
273
255
274
@ Test
256
- @ Order (21 )
275
+ @ Order (22 )
257
276
@ SneakyThrows
258
277
@ DisplayName ("Overloaded put stores provided value when current max is smaller than it" )
259
278
void overloadedPutStoresValueWhenCurrentMaxIsSmaller () {
@@ -269,7 +288,7 @@ void overloadedPutStoresValueWhenCurrentMaxIsSmaller() {
269
288
}
270
289
271
290
@ Test
272
- @ Order (22 )
291
+ @ Order (23 )
273
292
@ SneakyThrows
274
293
@ DisplayName ("Overloaded put returns old max value when the provided value is greater than it" )
275
294
void overloadedPutReturnsOldMaxValue () {
@@ -284,7 +303,7 @@ void overloadedPutReturnsOldMaxValue() {
284
303
}
285
304
286
305
@ Test
287
- @ Order (23 )
306
+ @ Order (24 )
288
307
@ SneakyThrows
289
308
@ DisplayName ("Overloaded put ignores provided value when the current max is greater than it" )
290
309
void overloadedPutIgnoresNewValueWhenCurrentMaxIsGreater () {
@@ -300,7 +319,7 @@ void overloadedPutIgnoresNewValueWhenCurrentMaxIsGreater() {
300
319
}
301
320
302
321
@ Test
303
- @ Order (24 )
322
+ @ Order (25 )
304
323
@ SneakyThrows
305
324
@ DisplayName ("Overloaded put returns provided value when the current max is greater" )
306
325
void overloadedPutReturnsProvidedValueWhenCurrentMaxIsGreater () {
@@ -315,7 +334,7 @@ void overloadedPutReturnsProvidedValueWhenCurrentMaxIsGreater() {
315
334
}
316
335
317
336
@ Test
318
- @ Order (25 )
337
+ @ Order (26 )
319
338
@ DisplayName ("getMax method exists" )
320
339
void getMaxExists () {
321
340
var getMaxMethodExists = Arrays .stream (HeterogeneousMaxHolder .class .getDeclaredMethods ())
@@ -325,7 +344,7 @@ void getMaxExists() {
325
344
}
326
345
327
346
@ Test
328
- @ Order (26 )
347
+ @ Order (27 )
329
348
@ DisplayName ("getMax declares one simple type param 'T'" )
330
349
void getMaxDeclaresOneTypeParam () {
331
350
var getMaxMethod = getGetMaxMethod ();
@@ -337,7 +356,7 @@ void getMaxDeclaresOneTypeParam() {
337
356
}
338
357
339
358
@ Test
340
- @ Order (27 )
359
+ @ Order (28 )
341
360
@ DisplayName ("getMax has one parameter" )
342
361
void getMaxHasOneParameter () {
343
362
var getMaxMethod = getGetMaxMethod ();
@@ -349,7 +368,7 @@ void getMaxHasOneParameter() {
349
368
}
350
369
351
370
@ Test
352
- @ Order (28 )
371
+ @ Order (29 )
353
372
@ DisplayName ("getMax param specifies type arguments" )
354
373
void getMaxParamSpecifyTypeArguments () {
355
374
var getMaxMethod = getGetMaxMethod ();
@@ -360,7 +379,7 @@ void getMaxParamSpecifyTypeArguments() {
360
379
}
361
380
362
381
@ Test
363
- @ Order (29 )
382
+ @ Order (30 )
364
383
@ DisplayName ("getMax returns value when it exists" )
365
384
void getMaxReturnsValueWhenItExists () {
366
385
givenMaxHolderWithData (String .class , "I am maximum" );
@@ -371,7 +390,7 @@ void getMaxReturnsValueWhenItExists() {
371
390
}
372
391
373
392
@ Test
374
- @ Order (30 )
393
+ @ Order (31 )
375
394
@ DisplayName ("getMax returns value when it exists" )
376
395
void getMaxReturnsNullWhenNoValueByGivenTypeExists () {
377
396
var returnedValue = callGetMax (String .class );
@@ -380,7 +399,7 @@ void getMaxReturnsNullWhenNoValueByGivenTypeExists() {
380
399
}
381
400
382
401
@ Test
383
- @ Order (31 )
402
+ @ Order (32 )
384
403
@ DisplayName ("HeterogeneousMaxHolder keeps track of value one per each type" )
385
404
void maxHolderKeepsTrackOfMultipleValuesPerType () {
386
405
callPut (String .class , "A" );
0 commit comments