Skip to content

Commit 5cc0984

Browse files
committed
Introduce test for gh-27390
1 parent 92cd680 commit 5cc0984

File tree

1 file changed

+75
-20
lines changed

1 file changed

+75
-20
lines changed

spring-beans/src/test/java/org/springframework/beans/BeanUtilsTests.java

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import static org.assertj.core.api.Assertions.assertThat;
5050
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5151
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
52+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
5253

5354
/**
5455
* Unit tests for {@link BeanUtils}.
@@ -85,19 +86,42 @@ void instantiateClassWithOptionalNullableType() throws NoSuchMethodException {
8586
}
8687

8788
@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"));
9494
}
9595

9696
@Test // gh-22531
9797
void instantiateClassWithMoreArgsThanParameters() throws NoSuchMethodException {
98-
Constructor<BeanWithPrimitiveTypes> ctor = BeanWithPrimitiveTypes.class.getDeclaredConstructor(int.class, boolean.class, String.class);
98+
Constructor<BeanWithPrimitiveTypes> constructor = getBeanWithPrimitiveTypesConstructor();
99+
99100
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);
101125
}
102126

103127
@Test
@@ -628,30 +652,61 @@ public String getValue() {
628652

629653
private static class BeanWithPrimitiveTypes {
630654

631-
private int counter;
632-
633655
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;
634663

635-
private String value;
636664

637665
@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) {
640668
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;
646676
}
647677

648678
public boolean isFlag() {
649679
return flag;
650680
}
651681

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;
654708
}
709+
655710
}
656711

657712
private static class PrivateBeanWithPrivateConstructor {

0 commit comments

Comments
 (0)