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.junit.Assert.assertEquals; 023import static org.junit.Assert.assertNotNull; 024import static org.junit.Assert.assertNull; 025import static org.junit.Assert.fail; 026 027import java.beans.IntrospectionException; 028 029import org.apache.commons.beanutils2.testbeans.MappedPropertyChildBean; 030import org.apache.commons.beanutils2.testbeans.MappedPropertyChildInterface; 031import org.apache.commons.beanutils2.testbeans.MappedPropertyTestBean; 032import org.apache.commons.beanutils2.testbeans.MappedPropertyTestInterface; 033import org.junit.Ignore; 034import org.junit.Test; 035 036/** 037 * <p> 038 * Test Case for the <code>MappedPropertyDescriptor</code>. 039 * </p> 040 * Extracted from BeanUtils1 041 */ 042public class MappedPropertyTestCase 043{ 044 045 @Test( expected = IntrospectionException.class ) 046 public void testConstructorNullClass() 047 throws Exception 048 { 049 new MappedPropertyDescriptor( null, MappedPropertyTestBean.class ); 050 } 051 052 @Test( expected = IntrospectionException.class ) 053 public void testConstructorEmptyStringClass() 054 throws Exception 055 { 056 new MappedPropertyDescriptor( "", MappedPropertyTestBean.class ); 057 } 058 059 @Test( expected = IntrospectionException.class ) 060 public void testConstructorNullNullNull() 061 throws Exception 062 { 063 new MappedPropertyDescriptor( null, null, null ); 064 } 065 066 @Test( expected = IntrospectionException.class ) 067 public void testConstructorEmptyStringNullNull() 068 throws Exception 069 { 070 new MappedPropertyDescriptor( "", null, null ); 071 } 072 073 /** 074 * Test valid method name 075 */ 076 @Test 077 public void testFound() 078 { 079 String property = "mapproperty"; 080 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 081 try 082 { 083 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 084 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 085 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 086 } 087 catch ( Exception ex ) 088 { 089 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 090 } 091 } 092 093 /** 094 * Test boolean "is" method name 095 */ 096 @Test 097 public void testBooleanMapped() 098 { 099 String property = "mappedBoolean"; 100 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 101 try 102 { 103 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 104 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 105 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 106 } 107 catch ( Exception ex ) 108 { 109 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 110 } 111 } 112 113 /** 114 * Test invalid method name 115 */ 116 @Test 117 public void testNotFound() 118 { 119 String property = "xxxxxxx"; 120 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 121 try 122 { 123 new MappedPropertyDescriptor( property, clazz ); 124 fail( "Property '" + property + "' found in " + clazz.getName() ); 125 } 126 catch ( Exception ex ) 127 { 128 // expected result 129 } 130 } 131 132 /** 133 * Test Mapped Property - Getter only 134 */ 135 @Test 136 public void testMappedGetterOnly() 137 { 138 String property = "mappedGetterOnly"; 139 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 140 try 141 { 142 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 143 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 144 assertNull( "Setter is found", desc.getMappedWriteMethod() ); 145 } 146 catch ( Exception ex ) 147 { 148 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 149 } 150 } 151 152 /** 153 * Test Mapped Property - Setter Only 154 */ 155 @Test 156 public void testMappedSetterOnly() 157 { 158 String property = "mappedSetterOnly"; 159 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 160 try 161 { 162 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 163 assertNull( "Getter is found", desc.getMappedReadMethod() ); 164 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 165 } 166 catch ( Exception ex ) 167 { 168 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 169 } 170 } 171 172 /** 173 * Test Mapped Property - Invalid Setter 174 */ 175 @Test 176 public void testInvalidSetter() 177 { 178 String property = "invalidSetter"; 179 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 180 try 181 { 182 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 183 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 184 assertNull( "Setter is found", desc.getMappedWriteMethod() ); 185 } 186 catch ( Exception ex ) 187 { 188 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 189 } 190 } 191 192 /** 193 * Test Mapped Property - Invalid Getter 194 */ 195 @Test 196 public void testInvalidGetter() 197 { 198 String property = "invalidGetter"; 199 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 200 try 201 { 202 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 203 assertNull( "Getter is found", desc.getMappedReadMethod() ); 204 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 205 } 206 catch ( Exception ex ) 207 { 208 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 209 } 210 } 211 212 /** 213 * Test Mapped Property - Different Types Expect to find the getDifferentTypes() method, but not the 214 * setDifferentTypes() method because setDifferentTypes() sets and Integer, while getDifferentTypes() returns a 215 * Long. 216 */ 217 @Test 218 public void testDifferentTypes() 219 { 220 String property = "differentTypes"; 221 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 222 try 223 { 224 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 225 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 226 assertNull( "Setter is found", desc.getMappedWriteMethod() ); 227 } 228 catch ( Exception ex ) 229 { 230 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 231 } 232 } 233 234 /** 235 * Test Mpa getter 236 */ 237 @Test 238 @Ignore // TODO review this test 239 public void testMapGetter() 240 { 241 MappedPropertyTestBean bean = new MappedPropertyTestBean(); 242 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 243 String property = "myMap"; 244 try 245 { 246 String testValue = "test value"; 247 String testKey = "testKey"; 248 // BeanUtils.setProperty( bean, "myMap(" + testKey + ")", "test value" ); 249 assertEquals( "Map getter", testValue, bean.getMyMap().get( testKey ) ); 250 } 251 catch ( Exception ex ) 252 { 253 fail( "Test set mapped property failed: " + ex ); 254 } 255 } 256 257 /** 258 * Test property with any two args 259 */ 260 @Test 261 public void testAnyArgsProperty() 262 { 263 String property = "anyMapped"; 264 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 265 try 266 { 267 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 268 assertNull( "Getter is found", desc.getMappedReadMethod() ); 269 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 270 } 271 catch ( Exception ex ) 272 { 273 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 274 } 275 } 276 277 /** 278 * Test property with two primitve args 279 */ 280 @Test 281 public void testPrimitiveArgsProperty() 282 { 283 String property = "mappedPrimitive"; 284 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 285 try 286 { 287 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 288 assertNull( "Getter is found", desc.getMappedReadMethod() ); 289 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 290 } 291 catch ( Exception ex ) 292 { 293 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 294 } 295 } 296 297 /** 298 * Test 'protected' mapped property 299 */ 300 @Test( expected = IntrospectionException.class ) 301 public void testProtected() 302 throws IntrospectionException 303 { 304 String property = "protectedProperty"; 305 Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class; 306 new MappedPropertyDescriptor( property, clazz ); 307 } 308 309 /** 310 * Test 'public' method in parent 311 */ 312 @Test 313 public void testPublicParentMethod() 314 { 315 String property = "mapproperty"; 316 Class<MappedPropertyChildBean> clazz = MappedPropertyChildBean.class; 317 try 318 { 319 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 320 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 321 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 322 } 323 catch ( Exception ex ) 324 { 325 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 326 } 327 } 328 329 /** 330 * Test 'protected' method in parent 331 */ 332 @Test( expected = IntrospectionException.class ) 333 public void testProtectedParentMethod() 334 throws IntrospectionException 335 { 336 String property = "protectedMapped"; 337 Class<MappedPropertyChildBean> clazz = MappedPropertyChildBean.class; 338 new MappedPropertyDescriptor( property, clazz ); 339 } 340 341 /** 342 * Test Interface with mapped property 343 */ 344 @Test 345 public void testInterfaceMapped() 346 { 347 String property = "mapproperty"; 348 Class<MappedPropertyTestInterface> clazz = MappedPropertyTestInterface.class; 349 try 350 { 351 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 352 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 353 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 354 } 355 catch ( Exception ex ) 356 { 357 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 358 } 359 } 360 361 /** 362 * Test property not found in interface 363 */ 364 @Test( expected = IntrospectionException.class ) 365 public void testInterfaceNotFound() 366 throws IntrospectionException 367 { 368 String property = "XXXXXX"; 369 Class<MappedPropertyTestInterface> clazz = MappedPropertyTestInterface.class; 370 new MappedPropertyDescriptor( property, clazz ); 371 } 372 373 /** 374 * Test Interface Inherited mapped property 375 */ 376 @Test 377 public void testChildInterfaceMapped() 378 { 379 String property = "mapproperty"; 380 Class<MappedPropertyChildInterface> clazz = MappedPropertyChildInterface.class; 381 try 382 { 383 MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz ); 384 assertNotNull( "Getter is missing", desc.getMappedReadMethod() ); 385 assertNotNull( "Setter is missing", desc.getMappedWriteMethod() ); 386 } 387 catch ( Exception ex ) 388 { 389 fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex ); 390 } 391 } 392}