1 17 package org.apache.jmeter.testbeans; 18 19 import java.beans.BeanInfo ; 20 import java.beans.IntrospectionException ; 21 import java.beans.Introspector ; 22 import java.beans.PropertyDescriptor ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import org.apache.jmeter.control.NextIsNullException; 30 import org.apache.jmeter.samplers.Sampler; 31 import org.apache.jmeter.testelement.AbstractTestElement; 32 import org.apache.jmeter.testelement.TestElement; 33 import org.apache.jmeter.testelement.TestElementTraverser; 34 import org.apache.jmeter.testelement.property.CollectionProperty; 35 import org.apache.jmeter.testelement.property.JMeterProperty; 36 import org.apache.jmeter.testelement.property.MapProperty; 37 import org.apache.jmeter.testelement.property.NullProperty; 38 import org.apache.jmeter.testelement.property.PropertyIterator; 39 import org.apache.jorphan.logging.LoggingManager; 40 import org.apache.jorphan.util.JOrphanUtils; 41 import org.apache.log.Logger; 42 43 55 public abstract class TestBean extends AbstractTestElement 56 { 57 private static final Logger log = LoggingManager.getLoggerForClass(); 58 59 62 private transient Map descriptors; 63 64 70 protected TestBean() 71 { 72 super(); 73 74 try 75 { 76 BeanInfo beanInfo= Introspector.getBeanInfo(this.getClass()); 78 PropertyDescriptor [] desc= beanInfo.getPropertyDescriptors(); 79 descriptors= new HashMap (); 80 for (int i=0; i<desc.length; i++) 81 { 82 descriptors.put(desc[i].getName(), desc[i]); 83 } 84 } 85 catch (IntrospectionException e) 86 { 87 log.error("Can't get beanInfo for "+this.getClass().getName(), 88 e); 89 throw new Error (e.toString()); } 91 } 92 93 99 public void prepare() 100 { 101 Object [] param= new Object [1]; 102 103 if (log.isDebugEnabled()) log.debug("Preparing "+this.getClass()); 104 105 for (PropertyIterator jprops= propertyIterator(); jprops.hasNext(); ) 106 { 107 JMeterProperty jprop= jprops.next(); 109 PropertyDescriptor descriptor= (PropertyDescriptor )descriptors.get(jprop.getName()); 110 111 if (descriptor == null) 112 { 113 if (log.isDebugEnabled()) 114 { 115 log.debug("Ignoring auxiliary property "+jprop.getName()); 116 } 117 continue; 118 } 119 120 Class type= descriptor.getPropertyType(); 121 Object value= unwrapProperty(jprop, type); 122 123 if (log.isDebugEnabled()) log.debug("Setting "+jprop.getName()+"="+value); 124 125 if (value != null || !type.isPrimitive()) 127 { 129 param[0]= value; 130 invokeOrBailOut(descriptor.getWriteMethod(), param); 131 } 132 } 133 } 134 135 143 private Object invokeOrBailOut(Method method, Object [] params) 144 { 145 try 146 { 147 return method.invoke(this, params); 148 } 149 catch (IllegalArgumentException e) 150 { 151 log.error("This should never happen.", e); 152 throw new Error (e.toString()); } 154 catch (IllegalAccessException e) 155 { 156 log.error("This should never happen.", e); 157 throw new Error (e.toString()); } 159 catch (InvocationTargetException e) 160 { 161 log.error("This should never happen.", e); 162 throw new Error (e.toString()); } 164 } 165 166 177 private static Object unwrapProperty(JMeterProperty property, Class type) 178 { 179 185 Object value; 186 if (property instanceof NullProperty) 187 { 188 value= null; 191 } 192 else if (type == boolean.class || type == Boolean .class) 193 { 194 value= JOrphanUtils.valueOf(property.getBooleanValue()); } 196 else if (type == double.class || type == Double .class) 197 { 198 value= new Double (property.getDoubleValue()); 199 } 200 else if (type == float.class || type == Float .class) 201 { 202 value= new Float (property.getFloatValue()); 203 } 204 else if (type == int.class || type == Integer .class) 205 { 206 value= new Integer (property.getIntValue()); 207 } 208 else if (type == long.class || type == Long .class) 209 { 210 value= new Long (property.getLongValue()); 211 } 212 else if (type == String .class) 213 { 214 value= property.getStringValue(); 215 } 216 else 217 { 218 value= property.getObjectValue(); 219 } 220 221 return value; 222 } 223 224 229 230 234 protected void addProperty(JMeterProperty property) 235 { 236 super.addProperty(property); 237 } 238 239 243 public void addTestElement(TestElement el) 244 { 245 for (Iterator descs= descriptors.values().iterator(); 247 descs.hasNext(); ) 248 { 249 PropertyDescriptor desc= (PropertyDescriptor )descs.next(); 250 if (desc.getPropertyType().isInstance(el) 251 && desc.getPropertyEditorClass() == null) 252 { 259 invokeOrBailOut(desc.getWriteMethod(), new Object [] { el }); 260 return; } 262 } 263 super.addTestElement(el); 265 } 266 267 271 public void clear() 272 { 273 super.clear(); 274 } 275 276 280 protected void clearTemporary(JMeterProperty property) 281 { 282 super.clearTemporary(property); 283 } 284 285 289 public Object clone() 290 { 291 return super.clone(); 292 } 293 294 298 protected void emptyTemporary() 299 { 300 super.emptyTemporary(); 301 } 302 303 307 public boolean equals(Object o) 308 { 309 return super.equals(o); 310 } 311 312 317 public String getName() 318 { 319 return super.getName(); 320 } 321 322 326 public JMeterProperty getProperty(String key) 327 { 328 return super.getProperty(key); 329 } 330 331 335 public boolean getPropertyAsBoolean(String key, boolean defaultVal) 336 { 337 return super.getPropertyAsBoolean(key, defaultVal); 338 } 339 340 344 public boolean getPropertyAsBoolean(String key) 345 { 346 return super.getPropertyAsBoolean(key); 347 } 348 349 353 public double getPropertyAsDouble(String key) 354 { 355 return super.getPropertyAsDouble(key); 356 } 357 358 362 public float getPropertyAsFloat(String key) 363 { 364 return super.getPropertyAsFloat(key); 365 } 366 367 371 public int getPropertyAsInt(String key) 372 { 373 return super.getPropertyAsInt(key); 374 } 375 376 380 public long getPropertyAsLong(String key) 381 { 382 return super.getPropertyAsLong(key); 383 } 384 385 389 public String getPropertyAsString(String key) 390 { 391 return super.getPropertyAsString(key); 392 } 393 394 398 public boolean isRunningVersion() 399 { 400 return super.isRunningVersion(); 401 } 402 403 407 public boolean isTemporary(JMeterProperty property) 408 { 409 return super.isTemporary(property); 410 } 411 412 416 protected void logProperties() 417 { 418 super.logProperties(); 419 } 420 421 425 protected void mergeIn(TestElement element) 426 { 427 super.mergeIn(element); 428 } 429 430 434 protected Sampler nextIsNull() throws NextIsNullException 435 { 436 return super.nextIsNull(); 437 } 438 439 443 public PropertyIterator propertyIterator() 444 { 445 return super.propertyIterator(); 446 } 447 448 452 public void recoverRunningVersion() 453 { 454 super.recoverRunningVersion(); 455 } 456 457 461 public void removeProperty(String key) 462 { 463 super.removeProperty(key); 464 } 465 466 470 public void setName(String name) 471 { 472 super.setName(name); 473 } 474 475 479 public void setProperty(JMeterProperty property) 480 { 481 super.setProperty(property); 482 } 483 484 488 public void setProperty(String name, String value) 489 { 490 super.setProperty(name, value); 491 } 492 493 497 public void setRunningVersion(boolean runningVersion) 498 { 499 super.setRunningVersion(runningVersion); 500 } 501 502 506 public void setTemporary(JMeterProperty property) 507 { 508 super.setTemporary(property); 509 } 510 511 515 public void traverse(TestElementTraverser traverser) 516 { 517 super.traverse(traverser); 518 } 519 520 524 protected void traverseCollection( 525 CollectionProperty col, 526 TestElementTraverser traverser) 527 { 528 super.traverseCollection(col, traverser); 529 } 530 531 535 protected void traverseMap(MapProperty map, TestElementTraverser traverser) 536 { 537 super.traverseMap(map, traverser); 538 } 539 540 544 protected void traverseProperty( 545 TestElementTraverser traverser, 546 JMeterProperty value) 547 { 548 super.traverseProperty(traverser, value); 549 } 550 } 551 | Popular Tags |