1 16 17 package org.springframework.beans.factory.config; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.List ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.beans.TypeMismatchException; 26 import org.springframework.beans.support.ArgumentConvertingMethodInvoker; 27 import org.springframework.util.MethodInvoker; 28 29 33 public class MethodInvokingFactoryBeanTests extends TestCase { 34 35 public void testParameterValidation() throws Exception { 36 String validationError = "improper validation of input properties"; 37 38 MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); 40 try { 41 mcfb.afterPropertiesSet(); 42 fail(validationError); 43 } 44 catch (IllegalArgumentException ex) { 45 } 47 48 mcfb = new MethodInvokingFactoryBean(); 49 mcfb.setTargetObject(this); 50 mcfb.setTargetMethod("whatever"); 51 try { 52 mcfb.afterPropertiesSet(); 53 fail(validationError); 54 } 55 catch (NoSuchMethodException ex) { 56 } 58 59 mcfb = new MethodInvokingFactoryBean(); 61 mcfb.setTargetClass(TestClass1.class); 62 mcfb.setTargetMethod("some.bogus.Method.name"); 63 try { 64 mcfb.afterPropertiesSet(); 65 fail(validationError); 66 } 67 catch (NoSuchMethodException ex) { 68 } 70 71 mcfb = new MethodInvokingFactoryBean(); 73 mcfb.setTargetClass(TestClass1.class); 74 mcfb.setTargetMethod("method1"); 75 try { 76 mcfb.afterPropertiesSet(); 77 fail(validationError); 78 } 79 catch (IllegalArgumentException ex) { 80 } 82 83 mcfb = new MethodInvokingFactoryBean(); 85 mcfb.setTargetObject(this); 86 try { 87 mcfb.afterPropertiesSet(); 88 fail(validationError); 89 } 90 catch (IllegalArgumentException ex) { 91 } 93 94 mcfb = new MethodInvokingFactoryBean(); 96 mcfb.setTargetObject(this); 97 mcfb.setTargetMethod("bogus"); 98 try { 99 mcfb.afterPropertiesSet(); 100 fail(validationError); 101 } 102 catch (NoSuchMethodException ex) { 103 } 105 106 TestClass1._staticField1 = 0; 108 mcfb = new MethodInvokingFactoryBean(); 109 mcfb.setTargetClass(TestClass1.class); 110 mcfb.setTargetMethod("staticMethod1"); 111 mcfb.afterPropertiesSet(); 112 113 TestClass1 tc1 = new TestClass1(); 115 mcfb = new MethodInvokingFactoryBean(); 116 mcfb.setTargetObject(tc1); 117 mcfb.setTargetMethod("method1"); 118 mcfb.afterPropertiesSet(); 119 } 120 121 public void testGetObjectType() throws Exception { 122 TestClass1 tc1 = new TestClass1(); 123 MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); 124 mcfb = new MethodInvokingFactoryBean(); 125 mcfb.setTargetObject(tc1); 126 mcfb.setTargetMethod("method1"); 127 mcfb.afterPropertiesSet(); 128 assertTrue(int.class.equals(mcfb.getObjectType())); 129 130 mcfb = new MethodInvokingFactoryBean(); 131 mcfb.setTargetClass(TestClass1.class); 132 mcfb.setTargetMethod("voidRetvalMethod"); 133 mcfb.afterPropertiesSet(); 134 Class objType = mcfb.getObjectType(); 135 assertTrue(objType.equals(MethodInvokingFactoryBean.VoidType.class)); 136 137 TestClass1._staticField1 = 0; 140 mcfb = new MethodInvokingFactoryBean(); 141 mcfb.setTargetClass(TestClass1.class); 142 mcfb.setTargetMethod("supertypes"); 143 mcfb.setArguments(new Object []{new ArrayList (), new ArrayList (), "hello"}); 144 mcfb.afterPropertiesSet(); 145 mcfb.getObjectType(); 146 147 mcfb = new MethodInvokingFactoryBean(); 149 mcfb.setTargetClass(TestClass1.class); 150 mcfb.setTargetMethod("supertypes"); 151 mcfb.setArguments(new Object []{"1", "2", "3"}); 152 try { 153 mcfb.afterPropertiesSet(); 154 } 155 catch (TypeMismatchException ex) { 156 } 158 } 159 160 public void testGetObject() throws Exception { 161 TestClass1 tc1 = new TestClass1(); 163 MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); 164 mcfb.setTargetObject(tc1); 165 mcfb.setTargetMethod("method1"); 166 mcfb.afterPropertiesSet(); 167 Integer i = (Integer ) mcfb.getObject(); 168 assertEquals(1, i.intValue()); 169 i = (Integer ) mcfb.getObject(); 170 assertEquals(1, i.intValue()); 171 172 tc1 = new TestClass1(); 174 mcfb = new MethodInvokingFactoryBean(); 175 mcfb.setTargetObject(tc1); 176 mcfb.setTargetMethod("method1"); 177 mcfb.setSingleton(false); 178 mcfb.afterPropertiesSet(); 179 i = (Integer ) mcfb.getObject(); 180 assertEquals(1, i.intValue()); 181 i = (Integer ) mcfb.getObject(); 182 assertEquals(2, i.intValue()); 183 184 TestClass1._staticField1 = 0; 186 mcfb = new MethodInvokingFactoryBean(); 187 mcfb.setTargetClass(TestClass1.class); 188 mcfb.setTargetMethod("staticMethod1"); 189 mcfb.afterPropertiesSet(); 190 i = (Integer ) mcfb.getObject(); 191 assertEquals(1, i.intValue()); 192 i = (Integer ) mcfb.getObject(); 193 assertEquals(1, i.intValue()); 194 195 TestClass1._staticField1 = 0; 197 mcfb = new MethodInvokingFactoryBean(); 198 mcfb.setStaticMethod("org.springframework.beans.factory.config.MethodInvokingFactoryBeanTests$TestClass1.staticMethod1"); 199 mcfb.setSingleton(false); 200 mcfb.afterPropertiesSet(); 201 i = (Integer ) mcfb.getObject(); 202 assertEquals(1, i.intValue()); 203 i = (Integer ) mcfb.getObject(); 204 assertEquals(2, i.intValue()); 205 206 mcfb = new MethodInvokingFactoryBean(); 208 mcfb.setTargetClass(TestClass1.class); 209 mcfb.setTargetMethod("voidRetvalMethod"); 210 mcfb.afterPropertiesSet(); 211 assertTrue(MethodInvokingFactoryBean.VOID.equals(mcfb.getObject())); 212 213 mcfb = new MethodInvokingFactoryBean(); 215 mcfb.setTargetClass(TestClass1.class); 216 mcfb.setTargetMethod("supertypes"); 217 mcfb.setArguments(new Object [] {new ArrayList (), new ArrayList (), "hello"}); 218 mcfb.afterPropertiesSet(); 220 } 221 222 public void testArgumentConversion() throws Exception { 223 MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); 224 mcfb.setTargetClass(TestClass1.class); 225 mcfb.setTargetMethod("supertypes"); 226 mcfb.setArguments(new Object [] {new ArrayList (), new ArrayList (), "hello", "bogus"}); 227 try { 228 mcfb.afterPropertiesSet(); 229 fail("Matched method with wrong number of args"); 230 } 231 catch (NoSuchMethodException ex) { 232 } 234 235 mcfb = new MethodInvokingFactoryBean(); 236 mcfb.setTargetClass(TestClass1.class); 237 mcfb.setTargetMethod("supertypes"); 238 mcfb.setArguments(new Object [] {new Integer (1), new Integer (2), new Integer (3)}); 239 try { 240 mcfb.afterPropertiesSet(); 241 Object x = mcfb.getObject(); 242 fail("Should have failed on getObject with mismatched argument types"); 243 } 244 catch (NoSuchMethodException ex) { 245 } 247 248 mcfb = new MethodInvokingFactoryBean(); 249 mcfb.setTargetClass(TestClass1.class); 250 mcfb.setTargetMethod("supertypes2"); 251 mcfb.setArguments(new Object [] {new ArrayList (), new ArrayList (), "hello", "bogus"}); 252 mcfb.afterPropertiesSet(); 253 assertEquals("hello", mcfb.getObject()); 254 255 mcfb = new MethodInvokingFactoryBean(); 256 mcfb.setTargetClass(TestClass1.class); 257 mcfb.setTargetMethod("supertypes2"); 258 mcfb.setArguments(new Object [] {new ArrayList (), new ArrayList (), "hello", new ArrayList ()}); 259 try { 260 mcfb.afterPropertiesSet(); 261 fail("Matched method when shouldn't have matched"); 262 } 263 catch (NoSuchMethodException ex) { 264 } 266 } 267 268 public void testInvokeWithNullArgument() throws Exception { 269 MethodInvoker methodInvoker = new MethodInvoker(); 270 methodInvoker.setTargetClass(TestClass1.class); 271 methodInvoker.setTargetMethod("nullArgument"); 272 methodInvoker.setArguments(new Object [] {null}); 273 methodInvoker.prepare(); 274 methodInvoker.invoke(); 275 } 276 277 public void testInvokeWithIntArgument() throws Exception { 278 ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker(); 279 methodInvoker.setTargetClass(TestClass1.class); 280 methodInvoker.setTargetMethod("intArgument"); 281 methodInvoker.setArguments(new Object [] {new Integer (5)}); 282 methodInvoker.prepare(); 283 methodInvoker.invoke(); 284 285 methodInvoker = new ArgumentConvertingMethodInvoker(); 286 methodInvoker.setTargetClass(TestClass1.class); 287 methodInvoker.setTargetMethod("intArgument"); 288 methodInvoker.setArguments(new Object [] {"5"}); 289 methodInvoker.prepare(); 290 methodInvoker.invoke(); 291 } 292 293 public void testPlainMethodInvoker() throws Exception { 294 TestClass1 tc1 = new TestClass1(); 296 MethodInvoker mi = new MethodInvoker(); 297 mi.setTargetObject(tc1); 298 mi.setTargetMethod("method1"); 299 mi.prepare(); 300 Integer i = (Integer ) mi.invoke(); 301 assertEquals(1, i.intValue()); 302 303 mi = new MethodInvoker(); 305 mi.setTargetClass(TestClass1.class); 306 mi.setTargetMethod("supertypes"); 307 mi.setArguments(new Object [] {new ArrayList (), new ArrayList (), "hello"}); 308 mi.prepare(); 309 assertEquals("hello", mi.invoke()); 310 311 mi = new MethodInvoker(); 313 mi.setTargetClass(TestClass1.class); 314 mi.setTargetMethod("supertypes2"); 315 mi.setArguments(new Object [] {new ArrayList (), new ArrayList (), "hello", "bogus"}); 316 try { 317 mi.prepare(); 318 fail("Shouldn't have matched without argument conversion"); 319 } 320 catch (NoSuchMethodException ex) { 321 } 323 } 324 325 326 public static class TestClass1 { 328 329 public static int _staticField1; 330 331 public int _field1 = 0; 332 333 public int method1() { 334 return ++_field1; 335 } 336 337 public static int staticMethod1() { 338 return ++_staticField1; 339 } 340 341 public static void voidRetvalMethod() { 342 } 343 344 public static void nullArgument(Object arg) { 345 } 346 347 public static void intArgument(int arg) { 348 } 349 350 public static String supertypes(Collection c, List l, String s) { 351 return s; 352 } 353 354 public static String supertypes2(Collection c, List l, String s, Integer i) { 355 return s; 356 } 357 358 public static String supertypes2(Collection c, List l, String s, String s2) { 359 return s; 360 } 361 } 362 363 } 364 | Popular Tags |