3636
3737package edu .rice .cs .drjava .ui ;
3838
39- import edu .rice .cs .drjava .model . MultiThreadedTestCase ;
39+ import edu .rice .cs .drjava .DrJavaTestCase ;
4040import edu .rice .cs .drjava .model .OpenDefinitionsDocument ;
4141import edu .rice .cs .drjava .model .SingleDisplayModel ;
4242import edu .rice .cs .drjava .project .DocFile ;
5151import edu .rice .cs .util .UnexpectedException ;
5252import edu .rice .cs .util .swing .Utilities ;
5353
54+ import java .awt .EventQueue ;
55+ import java .awt .Toolkit ;
5456import java .io .*;
5557import java .util .List ;
5658
5759/** Test functions of Project Facility working through the main frame and model. */
58- public final class ProjectMenuTest extends MultiThreadedTestCase {
59-
60+ public final class ProjectMenuTest extends DrJavaTestCase {
61+
6062 private volatile MainFrame _frame ;
6163
6264 private volatile SingleDisplayModel _model ;
@@ -84,7 +86,7 @@ public final class ProjectMenuTest extends MultiThreadedTestCase {
8486 public void setUp () throws Exception {
8587 // Perform Swing setup in event thread because the event thread is ALREADY running
8688 superSetUp (); // super.setUp() should be called first; contains an embedded invokeAndWait
87-
89+
8890 Utilities .invokeAndWait (new Runnable () {
8991 public void run () {
9092 try {
@@ -124,13 +126,23 @@ public void run() {
124126 }
125127 });
126128 }
127-
129+
128130 public void tearDown () throws Exception {
131+
129132 Utilities .invokeAndWait (new Runnable () {
130133 public void run () {
131134 IOUtil .deleteOnExitRecursively (_parent );
132135 _auxFile .delete ();
133136 //FileOps.deleteDirectory(_parent);
137+ }
138+ });
139+
140+ /* Ensure that all pending actions in the event queue have completed. */
141+ EventQueue current = java .awt .Toolkit .getDefaultToolkit ().getSystemEventQueue ();
142+ while (current .peekEvent () != null ) { Thread .sleep (200 ); }
143+
144+ Utilities .invokeAndWait (new Runnable () {
145+ public void run () {
134146 _frame .dispose ();
135147 _projFile = FileOps .NULL_FILE ;
136148 _model = null ;
@@ -144,92 +156,84 @@ public void testSetBuildDirectory() throws MalformedProjectFileException, IOExce
144156
145157// Utilities.showDebug("executing testSetBuildDirectory");
146158
147- //test set build directory when not in project mode
148- File f = FileOps .NULL_FILE ;
149- _model .setBuildDirectory (f );
150- Utilities .clearEventQueue (); // ensure that listener tasks have completed
151- assertEquals ("Build directory should not have been set" , FileOps .NULL_FILE , _model .getBuildDirectory ());
152-
153- // System.err.println("Opening Project File");
154- Utilities .invokeAndWait (new Runnable () {
155- public void run () {
156- try { _model .openProject (_projFile ); }
159+ Utilities .invokeAndWait (new Runnable () {
160+ public void run () {
161+ try {
162+ // Test set build directory when not in project mode
163+ File f = FileOps .NULL_FILE ;
164+ _model .setBuildDirectory (f );
165+ assertEquals ("Build directory should not have been set" , FileOps .NULL_FILE , _model .getBuildDirectory ());
166+
167+ // System.err.println("Opening Project File");
168+ _model .openProject (_projFile );
169+
170+ // System.err.println("Completed Opening Project File");
171+ // System.err.println("Project documents are: " + _model.getProjectDocuments());
172+
173+ assertEquals ("Build directory should not have been set" , FileOps .NULL_FILE , _model .getBuildDirectory ());
174+
175+ _model .setBuildDirectory (f );
176+ assertEquals ("Build directory should have been set" , f , _model .getBuildDirectory ());
177+ }
157178 catch (Exception e ) { throw new UnexpectedException (e ); }
158- }
179+ }
159180 });
160- // System.err.println("Completed Opening Project File");
161- // System.err.println("Project documents are: " + _model.getProjectDocuments());
162-
163- Utilities .clearEventQueue ();
164-
165- assertEquals ("Build directory should not have been set" , FileOps .NULL_FILE , _model .getBuildDirectory ());
166-
167- _model .setBuildDirectory (f );
168- Utilities .clearEventQueue ();
169- assertEquals ("Build directory should have been set" , f , _model .getBuildDirectory ());
170181 }
171182
172- public void testCloseAllClosesProject () throws MalformedProjectFileException , IOException {
173-
183+ public void testCloseAllClosesProject () {
174184// Utilities.showDebug("executing testCloseAllClosesProject");
175- Utilities .invokeAndWait (new Runnable () {
176- public void run () {
177- try { _model .openProject (_projFile ); }
178- catch (Exception e ) { throw new UnexpectedException (e ); }
179- }
180- });
181- Utilities .clearEventQueue ();
182-
183- assertTrue ("Project should have been opened" , _model .isProjectActive ());
184-
185- Utilities .invokeAndWait (new Runnable () {
186- public void run () {
187- try { _frame .closeAll (); }
185+ Utilities .invokeAndWait (new Runnable () {
186+ public void run () {
187+ try {
188+ _model .openProject (_projFile );
189+ assertTrue ("Project should have been opened" , _model .isProjectActive ());
190+
191+ _frame .closeAll ();
192+ assertFalse ("Project should have been closed" , _model .isProjectActive ());
193+ }
188194 catch (Exception e ) { throw new UnexpectedException (e ); }
189- }
195+ }
190196 });
191- Utilities .clearEventQueue ();
192-
193- assertFalse ("Project should have been closed" , _model .isProjectActive ());
194197 }
195198
196- public void testSaveProject () throws IOException , MalformedProjectFileException {
197-
199+ public void testSaveProject () {
198200// Utilities.showDebug("executing testSaveProject");
199201
200202 Utilities .invokeAndWait (new Runnable () {
201- public void run () {
202- _frame .openProject (new FileOpenSelector () {
203- public File [] getFiles () throws OperationCanceledException { return new File [] {_projFile }; }
204- });
205-
206- // open a new file and make it an auxiliary file
207- _frame .open (new FileOpenSelector () {
208- public File [] getFiles () throws OperationCanceledException { return new File [] {_auxFile }; }
209- });
210- _frame ._moveToAuxiliary ();
211-
212- List <OpenDefinitionsDocument > auxDocs = _model .getAuxiliaryDocuments ();
213- assertEquals ("One auxiliary document" , 1 , auxDocs .size ());
214- _frame .saveProject ();
215- _frame ._closeProject ();
216- }
203+ public void run () {
204+ try {
205+ _frame .openProject (new FileOpenSelector () {
206+ public File [] getFiles () throws OperationCanceledException { return new File [] { _projFile }; }
207+ });
208+
209+ // open a new file and make it an auxiliary file
210+ _frame .open (new FileOpenSelector () {
211+ public File [] getFiles () throws OperationCanceledException { return new File [] { _auxFile }; }
212+ });
213+ _frame ._moveToAuxiliary ();
214+
215+ List <OpenDefinitionsDocument > auxDocs = _model .getAuxiliaryDocuments ();
216+ assertEquals ("One auxiliary document" , 1 , auxDocs .size ());
217+ _frame .saveProject ();
218+ _frame ._closeProject ();
219+
220+ List <OpenDefinitionsDocument > docs = _model .getOpenDefinitionsDocuments ();
221+ assertEquals ("One empty document remaining" , 1 , docs .size ());
222+ assertEquals ("Name is (Untitled)" , "(Untitled)" , _model .getActiveDocument ().toString ());
223+
224+ ProjectFileIR pfir = ProjectFileParserFacade .ONLY .parse (_projFile );
225+ DocFile [] src = pfir .getSourceFiles ();
226+ // System.err.println(Arrays.toString(src));
227+ DocFile [] aux = pfir .getAuxiliaryFiles ();
228+ // System.err.println(Arrays.toString(aux));
229+ assertEquals ("Number of saved src files" , 2 , src .length );
230+ assertEquals ("Number of saved aux files" , 1 , aux .length );
231+ assertEquals ("wrong name for _file2" , _file2 .getCanonicalPath (), src [1 ].getCanonicalPath ()); // assumes same (not reverse) order
232+ assertEquals ("Wrong name for _file1" , _file1 .getCanonicalPath (), src [0 ].getCanonicalPath ());
233+ assertEquals ("Wrong aux file" , _auxFile .getCanonicalPath (), aux [0 ].getCanonicalPath ());
234+ }
235+ catch (Exception e ) { throw new UnexpectedException (e ); }
236+ }
217237 });
218- Utilities .clearEventQueue ();
219-
220- List <OpenDefinitionsDocument > docs = _model .getOpenDefinitionsDocuments ();
221- assertEquals ("One empty document remaining" , 1 , docs .size ());
222- assertEquals ("Name is (Untitled)" , "(Untitled)" , _model .getActiveDocument ().toString ());
223-
224- ProjectFileIR pfir = ProjectFileParserFacade .ONLY .parse (_projFile );
225- DocFile [] src = pfir .getSourceFiles ();
226- // System.err.println(Arrays.toString(src));
227- DocFile [] aux = pfir .getAuxiliaryFiles ();
228- // System.err.println(Arrays.toString(aux));
229- assertEquals ("Number of saved src files" , 2 , src .length );
230- assertEquals ("Number of saved aux files" , 1 , aux .length );
231- assertEquals ("wrong name for _file2" , _file2 .getCanonicalPath (), src [1 ].getCanonicalPath ()); // assumes same (not reverse) order
232- assertEquals ("Wrong name for _file1" , _file1 .getCanonicalPath (), src [0 ].getCanonicalPath ());
233- assertEquals ("Wrong aux file" , _auxFile .getCanonicalPath (), aux [0 ].getCanonicalPath ());
234238 }
235239}
0 commit comments