KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > foxtrot > test > FoxtrotTestCase


1 /**
2  * Copyright (c) 2002-2005, Simone Bordet
3  * All rights reserved.
4  *
5  * This software is distributable under the BSD license.
6  * See the terms of the BSD license in the documentation provided with this software.
7  */

8
9 package foxtrot.test;
10
11 import java.lang.reflect.Method JavaDoc;
12
13 import javax.swing.SwingUtilities JavaDoc;
14
15 import junit.framework.TestCase;
16 import foxtrot.WorkerThread;
17 import foxtrot.workers.SingleWorkerThread;
18
19 /**
20  * Base class for Foxtrot tests
21  *
22  * @version $Revision: 1.7 $
23  */

24 public class FoxtrotTestCase extends TestCase
25 {
26    protected boolean debug = false;
27
28    protected FoxtrotTestCase(String JavaDoc s)
29    {
30       super(s);
31    }
32
33    /**
34     * Invokes the given Runnable in the AWT Event Dispatch Thread and waits for its
35     * completion, either by returning or by throwing.
36     * This is necessary since tests are run in the "main" thread by JUnit and instead
37     * we have to start them from the AWT Event Dispatch Thread.
38     */

39    protected void invokeTest(final WorkerThread workerThread, final Runnable JavaDoc test, final Runnable JavaDoc callback) throws Exception JavaDoc
40    {
41       if (SwingUtilities.isEventDispatchThread()) fail("Tests cannot be invoked from the Event Dispatch Thread");
42
43       final Object JavaDoc lock = new Object JavaDoc();
44       final MutableInteger barrier = new MutableInteger(0);
45       final MutableHolder throwable = new MutableHolder(null);
46
47       // This call returns immediately. It posts on the AWT Event Queue
48
// a Runnable that is executed.
49
SwingUtilities.invokeLater(new Runnable JavaDoc()
50       {
51          public void run()
52          {
53             // Wait to run the test until we're ready
54
synchronized (lock)
55             {
56                while (barrier.get() < 1)
57                {
58                   try
59                   {
60                      lock.wait();
61                   }
62                   catch (InterruptedException JavaDoc ignored)
63                   {
64                      Thread.currentThread().interrupt();
65                      break;
66                   }
67                }
68             }
69
70             if (debug) System.out.println("Running test " + this);
71             // Run the test and collect exception thrown
72
try
73             {
74                test.run();
75             }
76             catch (Throwable JavaDoc x)
77             {
78                synchronized (lock)
79                {
80                   throwable.set(x);
81                }
82             }
83
84             if (debug) System.out.println("Test method completed, waiting for test completion for " + this);
85             // Here the test method has returned.
86
// However, there may be tasks pending in both the WorkerThread queue
87
// and in EventQueue, so here I guarantee that those pending will be
88
// executed before starting the next test
89
try
90             {
91                while (hasPendingTasks(workerThread))
92                {
93                   if (debug) System.out.println("Pending tasks for test " + this);
94                   sleep(100);
95                }
96             }
97             catch (Exception JavaDoc x)
98             {
99                throwable.set(x);
100             }
101             if (debug) System.out.println("No more tasks running for test " + this);
102             SwingUtilities.invokeLater(new Runnable JavaDoc()
103             {
104                public void run()
105                {
106                   synchronized (lock)
107                   {
108                      barrier.set(0);
109                      lock.notifyAll();
110                   }
111                }
112             });
113          }
114       });
115
116       // The call above returns immediately.
117
// Here we wait for the test to be finished.
118
if (debug) System.out.println("Test " + this + " launched");
119       synchronized (lock)
120       {
121          barrier.set(1);
122          lock.notifyAll();
123
124          while (barrier.get() > 0) lock.wait();
125       }
126
127       if (debug) System.out.println("Test " + this + " consumed all AWT events");
128
129       try
130       {
131          // Here the test is really finished
132
if (callback != null) callback.run();
133
134          synchronized (lock)
135          {
136             Throwable JavaDoc t = (Throwable JavaDoc)throwable.get();
137             if (t instanceof Error JavaDoc) throw (Error JavaDoc)t;
138             if (t instanceof Exception JavaDoc) throw (Exception JavaDoc)t;
139          }
140       }
141       finally
142       {
143          if (debug) System.out.println("Test " + this + " completed");
144          if (debug) System.out.println();
145       }
146    }
147
148    protected void sleep(long ms)
149    {
150       try
151       {
152          Thread.sleep(ms);
153       }
154       catch (InterruptedException JavaDoc ignored)
155       {
156       }
157    }
158
159    protected boolean isJRE14()
160    {
161       return loadClass("java.awt.SequencedEvent") != null;
162    }
163
164    private Class JavaDoc loadClass(String JavaDoc className)
165    {
166       // We ask directly to the boot classloader
167
try
168       {
169          return Class.forName(className, false, null);
170       }
171       catch (ClassNotFoundException JavaDoc ignored)
172       {
173       }
174       return null;
175    }
176
177    private boolean hasPendingTasks(WorkerThread workerThread) throws Exception JavaDoc
178    {
179       if (workerThread == null) return false;
180       if (workerThread instanceof SingleWorkerThread)
181       {
182          Method JavaDoc hasPendingTasks = SingleWorkerThread.class.getDeclaredMethod("hasPendingTasks", null);
183          hasPendingTasks.setAccessible(true);
184          Boolean JavaDoc result = (Boolean JavaDoc)hasPendingTasks.invoke(workerThread, null);
185          return result.booleanValue();
186       }
187       throw new IllegalArgumentException JavaDoc("Invalid WorkerThread " + workerThread);
188    }
189 }
190
Popular Tags