1 19 package org.netbeans.test.editor.app.core; 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 TestCallAction 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 TestCallAction(int num) { 68 this("call"+Integer.toString(num)); 69 } 70 71 public TestCallAction(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 TestCallAction(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 } 308 isPerforming=true; 309 310 getLogger().startPerforming(); 311 long sleeps=0; 312 while (getLogger().isPerforming() && sleeps < TIMEOUT) { 314 try { 315 Thread.currentThread().sleep(250); 316 sleeps+=250; 317 } catch (Exception ex) { 318 } 319 } 320 System.err.println("Sleeps="+sleeps); 321 } 322 } else { 323 isPerforming = false; 324 } 325 } 326 }.start(); 328 } 329 330 private static final long TIMEOUT = 60 * 1000; 331 332 public void performAndWait() { 333 perform(); 334 335 long time = System.currentTimeMillis(); 336 337 while (isPerforming) { 338 long actualTime = System.currentTimeMillis(); 339 340 if ((actualTime - time) > TIMEOUT) { 341 System.err.println("CallAction "+getName()+" timeout expired "+TIMEOUT+" ms."); 342 getLogger().stopPerforming(); 343 getLogger().clear(); 344 return; 345 } 346 try { 347 Thread.currentThread().sleep(500); 348 } catch (Exception ex) { 349 } 350 } 351 } 352 353 public Vector getPerformedActions() { 354 TestStep set = readStepToCall(toSet); 355 TestStep call = readStepToCall(toCall); 356 Collection setActions = set == null ? new Vector (0) : set.getChildNodes(); 357 Collection callActions = call == null ? new Vector (0) : call.getChildNodes(); 358 Vector res = new Vector (setActions); 359 360 res.addAll(callActions); 361 return res; 362 } 363 364 public void stop() { 365 getLogger().stopPerforming(); 366 } 367 368 public void fromXML(Element node) throws BadPropertyNameException { 369 super.fromXML(node); 370 if ((input = ParsingUtils.loadString(node, INPUT)) == null) { 371 input=""; 372 } 373 if ((output = ParsingUtils.loadString(node, OUTPUT)) == null) { 374 output=""; 375 } 376 if ((toCall = ParsingUtils.loadString(node, TOCALL)) == null) { 377 toCall = ParsingUtils.fromSafeString(node.getAttribute(TOCALL)); if (toCall == null) { 379 toCall=""; 380 } 381 } 382 if ((toSet = ParsingUtils.loadString(node, TOSET)) == null) { 383 toSet = ParsingUtils.fromSafeString(node.getAttribute(TOSET)); if (toSet == null) { 385 toSet=""; 386 } 387 } 388 if ((comment = ParsingUtils.loadString(node, COMMENT)) == null) { 389 comment=""; 390 } 391 enable = ParsingUtils.readBoolean(node,ENABLE); 392 repeat = ParsingUtils.parseInt(node.getAttribute(REPEAT),1); 393 loggerDelay = ParsingUtils.parseInt(node.getAttribute(LOGGERDELAY),50); 394 } 395 396 public Properties getProperties() { 397 Properties ret=super.getProperties(); 398 ret.put(INPUT, new MultiLineStringProperty(input)); 399 ret.put(OUTPUT, new MultiLineStringProperty(output)); 400 ret.put(TOCALL, new ArrayProperty(toCall,getToCalls(false))); 401 ret.put(TOSET, new ArrayProperty(toSet,getToCalls(true))); 402 ret.put(COMMENT, new MultiLineStringProperty(comment)); 403 ret.put(ENABLE, new BooleanProperty(enable)); 404 ret.put(REPEAT, new IntegerProperty(repeat)); 405 ret.put(LOGGERDELAY, new IntegerProperty(loggerDelay)); 406 return ret; 407 } 408 409 public Object getProperty(String name) throws BadPropertyNameException { 410 if (name.compareTo(INPUT) == 0) { 411 return new MultiLineStringProperty(input); 412 } else if (name.compareTo(OUTPUT) == 0) { 413 return new MultiLineStringProperty(output); 414 } else if (name.compareTo(TOCALL) == 0) { 415 return new ArrayProperty(toCall,getToCalls(false)); 416 } else if (name.compareTo(TOSET) == 0) { 417 return new ArrayProperty(toSet,getToCalls(false)); 418 } else if (name.compareTo(COMMENT) == 0) { 419 return new MultiLineStringProperty(comment); 420 } else if (name.compareTo(ENABLE) == 0) { 421 return new BooleanProperty(enable); 422 } else if (name.compareTo(REPEAT) == 0) { 423 return new IntegerProperty(repeat); 424 } else if (name.compareTo(LOGGERDELAY) == 0) { 425 return new IntegerProperty(loggerDelay); 426 } else { 427 return super.getProperty(name); 428 } 429 } 430 431 public void setProperty(String name, Object value) throws BadPropertyNameException { 432 if (value == null) { 433 throw new NullPointerException (); 434 } else if (name.compareTo(INPUT) == 0) { 435 setInput(((MultiLineStringProperty)(value)).getProperty()); 436 } else if (name.compareTo(OUTPUT) == 0) { 437 setOutput(((MultiLineStringProperty)(value)).getProperty()); 438 } else if (name.compareTo(TOCALL) == 0) { 439 setToCall(((ArrayProperty)(value)).getProperty()); 440 } else if (name.compareTo(TOSET) == 0) { 441 setToSet(((ArrayProperty)(value)).getProperty()); 442 } else if (name.compareTo(COMMENT) == 0) { 443 setComment(((MultiLineStringProperty)(value)).getProperty()); 444 } else if (name.compareTo(ENABLE) == 0) { 445 setEnabled(((BooleanProperty)value).getValue()); 446 } else if (name.compareTo(REPEAT) == 0) { 447 setRepeat(((IntegerProperty)(value)).getValue()); 448 } else if (name.compareTo(LOGGERDELAY) == 0) { 449 setLoggerDelay(((IntegerProperty)(value)).getValue()); 450 } else { 451 super.setProperty(name, value); 452 } 453 } 454 455 protected void registerActions() { 456 super.registerActions(); 457 ActionsCache.getDefault().addNodeAction(getClass(), new TestGrabInputAction()); 458 ActionsCache.getDefault().addNodeAction(getClass(), new TestGrabOutputAction()); 459 } 460 } 461 | Popular Tags |