26 27 import java.io.File; 28 import java.io.FileInputStream; 29 import java.io.IOException; 30 import java.util.ArrayList; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Properties; 34 import java.util.Set; 35 import org.jemmy.JemmyException; 36 import org.jemmy.action.ActionExecutor; 37 import org.jemmy.action.DefaultExecutor; 38 import org.jemmy.control.Wrap; 39 import org.jemmy.image.ImageCapturer; 40 import org.jemmy.image.ImageLoader; 41 import org.jemmy.input.CharBindingMap; 42 import org.jemmy.interfaces.ControlInterfaceFactory; 43 import org.jemmy.timing.Waiter; 44 45 /** 46 * 47 * @author shura, mrkam, erikgreijus 48 */ 49 public class Environment { 50 51 /** 52 * 53 */ 54 public static final String JEMMY_PROPERTIES_FILE_PROPERTY = "jemmy.properties"; 55 public static final String TIMEOUTS_FILE_PROPERTY = "timeouts"; 56 /** 57 * Information output for Environment class 58 */ 59 public static final String OUTPUT = Environment.class.getName() + ".OUTPUT"; 60 private final static Environment env = new Environment(null); 61 62 /** 63 * 64 * @return 65 */ 66 public static Environment getEnvironment() { 67 return env; 68 } 69 70 static { 71 env.setOutput(new TestOut(System.in, System.out, System.err)); 72 env.setExecutor(new DefaultExecutor()); 73 } 74 private HashMap<PropertyKey, Object> environment = new HashMap<PropertyKey, Object>(); 75 private Environment parent; 76 77 /** 78 * 79 * @param parent 80 */ 81 public Environment(Environment parent) { 82 this.parent = parent; 83 environment = new HashMap<PropertyKey, Object>(); 84 if (parent == null) { 85 loadProperties(System.getProperty(JEMMY_PROPERTIES_FILE_PROPERTY)); 86 } 87 } 88 89 /** 90 * 91 */ 92 public Environment() { 93 this(getEnvironment()); 94 } 95 96 /** 97 * 98 * @return 99 */ 100 public Environment getParentEnvironment() { 101 return parent; 102 } 103 104 /** 105 * 106 * @param parent 107 */ 108 public void setParentEnvironment(Environment parent) { 109 this.parent = parent; 110 } 111 112 public void loadProperties(String propFileName) { 113 if (propFileName == null || propFileName.length() == 0) { 114 propFileName = System.getProperty("user.home") + File.separator + ".jemmy.properties"; 115 } 116 File propFile = new File(propFileName); 117 System.out.println("Loading jemmy properties from " + propFile); 118 if (propFile.exists()) { 119 Properties props = new Properties(); 120 try { 121 props.load(new FileInputStream(propFile)); 122 } catch (IOException ex) { 123 throw new JemmyException("Unable to load properties", ex, propFileName); 124 } 125 for (String k : props.stringPropertyNames()) { 126 if (k.equals(TIMEOUTS_FILE_PROPERTY)) { 127 loadTimeouts(propFile.getParentFile(), props.getProperty(k)); 134 } 135 } 136 137 private void loadTimeouts(File propDir, String file) { 138 File timeoutsFile = new File(file); 139 if (!timeoutsFile.isAbsolute()) { 140 timeoutsFile = new File(propDir.getAbsolutePath() + File.separator + file); 141 } 142 System.out.println("Loading timeouts from " + timeoutsFile.getAbsolutePath()); 143 try { 144 Properties timeouts = new Properties(); 145 timeouts.load(new FileInputStream(timeoutsFile)); 146 for (String k : timeouts.stringPropertyNames()) { 147 setTimeout(k, Long.parseLong(timeouts.getProperty(k))); 148 } 149 } catch (IOException ex) { 150 throw new JemmyException("Unable to load timeouts", ex, timeoutsFile.getAbsolutePath()); 151 } 152 } 153 154 /** 155 * 156 * @param cls 157 * @return 158 */ 159 public List<?> get(Class cls) { 160 Set<PropertyKey> all = environment.keySet(); 161 ArrayList<Object> result = new ArrayList<Object>(); 162 for (PropertyKey key : all) { 163 if (key.getCls().equals(cls)) { 164 result.add(environment.get(key)); 165 } 166 } 167 return result; 168 } 169 170 /** 171 * 172 * @param defaultExecutor 173 * @return 174 */ 175 public ActionExecutor setExecutor(ActionExecutor defaultExecutor) { 176 return (ActionExecutor) setProperty(ActionExecutor.class, defaultExecutor); 177 } 178 179 /** 180 * 181 * @return 182 */ 183 public ActionExecutor getExecutor() { 184 ActionExecutor res = (ActionExecutor) getProperty(ActionExecutor.class); 185 if (res == null) { 186 String executorClassName = (String) getProperty(ActionExecutor.ACTION_EXECUTOR_PROPERTY); 187 try { 188 res = ActionExecutor.class.cast(Class.forName(executorClassName).newInstance()); 189 setExecutor(res); 190 } catch (InstantiationException ex) { 191 throw new JemmyException("Unable to instantiate executor ", ex, executorClassName); 192 } catch (IllegalAccessException ex) { 193 throw new JemmyException("Unable to instantiate executor ", ex, executorClassName); 194 } catch (ClassNotFoundException ex) { 195 throw new JemmyException("No executorclass ", ex, executorClassName); 196 } 197 } 198 return res; 199 } 200 201 public <T> T setProperty(Class<T> cls, Object ref, T obj) { 202 return setProperty(new PropertyKey<T>(cls, ref), obj); 208 209 private <T> T getProperty(Class<T> cls, Object ref) { 210 return getProperty(cls, ref, null); 211 } 212 213 @SuppressWarnings("unchecked") 214 public <T> T getProperty(Class cls, Object ref, T defaultValue) { 215 for (PropertyKey pk : environment.keySet()) { 216 if (pk.equals(new PropertyKey(cls, ref))) { 217 return (T) environment.get(pk); 218 } 219 } 220 if (getParentEnvironment() != null) { 221 return getParentEnvironment().getProperty(cls, ref, defaultValue); 222 } else { 223 return defaultValue; 224 } 225 } 226 227 /** 228 * 229 * @param <T> 230 * @param cls 231 * @param obj if null then property is removed 232 * @return 233 */ 234 public <T> T setProperty(Class<T> cls, T obj) { 235 return setProperty(cls, null, obj); 236 } 237 238 /** 239 * 240 * @param <T> 241 * @param cls 242 * @param obj if null then property is removed 243 * @return 244 */ 245 public <T> T setPropertyIfNotSet(Class<T> cls, T obj) { 246 return setPropertyIfNotSet(cls, null, obj); 247 } 248 249 /** 250 * 251 * @param <T> 252 * @param cls 253 * @return 254 */ 255 public <T> T getProperty(Class<T> cls) { 256 return getProperty(cls, null); 257 } 258 259 /** 260 * 261 * @param name 262 * @param obj if null then property is removed 263 * @return 264 */ 265 public Object setProperty(String name, Object obj) { 266 return setProperty(Object.class, name, obj); 267 } 268 269 /** 270 * 271 * @param name 272 * @param obj 273 * @return 274 */ 275 public Object setPropertyIfNotSet(String name, Object obj) { 276 return setPropertyIfNotSet(Object.class, name, obj); 277 } 278 279 /** 280 * 281 * @param name 282 * @return 283 */ 284 public Object getProperty(String name) { 285 return getProperty(Object.class, name); 286 } 287 288 /** 289 * 290 * @param name 291 * @param defaultValue 292 * @return 293 */ 294 public Object getProperty(String name, Object defaultValue) { 295 return getProperty(Environment.class, name, defaultValue); 296 } 297 298 private <T> T setProperty(PropertyKey<T> key, Object value) { 299 if (value == null) { 300 return key.cls.cast(environment.remove(key)); 301 } else { 302 return key.cls.cast(environment.put(key, value)); 303 } 304 } 305 306 private <T> T setPropertyIfNotSet(PropertyKey<T> key, T value) { 307 if (getParentEnvironment() != null) { 308 T res = key.cls.cast(getParentEnvironment().getProperty(key)); 309 if (res != null) { 310 return res; 311 } 312 } 313 T res = key.cls.cast(environment.get(key)); 314 if (res == null) { 315 return key.cls.cast(environment.put(key, value)); 316 } else { 317 return res; 318 } 319 } 320 321 private Object getProperty(PropertyKey key) { 322 return environment.get(key); 323 } 324 325 /** 326 * 327 * @param out 328 * @return 329 */ 330 public TestOut setOutput(TestOut out) { 331 return (TestOut) setProperty(TestOut.class, out); 332 } 333 334 /** 335 * 336 * @return 337 */ 338 public TestOut getOutput() { 339 return (TestOut) getProperty(TestOut.class); 340 } 341 342 /** 343 * Set some specific output. All classes which provide output should use 344 * some specific outputs. Please consult javadoc for a class in question. 345 * Use <code>null</code> to unset the property. 346 * 347 * @param outputName 348 * @param out 349 * @return 350 */ 351 public TestOut setOutput(String outputName, TestOut out) { 352 return (TestOut) setProperty(TestOut.class, outputName, out); 353 } 354 355 /** 356 * Initializes some specific output only if it is not yet set. 357 * 358 * @param outputName 359 * @param out 360 * @return 361 */ 362 public TestOut initOutput(String outputName, TestOut out) { 363 TestOut res = (TestOut) getProperty(TestOut.class, outputName); 364 if (res == null) { 365 return setOutput(outputName, out); 366 } else { 367 return res; 368 } 369 } 370 371 /** 372 * Get's a specific output. If nothing assigned, returns 373 * <code>getOutput()</code> 374 * 375 * @param outputName 376 * @return 377 */ 378 public TestOut getOutput(String outputName) { 379 TestOut res = (TestOut) getProperty(TestOut.class, outputName); 380 return (res != null) ? res : getOutput(); 381 } 382 383 /** 384 * 385 * @param timeout 386 * @return 387 */ 388 public Waiter getWaiter(Timeout timeout) { 389 return getWaiter(timeout.getName()); 390 } 391 392 /** 393 * 394 * @param timeoutName 395 * @return 396 */ 397 public Waiter getWaiter(String timeoutName) { 398 return new Waiter(getTimeout(timeoutName)); 399 } 400 401 /** 402 * 403 * @param timeout 404 * @return 405 */ 406 public Timeout getTimeout(Timeout timeout) { 407 return getTimeout(timeout.getName()); 408 } 409 410 /** 411 * 412 * @param name 413 * @return 414 */ 415 public Timeout getTimeout(String name) { 416 return (Timeout) getProperty(Timeout.class, name); 417 } 418 419 /** 420 * Sets timeout. 421 * 422 * @param timeout Timeout to set. 423 * @return replaced timeout if it was already set. 424 */ 425 public Timeout setTimeout(Timeout timeout) { 426 return (Timeout) setProperty(Timeout.class, timeout.getName(), timeout); 427 } 428 429 /** 430 * Initializes timeout only if it is not set. 431 * 432 * @param timeout Timeout to set. 433 * @return replaced timeout if it was already set. 434 */ 445 * @param timeout Timeout object instance which identifies the name of the 446 * timeout to set. 447 * @param value new value for the timout. 448 * @return replaced timeout if it was already set. 449 */ 450 public Timeout setTimeout(Timeout timeout, long value) { 451 return setTimeout(timeout.getName(), value); 452 } 453 454 /** 455 * Sets new value for the timeout. 456 * 457 * @param name Name of the timeout. 458 * @param value Value of the timeout. 459 * @return replaced timeout if it was already set. 460 */ 461 public Timeout setTimeout(String name, long value) { 462 return setTimeout(new Timeout(name, value)); 463 } 464 465 /** 466 * 467 * @return 468 */ 469 public CharBindingMap getBindingMap() { 470 return (CharBindingMap) getProperty(CharBindingMap.class); 471 } 472 473 /** 474 * 475 * @param map 476 * @return 477 */ 478 public CharBindingMap setBindingMap(CharBindingMap map) { 479 return (CharBindingMap) setProperty(CharBindingMap.class, map); 480 } 481 482 /** 483 * 484 * @return 485 */ 486 public ImageLoader getImageLoader() { 487 ImageLoader res = (ImageLoader) getProperty(ImageLoader.class); 488 if (res == null) { 489 String loaderClass = (String) getProperty(Wrap.IMAGE_LOADER_PROPERTY); 490 if (loaderClass == null) { 491 throw new IllegalStateException("No image loader provided!"); 492 } 493 try { 494 res = ImageLoader.class.cast(Class.forName(String.class.cast(loaderClass)).newInstance()); 495 setImageLoader(res); 496 } catch (InstantiationException ex) { 497 throw new JemmyException("Unable to instantiate image loader ", ex, loaderClass); 498 } catch (IllegalAccessException ex) { 499 throw new JemmyException("Unable to instantiate image loader ", ex, loaderClass); 500 } catch (ClassNotFoundException ex) { 501 throw new JemmyException("No image loader class ", ex, loaderClass); 502 } 503 } 504 return res; 505 } 506 507 /** 508 * 509 * @return 510 */ 511 public ImageCapturer getImageCapturer() { 512 ImageCapturer res = (ImageCapturer) getProperty(ImageCapturer.class); 513 if (res == null) { 514 String capturerClass = (String) getProperty(Wrap.IMAGE_CAPTURER_PROPERTY); 515 if (capturerClass == null) { 516 throw new IllegalStateException("No image capturer provided!"); 517 } 518 try { 519 res = ImageCapturer.class.cast(Class.forName(String.class.cast(capturerClass)).newInstance()); 520 setImageCapturer(res); 521 } catch (InstantiationException ex) { 522 throw new JemmyException("Unable to instantiate image capturer ", ex, capturerClass); 523 } catch (IllegalAccessException ex) { 524 throw new JemmyException("Unable to instantiate image capturer ", ex, capturerClass); 525 } catch (ClassNotFoundException ex) { 526 throw new JemmyException("No image capturer class ", ex, capturerClass); 527 } 528 } 529 return res; 530 } 531 532 /** 533 * 534 * @param imageLoader 535 * @return 536 */ 537 public ImageLoader setImageLoader(ImageLoader imageLoader) { 538 return (ImageLoader) setProperty(ImageLoader.class, imageLoader); 539 } 540 541 /** 542 * 543 * @param imageCapturer 544 * @return 545 */ 546 public ImageCapturer setImageCapturer(ImageCapturer imageCapturer) { 547 getOutput(OUTPUT).println("ImageCapturer set to " + imageCapturer); 548 return (ImageCapturer) setProperty(ImageCapturer.class, imageCapturer); 549 } 550 551 /** 552 * 553 * @return 554 */ 555 public ControlInterfaceFactory getInputFactory() { 556 ControlInterfaceFactory res = (ControlInterfaceFactory) getProperty(ControlInterfaceFactory.class); 557 if (res == null) { 558 String factoryClass = (String) getProperty(Wrap.INPUT_FACTORY_PROPERTY); 559 if (factoryClass != null) { 560 try { 561 res = ControlInterfaceFactory.class.cast(Class.forName(String.class.cast(factoryClass)).newInstance()); 562 setInputFactory(res); 563 } catch (InstantiationException ex) { 564 throw new JemmyException("Unable to instantiate input factory", ex, factoryClass); 565 } catch (IllegalAccessException ex) { 566 throw new JemmyException("Unable to instantiate input factory", ex, factoryClass); 567 } catch (ClassNotFoundException ex) { 568 throw new JemmyException("Unable to load input factory", ex, factoryClass); 569 } 570 } 571 } 572 return res; 573 } 574 575 /** 576 * 577 * @param factory 578 * @return 579 */ 580 public ControlInterfaceFactory setInputFactory(ControlInterfaceFactory factory) { 581 getOutput(OUTPUT).println("Input factory set to " + factory); 582 return (ControlInterfaceFactory) setProperty(ControlInterfaceFactory.class, factory); 583 } 584 585 private static class PropertyKey<TYPE> { 586 587 private Class<TYPE> cls; 588 private Object ref; 589 590 public PropertyKey(Class<TYPE> cls, Object ref) { 591 this.cls = cls; 592 this.ref = ref; 593 } 594 595 private PropertyKey(Class<TYPE> cls) { 596 this(cls, null); 597 } 598 599 public Class<TYPE> getCls() { | 26 27 import java.io.File; 28 import java.io.FileInputStream; 29 import java.io.IOException; 30 import java.util.ArrayList; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Properties; 34 import java.util.Set; 35 import org.jemmy.JemmyException; 36 import org.jemmy.action.ActionExecutor; 37 import org.jemmy.action.DefaultExecutor; 38 import org.jemmy.control.Wrap; 39 import org.jemmy.image.ImageCapturer; 40 import org.jemmy.image.ImageLoader; 41 import org.jemmy.input.CharBindingMap; 42 import org.jemmy.interfaces.ControlInterfaceFactory; 43 import org.jemmy.timing.Waiter; 44 45 /** 46 * @author shura, mrkam, erikgreijus 47 */ 48 public class Environment { 49 public static final String JEMMY_PROPERTIES_FILE_PROPERTY = "jemmy.properties"; 50 public static final String TIMEOUTS_FILE_PROPERTY = "timeouts"; 51 /** 52 * Information output for Environment class 53 */ 54 public static final String OUTPUT = Environment.class.getName() + ".OUTPUT"; 55 private final static Environment env = new Environment(null); 56 57 public static Environment getEnvironment() { 58 return env; 59 } 60 61 static { 62 env.setOutput(new TestOut(System.in, System.out, System.err)); 63 env.setExecutor(new DefaultExecutor()); 64 } 65 private HashMap<PropertyKey, Object> environment = new HashMap<PropertyKey, Object>(); 66 private Environment parent; 67 68 public Environment(Environment parent) { 69 this.parent = parent; 70 environment = new HashMap<PropertyKey, Object>(); 71 if (parent == null) { 72 loadProperties(System.getProperty(JEMMY_PROPERTIES_FILE_PROPERTY)); 73 } 74 } 75 76 public Environment() { 77 this(getEnvironment()); 78 } 79 80 public Environment getParentEnvironment() { 81 return parent; 82 } 83 84 public void setParentEnvironment(Environment parent) { 85 this.parent = parent; 86 } 87 88 public void loadProperties(String propFileName) { 89 if (propFileName == null || propFileName.length() == 0) { 90 propFileName = System.getProperty("user.home") + File.separator + ".jemmy.properties"; 91 } 92 File propFile = new File(propFileName); 93 System.out.println("Loading jemmy properties from " + propFile); 94 if (propFile.exists()) { 95 Properties props = new Properties(); 96 try { 97 props.load(new FileInputStream(propFile)); 98 } catch (IOException ex) { 99 throw new JemmyException("Unable to load properties", ex, propFileName); 100 } 101 for (String k : props.stringPropertyNames()) { 102 if (k.equals(TIMEOUTS_FILE_PROPERTY)) { 103 loadTimeouts(propFile.getParentFile(), props.getProperty(k)); 110 } 111 } 112 113 private void loadTimeouts(File propDir, String file) { 114 File timeoutsFile = new File(file); 115 if (!timeoutsFile.isAbsolute()) { 116 timeoutsFile = new File(propDir.getAbsolutePath() + File.separator + file); 117 } 118 System.out.println("Loading timeouts from " + timeoutsFile.getAbsolutePath()); 119 try { 120 Properties timeouts = new Properties(); 121 timeouts.load(new FileInputStream(timeoutsFile)); 122 for (String k : timeouts.stringPropertyNames()) { 123 setTimeout(k, Long.parseLong(timeouts.getProperty(k))); 124 } 125 } catch (IOException ex) { 126 throw new JemmyException("Unable to load timeouts", ex, timeoutsFile.getAbsolutePath()); 127 } 128 } 129 130 public List<?> get(Class cls) { 131 Set<PropertyKey> all = environment.keySet(); 132 ArrayList<Object> result = new ArrayList<Object>(); 133 for (PropertyKey key : all) { 134 if (key.getCls().equals(cls)) { 135 result.add(environment.get(key)); 136 } 137 } 138 return result; 139 } 140 141 public ActionExecutor setExecutor(ActionExecutor defaultExecutor) { 142 return (ActionExecutor) setProperty(ActionExecutor.class, defaultExecutor); 143 } 144 145 public ActionExecutor getExecutor() { 146 ActionExecutor res = (ActionExecutor) getProperty(ActionExecutor.class); 147 if (res == null) { 148 String executorClassName = (String) getProperty(ActionExecutor.ACTION_EXECUTOR_PROPERTY); 149 try { 150 res = ActionExecutor.class.cast(Class.forName(executorClassName).newInstance()); 151 setExecutor(res); 152 } catch (InstantiationException ex) { 153 throw new JemmyException("Unable to instantiate executor ", ex, executorClassName); 154 } catch (IllegalAccessException ex) { 155 throw new JemmyException("Unable to instantiate executor ", ex, executorClassName); 156 } catch (ClassNotFoundException ex) { 157 throw new JemmyException("No executorclass ", ex, executorClassName); 158 } 159 } 160 return res; 161 } 162 163 public <T> T setProperty(Class<T> cls, Object ref, T obj) { 164 return setProperty(new PropertyKey<T>(cls, ref), obj); 170 171 private <T> T getProperty(Class<T> cls, Object ref) { 172 return getProperty(cls, ref, null); 173 } 174 175 @SuppressWarnings("unchecked") 176 public <T> T getProperty(Class cls, Object ref, T defaultValue) { 177 for (PropertyKey pk : environment.keySet()) { 178 if (pk.equals(new PropertyKey(cls, ref))) { 179 return (T) environment.get(pk); 180 } 181 } 182 if (getParentEnvironment() != null) { 183 return getParentEnvironment().getProperty(cls, ref, defaultValue); 184 } else { 185 return defaultValue; 186 } 187 } 188 189 /** 190 * @param <T> todo document 191 * @param cls todo document 192 * @param obj if null then property is removed 193 * @return todo document 194 */ 195 public <T> T setProperty(Class<T> cls, T obj) { 196 return setProperty(cls, null, obj); 197 } 198 199 /** 200 * @param <T> todo document 201 * @param cls todo document 202 * @param obj if null then property is removed 203 * @return todo document 204 */ 205 public <T> T setPropertyIfNotSet(Class<T> cls, T obj) { 206 return setPropertyIfNotSet(cls, null, obj); 207 } 208 209 public <T> T getProperty(Class<T> cls) { 210 return getProperty(cls, null); 211 } 212 213 /** 214 * @param name todo document 215 * @param obj if null then property is removed 216 * @return todo document 217 */ 218 public Object setProperty(String name, Object obj) { 219 return setProperty(Object.class, name, obj); 220 } 221 222 public Object setPropertyIfNotSet(String name, Object obj) { 223 return setPropertyIfNotSet(Object.class, name, obj); 224 } 225 226 public Object getProperty(String name) { 227 return getProperty(Object.class, name); 228 } 229 230 public Object getProperty(String name, Object defaultValue) { 231 return getProperty(Environment.class, name, defaultValue); 232 } 233 234 private <T> T setProperty(PropertyKey<T> key, Object value) { 235 if (value == null) { 236 return key.cls.cast(environment.remove(key)); 237 } else { 238 return key.cls.cast(environment.put(key, value)); 239 } 240 } 241 242 private <T> T setPropertyIfNotSet(PropertyKey<T> key, T value) { 243 if (getParentEnvironment() != null) { 244 T res = key.cls.cast(getParentEnvironment().getProperty(key)); 245 if (res != null) { 246 return res; 247 } 248 } 249 T res = key.cls.cast(environment.get(key)); 250 if (res == null) { 251 return key.cls.cast(environment.put(key, value)); 252 } else { 253 return res; 254 } 255 } 256 257 private Object getProperty(PropertyKey key) { 258 return environment.get(key); 259 } 260 261 public TestOut setOutput(TestOut out) { 262 return (TestOut) setProperty(TestOut.class, out); 263 } 264 265 public TestOut getOutput() { 266 return (TestOut) getProperty(TestOut.class); 267 } 268 269 /** 270 * Set some specific output. All classes which provide output should use 271 * some specific outputs. Please consult javadoc for a class in question. 272 * Use <code>null</code> to unset the property. 273 * 274 * @param outputName todo document 275 * @param out todo document 276 * @return todo document 277 */ 278 public TestOut setOutput(String outputName, TestOut out) { 279 return (TestOut) setProperty(TestOut.class, outputName, out); 280 } 281 282 /** 283 * Initializes some specific output only if it is not yet set. 284 * 285 * @param outputName todo document 286 * @param out todo document 287 * @return todo document 288 */ 289 public TestOut initOutput(String outputName, TestOut out) { 290 TestOut res = (TestOut) getProperty(TestOut.class, outputName); 291 if (res == null) { 292 return setOutput(outputName, out); 293 } else { 294 return res; 295 } 296 } 297 298 /** 299 * Get's a specific output. If nothing assigned, returns 300 * <code>getOutput()</code> 301 * 302 * @param outputName todo document 303 * @return todo document 304 */ 305 public TestOut getOutput(String outputName) { 306 TestOut res = (TestOut) getProperty(TestOut.class, outputName); 307 return (res != null) ? res : getOutput(); 308 } 309 310 public Waiter getWaiter(Timeout timeout) { 311 return getWaiter(timeout.getName()); 312 } 313 314 public Waiter getWaiter(String timeoutName) { 315 return new Waiter(getTimeout(timeoutName)); 316 } 317 318 public Timeout getTimeout(Timeout timeout) { 319 return getTimeout(timeout.getName()); 320 } 321 322 public Timeout getTimeout(String name) { 323 return (Timeout) getProperty(Timeout.class, name); 324 } 325 326 /** 327 * Sets timeout. 328 * 329 * @param timeout Timeout to set. 330 * @return replaced timeout if it was already set. 331 */ 332 public Timeout setTimeout(Timeout timeout) { 333 return (Timeout) setProperty(Timeout.class, timeout.getName(), timeout); 334 } 335 336 /** 337 * Initializes timeout only if it is not set. 338 * 339 * @param timeout Timeout to set. 340 * @return replaced timeout if it was already set. 341 */ 352 * @param timeout Timeout object instance which identifies the name of the 353 * timeout to set. 354 * @param value new value for the timout. 355 * @return replaced timeout if it was already set. 356 */ 357 public Timeout setTimeout(Timeout timeout, long value) { 358 return setTimeout(timeout.getName(), value); 359 } 360 361 /** 362 * Sets new value for the timeout. 363 * 364 * @param name Name of the timeout. 365 * @param value Value of the timeout. 366 * @return replaced timeout if it was already set. 367 */ 368 public Timeout setTimeout(String name, long value) { 369 return setTimeout(new Timeout(name, value)); 370 } 371 372 public CharBindingMap getBindingMap() { 373 return (CharBindingMap) getProperty(CharBindingMap.class); 374 } 375 376 public CharBindingMap setBindingMap(CharBindingMap map) { 377 return (CharBindingMap) setProperty(CharBindingMap.class, map); 378 } 379 380 public ImageLoader getImageLoader() { 381 ImageLoader res = (ImageLoader) getProperty(ImageLoader.class); 382 if (res == null) { 383 String loaderClass = (String) getProperty(Wrap.IMAGE_LOADER_PROPERTY); 384 if (loaderClass == null) { 385 throw new IllegalStateException("No image loader provided!"); 386 } 387 try { 388 res = ImageLoader.class.cast(Class.forName(String.class.cast(loaderClass)).newInstance()); 389 setImageLoader(res); 390 } catch (InstantiationException ex) { 391 throw new JemmyException("Unable to instantiate image loader ", ex, loaderClass); 392 } catch (IllegalAccessException ex) { 393 throw new JemmyException("Unable to instantiate image loader ", ex, loaderClass); 394 } catch (ClassNotFoundException ex) { 395 throw new JemmyException("No image loader class ", ex, loaderClass); 396 } 397 } 398 return res; 399 } 400 401 public ImageCapturer getImageCapturer() { 402 ImageCapturer res = (ImageCapturer) getProperty(ImageCapturer.class); 403 if (res == null) { 404 String capturerClass = (String) getProperty(Wrap.IMAGE_CAPTURER_PROPERTY); 405 if (capturerClass == null) { 406 throw new IllegalStateException("No image capturer provided!"); 407 } 408 try { 409 res = ImageCapturer.class.cast(Class.forName(String.class.cast(capturerClass)).newInstance()); 410 setImageCapturer(res); 411 } catch (InstantiationException ex) { 412 throw new JemmyException("Unable to instantiate image capturer ", ex, capturerClass); 413 } catch (IllegalAccessException ex) { 414 throw new JemmyException("Unable to instantiate image capturer ", ex, capturerClass); 415 } catch (ClassNotFoundException ex) { 416 throw new JemmyException("No image capturer class ", ex, capturerClass); 417 } 418 } 419 return res; 420 } 421 422 public ImageLoader setImageLoader(ImageLoader imageLoader) { 423 return (ImageLoader) setProperty(ImageLoader.class, imageLoader); 424 } 425 426 public ImageCapturer setImageCapturer(ImageCapturer imageCapturer) { 427 getOutput(OUTPUT).println("ImageCapturer set to " + imageCapturer); 428 return (ImageCapturer) setProperty(ImageCapturer.class, imageCapturer); 429 } 430 431 public ControlInterfaceFactory getInputFactory() { 432 ControlInterfaceFactory res = (ControlInterfaceFactory) getProperty(ControlInterfaceFactory.class); 433 if (res == null) { 434 String factoryClass = (String) getProperty(Wrap.INPUT_FACTORY_PROPERTY); 435 if (factoryClass != null) { 436 try { 437 res = ControlInterfaceFactory.class.cast(Class.forName(String.class.cast(factoryClass)).newInstance()); 438 setInputFactory(res); 439 } catch (InstantiationException ex) { 440 throw new JemmyException("Unable to instantiate input factory", ex, factoryClass); 441 } catch (IllegalAccessException ex) { 442 throw new JemmyException("Unable to instantiate input factory", ex, factoryClass); 443 } catch (ClassNotFoundException ex) { 444 throw new JemmyException("Unable to load input factory", ex, factoryClass); 445 } 446 } 447 } 448 return res; 449 } 450 451 public ControlInterfaceFactory setInputFactory(ControlInterfaceFactory factory) { 452 getOutput(OUTPUT).println("Input factory set to " + factory); 453 return (ControlInterfaceFactory) setProperty(ControlInterfaceFactory.class, factory); 454 } 455 456 private static class PropertyKey<TYPE> { 457 458 private Class<TYPE> cls; 459 private Object ref; 460 461 public PropertyKey(Class<TYPE> cls, Object ref) { 462 this.cls = cls; 463 this.ref = ref; 464 } 465 466 private PropertyKey(Class<TYPE> cls) { 467 this(cls, null); 468 } 469 470 public Class<TYPE> getCls() { |