001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model;
038:
039: import java.io.*;
040:
041: import javax.swing.text.BadLocationException;
042:
043: //import edu.rice.cs.drjava.model.compiler.*;
044: import edu.rice.cs.util.UnexpectedException;
045: import edu.rice.cs.util.swing.Utilities;
046: import edu.rice.cs.util.text.EditDocumentException;
047:
048: /** Tests to ensure that compilation behaves correctly in border cases.
049: * @version $Id: GlobalModelCompileTest.java 4255 2007-08-28 19:17:37Z mgricken $
050: */
051: public final class GlobalModelCompileTest extends GlobalModelTestCase {
052: /** Tests calling compileAll with no source files works. Does not reset interactions. */
053: public void testCompileAllWithNoFiles()
054: throws BadLocationException, IOException,
055: InterruptedException {
056: // Open one empty doc
057: _model.newFile();
058:
059: CompileShouldSucceedListener listener = new CompileShouldSucceedListener(
060: false);
061: _model.addListener(listener);
062: _model.getCompilerModel().compileAll();
063: listener.waitCompileDone();
064: if (_model.getCompilerModel().getNumErrors() > 0) {
065: fail("compile failed: " + getCompilerErrorString());
066: }
067: assertCompileErrorsPresent("compile should succeed", false);
068: listener.checkCompileOccurred();
069: _model.removeListener(listener);
070: }
071:
072: /** Tests that the interactions pane is reset after a successful compile. */
073: public void testCompileResetsInteractions()
074: throws BadLocationException, IOException,
075: InterruptedException, EditDocumentException {
076:
077: // System.err.println("Starting testCompileResetsInteractions");
078:
079: OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
080: final File file = new File(_tempDir, "DrJavaTestFoo.java");
081: doc.saveFile(new FileSelector(file));
082:
083: // Use the interpreter so resetInteractions is not optimized to a no-op
084: interpret("0");
085:
086: CompileShouldSucceedListener listener = new CompileShouldSucceedListener(
087: true);
088: _model.setResetAfterCompile(true);
089: _model.addListener(listener);
090: _model.getCompilerModel().compileAll();
091: listener.waitCompileDone();
092:
093: if (_model.getCompilerModel().getNumErrors() > 0) {
094: // System.err.println("Compile failed");
095: fail("compile failed: " + getCompilerErrorString());
096: }
097: listener.waitResetDone();
098:
099: // System.err.println("Reached end of compilation");
100: assertCompileErrorsPresent("compile should succeed", false);
101: listener.checkCompileOccurred();
102:
103: // System.err.println("Checked that compile occurred");
104: _model.removeListener(listener);
105: // System.err.println("Removed compilation listener");
106: }
107:
108: /** If we try to compile an unsaved file, and if we don't save when asked to saveAllBeforeProceeding, it should
109: * not do the compile or any other actions.
110: */
111: public void testCompileAbortsIfUnsaved()
112: throws BadLocationException, IOException {
113: final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
114:
115: TestListener listener = new TestListener() {
116: public void saveBeforeCompile() {
117: assertModified(true, doc);
118: synchronized (this ) {
119: saveBeforeCompileCount++;
120: }
121: // since we don't actually save the compile should abort
122: }
123: };
124:
125: _model.addListener(listener);
126: doc.startCompile();
127: listener.assertSaveBeforeCompileCount(1);
128: assertModified(true, doc);
129: assertContents(FOO_TEXT, doc);
130: _model.removeListener(listener);
131: }
132:
133: /** If we try to compile while any files are unsaved, and if we don't save when asked to saveAllBeforeProceeding,
134: * it should not do the compile or any other actions.
135: */
136: public void testCompileAbortsIfAnyUnsaved()
137: throws BadLocationException, IOException {
138: final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
139: final OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT);
140:
141: TestListener listener = new TestListener() {
142: public void saveBeforeCompile() {
143: assertModified(true, doc);
144: assertModified(true, doc2);
145: synchronized (this ) {
146: saveBeforeCompileCount++;
147: }
148: // since we don't actually save the compile should abort
149: }
150: };
151:
152: _model.addListener(listener);
153: doc.startCompile();
154: listener.assertSaveBeforeCompileCount(1);
155: assertModified(true, doc);
156: assertModified(true, doc2);
157: assertContents(FOO_TEXT, doc);
158: assertContents(BAR_TEXT, doc2);
159: _model.removeListener(listener);
160: }
161:
162: /** If we try to compile while any files (including the active file) are unsaved but we do save it from within saveAllBeforeProceeding, the
163: * compile should occur happily. Doesn't reset interactions because no interpretations are performed.
164: */
165: public void testCompileAnyUnsavedButSaveWhenAsked()
166: throws BadLocationException, IOException,
167: InterruptedException {
168: final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
169: final OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT);
170: final File file = tempFile();
171: final File file2 = tempFile(2);
172:
173: CompileShouldSucceedListener listener = new CompileShouldSucceedListener(
174: false) {
175: public void saveBeforeCompile() {
176: assertModified(true, doc);
177: assertModified(true, doc2);
178: assertSaveCount(0);
179: assertCompileStartCount(0);
180: assertCompileEndCount(0);
181: assertInterpreterReadyCount(0);
182: assertConsoleResetCount(0);
183:
184: try {
185: doc.saveFile(new FileSelector(file));
186: doc2.saveFile(new FileSelector(file2));
187: } catch (IOException ioe) {
188: fail("Save produced exception: " + ioe);
189: }
190:
191: synchronized (this ) {
192: saveBeforeCompileCount++;
193: }
194: }
195:
196: public void fileSaved(OpenDefinitionsDocument doc) {
197: assertModified(false, doc);
198: assertSaveBeforeCompileCount(0);
199: assertCompileStartCount(0);
200: assertCompileEndCount(0);
201: assertInterpreterReadyCount(0);
202: assertConsoleResetCount(0);
203:
204: try {
205: doc.getFile();
206: } catch (FileMovedException fme) {
207: // We know file should exist
208: fail("file does not exist");
209: }
210: //assertEquals("file saved", file, f);
211: synchronized (this ) {
212: saveCount++;
213: }
214: }
215: };
216:
217: _model.addListener(listener);
218: doc.startCompile();
219: listener.waitCompileDone();
220:
221: if (_model.getCompilerModel().getNumErrors() > 0) {
222: fail("compile failed: " + getCompilerErrorString());
223: }
224:
225: // Check events fired
226: listener.assertSaveBeforeCompileCount(1);
227: listener.assertSaveCount(2);
228: assertCompileErrorsPresent("compile should succeed", false);
229: listener.checkCompileOccurred();
230:
231: // Make sure .class exists
232: File compiled = classForJava(file, "DrJavaTestFoo");
233: assertTrue("Class file doesn't exist after compile", compiled
234: .exists());
235: _model.removeListener(listener);
236: }
237:
238: /** If we try to compile while any files (but not the active file) are unsaved but we do save it from within
239: * saveAllBeforeProceeding, the compile should occur happily. Does not reset interactions.
240: */
241: public void testCompileActiveSavedAnyUnsavedButSaveWhenAsked()
242: throws BadLocationException, IOException,
243: InterruptedException {
244:
245: final OpenDefinitionsDocument doc = setupDocument(FOO_TEXT);
246: final OpenDefinitionsDocument doc2 = setupDocument(BAR_TEXT);
247: final File file = tempFile();
248: final File file2 = tempFile(1);
249:
250: CompileShouldSucceedListener listener = new CompileShouldSucceedListener(
251: false) {
252: public void saveBeforeCompile() {
253: assertModified(false, doc);
254: assertModified(true, doc2);
255: assertSaveCount(0);
256: assertCompileStartCount(0);
257: assertCompileEndCount(0);
258: assertInterpreterReadyCount(0);
259: assertConsoleResetCount(0);
260:
261: try {
262: doc2.saveFile(new FileSelector(file2));
263: } catch (IOException ioe) {
264: fail("Save produced exception: " + ioe);
265: }
266:
267: synchronized (this ) {
268: saveBeforeCompileCount++;
269: }
270: assertModified(false, doc);
271: assertModified(false, doc2);
272: assertTrue(!_model.hasModifiedDocuments());
273: }
274:
275: public void fileSaved(OpenDefinitionsDocument doc) {
276: assertModified(false, doc);
277: assertSaveBeforeCompileCount(0);
278: assertCompileStartCount(0);
279: assertCompileEndCount(0);
280: assertInterpreterReadyCount(0);
281: assertConsoleResetCount(0);
282:
283: File f = null;
284: try {
285: f = doc.getFile();
286: } catch (FileMovedException fme) {
287: // We know file should exist
288: fail("file does not exist");
289: }
290: assertEquals("file saved", file2, f);
291: synchronized (this ) {
292: saveCount++;
293: }
294: }
295: };
296:
297: assertModified(true, doc);
298: doc.saveFile(new FileSelector(file));
299: assertModified(false, doc);
300: assertModified(true, doc2);
301: _model.addListener(listener);
302: doc.startCompile();
303: listener.waitCompileDone();
304: if (_model.getCompilerModel().getNumErrors() > 0) {
305: fail("compile failed: " + getCompilerErrorString());
306: }
307: assertTrue(!_model.hasModifiedDocuments());
308:
309: // Check events fired
310: listener.assertCompileStartCount(1);
311: listener.assertSaveBeforeCompileCount(1);
312: listener.assertSaveCount(1);
313: assertCompileErrorsPresent("compile should succeed", false);
314: listener.checkCompileOccurred();
315:
316: // Make sure .class exists
317: File compiled = classForJava(file, "DrJavaTestFoo");
318: assertTrue("Class file doesn't exist after compile", compiled
319: .exists());
320: _model.removeListener(listener);
321: }
322: }
|