1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.commons.beanutils2.Argument.argument;
23 import static org.apache.commons.beanutils2.BeanUtils.on;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26
27 import org.apache.commons.beanutils2.testbeans.AbstractTestBean;
28 import org.apache.commons.beanutils2.testbeans.TestBean;
29 import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33
34
35
36
37
38
39 public class ConstructorsTestCase
40 {
41
42 @Rule
43 public ExpectedException thrown = ExpectedException.none();
44
45
46
47 @Test
48 public void invokeConstructor()
49 throws Exception
50 {
51 {
52 TestBean obj = on( TestBean.class ).invokeConstructor( argument( "TEST" ) ).get();
53 assertNotNull( obj );
54 assertEquals( "TEST", obj.getStringProperty() );
55 }
56 {
57 TestBean obj = on( TestBean.class ).invokeConstructor( argument( new Float( 17.3f ) ) ).get();
58 assertNotNull( obj );
59 assertEquals( 17.3f, obj.getFloatProperty(), 0.0f );
60 }
61 }
62
63 @Test( expected = NoSuchConstructorException.class )
64 public void invokeConstructorWithInvalidArgument()
65 throws Exception
66 {
67 on( TestBean.class ).invokeConstructor( argument( (byte) 6 ) ).get();
68 }
69
70 @Test( expected = NullPointerException.class )
71 public void invokeConstructorWithNull()
72 throws Exception
73 {
74 on( TestBean.class ).invokeConstructor( (Argument<?>) null );
75 }
76
77 @Test
78 public void invokeConstructorWithArgArray()
79 throws Exception
80 {
81 TestBean obj = on( TestBean.class ).invokeConstructor( argument( new Float( 17.3f ) ), argument( "TEST" ) ).get();
82 assertNotNull( obj );
83 assertEquals( 17.3f, obj.getFloatProperty(), 0.0f );
84 assertEquals( "TEST", obj.getStringProperty() );
85 }
86
87 @Test( expected = NoSuchConstructorException.class )
88 public void invokeConstrucotrWithInvalidArgArray()
89 throws Exception
90 {
91 on( TestBean.class ).invokeConstructor( argument( (byte) 17 ), argument( "TEST" ) ).get();
92 }
93
94 @Test( expected = NullPointerException.class )
95 public void invokeConstructorWithNullArray()
96 throws Exception
97 {
98 on( TestBean.class ).invokeConstructor( null, null, null );
99 }
100
101 @Test
102 public void invokeConstructorWithTypeArray()
103 throws Exception
104 {
105 {
106 TestBean obj = on( TestBean.class ).invokeConstructor( argument( Boolean.TYPE, Boolean.TRUE ),
107 argument( String.class, "TEST" ) ).get();
108 assertNotNull( obj );
109 assertEquals( true, obj.getBooleanProperty() );
110 assertEquals( "TEST", obj.getStringProperty() );
111 }
112 {
113 TestBean obj = on( TestBean.class ).invokeConstructor( argument( Boolean.class, Boolean.TRUE ),
114 argument( String.class, "TEST" ) ).get();
115 assertNotNull( obj );
116 assertEquals( true, obj.isBooleanSecond() );
117 assertEquals( "TEST", obj.getStringProperty() );
118 }
119 }
120
121 @Test( expected = NoSuchConstructorException.class )
122 public void invokeConstructorWithInvalidTypeArray() throws Exception
123 {
124 on( TestBean.class ).invokeConstructor( argument( String.class, "TEST" ),
125 argument( Boolean.TYPE, Boolean.TRUE )).get();
126 }
127
128 @Test
129 public void invokeExactConstructor()
130 throws Exception
131 {
132 {
133 TestBean obj = on( TestBean.class ).invokeExactConstructor( argument( "TEST" ) ).get();
134 assertNotNull( obj );
135 assertEquals( "TEST", obj.getStringProperty() );
136 }
137 {
138 TestBean obj = on( TestBean.class ).invokeExactConstructor( argument( Boolean.TRUE ) ).get();
139 assertNotNull( obj );
140
141 assertEquals( true, obj.isBooleanSecond() );
142 }
143 }
144
145 @Test( expected = NoSuchConstructorException.class )
146 public void invokeExactConstructorWithInvalidArgument()
147 throws Exception
148 {
149 on( TestBean.class ).invokeExactConstructor( argument( new Float( 17.3f ) ) ).get();
150 }
151
152 @Test( expected = NullPointerException.class )
153 public void invokeExactConstructorWithNull()
154 throws Exception
155 {
156 on( TestBean.class ).invokeExactConstructor( (Argument<?>) null );
157 }
158
159 @Test
160 public void invokeExactConstructorWithArgArray()
161 throws Exception
162 {
163 TestBean obj = on( TestBean.class ).invokeExactConstructor( argument( Boolean.TRUE ),
164 argument( "TEST" ) ).get();
165 assertNotNull( obj );
166 assertEquals( true, obj.isBooleanSecond() );
167 assertEquals( "TEST", obj.getStringProperty() );
168 }
169
170 @Test( expected = NoSuchConstructorException.class )
171 public void invokeExactConstructorWithInvalidArgArray() throws Exception
172 {
173 on( TestBean.class ).invokeExactConstructor( argument( new Float( 17.3f ) ),argument( "TEST" ) ).get();
174 }
175
176 @Test( expected = NullPointerException.class )
177 public void invokeExactConstructorWithNullArray()
178 throws Exception
179 {
180 on( TestBean.class ).invokeExactConstructor( null, null, null );
181 }
182
183 @Test
184 public void invokeExactConstructorWithTypeArray()
185 throws Exception
186 {
187 {
188 TestBean obj = on( TestBean.class ).invokeExactConstructor( argument( Boolean.TYPE, Boolean.TRUE ),
189 argument( String.class, "TEST" ) ).get();
190 assertNotNull( obj );
191 assertEquals( true, obj.getBooleanProperty() );
192 assertEquals( "TEST", obj.getStringProperty() );
193 }
194 {
195 TestBean obj = on( TestBean.class ).invokeExactConstructor( argument( Boolean.class, Boolean.TRUE ),
196 argument( String.class, "TEST" ) ).get();
197 assertNotNull( obj );
198 assertEquals( true, obj.isBooleanSecond() );
199 assertEquals( "TEST", obj.getStringProperty() );
200 }
201 {
202 TestBean obj = on( TestBean.class ).invokeExactConstructor( argument( Float.TYPE, new Float( 17.3f ) ),
203 argument( String.class, "TEST" ) ).get();
204 assertNotNull( obj );
205
206 assertEquals( 17.3f, obj.getFloatProperty(), 0.0f );
207 assertEquals( "TEST", obj.getStringProperty() );
208 }
209 }
210
211 @Test( expected = NoSuchConstructorException.class )
212 public void invokeExactConstructorWithInvalidTypeArray()
213 throws Exception
214 {
215 on( TestBean.class ).invokeExactConstructor( argument( Float.class, new Float( 17.3f ) ),
216 argument( String.class, "TEST" ) ).get();
217 }
218
219 @Test( expected = ConstructorInvocationException.class )
220 public void invokeConstructorThatThrowsException()
221 throws Exception
222 {
223 on( ThrowingExceptionBean.class ).invokeConstructor( argument( RuntimeException.class,
224 new RuntimeException() ) );
225 }
226
227 @Test( expected = ConstructorInvocationException.class )
228 public void invokeExactConstructorThatThrowsException()
229 throws Exception
230 {
231 on( ThrowingExceptionBean.class ).invokeExactConstructor( argument( RuntimeException.class,
232 new RuntimeException() ) );
233 }
234
235
236 @Test( expected = NoSuchConstructorException.class )
237 public void invokePrivateConstructor()
238 throws Exception
239 {
240 on( ThrowingExceptionBean.class ).invokeConstructor( argument( Integer.class, Integer.valueOf( 4711 ) ) );
241 }
242
243
244 @Test( expected = NoSuchConstructorException.class )
245 public void invokeProtectedConstructor()
246 throws Exception
247 {
248 on( ThrowingExceptionBean.class ).invokeConstructor( argument( Short.class, Short.valueOf( (short) 4711 ) ) );
249 }
250
251
252 @Test( expected = NoSuchConstructorException.class )
253 public void invokeDefaultConstructor()
254 throws Exception
255 {
256 on( ThrowingExceptionBean.class ).invokeConstructor( argument( Byte.class, Byte.valueOf( (byte) 4711 ) ) );
257 }
258
259 @Test
260 public void invokeConstructorOnAbstract()
261 throws Exception
262 {
263 thrown.expect( BeanInstantiationException.class );
264 thrown.expectMessage( "abstract" );
265 on( AbstractTestBean.class ).invokeConstructor();
266 }
267
268 @Test
269 public void invokeConstructorOnInterface()
270 throws Exception
271 {
272 thrown.expect( BeanInstantiationException.class );
273 thrown.expectMessage( "interface" );
274 on( ClassAccessor.class ).invokeConstructor();
275 }
276
277 @Test
278 public void invokeConstructorOnArray()
279 throws Exception
280 {
281 thrown.expect( BeanInstantiationException.class );
282 thrown.expectMessage( "array" );
283 on( String[].class ).invokeConstructor();
284 }
285
286 @Test
287 public void invokeConstructorOnPrimitive()
288 throws Exception
289 {
290 thrown.expect( BeanInstantiationException.class );
291 thrown.expectMessage( "primitive" );
292 on( int.class ).invokeConstructor();
293 }
294
295 @Test
296 public void invokeConstructorOnVoid()
297 throws Exception
298 {
299 thrown.expect( BeanInstantiationException.class );
300 thrown.expectMessage( "void" );
301 on( void.class ).invokeConstructor();
302 }
303
304 @Test( expected = NoSuchConstructorException.class )
305 public void newInstanceWithoutDefaultConstructor()
306 {
307 on( NoDefaultConstructor.class ).newInstance();
308 }
309
310 public static class NoDefaultConstructor
311 {
312
313 NoDefaultConstructor( int i )
314 {
315
316 }
317 }
318
319 }