KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > GlobalModelCompileTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model;
35
36 import java.io.*;
37
38 import javax.swing.text.BadLocationException JavaDoc;
39
40 //import edu.rice.cs.drjava.model.compiler.*;
41
import edu.rice.cs.util.UnexpectedException;
42 import edu.rice.cs.util.swing.Utilities;
43 import edu.rice.cs.util.text.EditDocumentException;
44
45 /** Tests to ensure that compilation behaves correctly in border cases.
46  * @version $Id: GlobalModelCompileTest.java 3839 2006-05-14 20:28:51Z rcartwright $
47  */

48 public final class GlobalModelCompileTest extends GlobalModelTestCase {
49   /** Tests calling compileAll with no source files works. Does not reset interactions. */
50   public void testCompileAllWithNoFiles() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
51     // Open one empty doc
52
_model.newFile();
53     
54     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false);
55     _model.addListener(listener);
56     _model.getCompilerModel().compileAll();
57     listener.waitCompileDone();
58     if (_model.getCompilerModel().getNumErrors() > 0) {
59       fail("compile failed: " + getCompilerErrorString());
60     }
61     assertCompileErrorsPresent("compile should succeed", false);
62     listener.checkCompileOccurred();
63     _model.removeListener(listener);
64   }
65
66   /** Tests that the interactions pane is reset after a successful compile. */
67   public void testCompileResetsInteractions() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc,
68     EditDocumentException {
69     
70 // System.err.println("Starting testCompileResetsInteractions");
71

72     OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
73     final File file = new File(_tempDir, "DrJavaTestFoo.java");
74     doc.saveFile(new FileSelector(file));
75     
76     // Use the interpreter so resetInteractions is not optimized to a no-op
77
interpret("0");
78
79     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(true);
80     _model.setResetAfterCompile(true);
81     _model.addListener(listener);
82     _model.getCompilerModel().compileAll();
83     listener.waitCompileDone();
84     
85     if (_model.getCompilerModel().getNumErrors() > 0) {
86 // System.err.println("Compile failed");
87
fail("compile failed: " + getCompilerErrorString());
88     }
89     listener.waitResetDone();
90
91 // System.err.println("Reached end of compilation");
92
assertCompileErrorsPresent("compile should succeed", false);
93     listener.checkCompileOccurred();
94  
95 // System.err.println("Checked that compile occurred");
96
_model.removeListener(listener);
97 // System.err.println("Removed compilation listener");
98
}
99
100   /** If we try to compile an unsaved file, and if we don't save when asked to saveAllBeforeProceeding, it should
101    * not do the compile or any other actions.
102    */

103   public void testCompileAbortsIfUnsaved() throws BadLocationException JavaDoc, IOException {
104     final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
105
106     TestListener listener = new TestListener() {
107       public void saveBeforeCompile() {
108         assertModified(true, doc);
109         synchronized(this) { saveBeforeCompileCount++; }
110         // since we don't actually save the compile should abort
111
}
112     };
113
114     _model.addListener(listener);
115     doc.startCompile();
116     listener.assertSaveBeforeCompileCount(1);
117     assertModified(true, doc);
118     assertContents(FOO_TEXT, doc);
119     _model.removeListener(listener);
120   }
121
122   /** If we try to compile while any files are unsaved, and if we don't save when asked to saveAllBeforeProceeding,
123    * it should not do the compile or any other actions.
124    */

125   public void testCompileAbortsIfAnyUnsaved() throws BadLocationException JavaDoc, IOException {
126     final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
127     final OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT);
128
129     TestListener listener = new TestListener() {
130       public void saveBeforeCompile() {
131         assertModified(true, doc);
132         assertModified(true, doc2);
133         synchronized(this) { saveBeforeCompileCount++; }
134         // since we don't actually save the compile should abort
135
}
136     };
137
138     _model.addListener(listener);
139     doc.startCompile();
140     listener.assertSaveBeforeCompileCount(1);
141     assertModified(true, doc);
142     assertModified(true, doc2);
143     assertContents(FOO_TEXT, doc);
144     assertContents(BAR_TEXT, doc2);
145     _model.removeListener(listener);
146   }
147
148   /** If we try to compile while any files (including the active file) are unsaved but we do save it from within saveAllBeforeProceeding, the
149    * compile should occur happily. Doesn't reset interactions because no interpretations are performed.
150    */

151   public void testCompileAnyUnsavedButSaveWhenAsked() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
152     final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
153     final OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT);
154     final File file = tempFile();
155     final File file2 = tempFile(2);
156
157     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false) {
158       public void saveBeforeCompile() {
159         assertModified(true, doc);
160         assertModified(true, doc2);
161         assertSaveCount(0);
162         assertCompileStartCount(0);
163         assertCompileEndCount(0);
164         assertInterpreterReadyCount(0);
165         assertConsoleResetCount(0);
166
167         try {
168           doc.saveFile(new FileSelector(file));
169           doc2.saveFile(new FileSelector(file2));
170         }
171         catch (IOException ioe) { fail("Save produced exception: " + ioe); }
172
173         synchronized(this) { saveBeforeCompileCount++; }
174       }
175
176       public void fileSaved(OpenDefinitionsDocument doc) {
177         assertModified(false, doc);
178         assertSaveBeforeCompileCount(0);
179         assertCompileStartCount(0);
180         assertCompileEndCount(0);
181         assertInterpreterReadyCount(0);
182         assertConsoleResetCount(0);
183
184         try { doc.getFile(); }
185         catch (FileMovedException fme) {
186           // We know file should exist
187
fail("file does not exist");
188         }
189         //assertEquals("file saved", file, f);
190
synchronized(this) { saveCount++; }
191       }
192     };
193
194     _model.addListener(listener);
195     doc.startCompile();
196     listener.waitCompileDone();
197     
198     if (_model.getCompilerModel().getNumErrors() > 0) {
199       fail("compile failed: " + getCompilerErrorString());
200     }
201
202     // Check events fired
203
listener.assertSaveBeforeCompileCount(1);
204     listener.assertSaveCount(2);
205     assertCompileErrorsPresent("compile should succeed", false);
206     listener.checkCompileOccurred();
207
208     // Make sure .class exists
209
File compiled = classForJava(file, "DrJavaTestFoo");
210     assertTrue("Class file doesn't exist after compile", compiled.exists());
211     _model.removeListener(listener);
212   }
213
214   /** If we try to compile while any files (but not the active file) are unsaved but we do save it from within
215    * saveAllBeforeProceeding, the compile should occur happily. Does not reset interactions.
216    */

217   public void testCompileActiveSavedAnyUnsavedButSaveWhenAsked() throws BadLocationException JavaDoc, IOException,
218     InterruptedException JavaDoc {
219     
220     final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
221     final OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT);
222     final File file = tempFile();
223     final File file2 = tempFile(1);
224
225     CompileShouldSucceedListener listener = new CompileShouldSucceedListener(false) {
226       public void saveBeforeCompile() {
227         assertModified(false, doc);
228         assertModified(true, doc2);
229         assertSaveCount(0);
230         assertCompileStartCount(0);
231         assertCompileEndCount(0);
232         assertInterpreterReadyCount(0);
233         assertConsoleResetCount(0);
234
235         try { doc2.saveFile(new FileSelector(file2)); }
236         catch (IOException ioe) { fail("Save produced exception: " + ioe); }
237
238         synchronized(this) { saveBeforeCompileCount++; }
239         assertModified(false, doc);
240         assertModified(false, doc2);
241         assertTrue(!_model.hasModifiedDocuments());
242       }
243
244       public void fileSaved(OpenDefinitionsDocument doc) {
245         assertModified(false, doc);
246         assertSaveBeforeCompileCount(0);
247         assertCompileStartCount(0);
248         assertCompileEndCount(0);
249         assertInterpreterReadyCount(0);
250         assertConsoleResetCount(0);
251
252         File f = null;
253         try { f = doc.getFile(); }
254         catch (FileMovedException fme) {
255           // We know file should exist
256
fail("file does not exist");
257         }
258         assertEquals("file saved", file2, f);
259         synchronized(this) { saveCount++; }
260       }
261     };
262
263     assertModified(true, doc);
264     doc.saveFile(new FileSelector(file));
265     assertModified(false, doc);
266     assertModified(true, doc2);
267     _model.addListener(listener);
268     doc.startCompile();
269     listener.waitCompileDone();
270     if (_model.getCompilerModel().getNumErrors() > 0) {
271       fail("compile failed: " + getCompilerErrorString());
272     }
273     assertTrue(!_model.hasModifiedDocuments());
274
275     // Check events fired
276
listener.assertCompileStartCount(1);
277     listener.assertSaveBeforeCompileCount(1);
278     listener.assertSaveCount(1);
279     assertCompileErrorsPresent("compile should succeed", false);
280     listener.checkCompileOccurred();
281
282     // Make sure .class exists
283
File compiled = classForJava(file, "DrJavaTestFoo");
284     assertTrue("Class file doesn't exist after compile", compiled.exists());
285     _model.removeListener(listener);
286   }
287 }
288
Popular Tags