Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 479   Methods: 30
NCLOC: 386   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ScriptTest.java 30% 93% 83.3% 89.2%
coverage coverage
 1   
 package abbot.script;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.io.*;
 5   
 import java.util.Iterator;
 6   
 
 7   
 import javax.swing.*;
 8   
 import java.awt.BorderLayout;
 9   
 import java.util.*;
 10   
 
 11   
 import junit.extensions.abbot.*;
 12   
 import junit.extensions.abbot.Timer;
 13   
 import org.jdom.Element;
 14   
 
 15   
 import abbot.i18n.Strings;
 16   
 import abbot.tester.Robot;
 17   
 
 18   
 /** 
 19   
  * Verify the script works as advertised.
 20   
  */
 21   
 public class ScriptTest extends ComponentTestFixture implements XMLConstants {
 22   
 
 23   
     private Script script;
 24   
     private static final String ENCODING = "UTF-8";
 25   
 
 26  19
     protected void setUp() {
 27  19
         script = new Script(getHierarchy());
 28  19
         ComponentReference.cacheOnCreation = false;
 29   
     }
 30   
 
 31  19
     protected void tearDown() {
 32  19
         ComponentReference.cacheOnCreation = true;
 33   
     }
 34   
 
 35  8
     public static Reader stringToReader(String script) throws Throwable {
 36  8
         ByteArrayInputStream ba =
 37   
             new ByteArrayInputStream(script.getBytes(ENCODING));
 38  8
         InputStreamReader reader = new InputStreamReader(ba, ENCODING);
 39  8
         return reader;
 40   
     }
 41   
 
 42  1
     public void testDefaultDescription() {
 43  1
         String filename = script.getFile().getName();
 44  1
         assertTrue("Default script description should omit the filename "
 45   
                    + "if the script is a temporary file: "
 46   
                    + script.getDescription(),
 47   
                    script.getDescription().indexOf(filename) == -1);
 48   
     }
 49   
     
 50   
     // need an automatic way to generate all component ref attributes
 51   
     // using a hand-coded array isn't any more reliable than the original
 52   
     // edits of abbot.xsd.
 53   
     /*
 54   
     public void testComponentValidation() throws Throwable {
 55   
         Frame f = new Frame(getName());
 56   
         ComponentReference ref = script.addComponent(f);
 57   
         Iterator iter = VALID_ATTRIBUTES.iterator();
 58   
         // Make sure our XSD has included ALL the valid cref attributes
 59   
         while (iter.hasNext()) {
 60   
             ref.setAttribute((String)iter.next(), "0");
 61   
         }
 62   
         script.save();
 63   
         script.load();
 64   
     }
 65   
     */
 66   
 
 67  1
     public void testLaunchAsUIContext() {
 68  1
         script.clear();
 69  1
         Launch launch = new Launch(script, getName(), "any.cls.name", "method", new String[] { "[]" });
 70  1
         script.addStep(launch);
 71  1
         assertEquals("Launch should be UIContext", launch, script.getUIContext());
 72   
     }
 73   
     
 74  1
     public void testFixtureAsUIContext() {
 75  1
         script.clear();
 76  1
         Fixture fixture = new Fixture(script, new HashMap());
 77  1
         script.addStep(fixture);
 78  1
         assertEquals("Fixture should be UIContext", fixture, script.getUIContext());
 79   
     }
 80   
 
 81  1
     public void testCatchInvalidScript() throws Throwable {
 82  1
         String bogus = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 83   
             + "<AWTTestScript desc=\"ignored\">\r\n"
 84   
             + "  <terminate/>\r\n"
 85   
             + "  <component id=\"id\"/>\r\n"
 86   
             + "</AWTTestScript>\r\n\r\n";
 87  1
         try {
 88  1
             script.load(stringToReader(bogus));
 89  0
             fail("Expected an invalid script exception");
 90   
         }
 91   
         catch(InvalidScriptException ise) {
 92   
         }
 93   
     }
 94   
 
 95  1
     public void testInValidLaunchLocation() throws Throwable {
 96  1
         String valid = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 97   
             + "<AWTTestScript>\r\n"
 98   
             + "  <sequence/>\r\n"
 99   
             + "  <launch class=\"none\" method=\"main\" args=\"[]\"/>\r\n"
 100   
             + "</AWTTestScript>\r\n\r\n";
 101  1
         try {
 102  1
             script.load(stringToReader(valid));
 103  0
             fail("A script with a launch step in other than position 0 should be detected as invalid");
 104   
         }
 105   
         catch(InvalidScriptException e) {
 106   
         }
 107   
     }
 108   
 
 109  1
     public void testTerminatePositionValidation() throws Throwable {
 110  1
         String valid = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 111   
             + "<AWTTestScript>\r\n"
 112   
             + "  <terminate/>\r\n"
 113   
             + "  <launch class=\"none\" method=\"main\" args=\"[]\"/>\r\n"
 114   
             + "</AWTTestScript>\r\n\r\n";
 115  1
         try {
 116  1
             script.load(stringToReader(valid));
 117  0
             fail("A script with a terminate step in other than last position should be detected as invalid");
 118   
         }
 119   
         catch(InvalidScriptException e) {
 120   
         }
 121   
     }
 122   
 
 123  1
     public void testFiles() throws Throwable {
 124  1
         File tmp = File.createTempFile(getName(), ".xml");
 125  1
         tmp.deleteOnExit();
 126  1
         script.setFile(tmp);
 127  1
         assertTrue("Script file should always be absolute",
 128   
                    script.getFile().isAbsolute());
 129  1
         assertTrue("Script directory should always be absolute",
 130   
                    script.getDirectory().isAbsolute());
 131  1
         assertEquals("Script filename should be absolute",
 132   
                      tmp.getPath(), script.getFilename());
 133   
 
 134  1
         script.setFile(new File(tmp.getName()));
 135  1
         script.setRelativeTo(tmp.getParentFile());
 136  1
         assertTrue("Script file should always be absolute",
 137   
                    script.getFile().isAbsolute());
 138  1
         assertTrue("Script directory should always be absolute",
 139   
                    script.getDirectory().isAbsolute());
 140  1
         assertEquals("Script filename should be relative",
 141   
                      tmp.getName(), script.getFilename());
 142   
     }
 143   
 
 144  1
     public void testDirty() throws Throwable {
 145  1
         script.save();
 146  1
         script.getFile().deleteOnExit();
 147  1
         script.load();
 148  1
         assertTrue("Script should not be dirty immediately after load",
 149   
                    !script.isDirty());
 150  1
         script.addStep(new Script(getHierarchy()));
 151  1
         assertTrue("Script should be dirty after modifications",
 152   
                    script.isDirty());
 153  1
         script.removeStep(0);
 154  1
         assertTrue("Script should not be dirty", !script.isDirty());
 155   
     }
 156   
 
 157   
     static final String ID = "button";
 158   
     static final String TEXT = "\u65E5\u008Aencoded";
 159   
     static final String CLASS = "javax.swing.JButton";
 160   
     private static final String unicodeData = 
 161   
         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 162   
         + "<AWTTestScript>\r\n"
 163   
         + "  <component class=\"" + CLASS + "\" id=\""
 164   
         + ID + "\" text=\"" + TEXT + "\" />\r\n"
 165   
         + "  <terminate />\r\n"
 166   
         + "</AWTTestScript>\r\n\r\n";
 167  1
     public void testWrite() throws Throwable {
 168  1
         StringWriter writer = new StringWriter();
 169  1
         Element el = new Element(XMLConstants.TAG_COMPONENT).
 170   
             setAttribute(XMLConstants.TAG_ID, ID).
 171   
             setAttribute(XMLConstants.TAG_TEXT, TEXT).
 172   
             setAttribute(XMLConstants.TAG_CLASS, CLASS);
 173  1
         ComponentReference ref = script.addComponentReference(el);
 174  1
         assertEquals("Incorrect tag encoding",
 175   
                      TEXT, ref.getAttribute(XMLConstants.TAG_TEXT));
 176  1
         script.addStep(new Terminate(script, (String)null));
 177  1
         script.save(writer);
 178   
 
 179   
         // Note that this is all Unicode, NOT UTF-8
 180  1
         assertEquals("Incorrect XML written", unicodeData, writer.toString());
 181   
     }
 182   
 
 183  1
     public void testRead() throws Throwable {
 184  1
         try {
 185  1
             script.load(stringToReader(unicodeData));
 186  1
             assertEquals("Wrong number of references",
 187   
                          1, script.getComponentReferences().size());
 188  1
             assertEquals("Wrong number of steps", 1, script.size());
 189  1
             ComponentReference ref = (ComponentReference)
 190   
                 script.getComponentReferences().iterator().next();
 191   
             // Ensure all attributes are saved with unicode
 192  1
             assertEquals("Wrong tag",
 193   
                          TEXT, ref.getAttribute(XMLConstants.TAG_TEXT));
 194   
         }
 195   
         catch(InvalidScriptException ise) {
 196  0
             fail(ise.getMessage());
 197   
         }
 198   
     }
 199   
 
 200   
     /** Ensure that a nested script maintains its own context. */
 201  1
     public void testNestedReference() throws Throwable {
 202  1
         Script nested = new Script(getHierarchy());
 203  1
         nested.load(stringToReader(unicodeData));
 204  1
         script.addStep(nested);
 205  1
         assertEquals("Wrong resolver", script, script.getResolver());
 206  1
         assertEquals("Wrong nested resolver", nested, nested.getResolver());
 207  1
         assertEquals("Wrong resolver for nested step",
 208   
                      nested, ((Step)nested.steps().get(0)).getResolver());
 209   
 
 210  1
         String nestedScriptData = 
 211   
             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 212   
             + "<AWTTestScript>\r\n"
 213   
             + "  <script filename=\"nested.xml\" >\r\n"
 214   
             + "  </script>\r\n"
 215   
             + "</AWTTestScript>\r\n\r\n";
 216  1
         script.load(stringToReader(nestedScriptData));
 217  1
         nested = (Script)script.steps().get(0);
 218  1
         assertEquals("Wrong resolver for nested script",
 219   
                      nested, nested.getResolver());
 220   
     }
 221   
 
 222   
     /** A nested script should have *only* its filename attribute saved. */
 223  1
     public void testSaveNestedScript() throws Throwable {
 224  1
         String fullNestedScript = 
 225   
             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 226   
             + "<AWTTestScript>\r\n"
 227   
             + "  <script filename=\"nested.xml\" >\r\n"
 228   
             + "    <comment desc=\"Filler\" />\r\n"
 229   
             + "  </script>\r\n"
 230   
             + "</AWTTestScript>\r\n\r\n";
 231  1
         String savedNestedScript = 
 232   
             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
 233   
             + "<AWTTestScript>\r\n"
 234   
             + "  <script filename=\"nested.xml\" />\r\n"
 235   
             + "</AWTTestScript>\r\n\r\n";
 236  1
         script.load(stringToReader(fullNestedScript));
 237  1
         StringWriter writer = new StringWriter();
 238  1
         script.save(writer);
 239  1
         assertEquals("Incorrectly saved",
 240   
                      savedNestedScript, writer.toString());
 241  1
         Script nested = (Script)script.steps().get(0);
 242  1
         assertEquals("Wrong relative directory", 
 243   
                      script.getDirectory(), nested.getRelativeTo());
 244   
     }
 245   
 
 246  1
     public void testSetFileRelative() {
 247  1
         String relativePath = "relative.xml";
 248  1
         String relativeDir = "relative";
 249  1
         String absolutePath = new File(relativePath).getAbsolutePath();
 250  1
         String absoluteDir = new File(relativeDir).getAbsolutePath();
 251   
 
 252  1
         script.setFile(new File(relativePath));
 253  1
         assertEquals("Wrong relative filename",
 254   
                      relativePath, script.getFilename());
 255  1
         assertEquals("Wrong relative dir",
 256   
                      new File(System.getProperty("user.dir")),
 257   
                      script.getRelativeTo());
 258  1
         assertEquals("Wrong file",
 259   
                      new File(script.getRelativeTo(), relativePath),
 260   
                      script.getFile());
 261   
 
 262  1
         script.setRelativeTo(new File(absoluteDir));
 263  1
         assertEquals("Wrong relative filename (1)",
 264   
                      relativePath, script.getFilename());
 265  1
         assertEquals("Wrong relative dir (1)",
 266   
                      new File(absoluteDir), script.getRelativeTo());
 267  1
         assertEquals("Wrong file (1)",
 268   
                      new File(absoluteDir, relativePath), script.getFile());
 269   
 
 270  1
         script.setRelativeTo(new File(relativeDir));
 271  1
         assertEquals("Wrong relative filename (2)",
 272   
                      relativePath, script.getFilename());
 273  1
         assertEquals("Wrong relative dir (2)",
 274   
                      new File(relativeDir), script.getRelativeTo());
 275  1
         assertEquals("Wrong file (2)",
 276   
                      new File(relativeDir, relativePath), script.getFile());
 277   
 
 278  1
         script.setFile(new File(absolutePath));
 279  1
         assertEquals("Wrong relative filename (3)",
 280   
                      absolutePath, script.getFilename());
 281  1
         assertEquals("Wrong relative dir (3)",
 282   
                      new File(relativeDir), script.getRelativeTo());
 283  1
         assertEquals("Wrong file (3)",
 284   
                      new File(absolutePath), script.getFile());
 285   
         
 286  1
         script.setFile(new File(absoluteDir, relativePath));
 287  1
         script.setRelativeTo(new File(absoluteDir));
 288  1
         assertEquals("Wrong relative filename (4)",
 289   
                      relativePath, script.getFilename());
 290  1
         assertEquals("Wrong relative dir (4)",
 291   
                      new File(absoluteDir), script.getRelativeTo());
 292  1
         assertEquals("Wrong file (4)",
 293   
                      new File(absoluteDir, relativePath), script.getFile());
 294   
     }
 295   
 
 296   
     /** Any given component, if added multiple times, should result in the
 297   
      * same component reference.
 298   
      */
 299  1
     public void testReferenceReuse() throws Throwable {
 300  1
         JTextField tf = new JTextField();
 301  1
         showFrame(tf);
 302   
 
 303  1
         ComponentReference ref = script.addComponent(tf);
 304  1
         assertEquals("New reference does not match the component"
 305   
                      + " from which it was created", tf, ref.getComponent());
 306   
 
 307  1
         ComponentReference ref2 = script.addComponent(tf);
 308  1
         assertEquals("Same component should always result in the same ref",
 309   
                      ref, ref2);
 310   
 
 311  1
         assertEquals("Reverse lookup of reference by component failed",
 312   
                      ref, script.getComponentReference(tf));
 313   
     }
 314   
 
 315   
     /** Any number of unique components should all result in unique
 316   
      * references.
 317   
      */
 318  1
     public void testUniqueReferences() {
 319  1
         JTextField tf1 = new JTextField();
 320  1
         JTextField tf2 = new JTextField();
 321  1
         JButton b1 = new JButton("Button");
 322  1
         JButton b2 = new JButton("Button");
 323  1
         JPanel pane = new JPanel();
 324  1
         pane.add(tf1);
 325  1
         pane.add(tf2);
 326  1
         pane.add(b1);
 327  1
         pane.add(b2);
 328  1
         showFrame(pane);
 329   
 
 330  1
         ComponentReference cr1 = script.addComponent(tf1);
 331  1
         ComponentReference cr2 = script.addComponent(tf2);
 332  1
         assertTrue("Should get two unique text field references", cr1 != cr2);
 333   
 
 334  1
         ComponentReference cr3 = script.addComponent(b1);
 335  1
         ComponentReference cr4 = script.addComponent(b2);
 336  1
         assertTrue("Should get two unique button references", cr3 != cr4);
 337   
     }
 338   
 
 339  1
     public void testReferenceOrdering() throws Throwable {
 340  1
         HashMap map = new HashMap();
 341  1
         ComponentReference cr1 =
 342   
             new ComponentReference(getResolver(), JButton.class, map);
 343  1
         ComponentReference cr2 =
 344   
             new ComponentReference(getResolver(), JButton.class, map);
 345  1
         ComponentReference cr3 =
 346   
             new ComponentReference(getResolver(), JButton.class, map);
 347  1
         Script s1 = new Script(getHierarchy());
 348  1
         Script s2 = new Script(getHierarchy());
 349  1
         s1.addComponentReference(cr1);
 350  1
         s1.addComponentReference(cr2);
 351  1
         s1.addComponentReference(cr3);
 352  1
         s2.addComponentReference(cr3);
 353  1
         s2.addComponentReference(cr2);
 354  1
         s2.addComponentReference(cr1);
 355   
 
 356  1
         Iterator iter1 = s1.getComponentReferences().iterator();
 357  1
         Iterator iter2 = s2.getComponentReferences().iterator();
 358  1
         while (iter1.hasNext()) {
 359  3
             assertTrue("Reference counts don't match", iter2.hasNext());
 360  3
             assertEquals("Reference ordering not maintained",
 361   
                          iter1.next(), iter2.next());
 362   
         }
 363   
     }
 364   
 
 365   
     /** When referencing a new reference's parent, make sure we don't map it
 366   
         inappropriately to an existing reference.
 367   
     */
 368  1
     public void testCreateProperWindowAncestor() {
 369  1
         JOptionPane pane = new JOptionPane();
 370  1
         Dialog d = pane.createDialog(null, getName());
 371  1
         showWindow(d);
 372  1
         ComponentReference ref1 = script.addComponent(d);
 373  1
         hideWindow(d);
 374  1
         final JFileChooser chooser = new JFileChooser();
 375  1
         SwingUtilities.invokeLater(new Runnable() {
 376  1
             public void run() {
 377  1
                 chooser.showOpenDialog(null);
 378   
             }
 379   
         });
 380   
         // Wait for the dialog to post
 381  1
         Window w;
 382  1
         Timer timer = new Timer();
 383  ?
         while ((w = SwingUtilities.getWindowAncestor(chooser)) == null) {
 384  0
             if (timer.elapsed() > 5000)
 385  0
                 fail("File chooser has no window");
 386  0
             getRobot().sleep();
 387   
         }
 388  1
         assertTrue("Wrong window selected: " + d, d != w);
 389   
 
 390  1
         timer.reset();
 391  1
         while (!w.isShowing()) {
 392  0
             if (timer.elapsed() > 5000)
 393  0
                 fail("File chooser window is not showing");
 394  0
             getRobot().sleep();
 395   
         }
 396   
 
 397   
         // NOTE: making the JFileChooser reference triggered a bug where this
 398   
         // reference's dialog/window would mistakenly use the existing dialog
 399   
         // reference (ref1) instead of creating a new one.
 400  1
         ComponentReference ref2 = script.addComponent(chooser);
 401  1
         ComponentReference ref3 = script.addComponent(w);
 402  1
         assertTrue("Dialog for file chooser " + ref2
 403   
                    + " should get a unique reference for " 
 404   
                    + Robot.toString(w) + ", instead got " + ref3, 
 405   
                    !ref1.equals(ref3));
 406   
     }
 407   
 
 408   
     private class DefaultStep extends Step {
 409  3
         public DefaultStep(Resolver r) { super(r, new HashMap()); }
 410  0
         public void runStep() { }
 411  0
         public String getXMLTag() { return "defaultstep"; }
 412  0
         public String getUsage() { return "blah"; }
 413  0
         public String getDefaultDescription() {
 414  0
             return "dummy step for " + getName();
 415   
         }
 416   
     }
 417   
 
 418  1
     public void testLineReporting() {
 419  1
         Script script = new Script(getHierarchy());
 420  1
         Step step = new DefaultStep(script);
 421  1
         script.addStep(step);
 422  1
         assertEquals("Wrong file reported for first step",
 423   
                      script.getFile(), Script.getFile(step));
 424  1
         assertEquals("Wrong line number for first step",
 425   
                      3, Script.getLine(step));
 426   
         
 427  1
         HashMap map = new HashMap();
 428  1
         ComponentReference cr1 =
 429   
             new ComponentReference(script, JButton.class, map);
 430  1
         ComponentReference cr2 =
 431   
             new ComponentReference(script, JButton.class, map);
 432  1
         assertEquals("Wrong line number after refs added",
 433   
                      5, Script.getLine(step));
 434   
 
 435  1
         Sequence seq = new Sequence(script, "");
 436  1
         script.addStep(seq);
 437  1
         assertEquals("Wrong line number for added sequence",
 438   
                      6, Script.getLine(seq));
 439   
 
 440  1
         step = new DefaultStep(script);
 441  1
         seq.addStep(step);
 442  1
         assertEquals("Wrong line number for step within sequence",
 443   
                      7, Script.getLine(step));
 444   
 
 445  1
         seq = new Sequence(script, "");
 446  1
         script.addStep(seq);
 447  1
         assertEquals("Wrong line number for step after sequence",
 448   
                      9, Script.getLine(seq));
 449   
 
 450  1
         step = new DefaultStep(script);
 451  1
         script.addStep(step);
 452  1
         assertEquals("Wrong line number for step after empty sequence",
 453   
                      10, Script.getLine(step));
 454   
     }
 455   
 
 456  1
     public void testIDChangeDetection() {
 457  1
         Script script = new Script(getHierarchy());
 458  1
         JLabel label = new JLabel(getName());
 459  1
         showFrame(label);
 460  1
         ComponentReference ref = script.addComponent(label);
 461  1
         assertEquals("Default id lookup failed with " + ref.getID(),
 462   
                      ref, script.getComponentReference(ref.getID()));
 463   
 
 464  1
         ref.setAttribute(XMLConstants.TAG_ID, "goober");
 465  1
         assertEquals("Changed ref id not detected: " + ref.getID(),
 466   
                      ref, script.getComponentReference(ref.getID()));
 467   
     }
 468   
 
 469   
 
 470   
 
 471  19
     public ScriptTest(String name) {
 472  19
         super(name);
 473   
     }
 474   
 
 475  0
     public static void main(String[] args) {
 476  0
         TestHelper.runTests(args, ScriptTest.class);
 477   
     }
 478   
 }
 479