4747import java .io .File ;
4848import java .util .HashMap ;
4949
50- /** The panel that displays all the testing errors.
50+ /** The panel that displays all the testing errors. All non-trivial methods in this class other than constructors should
51+ * only be called from the dispatch thread, except during set up of MainFrame.
5152 * @version $Id$
5253 */
5354public class JUnitPanel extends ErrorPanel {
@@ -80,7 +81,7 @@ private static final SimpleAttributeSet _getTestFailAttributes() {
8081 private static final String TEST_OUT_OF_SYNC =
8182 "The documents being tested have been modified and should be recompiled!\n " ;
8283
83- protected JUnitErrorListPane _errorListPane ;
84+ protected volatile JUnitErrorListPane _errorListPane ;
8485 private final MainFrame _mainFrame ; // only used in assert statements
8586 private volatile int _testCount ;
8687 private volatile boolean _testsSuccessful ;
@@ -134,6 +135,7 @@ public JUnitPanel(SingleDisplayModel model, MainFrame frame) {
134135 * @param newSet Style containing new attributes to use.
135136 */
136137 protected void _updateStyles (AttributeSet newSet ) {
138+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
137139 super ._updateStyles (newSet );
138140 OUT_OF_SYNC_ATTRIBUTES .addAttributes (newSet );
139141 StyleConstants .setBold (OUT_OF_SYNC_ATTRIBUTES , true ); // should always be bold
@@ -143,6 +145,7 @@ protected void _updateStyles(AttributeSet newSet) {
143145
144146 /** called when work begins */
145147 public void setJUnitInProgress () {
148+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
146149 _errorListPane .setJUnitInProgress ();
147150 }
148151
@@ -156,6 +159,7 @@ protected void _close() {
156159
157160 /** Reset the errors to the current error information. */
158161 public void reset () {
162+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
159163 JUnitErrorModel junitErrorModel = getModel ().getJUnitModel ().getJUnitErrorModel ();
160164 boolean testsHaveRun = false ;
161165 if (junitErrorModel != null ) {
@@ -168,9 +172,10 @@ public void reset() {
168172 }
169173
170174 /** Resets the progress bar to start counting the given number of tests.
171- * @param numTests number of tests to be counted
172- */
175+ * @param numTests number of tests to be counted
176+ */
173177 public void progressReset (int numTests ) {
178+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
174179 _progressBar .reset ();
175180 _progressBar .start (numTests );
176181 _testsSuccessful = true ;
@@ -181,6 +186,7 @@ public void progressReset(int numTests) {
181186 * @param successful Whether the last test was successful or not.
182187 */
183188 public void progressStep (boolean successful ) {
189+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
184190 _testCount ++;
185191 _testsSuccessful &= successful ;
186192 _progressBar .step (_testCount , _testsSuccessful );
@@ -189,6 +195,7 @@ public void progressStep(boolean successful) {
189195 public void testStarted (String className , String testName ) { }
190196
191197 private void _displayStackTrace (JUnitError e ) {
198+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
192199 _errorLabel .setText ((e .isWarning () ? "Error: " : "Failure: " ) +
193200 e .message ());
194201 _fileLabel .setText ("File: " + (new File (e .fileName ())).getName ());
@@ -242,17 +249,19 @@ private String _getClassFromName(String name) {
242249 else throw new IllegalArgumentException ("Name does not contain any parens: " + name );
243250 }
244251
245- /** Provides the ability to display the name of the test being run.
252+ /** Provides the the name of the test being run to the JUnitPanel.
246253 * @param name the name of the test being run
247254 */
248255 public void testStarted (String name ) {
256+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
249257 if (name .indexOf ('(' ) < 0 ) return ;
250258
251259 String testName = _getTestFromName (name );
252260 String className = _getClassFromName (name );
253261 String fullName = className + "." + testName ;
254262 if (fullName .equals (JUNIT_WARNING )) return ;
255263 ErrorDocument doc = getErrorDocument ();
264+ // TODO: convert this GUI operatoin to a Runnable and use invokeLater
256265 try {
257266 int len = doc .getLength ();
258267 // Insert the classname if it has changed
@@ -276,18 +285,19 @@ public void testStarted(String name) {
276285 }
277286 }
278287
279- /** Displays the results of a test that has finished.
288+ /** Displays the results of a test that has finished in the JUnitPanel.
280289 * @param name the name of the test
281290 * @param wasSuccessful whether the test was successful
282291 * @param causedError whether the test caused an error
283292 */
284293 public void testEnded (String name , boolean wasSuccessful , boolean causedError ) {
294+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
285295 if (name .indexOf ('(' )<0 ) return ;
286296
287297 String testName = _getTestFromName (name );
288298 String fullName = _getClassFromName (name ) + "." + testName ;
289299 if (fullName .equals (JUNIT_WARNING )) return ;
290-
300+ // TODO: convert this GUI operation to a Runnable and use invokeLater
291301 ErrorDocument doc = getErrorDocument ();
292302 Position namePos = _runningTestNamePositions .get (fullName );
293303 AttributeSet set ;
@@ -319,6 +329,7 @@ public void setJUnitInProgress() {
319329
320330 /** Used to show that testing was unsuccessful. */
321331 protected void _updateWithErrors () throws BadLocationException {
332+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
322333 //DefaultStyledDocument doc = new DefaultStyledDocument();
323334 ErrorDocument doc = getErrorDocument ();
324335// _checkSync(doc);
@@ -415,6 +426,7 @@ protected void _updateNoErrors(boolean haveTestsRun) throws BadLocationException
415426
416427 private void _setupStackTraceFrame () {
417428 //DrJava.consoleOut().println("Stack Trace for Error: \n" + e.stackTrace());
429+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
418430 JDialog _dialog = new JDialog (_frame ,"JUnit Error Stack Trace" ,false );
419431 _stackFrame = _dialog ;
420432 _stackTextArea = new JTextArea ();
@@ -453,6 +465,7 @@ public void actionPerformed(ActionEvent e) {
453465 * and enabling the _showStackTraceButton.
454466 */
455467 public void selectItem (DJError error ) {
468+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
456469 super .selectItem (error );
457470 _error = (JUnitError ) error ;
458471 _showStackTraceButton .setEnabled (true );
@@ -461,6 +474,7 @@ public void selectItem(DJError error) {
461474
462475 /** Overrides _removeListHighlight in ErrorListPane to disable the _showStackTraceButton. */
463476 protected void _removeListHighlight () {
477+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
464478 super ._removeListHighlight ();
465479 _showStackTraceButton .setEnabled (false );
466480 }
@@ -497,6 +511,7 @@ public void mouseReleased(MouseEvent e) {
497511 * @return true iff the mouse click is over an error
498512 */
499513 private boolean _selectError (MouseEvent e ) {
514+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
500515 //TODO: get rid of cast in the next line, if possible
501516 _error = (JUnitError )_errorAtPoint (e .getPoint ());
502517
@@ -513,7 +528,10 @@ private boolean _selectError(MouseEvent e) {
513528 /** Shows the popup menu for this mouse adapter.
514529 * @param e the MouseEvent correponding to this click
515530 */
516- protected void _popupAction (MouseEvent e ) { _popMenu .show (e .getComponent (), e .getX (), e .getY ()); }
531+ protected void _popupAction (MouseEvent e ) {
532+ assert ! _mainFrame .isVisible () || EventQueue .isDispatchThread ();
533+ _popMenu .show (e .getComponent (), e .getX (), e .getY ());
534+ }
517535 }
518536 public String getErrorDocumentTitle () { return "Javadoc Errors" ; }
519537 }
@@ -532,6 +550,7 @@ public JUnitProgressBar() {
532550 }
533551
534552 private Color getStatusColor () {
553+ assert EventQueue .isDispatchThread ();
535554 if (_hasError ) {
536555 return Color .red ;
537556 }
@@ -541,17 +560,20 @@ private Color getStatusColor() {
541560 }
542561
543562 public void reset () {
563+ assert EventQueue .isDispatchThread ();
544564 _hasError = false ;
545565 setForeground (getStatusColor ());
546566 setValue (0 );
547567 }
548568
549569 public void start (int total ) {
570+ assert EventQueue .isDispatchThread ();
550571 setMaximum (total );
551572 reset ();
552573 }
553574
554575 public void step (int value , boolean successful ) {
576+ assert EventQueue .isDispatchThread ();
555577 setValue (value );
556578 if (!_hasError && !successful ) {
557579 _hasError = true ;
0 commit comments