|
18 | 18 | import org.junit.jupiter.api.Test; |
19 | 19 |
|
20 | 20 | import java.lang.reflect.ParameterizedType; |
| 21 | +import java.lang.reflect.Type; |
21 | 22 | import java.math.BigInteger; |
22 | 23 | import java.net.URI; |
23 | 24 | import java.nio.file.Path; |
|
29 | 30 | import java.time.LocalDateTime; |
30 | 31 | import java.time.LocalTime; |
31 | 32 | import java.time.Period; |
| 33 | +import java.util.Arrays; |
32 | 34 | import java.util.Date; |
33 | 35 | import java.util.HashMap; |
34 | 36 | import java.util.List; |
35 | 37 | import java.util.Map; |
| 38 | +import java.util.SequencedMap; |
| 39 | +import java.util.SequencedSet; |
36 | 40 | import java.util.Set; |
| 41 | +import java.util.SortedMap; |
| 42 | +import java.util.SortedSet; |
37 | 43 | import java.util.UUID; |
38 | 44 |
|
39 | 45 | import static org.httprpc.kilo.util.Collections.*; |
@@ -265,19 +271,32 @@ public void testPathCoercion() { |
265 | 271 | @Test |
266 | 272 | public void testListCoercion() { |
267 | 273 | assertInstanceOf(List.class, BeanAdapter.coerce(listOf(), List.class)); |
268 | | - assertThrows(IllegalArgumentException.class, () -> BeanAdapter.coerce(123, List.class)); |
| 274 | + |
| 275 | + assertInstanceOf(List.class, BeanAdapter.coerceGeneric(listOf(), parameterizedTypeOf(List.class))); |
| 276 | + |
| 277 | + assertThrows(IllegalArgumentException.class, () -> BeanAdapter.coerce(0, List.class)); |
269 | 278 | } |
270 | 279 |
|
271 | 280 | @Test |
272 | 281 | public void testMapCoercion() { |
273 | 282 | assertInstanceOf(Map.class, BeanAdapter.coerce(mapOf(), Map.class)); |
274 | | - assertThrows(IllegalArgumentException.class, () -> BeanAdapter.coerce(123, Map.class)); |
| 283 | + |
| 284 | + assertInstanceOf(Map.class, BeanAdapter.coerceGeneric(mapOf(), parameterizedTypeOf(Map.class))); |
| 285 | + assertInstanceOf(SequencedMap.class, BeanAdapter.coerceGeneric(mapOf(), parameterizedTypeOf(SequencedMap.class))); |
| 286 | + assertInstanceOf(SortedMap.class, BeanAdapter.coerceGeneric(mapOf(), parameterizedTypeOf(SortedMap.class))); |
| 287 | + |
| 288 | + assertThrows(IllegalArgumentException.class, () -> BeanAdapter.coerce(0, Map.class)); |
275 | 289 | } |
276 | 290 |
|
277 | 291 | @Test |
278 | 292 | public void testSetCoercion() { |
279 | 293 | assertInstanceOf(Set.class, BeanAdapter.coerce(setOf(), Set.class)); |
280 | | - assertThrows(IllegalArgumentException.class, () -> BeanAdapter.coerce(123, Set.class)); |
| 294 | + |
| 295 | + assertInstanceOf(Set.class, BeanAdapter.coerceGeneric(setOf(), parameterizedTypeOf(Set.class))); |
| 296 | + assertInstanceOf(SequencedSet.class, BeanAdapter.coerceGeneric(setOf(), parameterizedTypeOf(SequencedSet.class))); |
| 297 | + assertInstanceOf(SortedSet.class, BeanAdapter.coerceGeneric(setOf(), parameterizedTypeOf(SortedSet.class))); |
| 298 | + |
| 299 | + assertThrows(IllegalArgumentException.class, () -> BeanAdapter.coerce(0, Set.class)); |
281 | 300 | } |
282 | 301 |
|
283 | 302 | @Test |
@@ -486,4 +505,27 @@ public void testToType() { |
486 | 505 |
|
487 | 506 | assertEquals(listOf(1, 2, 3), integers); |
488 | 507 | } |
| 508 | + |
| 509 | + private static Type parameterizedTypeOf(Class<?> rawType) { |
| 510 | + return new ParameterizedType() { |
| 511 | + @Override |
| 512 | + public Type[] getActualTypeArguments() { |
| 513 | + var actualTypeArguments = new Type[rawType.getTypeParameters().length]; |
| 514 | + |
| 515 | + Arrays.fill(actualTypeArguments, Object.class); |
| 516 | + |
| 517 | + return actualTypeArguments; |
| 518 | + } |
| 519 | + |
| 520 | + @Override |
| 521 | + public Type getRawType() { |
| 522 | + return rawType; |
| 523 | + } |
| 524 | + |
| 525 | + @Override |
| 526 | + public Type getOwnerType() { |
| 527 | + return null; |
| 528 | + } |
| 529 | + }; |
| 530 | + } |
489 | 531 | } |
0 commit comments