1 18 19 36 37 package org.apache.jmeter.testelement.property; 38 39 import java.util.Collection ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 import org.apache.jmeter.testelement.TestElement; 44 import org.apache.jorphan.logging.LoggingManager; 45 import org.apache.log.Logger; 46 47 50 public abstract class AbstractProperty implements JMeterProperty 51 { 52 protected static final Logger log = LoggingManager.getLoggerForClass(); 53 private String name; 54 private boolean runningVersion = false; 55 56 public AbstractProperty(String name) 57 { 58 if (name == null) 59 throw new IllegalArgumentException ("Name cannot be null"); 60 this.name = name; 61 } 62 63 public AbstractProperty() 64 { 65 this(""); 66 } 67 68 protected boolean isEqualType(JMeterProperty prop) 69 { 70 if (this.getClass().equals(prop.getClass())) 71 { 72 return true; 73 } 74 else 75 { 76 return false; 77 } 78 } 79 80 83 public boolean isRunningVersion() 84 { 85 return runningVersion; 86 } 87 88 91 public String getName() 92 { 93 return name; 94 } 95 96 public void setName(String name) 97 { 98 if (name == null) 99 throw new IllegalArgumentException ("Name cannot be null"); 100 this.name = name; 101 } 102 103 106 public void setRunningVersion(boolean runningVersion) 107 { 108 this.runningVersion = runningVersion; 109 } 110 111 protected PropertyIterator getIterator(Collection values) 112 { 113 return new PropertyIteratorImpl(values); 114 } 115 116 119 public Object clone() 120 { 121 try 122 { 123 AbstractProperty prop = 124 (AbstractProperty) this.getClass().newInstance(); 125 prop.name = name; 126 prop.runningVersion = runningVersion; 127 return prop; 128 } 129 catch (InstantiationException e) 130 { 131 return null; 132 } 133 catch (IllegalAccessException e) 134 { 135 return null; 136 } 137 } 138 139 143 public int getIntValue() 144 { 145 String val = getStringValue(); 146 if (val == null) 147 { 148 return 0; 149 } 150 try 151 { 152 return Integer.parseInt(val); 153 } 154 catch (NumberFormatException e) 155 { 156 return 0; 157 } 158 } 159 160 164 public long getLongValue() 165 { 166 String val = getStringValue(); 167 if (val == null) 168 { 169 return 0; 170 } 171 try 172 { 173 return Long.parseLong(val); 174 } 175 catch (NumberFormatException e) 176 { 177 return 0; 178 } 179 } 180 181 185 public double getDoubleValue() 186 { 187 String val = getStringValue(); 188 if (val == null) 189 { 190 return 0; 191 } 192 try 193 { 194 return Double.parseDouble(val); 195 } 196 catch (NumberFormatException e) 197 { 198 log.error("Tried to parse a non-number string to an integer", e); 199 return 0; 200 } 201 } 202 203 207 public float getFloatValue() 208 { 209 String val = getStringValue(); 210 if (val == null) 211 { 212 return 0; 213 } 214 try 215 { 216 return Float.parseFloat(val); 217 } 218 catch (NumberFormatException e) 219 { 220 log.error("Tried to parse a non-number string to an integer", e); 221 return 0; 222 } 223 } 224 225 229 public boolean getBooleanValue() 230 { 231 String val = getStringValue(); 232 if (val == null) 233 { 234 return false; 235 } 236 return Boolean.valueOf(val).booleanValue(); 237 } 238 239 244 public boolean equals(Object o) 245 { 246 if (!(o instanceof JMeterProperty)) return false; 247 if (this == o) return true; 248 JMeterProperty jpo = (JMeterProperty) o; 249 if (!name.equals(jpo.getName())) return false; 250 String s1 = getStringValue(); 251 String s2 = jpo.getStringValue(); 252 return s1 == null ? s2 == null : s1.equals(s2); 253 } 254 255 public int hashCode() 256 { 257 int result = 17; 258 result = result * 37 + name.hashCode(); String s = getStringValue(); 260 result = result * 37 + (s == null ? 0 : s.hashCode()); 261 return result; 262 } 263 264 273 public int compareTo(Object arg0) 274 { 275 if (arg0 instanceof JMeterProperty) 276 { 277 String val = getStringValue(); 282 String val2 = ((JMeterProperty)arg0).getStringValue(); 283 if (val == null) 284 { 285 log.warn( 286 "Warning: Unexpected null value for property: " + name); 287 288 if (val2 == null) 289 { 290 return 0; 292 } 293 else 294 { 295 return -1; 296 } 297 } 298 return val.compareTo(val2); 299 } 300 else 301 { 302 return -1; 303 } 304 } 305 306 310 protected Class getPropertyType() 311 { 312 return getClass(); 313 } 314 315 protected JMeterProperty getBlankProperty() 316 { 317 try 318 { 319 JMeterProperty prop = 320 (JMeterProperty) getPropertyType().newInstance(); 321 if (prop instanceof NullProperty) 322 { 323 return new StringProperty(); 324 } 325 return prop; 326 } 327 catch (Exception e) 328 { 329 return new StringProperty(); 330 } 331 } 332 333 protected Collection normalizeList(Collection coll) 334 { 335 Iterator iter = coll.iterator(); 336 Collection newColl = null; 337 while (iter.hasNext()) 338 { 339 Object item = iter.next(); 340 if (newColl == null) 341 { 342 try 343 { 344 newColl = (Collection ) coll.getClass().newInstance(); 345 } 346 catch (Exception e) 347 { 348 log.error("Bad collection", e); 349 } 350 } 351 newColl.add(convertObject(item)); 352 } 353 if (newColl != null) 354 { 355 return newColl; 356 } 357 else 358 { 359 return coll; 360 } 361 } 362 363 367 protected Map normalizeMap(Map coll) 368 { 369 Iterator iter = coll.keySet().iterator(); 370 Map newColl = null; 371 while (iter.hasNext()) 372 { 373 Object item = iter.next(); 374 Object prop = coll.get(item); 375 if (newColl == null) 376 { 377 try 378 { 379 newColl = (Map ) coll.getClass().newInstance(); 380 } 381 catch (Exception e) 382 { 383 log.error("Bad collection", e); 384 } 385 } 386 newColl.put(item, convertObject(prop)); 387 } 388 if (newColl != null) 389 { 390 return newColl; 391 } 392 else 393 { 394 return coll; 395 } 396 } 397 398 protected JMeterProperty convertObject(Object item) 399 { 400 if (item instanceof JMeterProperty) 401 { 402 return (JMeterProperty) item; 403 } 404 else if (item instanceof TestElement) 405 { 406 return new TestElementProperty( 407 ((TestElement) item).getPropertyAsString(TestElement.NAME), 408 (TestElement) item); 409 } 410 else if (item instanceof Collection ) 411 { 412 return new CollectionProperty( 413 "" + item.hashCode(), 414 (Collection ) item); 415 } 416 else if (item instanceof Map ) 417 { 418 return new MapProperty("" + item.hashCode(), (Map ) item); 419 } 420 else 421 { 422 JMeterProperty prop = getBlankProperty(); 423 prop.setName(item.toString()); 424 prop.setObjectValue(item); 425 return prop; 426 } 427 } 428 429 434 public String toString() 435 { 436 return getStringValue(); 438 } 439 440 443 public void mergeIn(JMeterProperty prop) 444 { 445 } 446 447 } 448 | Popular Tags |