001: package abbot.script;
002:
003: import junit.extensions.abbot.*;
004: import javax.swing.*;
005:
006: public class LaunchTest extends ResolverFixture {
007:
008: private static volatile boolean terminate;
009: private Launch launch;
010: private ClassLoader classLoader;
011:
012: protected void setUp() {
013: Resolver r = getResolver();
014: launch = new Launch(r, null, DummyLaunch.class.getName(),
015: "main", new String[] { "[]" });
016: // Assumes a little something about the test environment
017: launch.setClasspath("classes");
018: terminate = false;
019: classLoader = Thread.currentThread().getContextClassLoader();
020: }
021:
022: protected void tearDown() {
023: terminate = true;
024: launch.terminate();
025: }
026:
027: public void testLaunchState() throws Throwable {
028: launch.launch(new StepRunner());
029: assertTrue("Launch should be launched", launch.isLaunched());
030: }
031:
032: public void testRunState() throws Throwable {
033: launch.runStep();
034: assertTrue("Launch should be launched", launch.isLaunched());
035: }
036:
037: public void testGetContextBeforeRunning() {
038: ClassLoader cl = launch.getContextClassLoader();
039: assertTrue("Launch did not provide a class loader",
040: cl != classLoader);
041: }
042:
043: public void testRunWithNewContext() throws Throwable {
044: ClassLoader cl = launch.getContextClassLoader();
045: launch.runStep();
046: assertTrue("Launch did not install a class loader: " + cl,
047: cl != classLoader);
048: launch.terminate();
049: assertEquals("Launch did not uninstall the class loader",
050: classLoader, Thread.currentThread()
051: .getContextClassLoader());
052: launch.runStep();
053: ClassLoader cl2 = launch.getContextClassLoader();
054: assertTrue(
055: "Each launch should use a fresh class loader: " + cl,
056: cl != cl2);
057: }
058:
059: public void testContextThreaded() throws Throwable {
060: launch.setThreaded(true);
061: launch.setMethodName("mainThreaded");
062: launch.runStep();
063: ClassLoader cl = Thread.currentThread().getContextClassLoader();
064: assertTrue("Launch did not install a class loader",
065: cl != classLoader);
066: // FIXME check for existence of the launch thread as well
067: launch.terminate();
068: assertEquals("Launch did not uninstall the class loader",
069: classLoader, Thread.currentThread()
070: .getContextClassLoader());
071: }
072:
073: private boolean error;
074:
075: public void testContextLoaderOnlyInstalledAtRun() throws Throwable {
076: final ClassLoader cl = launch.getContextClassLoader();
077: SwingUtilities.invokeAndWait(new Runnable() {
078: public void run() {
079: error = Thread.currentThread().getContextClassLoader() == cl;
080: }
081: });
082: assertFalse(
083: "Class loader should not yet be installed as EDT context",
084: error);
085: launch.runStep();
086: SwingUtilities.invokeAndWait(new Runnable() {
087: public void run() {
088: error = Thread.currentThread().getContextClassLoader() != cl;
089: }
090: });
091: assertFalse(
092: "Class loader should be installed as EDT context when run",
093: error);
094: }
095:
096: public static void main(String[] args) {
097: TestHelper.runTests(args, LaunchTest.class);
098: }
099:
100: public static class DummyLaunch {
101:
102: public static void main(String[] args) {
103: }
104:
105: public static void mainThreaded(String[] args) {
106: // wait for the external flag to get set
107: while (!terminate) {
108: try {
109: Thread.sleep(100);
110: } catch (InterruptedException e) {
111: }
112: }
113: }
114: }
115: }
|