001package org.apache.commons.beanutils2; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.apache.commons.beanutils2.Argument.argument; 023import static org.apache.commons.beanutils2.BeanUtils.on; 024import static org.junit.Assert.assertEquals; 025import static org.junit.Assert.assertNotNull; 026 027import org.apache.commons.beanutils2.testbeans.AbstractTestBean; 028import org.apache.commons.beanutils2.testbeans.TestBean; 029import org.apache.commons.beanutils2.testbeans.ThrowingExceptionBean; 030import org.junit.Rule; 031import org.junit.Test; 032import org.junit.rules.ExpectedException; 033 034/** 035 * <p> 036 * Test case for <code>ClassAccessor</code> 037 * </p> 038 */ 039public class ConstructorsTestCase 040{ 041 042 @Rule 043 public ExpectedException thrown = ExpectedException.none(); 044 045 // ------------------------------------------------ Individual Test Methods 046 047 @Test 048 public void invokeConstructor() 049 throws Exception 050 { 051 { 052 TestBean obj = on( TestBean.class ).invokeConstructor( argument( "TEST" ) ).get(); 053 assertNotNull( obj ); 054 assertEquals( "TEST", obj.getStringProperty() ); 055 } 056 { 057 TestBean obj = on( TestBean.class ).invokeConstructor( argument( new Float( 17.3f ) ) ).get(); 058 assertNotNull( obj ); 059 assertEquals( 17.3f, obj.getFloatProperty(), 0.0f ); 060 } 061 } 062 063 @Test( expected = NoSuchConstructorException.class ) 064 public void invokeConstructorWithInvalidArgument() 065 throws Exception 066 { 067 on( TestBean.class ).invokeConstructor( argument( (byte) 6 ) ).get(); 068 } 069 070 @Test( expected = NullPointerException.class ) 071 public void invokeConstructorWithNull() 072 throws Exception 073 { 074 on( TestBean.class ).invokeConstructor( (Argument<?>) null ); 075 } 076 077 @Test 078 public void invokeConstructorWithArgArray() 079 throws Exception 080 { 081 TestBean obj = on( TestBean.class ).invokeConstructor( argument( new Float( 17.3f ) ), argument( "TEST" ) ).get(); 082 assertNotNull( obj ); 083 assertEquals( 17.3f, obj.getFloatProperty(), 0.0f ); 084 assertEquals( "TEST", obj.getStringProperty() ); 085 } 086 087 @Test( expected = NoSuchConstructorException.class ) 088 public void invokeConstrucotrWithInvalidArgArray() 089 throws Exception 090 { 091 on( TestBean.class ).invokeConstructor( argument( (byte) 17 ), argument( "TEST" ) ).get(); 092 } 093 094 @Test( expected = NullPointerException.class ) 095 public void invokeConstructorWithNullArray() 096 throws Exception 097 { 098 on( TestBean.class ).invokeConstructor( null, null, null ); 099 } 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 // FIXME Why don't we see a ConstructorNotAccessibleException? 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 // FIXME Why don't we see a ConstructorNotAccessibleException? 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 // FIXME Why don't we see a ConstructorNotAccessibleException? 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 // do nothing here 316 } 317 } 318 319}