Skip to content

Commit 514e2e5

Browse files
committed
Cleaned up the volatile attributes on some private variables involved in JUnit testing because they can potentially be accessed by multiple
threads. Added assert statements regarding the dispatch thread. Changes to be committed: modified: src/edu/rice/cs/drjava/model/junit/JUnitTestRunner.java modified: src/edu/rice/cs/drjava/model/repl/newjvm/InterpreterJVM.java modified: src/edu/rice/cs/drjava/model/repl/newjvm/MainJVM.java modified: src/edu/rice/cs/drjava/ui/JUnitPanel.java modified: src/edu/rice/cs/drjava/ui/MainFrame.java Changes not staged for commit: modified: src/edu/rice/cs/drjava/model/junit/JUnitTestRunner.java modified: src/edu/rice/cs/drjava/model/repl/newjvm/InterpreterJVM.java modified: src/edu/rice/cs/drjava/model/repl/newjvm/MainJVM.java
1 parent 816d98b commit 514e2e5

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

drjava/src/edu/rice/cs/drjava/model/junit/JUnitTestRunner.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
import edu.rice.cs.util.UnexpectedException;
3636

3737
/** DrJava's own testrunner. It updates the document in the JUnit pane as error and failure events are fired.
38-
* These methods run inan auxiliary thread.
38+
* These methods run inan auxiliary thread. Many methods are synchronized for atomicity to maintain the
39+
* consistency of local state.
3940
* @version $Id$
4041
*/
4142
public class JUnitTestRunner extends BaseTestRunner {
@@ -56,8 +57,6 @@ public class JUnitTestRunner extends BaseTestRunner {
5657

5758
/** The current number of failures in the result. */
5859
private int _failureCount;
59-
60-
6160

6261
/** Standard constructor.
6362
* @param jmc a JUnitModelCallback

drjava/src/edu/rice/cs/drjava/model/repl/newjvm/InterpreterJVM.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.*;
3333

3434
import java.rmi.*;
35+
import java.awt.EventQueue;
3536

3637
// NOTE: Do NOT import/use the config framework in this class!
3738
// (This class runs in a different JVM, and will not share the config object)

drjava/src/edu/rice/cs/drjava/model/repl/newjvm/MainJVM.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828
* END_COPYRIGHT_BLOCK*/
2929
package edu.rice.cs.drjava.model.repl.newjvm;
3030

31-
import java.rmi.*;
31+
import java.awt.EventQueue;
3232
import java.io.*;
3333
import java.net.SocketException;
34+
import java.rmi.*;
3435

3536
import java.util.List;
3637
import java.util.ArrayList;
@@ -257,7 +258,7 @@ public void testSuiteStarted(int numTests) {
257258
/** Called when a particular test is started. Forwards from the slave JVM to the local JUnit model.
258259
* @param testName The name of the test being started.
259260
*/
260-
public void testStarted(String testName) {
261+
public void testStarted(String testName) {;
261262
_junitModel.testStarted(testName);
262263
}
263264

drjava/src/edu/rice/cs/drjava/ui/JUnitPanel.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
import java.io.File;
4848
import 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
*/
5354
public 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;

drjava/src/edu/rice/cs/drjava/ui/MainFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9548,7 +9548,7 @@ public void junitSuiteStarted(final int numTests) {
95489548

95499549
public void junitTestStarted(final String name) {
95509550
assert EventQueue.isDispatchThread();
9551-
_junitPanel.getErrorListPane().testStarted(name); /* this does nothing! */
9551+
_junitPanel.getErrorListPane().testStarted(name); /* passes test name to errorListPane */
95529552
}
95539553

95549554
public void junitTestEnded(final String name, final boolean succeeded, final boolean causedError) {

0 commit comments

Comments
 (0)