|
44 | 44 | import static org.assertj.core.api.Assertions.assertThat;
|
45 | 45 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
46 | 46 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 47 | +import static org.assertj.core.api.SoftAssertions.assertSoftly; |
47 | 48 |
|
48 | 49 | /**
|
49 | 50 | * Unit tests for {@link BeanUtils}.
|
@@ -80,19 +81,42 @@ void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodException
|
80 | 81 | }
|
81 | 82 |
|
82 | 83 | @Test // gh-22531
|
83 |
| - void testInstantiateClassWithOptionalPrimitiveType() throws NoSuchMethodException { |
84 |
| - Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class); |
85 |
| - BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(ctor, null, null, "foo"); |
86 |
| - assertThat(bean.getCounter()).isEqualTo(0); |
87 |
| - assertThat(bean.isFlag()).isEqualTo(false); |
88 |
| - assertThat(bean.getValue()).isEqualTo("foo"); |
| 84 | + void instantiateClassWithFewerArgsThanParameters() throws NoSuchMethodException { |
| 85 | + Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor(); |
| 86 | + |
| 87 | + assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> |
| 88 | + BeanUtils.instantiateClass(constructor, null, null, "foo")); |
89 | 89 | }
|
90 | 90 |
|
91 |
| - @Test // gh-22531 |
92 |
| - void testInstantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException { |
93 |
| - Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class); |
| 91 | + @Test // gh-22531 |
| 92 | + void instantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException { |
| 93 | + Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor(); |
| 94 | + |
94 | 95 | assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() ->
|
95 |
| - BeanUtils.instantiateClass(ctor, null, null, "foo", null)); |
| 96 | + BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo", null)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test // gh-22531, gh-27390 |
| 100 | + void instantiateClassWithOptionalPrimitiveTypes() throws NoSuchMethodException { |
| 101 | + Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor(); |
| 102 | + |
| 103 | + BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo"); |
| 104 | + |
| 105 | + assertSoftly(softly -> { |
| 106 | + softly.assertThat(bean.isFlag()).isEqualTo(false); |
| 107 | + softly.assertThat(bean.getByteCount()).isEqualTo((byte) 0); |
| 108 | + softly.assertThat(bean.getShortCount()).isEqualTo((short) 0); |
| 109 | + softly.assertThat(bean.getIntCount()).isEqualTo(0); |
| 110 | + softly.assertThat(bean.getLongCount()).isEqualTo(0L); |
| 111 | + softly.assertThat(bean.getFloatCount()).isEqualTo(0F); |
| 112 | + softly.assertThat(bean.getDoubleCount()).isEqualTo(0D); |
| 113 | + softly.assertThat(bean.getText()).isEqualTo("foo"); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + private Constructor<BeanWithPrimitiveTypes> getBeanWithPrimitiveTypesConstructor() throws NoSuchMethodException { |
| 118 | + return BeanWithPrimitiveTypes.class.getConstructor(boolean.class, byte.class, short.class, int.class, |
| 119 | + long.class, float.class, double.class, String.class); |
96 | 120 | }
|
97 | 121 |
|
98 | 122 | @Test
|
@@ -535,30 +559,61 @@ public String getValue() {
|
535 | 559 |
|
536 | 560 | private static class BeanWithPrimitiveTypes {
|
537 | 561 |
|
538 |
| - private int counter; |
539 |
| - |
540 | 562 | private boolean flag;
|
| 563 | + private byte byteCount; |
| 564 | + private short shortCount; |
| 565 | + private int intCount; |
| 566 | + private long longCount; |
| 567 | + private float floatCount; |
| 568 | + private double doubleCount; |
| 569 | + private String text; |
541 | 570 |
|
542 |
| - private String value; |
543 | 571 |
|
544 | 572 | @SuppressWarnings("unused")
|
545 |
| - public BeanWithPrimitiveTypes(int counter, boolean flag, String value) { |
546 |
| - this.counter = counter; |
| 573 | + public BeanWithPrimitiveTypes(boolean flag, byte byteCount, short shortCount, int intCount, long longCount, |
| 574 | + float floatCount, double doubleCount, String text) { |
547 | 575 | this.flag = flag;
|
548 |
| - this.value = value; |
549 |
| - } |
550 |
| - |
551 |
| - public int getCounter() { |
552 |
| - return counter; |
| 576 | + this.byteCount = byteCount; |
| 577 | + this.shortCount = shortCount; |
| 578 | + this.intCount = intCount; |
| 579 | + this.longCount = longCount; |
| 580 | + this.floatCount = floatCount; |
| 581 | + this.doubleCount = doubleCount; |
| 582 | + this.text = text; |
553 | 583 | }
|
554 | 584 |
|
555 | 585 | public boolean isFlag() {
|
556 | 586 | return flag;
|
557 | 587 | }
|
558 | 588 |
|
559 |
| - public String getValue() { |
560 |
| - return value; |
| 589 | + public byte getByteCount() { |
| 590 | + return byteCount; |
| 591 | + } |
| 592 | + |
| 593 | + public short getShortCount() { |
| 594 | + return shortCount; |
| 595 | + } |
| 596 | + |
| 597 | + public int getIntCount() { |
| 598 | + return intCount; |
| 599 | + } |
| 600 | + |
| 601 | + public long getLongCount() { |
| 602 | + return longCount; |
| 603 | + } |
| 604 | + |
| 605 | + public float getFloatCount() { |
| 606 | + return floatCount; |
| 607 | + } |
| 608 | + |
| 609 | + public double getDoubleCount() { |
| 610 | + return doubleCount; |
| 611 | + } |
| 612 | + |
| 613 | + public String getText() { |
| 614 | + return text; |
561 | 615 | }
|
| 616 | + |
562 | 617 | }
|
563 | 618 |
|
564 | 619 | private static class PrivateBeanWithPrivateConstructor {
|
|
0 commit comments