1 19 package org.netbeans.test.editor.suites.focus.data.testfiles.FocusTestPerformer; 20 21 import org.netbeans.test.editor.app.gui.*; 22 import java.beans.*; 23 import java.util.ArrayList ; 24 import javax.swing.SwingUtilities ; 25 import org.netbeans.test.editor.app.util.Scheduler; 26 import org.netbeans.test.editor.app.core.TestAction; 27 import org.w3c.dom.Element ; 28 29 import java.util.Vector ; 30 import java.util.Collection ; 31 32 import org.netbeans.test.editor.app.Main; 33 import org.netbeans.test.editor.app.core.properties.ArrayProperty; 34 import org.netbeans.test.editor.app.core.properties.BadPropertyNameException; 35 import org.netbeans.test.editor.app.core.properties.BooleanProperty; 36 import org.netbeans.test.editor.app.core.properties.IntegerProperty; 37 import org.netbeans.test.editor.app.core.properties.MultiLineStringProperty; 38 import org.netbeans.test.editor.app.core.properties.Properties; 39 import org.netbeans.test.editor.app.gui.actions.TestDeleteAction; 40 import org.netbeans.test.editor.app.gui.actions.TestGrabInputAction; 41 import org.netbeans.test.editor.app.gui.actions.TestGrabOutputAction; 42 import org.netbeans.test.editor.app.gui.tree.ActionsCache; 43 import org.netbeans.test.editor.app.util.ParsingUtils; 44 45 50 public class Test extends TestAction { 51 52 public static final String INPUT="Input"; 53 public static final String OUTPUT="Output"; 54 public static final String COMMENT="Comment"; 55 public static final String TOCALL="Call"; 56 public static final String TOSET="Set"; 57 public static final String ENABLE="Enable"; 58 public static final String REPEAT="Repeat"; 59 public static final String LOGGERDELAY="Delay"; 60 61 private String input,output,toCall,toSet,comment; 62 private int repeat,loggerDelay; 63 64 private boolean enable; 65 66 67 public Test(int num) { 68 this("call"+Integer.toString(num)); 69 } 70 71 public Test(String name) { 72 super(name); 73 input=""; 74 output=""; 75 toCall=""; 76 toSet=""; 77 comment=""; 78 enable=true; 79 loggerDelay=50; 80 repeat=1; 81 } 82 83 public Test(Element node) { 84 super(node); 85 86 if ((input = ParsingUtils.loadString(node, INPUT)) == null) { 87 input=""; 88 } 89 if ((output = ParsingUtils.loadString(node, OUTPUT)) == null) { 90 output=""; 91 } 92 if ((toCall = ParsingUtils.loadString(node, TOCALL)) == null) { 93 toCall = ParsingUtils.fromSafeString(node.getAttribute(TOCALL)); if (toCall == null) { 95 toCall=""; 96 } 97 } 98 if ((toSet = ParsingUtils.loadString(node, TOSET)) == null) { 99 toSet = ParsingUtils.fromSafeString(node.getAttribute(TOSET)); if (toSet == null) { 101 toSet=""; 102 } 103 } 104 if ((comment = ParsingUtils.loadString(node, COMMENT)) == null) { 105 comment=""; 106 } 107 enable = ParsingUtils.readBoolean(node,ENABLE); 108 repeat = ParsingUtils.parseInt(node.getAttribute(REPEAT),1); 109 loggerDelay = ParsingUtils.parseInt(node.getAttribute(LOGGERDELAY),50); 110 } 111 112 public Element toXML(Element node) { 113 node = super.toXML(node); 114 node = ParsingUtils.saveString(node, INPUT, input); 115 node = ParsingUtils.saveString(node, OUTPUT, output); 116 node = ParsingUtils.saveString(node, COMMENT, comment); 117 node.setAttribute(TOCALL, ParsingUtils.toSafeString(toCall)); 118 node.setAttribute(TOSET, ParsingUtils.toSafeString(toSet)); 119 node.setAttribute(ENABLE, enable ? "true" : "false"); 120 node.setAttribute(REPEAT, Integer.toString(repeat)); 121 node.setAttribute(LOGGERDELAY, Integer.toString(loggerDelay)); 122 return node; 123 } 124 125 public void setRepeat(int i) { 126 int oldValue = repeat; 127 repeat = i; 128 firePropertyChange(REPEAT,new Integer (oldValue),new Integer (repeat)); 129 } 130 131 public int getRepeat() { 132 return repeat; 133 } 134 135 public void setLoggerDelay(int value) { 136 int oldValue = repeat; 137 loggerDelay = value; 138 firePropertyChange(LOGGERDELAY,new Integer (oldValue),new Integer (loggerDelay)); 139 } 140 141 public int getLoggerDelay() { 142 return loggerDelay; 143 } 144 145 public void setInput(String value) { 146 String oldValue = input; 147 input = value; 148 firePropertyChange(INPUT, oldValue, input); 149 } 150 151 public String getInput() { 152 return input; 153 } 154 155 public void setOutput(String value) { 156 String oldValue = output; 157 output = value; 158 firePropertyChange(OUTPUT, oldValue, output); 159 } 160 161 public String getOutput() { 162 return output; 163 } 164 165 public void setToCall(String value) { 166 String oldValue = toCall; 167 toCall = value; 168 firePropertyChange(TOCALL, oldValue, toCall); 169 } 170 171 public String getToCall() { 172 return toCall; 173 } 174 175 public void setToSet(String value) { 176 String oldValue = toSet; 177 toSet = value; 178 firePropertyChange(TOSET, oldValue, toSet); 179 } 180 181 public String getToSet() { 182 return toSet; 183 } 184 185 public void setComment(String value) { 186 String oldValue = comment; 187 comment = value; 188 firePropertyChange(COMMENT, oldValue, comment); 189 } 190 191 public String getComment() { 192 return comment; 193 } 194 195 public void setEnabled(boolean value) { 196 boolean oldValue = enable; 197 enable = value; 198 firePropertyChange(ENABLE, oldValue ? Boolean.TRUE : Boolean.FALSE, enable ? Boolean.TRUE : Boolean.FALSE); 199 } 200 201 public boolean isEnable() { 202 return enable; 203 } 204 205 private String [] getToCalls(boolean empty) { 206 TestStep st; 207 ArrayList lst=new ArrayList (); 208 209 if (empty) 210 lst.add(""); 211 for(int i=0;i < owner.getChildCount();i++) { 212 if (owner.get(i) instanceof TestStep) { 213 st=(TestStep)(owner.get(i)); 214 lst.add(st.getName()); 215 } 216 } 217 return (String [])(lst.toArray(new String [] {})); 218 } 219 220 private TestStep readStepToCall(String toCall) { 221 for(int i=0;i < owner.getChildCount();i++) { 222 TestNode n = owner.get(i); 223 if (n == null) 224 System.err.println("Node: "+toCall+" got from owner is null!"); 225 else { 226 if (n.getName().equals(toCall) && n instanceof TestStep) { 227 return (TestStep)n; 228 } 229 } 230 } 231 return null; 232 } 233 234 public void grabInput() { 235 Scheduler.getDefault().addTask(new Thread () { 236 public void run() { 237 String old=input; 238 input=Main.frame.getEditor().getText(); 239 firePropertyChange(INPUT,old ,input ); 240 } 241 }); 242 } 243 244 public void grabOutput() { 245 Scheduler.getDefault().addTask(new Thread () { 246 public void run() { 247 String old=output; 248 output=Main.frame.getEditor().getText(); 249 firePropertyChange(INPUT,old ,output ); 250 } 251 }); 252 } 253 254 long time=0; 255 long memory=0; 256 257 public void perform() { 258 if (!enable) return; 259 System.err.println("Call action: "+name+" starts performing."); 260 System.err.println(">>>>>Comment: "+comment); 261 isPerforming=true; 262 263 new Thread () { 265 public void run() { 266 if (!enable) return; 267 TestStep call; 268 TestStep set; 269 Main.frame.getEditor().grabFocus(); 270 Main.frame.getEditor().requestFocus(); 271 call=readStepToCall(toCall); 272 set=readStepToCall(toSet); 273 if (set != null) { 274 set.perform(); 275 } 276 if (call != null) { 277 getLogger().setDelay(getLoggerDelay()); 278 time=System.currentTimeMillis(); 279 memory=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory(); 280 for(int i=0;i < repeat;i++) { 281 getLogger().clear(); 282 getLogger().loadActions(call); 283 Main.frame.getEditor().setText(input); 284 if (i == repeat-1) { 285 getLogger().addPropertyChangeListener(new PropertyChangeListener() { 286 public void propertyChange(final java.beans.PropertyChangeEvent p1) { 287 if (p1.getPropertyName().compareTo(Logger.PERFORMING) == 0) { 288 if (!((Boolean )(p1.getNewValue())).booleanValue()) { 289 time=System.currentTimeMillis()-time; 290 memory=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()-memory; 291 String content=Main.frame.getEditor().getText(); 292 System.err.println("Call action: "+name+" finished performing"); 293 if (!Test.isTesting()) { 294 if (content.compareTo(output) != 0 ) 295 System.err.println("***************** Call action: "+name+" error in outputs comparation. ******************"); 296 } else { 297 System.out.print(content); 298 } 299 System.err.println("Test time="+(time/1000.0)+" (s)."); 300 System.err.println("Test memory="+(memory/1024.0)+" (KB)."); 301 getLogger().removePropertyChangeListener(this); 302 isPerforming=false; 303 } 304 } 305 } 306 }); 307 } else { 308 getLogger().addPropertyChangeListener(new PropertyChangeListener() { 309 public void propertyChange(final java.beans.PropertyChangeEvent p1) { 310 if (p1.getPropertyName().compareTo(Logger.PERFORMING) == 0) { 311 if (!((Boolean )(p1.getNewValue())).booleanValue()) { 312 getLogger().removePropertyChangeListener(this); 313 isPerforming=false; 314 } 315 } 316 } 317 }); 318 } 319 isPerforming=true; 320 321 getLogger().startPerforming(); 322 long sleeps=0; 323 while (isPerforming && sleeps < 600000) { try { 325 Thread.currentThread().sleep(250); 326 sleeps+=250; 327 } catch (Exception ex) { 328 } 329 } 330 System.err.println("Sleeps="+sleeps); 331 } 332 } else { 333 isPerforming = false; 334 } 335 } 336 }.start(); 338 } 339 340 private static final long TIMEOUT = 60 * 1000; 341 342 public void performAndWait() { 343 perform(); 344 345 long time = System.currentTimeMillis(); 346 347 while (isPerforming) { 348 long actualTime = System.currentTimeMillis(); 349 350 if ((actualTime - time) > TIMEOUT) { 351 return; 352 } 353 Thread.yield(); 354 } 355 } 356 357 public Vector getPerformedActions() { 358 TestStep set = readStepToCall(toSet); 359 TestStep call = readStepToCall(toCall); 360 Collection setActions = set == null ? new Vector (0) : set.getChildNodes(); 361 Collection callActions = call == null ? new Vector (0) : call.getChildNodes(); 362 Vector res = new Vector (setActions); 363 364 res.addAll(callActions); 365 return res; 366 } 367 368 public void stop() { 369 getLogger().stopPerforming(); 370 } 371 372 public void fromXML(Element node) throws BadPropertyNameException { 373 super.fromXML(node); 374 if ((input = ParsingUtils.loadString(node, INPUT)) == null) { 375 input=""; 376 } 377 if ((output = ParsingUtils.loadString(node, OUTPUT)) == null) { 378 output=""; 379 } 380 if ((toCall = ParsingUtils.loadString(node, TOCALL)) == null) { 381 toCall = ParsingUtils.fromSafeString(node.getAttribute(TOCALL)); if (toCall == null) { 383 toCall=""; 384 } 385 } 386 if ((toSet = ParsingUtils.loadString(node, TOSET)) == null) { 387 toSet = ParsingUtils.fromSafeString(node.getAttribute(TOSET)); if (toSet == null) { 389 toSet=""; 390 } 391 } 392 if ((comment = ParsingUtils.loadString(node, COMMENT)) == null) { 393 comment=""; 394 } 395 enable = ParsingUtils.readBoolean(node,ENABLE); 396 repeat = ParsingUtils.parseInt(node.getAttribute(REPEAT),1); 397 loggerDelay = ParsingUtils.parseInt(node.getAttribute(LOGGERDELAY),50); 398 } 399 400 public Properties getProperties() { 401 Properties ret=super.getProperties(); 402 ret.put(INPUT, new MultiLineStringProperty(input)); 403 ret.put(OUTPUT, new MultiLineStringProperty(output)); 404 ret.put(TOCALL, new ArrayProperty(toCall,getToCalls(false))); 405 ret.put(TOSET, new ArrayProperty(toSet,getToCalls(true))); 406 ret.put(COMMENT, new MultiLineStringProperty(comment)); 407 ret.put(ENABLE, new BooleanProperty(enable)); 408 ret.put(REPEAT, new IntegerProperty(repeat)); 409 ret.put(LOGGERDELAY, new IntegerProperty(loggerDelay)); 410 return ret; 411 } 412 413 public Object getProperty(String name) throws BadPropertyNameException { 414 if (name.compareTo(INPUT) == 0) { 415 return new MultiLineStringProperty(input); 416 } else if (name.compareTo(OUTPUT) == 0) { 417 return new MultiLineStringProperty(output); 418 } else if (name.compareTo(TOCALL) == 0) { 419 return new ArrayProperty(toCall,getToCalls(false)); 420 } else if (name.compareTo(TOSET) == 0) { 421 return new ArrayProperty(toSet,getToCalls(false)); 422 } else if (name.compareTo(COMMENT) == 0) { 423 return new MultiLineStringProperty(comment); 424 } else if (name.compareTo(ENABLE) == 0) { 425 return new BooleanProperty(enable); 426 } else if (name.compareTo(REPEAT) == 0) { 427 return new IntegerProperty(repeat); 428 } else if (name.compareTo(LOGGERDELAY) == 0) { 429 return new IntegerProperty(loggerDelay); 430 } else { 431 return super.getProperty(name); 432 } 433 } 434 435 public void setProperty(String name, Object value) throws BadPropertyNameException { 436 if (value == null) { 437 throw new NullPointerException (); 438 } else if (name.compareTo(INPUT) == 0) { 439 setInput(((MultiLineStringProperty)(value)).getProperty()); 440 } else if (name.compareTo(OUTPUT) == 0) { 441 setOutput(((MultiLineStringProperty)(value)).getProperty()); 442 } else if (name.compareTo(TOCALL) == 0) { 443 setToCall(((ArrayProperty)(value)).getProperty()); 444 } else if (name.compareTo(TOSET) == 0) { 445 setToSet(((ArrayProperty)(value)).getProperty()); 446 } else if (name.compareTo(COMMENT) == 0) { 447 setComment(((MultiLineStringProperty)(value)).getProperty()); 448 } else if (name.compareTo(ENABLE) == 0) { 449 setEnabled(((BooleanProperty)value).getValue()); 450 } else if (name.compareTo(REPEAT) == 0) { 451 setRepeat(((IntegerProperty)(value)).getValue()); 452 } else if (name.compareTo(LOGGERDELAY) == 0) { 453 setLoggerDelay(((IntegerProperty)(value)).getValue()); 454 } else { 455 super.setProperty(name, value); 456 } 457 } 458 459 protected void registerActions() { 460 super.registerActions(); 461 ActionsCache.getDefault().addNodeAction(getClass(), new TestGrabInputAction()); 462 ActionsCache.getDefault().addNodeAction(getClass(), new TestGrabOutputAction()); 463 } 464 } 465 | Popular Tags |