Skip to content

Commit 4d1cdf6

Browse files
committed
Introduce test for gh-27390
1 parent e1ba3e7 commit 4d1cdf6

File tree

1 file changed

+77
-22
lines changed

1 file changed

+77
-22
lines changed

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

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static org.assertj.core.api.Assertions.assertThat;
4545
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4646
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
47+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
4748

4849
/**
4950
* Unit tests for {@link BeanUtils}.
@@ -80,19 +81,42 @@ void testInstantiateClassWithOptionalNullableType() throws NoSuchMethodException
8081
}
8182

8283
@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"));
8989
}
9090

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+
9495
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);
96120
}
97121

98122
@Test
@@ -535,30 +559,61 @@ public String getValue() {
535559

536560
private static class BeanWithPrimitiveTypes {
537561

538-
private int counter;
539-
540562
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;
541570

542-
private String value;
543571

544572
@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) {
547575
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;
553583
}
554584

555585
public boolean isFlag() {
556586
return flag;
557587
}
558588

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;
561615
}
616+
562617
}
563618

564619
private static class PrivateBeanWithPrivateConstructor {

0 commit comments

Comments
 (0)