|
49 | 49 | import static org.assertj.core.api.Assertions.assertThat;
|
50 | 50 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
51 | 51 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
| 52 | +import static org.assertj.core.api.SoftAssertions.assertSoftly; |
52 | 53 |
|
53 | 54 | /**
|
54 | 55 | * Unit tests for {@link BeanUtils}.
|
@@ -85,19 +86,42 @@ void instantiateClassWithOptionalNullableType() throws NoSuchMethodException {
|
85 | 86 | }
|
86 | 87 |
|
87 | 88 | @Test // gh-22531
|
88 |
| - void instantiateClassWithOptionalPrimitiveType() throws NoSuchMethodException { |
89 |
| - Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class); |
90 |
| - BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(ctor, null, null, "foo"); |
91 |
| - assertThat(bean.getCounter()).isEqualTo(0); |
92 |
| - assertThat(bean.isFlag()).isEqualTo(false); |
93 |
| - assertThat(bean.getValue()).isEqualTo("foo"); |
| 89 | + void instantiateClassWithFewerArgsThanParameters() throws NoSuchMethodException { |
| 90 | + Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor(); |
| 91 | + |
| 92 | + assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> |
| 93 | + BeanUtils.instantiateClass(constructor, null, null, "foo")); |
94 | 94 | }
|
95 | 95 |
|
96 | 96 | @Test // gh-22531
|
97 | 97 | void instantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException {
|
98 |
| - Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class); |
| 98 | + Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor(); |
| 99 | + |
99 | 100 | assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() ->
|
100 |
| - BeanUtils.instantiateClass(ctor, null, null, "foo", null)); |
| 101 | + BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo", null)); |
| 102 | + } |
| 103 | + |
| 104 | + @Test // gh-22531, gh-27390 |
| 105 | + void instantiateClassWithOptionalPrimitiveTypes() throws NoSuchMethodException { |
| 106 | + Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor(); |
| 107 | + |
| 108 | + BeanWithPrimitiveTypes bean = BeanUtils.instantiateClass(constructor, null, null, null, null, null, null, null, "foo"); |
| 109 | + |
| 110 | + assertSoftly(softly -> { |
| 111 | + softly.assertThat(bean.isFlag()).isEqualTo(false); |
| 112 | + softly.assertThat(bean.getByteCount()).isEqualTo((byte) 0); |
| 113 | + softly.assertThat(bean.getShortCount()).isEqualTo((short) 0); |
| 114 | + softly.assertThat(bean.getIntCount()).isEqualTo(0); |
| 115 | + softly.assertThat(bean.getLongCount()).isEqualTo(0L); |
| 116 | + softly.assertThat(bean.getFloatCount()).isEqualTo(0F); |
| 117 | + softly.assertThat(bean.getDoubleCount()).isEqualTo(0D); |
| 118 | + softly.assertThat(bean.getText()).isEqualTo("foo"); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + private Constructor<BeanWithPrimitiveTypes> getBeanWithPrimitiveTypesConstructor() throws NoSuchMethodException { |
| 123 | + return BeanWithPrimitiveTypes.class.getConstructor(boolean.class, byte.class, short.class, int.class, |
| 124 | + long.class, float.class, double.class, String.class); |
101 | 125 | }
|
102 | 126 |
|
103 | 127 | @Test
|
@@ -628,30 +652,61 @@ public String getValue() {
|
628 | 652 |
|
629 | 653 | private static class BeanWithPrimitiveTypes {
|
630 | 654 |
|
631 |
| - private int counter; |
632 |
| - |
633 | 655 | private boolean flag;
|
| 656 | + private byte byteCount; |
| 657 | + private short shortCount; |
| 658 | + private int intCount; |
| 659 | + private long longCount; |
| 660 | + private float floatCount; |
| 661 | + private double doubleCount; |
| 662 | + private String text; |
634 | 663 |
|
635 |
| - private String value; |
636 | 664 |
|
637 | 665 | @SuppressWarnings("unused")
|
638 |
| - public BeanWithPrimitiveTypes(int counter, boolean flag, String value) { |
639 |
| - this.counter = counter; |
| 666 | + public BeanWithPrimitiveTypes(boolean flag, byte byteCount, short shortCount, int intCount, long longCount, |
| 667 | + float floatCount, double doubleCount, String text) { |
640 | 668 | this.flag = flag;
|
641 |
| - this.value = value; |
642 |
| - } |
643 |
| - |
644 |
| - public int getCounter() { |
645 |
| - return counter; |
| 669 | + this.byteCount = byteCount; |
| 670 | + this.shortCount = shortCount; |
| 671 | + this.intCount = intCount; |
| 672 | + this.longCount = longCount; |
| 673 | + this.floatCount = floatCount; |
| 674 | + this.doubleCount = doubleCount; |
| 675 | + this.text = text; |
646 | 676 | }
|
647 | 677 |
|
648 | 678 | public boolean isFlag() {
|
649 | 679 | return flag;
|
650 | 680 | }
|
651 | 681 |
|
652 |
| - public String getValue() { |
653 |
| - return value; |
| 682 | + public byte getByteCount() { |
| 683 | + return byteCount; |
| 684 | + } |
| 685 | + |
| 686 | + public short getShortCount() { |
| 687 | + return shortCount; |
| 688 | + } |
| 689 | + |
| 690 | + public int getIntCount() { |
| 691 | + return intCount; |
| 692 | + } |
| 693 | + |
| 694 | + public long getLongCount() { |
| 695 | + return longCount; |
| 696 | + } |
| 697 | + |
| 698 | + public float getFloatCount() { |
| 699 | + return floatCount; |
| 700 | + } |
| 701 | + |
| 702 | + public double getDoubleCount() { |
| 703 | + return doubleCount; |
| 704 | + } |
| 705 | + |
| 706 | + public String getText() { |
| 707 | + return text; |
654 | 708 | }
|
| 709 | + |
655 | 710 | }
|
656 | 711 |
|
657 | 712 | private static class PrivateBeanWithPrivateConstructor {
|
|
0 commit comments