1+ package ch .jalu .typeresolver .reflect ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import java .lang .reflect .Constructor ;
6+ import java .util .ArrayList ;
7+ import java .util .Optional ;
8+
9+ import static org .hamcrest .MatcherAssert .assertThat ;
10+ import static org .hamcrest .Matchers .equalTo ;
11+ import static org .hamcrest .Matchers .instanceOf ;
12+ import static org .hamcrest .Matchers .notNullValue ;
13+ import static org .junit .jupiter .api .Assertions .assertThrows ;
14+
15+ /**
16+ * Test for {@link ConstructorUtils}.
17+ */
18+ class ConstructorUtilsTest {
19+
20+ @ Test
21+ void shouldReturnConstructorIfApplicable () throws NoSuchMethodException {
22+ // given / when
23+ Optional <Constructor <Sample >> constr1 = ConstructorUtils .tryFindConstructor (Sample .class , int .class , String .class );
24+ Optional <Constructor <Sample >> constr2 = ConstructorUtils .tryFindConstructor (Sample .class , int .class , long .class );
25+
26+ // then
27+ assertThat (constr1 , equalTo (Optional .of (Sample .class .getDeclaredConstructor (int .class , String .class ))));
28+ assertThat (constr2 , equalTo (Optional .empty ()));
29+ }
30+
31+ @ Test
32+ void shouldReturnConstructor () throws NoSuchMethodException {
33+ // given / when
34+ Constructor <Sample > constr = ConstructorUtils .getConstructorOrThrow (Sample .class , int .class , String .class );
35+
36+ // then
37+ assertThat (constr , equalTo (Sample .class .getDeclaredConstructor (int .class , String .class )));
38+ }
39+
40+ @ Test
41+ void shouldThrowForNonExistentConstructor () {
42+ // given / when
43+ IllegalStateException ex = assertThrows (IllegalStateException .class ,
44+ () -> ConstructorUtils .getConstructorOrThrow (Sample .class , String .class , int .class ));
45+
46+ // then
47+ assertThat (ex .getMessage (), equalTo ("No constructor on 'class ch.jalu.typeresolver.reflect.ConstructorUtilsTest$Sample' matches the parameter types: [String, int]" ));
48+ }
49+
50+ @ Test
51+ void shouldInvokeConstructor () throws NoSuchMethodException {
52+ // given
53+ Constructor <Sample > constr = Sample .class .getDeclaredConstructor (int .class , String .class );
54+
55+ // when
56+ Sample result = ConstructorUtils .invokeConstructor (constr , 3 , "test" );
57+
58+ // then
59+ assertThat (result .size , equalTo (3 ));
60+ assertThat (result .str , equalTo ("test" ));
61+ }
62+
63+ @ Test
64+ void shouldWrapIllegalArgumentException () throws NoSuchMethodException {
65+ // given
66+ Constructor <Sample > constr = Sample .class .getDeclaredConstructor (int .class , String .class );
67+
68+ // when
69+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
70+ () -> ConstructorUtils .invokeConstructor (constr , true , "test" ));
71+
72+ // then
73+ assertThat (ex .getMessage (), equalTo ("Failed to call constructor for 'class ch.jalu.typeresolver.reflect.ConstructorUtilsTest$Sample'" ));
74+ assertThat (ex .getCause (), instanceOf (IllegalArgumentException .class ));
75+ }
76+
77+ @ Test
78+ void shouldWrapReflectiveOperationException () throws NoSuchMethodException {
79+ // given
80+ Constructor <Sample > constr = Sample .class .getDeclaredConstructor (int [].class );
81+
82+ // when
83+ IllegalStateException ex = assertThrows (IllegalStateException .class ,
84+ () -> ConstructorUtils .invokeConstructor (constr , new int [0 ]));
85+
86+ // then
87+ assertThat (ex .getMessage (), equalTo ("Failed to call constructor for 'class ch.jalu.typeresolver.reflect.ConstructorUtilsTest$Sample'" ));
88+ assertThat (ex .getCause (), instanceOf (IllegalAccessException .class ));
89+ }
90+
91+ @ Test
92+ void shouldCreateObjectFromZeroArgsConstructor () {
93+ // given / when
94+ NoArgsBean noArgsBean = ConstructorUtils .newInstanceFromZeroArgsConstructor (NoArgsBean .class );
95+ ArrayList arrayList = ConstructorUtils .newInstanceFromZeroArgsConstructor (ArrayList .class );
96+
97+ // then
98+ assertThat (noArgsBean , notNullValue ());
99+ assertThat (arrayList , notNullValue ());
100+ }
101+
102+ @ Test
103+ void shouldCreateObjectFromPrivateConstructor () {
104+ // given / when / then
105+ assertThat (ConstructorUtils .newInstanceFromZeroArgsConstructor (ConstructorUtils .class ), notNullValue ());
106+ }
107+
108+ @ Test
109+ void shouldThrowForMissingZeroArgsConstructor () {
110+ // given / when
111+ IllegalStateException ex = assertThrows (IllegalStateException .class ,
112+ () -> ConstructorUtils .newInstanceFromZeroArgsConstructor (Integer .class ));
113+
114+ // then
115+ assertThat (ex .getMessage (), equalTo ("Expected class 'java.lang.Integer' to have a zero-args constructor" ));
116+ }
117+
118+ @ Test
119+ void shouldCreateToStringForConstructor () throws NoSuchMethodException {
120+ // given
121+ Constructor <?> constr1 = NoArgsBean .class .getDeclaredConstructor ();
122+ Constructor <?> constr2 = Integer .class .getDeclaredConstructor (int .class );
123+ Constructor <?> constr3 = Sample .class .getDeclaredConstructor (int .class , String .class );
124+
125+ // when / then
126+ assertThat (ConstructorUtils .simpleToString (constr1 ), equalTo ("NoArgsBean()" ));
127+ assertThat (ConstructorUtils .simpleToString (constr2 ), equalTo ("Integer(int)" ));
128+ assertThat (ConstructorUtils .simpleToString (constr3 ), equalTo ("Sample(int, String)" ));
129+ }
130+
131+ static final class Sample {
132+
133+ int size ;
134+ String str ;
135+
136+ Sample (int size , String str ) {
137+ this .size = size ;
138+ this .str = str ;
139+ }
140+
141+ private Sample (int [] array ) {
142+ }
143+ }
144+
145+ static final class NoArgsBean {
146+
147+ public NoArgsBean () {
148+ }
149+ }
150+ }
0 commit comments