All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.abbot.script.ScriptTest Maven / Gradle / Ivy

The newest version!
package abbot.script;

import abbot.finder.AWTHierarchy;
import abbot.finder.Hierarchy;

import java.awt.*;
import java.io.*;
import java.util.Iterator;

import javax.swing.*;
import java.util.*;

import junit.extensions.abbot.*;
import junit.extensions.abbot.Timer;
import org.jdom.Element;

import abbot.tester.Robot;

/** 
 * Verify the script works as advertised.
 */
public class ScriptTest extends ResolverFixture implements XMLConstants {

    private Script script;
    private File baseDir;
    private File otherRoot;    
    private static final String ENCODING = "UTF-8";

    protected void setUp() throws IOException {
        script = new Script(getHierarchy());
        
        // Create a base dir
        
        baseDir = File.createTempFile("baseDir","");
        baseDir.deleteOnExit();
        
        // Configure a file in another root to make sure that we get absolute 
        // paths
        
        if (System.getProperty("os.name").toLowerCase().startsWith("window")) {
            otherRoot = new File("w:\\root");
        }
        else {
            otherRoot = new File("/newroot");
        }
        
        // Finally set the location relative to location on the script
        // so all start at the same position
        //
        
        script.setRelativeTo(baseDir);
        
        //
        
        ComponentReference.cacheOnCreation = false;
    }

    protected void tearDown() {
        ComponentReference.cacheOnCreation = true;
    }

    public static Reader stringToReader(String script) throws Throwable {
        ByteArrayInputStream ba =
            new ByteArrayInputStream(script.getBytes(ENCODING));
        InputStreamReader reader = new InputStreamReader(ba, ENCODING);
        return reader;
    }

    public void testDefaultDescription() {
        String filename = script.getFile().getName();
        assertTrue("Default script description should omit the filename "
                   + "if the script is a temporary file: "
                   + script.getDescription(),
                   script.getDescription().indexOf(filename) == -1);
    }
    
    // need an automatic way to generate all component ref attributes
    // using a hand-coded array isn't any more reliable than the original
    // edits of abbot.xsd.
    /*
    public void testComponentValidation() throws Throwable {
        Frame f = new Frame(getName());
        ComponentReference ref = script.addComponent(f);
        Iterator iter = VALID_ATTRIBUTES.iterator();
        // Make sure our XSD has included ALL the valid cref attributes
        while (iter.hasNext()) {
            ref.setAttribute((String)iter.next(), "0");
        }
        script.save();
        script.load();
    }
    */

    public void testLaunchAsUIContext() {
        script.clear();
        Launch launch = new Launch(script, getName(), "any.cls.name", "method", new String[] { "[]" });
        script.addStep(launch);
        assertEquals("Launch should be UIContext", launch, script.getUIContext());
    }
    
    public void testFixtureAsUIContext() {
        script.clear();
        Fixture fixture = new Fixture(script, new HashMap());
        script.addStep(fixture);
        assertEquals("Fixture should be UIContext", fixture, script.getUIContext());
    }

    public void testCatchInvalidScript() throws Throwable {
        String bogus = "\r\n"
            + "\r\n"
            + "  \r\n"
            + "  \r\n"
            + "\r\n\r\n";
        try {
            script.load(stringToReader(bogus));
            fail("Expected an invalid script exception");
        }
        catch(InvalidScriptException ise) {
        }
    }

    public void testInValidLaunchLocation() throws Throwable {
        String valid = "\r\n"
            + "\r\n"
            + "  \r\n"
            + "  \r\n"
            + "\r\n\r\n";
        try {
            script.load(stringToReader(valid));
            fail("A script with a launch step in other than position 0 should be detected as invalid");
        }
        catch(InvalidScriptException e) {
        }
    }

    public void testTerminatePositionValidation() throws Throwable {
        String valid = "\r\n"
            + "\r\n"
            + "  \r\n"
            + "  \r\n"
            + "\r\n\r\n";
        try {
            script.load(stringToReader(valid));
            fail("A script with a terminate step in other than last position should be detected as invalid");
        }
        catch(InvalidScriptException e) {
        }
    }



    public void testDirty() throws Throwable {
        script.save();
        script.getFile().deleteOnExit();
        script.load();
        assertTrue("Script should not be dirty immediately after load",
                   !script.isDirty());
        script.addStep(new Script(getHierarchy()));
        assertTrue("Script should be dirty after modifications",
                   script.isDirty());
        script.removeStep(0);
        assertTrue("Script should not be dirty", !script.isDirty());
    }

    static final String ID = "button";
    static final String TEXT = "\u65E5\u008Aencoded";
    static final String CLASS = "javax.swing.JButton";
    private static final String unicodeData = 
        "\r\n"
        + "\r\n"
        + "  \r\n"
        + "  \r\n"
        + "\r\n\r\n";
    public void testWrite() throws Throwable {
        StringWriter writer = new StringWriter();
        Element el = new Element(XMLConstants.TAG_COMPONENT).
            setAttribute(XMLConstants.TAG_ID, ID).
            setAttribute(XMLConstants.TAG_TEXT, TEXT).
            setAttribute(XMLConstants.TAG_CLASS, CLASS);
        ComponentReference ref = script.addComponentReference(el);
        assertEquals("Incorrect tag encoding",
                     TEXT, ref.getAttribute(XMLConstants.TAG_TEXT));
        script.addStep(new Terminate(script, (String)null));
        script.save(writer);

        // Note that this is all Unicode, NOT UTF-8
        assertEquals("Incorrect XML written", unicodeData, writer.toString());
    }

    public void testRead() throws Throwable {
        try {
            script.load(stringToReader(unicodeData));
            assertEquals("Wrong number of references",
                         1, script.getComponentReferences().size());
            assertEquals("Wrong number of steps", 1, script.size());
            ComponentReference ref = (ComponentReference)
                script.getComponentReferences().iterator().next();
            // Ensure all attributes are saved with unicode
            assertEquals("Wrong tag",
                         TEXT, ref.getAttribute(XMLConstants.TAG_TEXT));
        }
        catch(InvalidScriptException ise) {
            fail(ise.getMessage());
        }
    }

    /** Ensure that a nested script maintains its own context. */
    public void testNestedReference() throws Throwable {
        Script nested = new Script(getHierarchy());
        nested.load(stringToReader(unicodeData));
        script.addStep(nested);
        assertEquals("Wrong resolver", script, script.getResolver());
        assertEquals("Wrong nested resolver", nested, nested.getResolver());
        assertEquals("Wrong resolver for nested step",
                     nested, ((Step)nested.steps().get(0)).getResolver());

        String nestedScriptData = 
            "\r\n"
            + "\r\n"
            + "  \r\n"
            + "\r\n\r\n";
        script.load(stringToReader(nestedScriptData));
        nested = (Script)script.steps().get(0);
        assertEquals("Wrong resolver for nested script",
                     nested, nested.getResolver());
    }

    /** A nested script should have *only* its filename attribute saved. */
    public void testSaveNestedScript() throws Throwable {
        String fullNestedScript = 
            "\r\n"
            + "\r\n"
            + "  \r\n"
            + "\r\n\r\n";
        String savedNestedScript = 
            "\r\n"
            + "\r\n"
            + "