KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.*;
37
38 import java.io.*;
39
40 import javax.swing.text.BadLocationException JavaDoc;
41 import javax.swing.text.Position JavaDoc;
42
43 import edu.rice.cs.drjava.model.compiler.*;
44
45 /** Tests to ensure that compilation fails when expected, and that the errors
46  * are reported correctly.
47  *
48  * Every test in this class is run for *each* of the compilers that is available.
49  *
50  * @version $Id: GlobalModelCompileErrorsTest.java 3839 2006-05-14 20:28:51Z rcartwright $
51  */

52 public final class GlobalModelCompileErrorsTest extends GlobalModelTestCase {
53   
54   private static final String JavaDoc FOO_MISSING_CLOSE_TEXT = "class DrJavaTestFoo {";
55   private static final String JavaDoc BAR_MISSING_SEMI_TEXT = "class DrJavaTestBar { int x }";
56   private static final String JavaDoc FOO_PACKAGE_AFTER_IMPORT = "import java.util.*;\npackage a;\n" + FOO_TEXT;
57   private static final String JavaDoc FOO_PACKAGE_INSIDE_CLASS = "class DrJavaTestFoo { package a; }";
58   private static final String JavaDoc FOO_PACKAGE_AS_FIELD = "class DrJavaTestFoo { int package; }";
59   private static final String JavaDoc FOO_PACKAGE_AS_FIELD_2 = "class DrJavaTestFoo { int package = 5; }";
60   private static final String JavaDoc BAR_MISSING_SEMI_TEXT_MULTIPLE_LINES =
61     "class DrJavaTestFoo {\n int a = 5;\n int x\n }";
62   
63 // /** Overrides setUp in order to save the Untitled file that resides in the model currently, so that saveBeforeCompile will not cause a failure*/
64
// public void setUp() throws IOException{
65
// super.setUp();
66
// _model.getOpenDefinitionsDocuments().get(0).saveFile(new FileSelector(new File(_tempDir, "blank document")));
67
// }
68

69 // /** Overrides {@link TestCase#runBare} to interatively run this test case for each compiler, without resetting
70
// * the interactions JVM. This method is called once per test method, and it magically invokes the method.
71
// */
72
// public void runBare() throws Throwable {
73
// CompilerInterface[] compilers = CompilerRegistry.ONLY.getAvailableCompilers();
74
// for (int i = 0; i < compilers.length; i++) {
75
// //System.out.println("Run " + i + ": " + compilers[i]);
76
// setUp();
77
// _model.getCompilerModel().setActiveCompiler(compilers[i]);
78
// try { runTest(); }
79
// finally { tearDown(); }
80
// }
81
// }
82

83   /** Gets the name of the compiler.
84    * @return the string representation of the active compiler
85    */

86   private String JavaDoc _name() {
87     return "compiler=" + _model.getCompilerModel().getActiveCompiler().getName() + ": ";
88   }
89
90   /** Tests calling compileAll with different source roots works if the files have errors in them. (Each file
91    * has 1 error.)
92    * Note that this testcase will fail if several compilers can be found through the .drjava file.
93    * As the test is then run one time per compiler it can find.
94    */

95   public void testCompileAllFailsDifferentSourceRoots() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
96     
97     File aDir = new File(_tempDir, "a");
98     File bDir = new File(_tempDir, "b");
99     aDir.mkdir();
100     bDir.mkdir();
101     
102     OpenDefinitionsDocument doc = setupDocument(FOO_MISSING_CLOSE_TEXT);
103     final File file = new File(aDir, "DrJavaTestFoo.java");
104     doc.saveFile(new FileSelector(file));
105                       
106     OpenDefinitionsDocument doc2 = setupDocument(BAR_MISSING_SEMI_TEXT);
107     final File file2 = new File(bDir, "DrJavaTestBar.java");
108     doc2.saveFile(new FileSelector(file2));
109     
110     CompileShouldFailListener listener = new CompileShouldFailListener();
111
112     _model.addListener(listener);
113     
114     CompilerModel cm = _model.getCompilerModel();
115     cm.compileAll();
116     listener.waitCompileDone();
117
118     assertCompileErrorsPresent(_name(), true);
119     assertEquals("Should have 2 compiler errors", 2, _model.getCompilerModel().getNumErrors());
120     listener.checkCompileOccurred();
121
122     // Make sure .class does not exist for both files
123
File compiled = classForJava(file, "DrJavaTestFoo");
124     assertEquals(_name() + "Class file exists after failing compile (1)", false, compiled.exists());
125     File compiled2 = classForJava(file2, "DrJavaTestBar");
126     assertEquals(_name() + "Class file exists after failing compile (2)", false, compiled2.exists());
127     _model.removeListener(listener);
128   }
129
130   /** Creates a source file with "package" as a field name and ensures that compile starts but fails due to
131    * the invalid field name.
132    */

133   public void testCompilePackageAsField() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
134     OpenDefinitionsDocument doc = setupDocument(FOO_PACKAGE_AS_FIELD);
135     final File file = tempFile();
136     doc.saveFile(new FileSelector(file));
137     
138     CompileShouldFailListener listener = new CompileShouldFailListener();
139     _model.addListener(listener);
140     
141     doc.startCompile();
142     listener.waitCompileDone();
143     listener.checkCompileOccurred();
144
145     // There better be an error since "package" can not be an identifier!
146
assertCompileErrorsPresent(_name(), true);
147
148     File compiled = classForJava(file, "DrJavaTestFoo");
149     assertEquals(_name() + "Class file exists after failing compile", false, compiled.exists());
150     _model.removeListener(listener);
151   }
152
153   /** Creates a source file with "package" as a field name and ensures that compile starts but fails due to the
154    * invalid field name. This is different from {@link #testCompilePackageAsField} as it initializes the field.
155    */

156   public void testCompilePackageAsField2() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
157     OpenDefinitionsDocument doc = setupDocument(FOO_PACKAGE_AS_FIELD_2);
158     final File file = tempFile();
159     doc.saveFile(new FileSelector(file));
160     
161     CompileShouldFailListener listener = new CompileShouldFailListener();
162     _model.addListener(listener);
163     
164     doc.startCompile();
165     listener.waitCompileDone();
166     listener.checkCompileOccurred();
167
168     // There better be an error since "package" can not be an identifier!
169
assertCompileErrorsPresent(_name(), true);
170
171     File compiled = classForJava(file, "DrJavaTestFoo");
172     assertEquals(_name() + "Class file exists after failing compile", false, compiled.exists());
173     _model.removeListener(listener);
174   }
175
176   /** Tests compiling an invalid file and checks to make sure the class file was not created. */
177   public void testCompileMissingCloseSquiggly() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
178     OpenDefinitionsDocument doc = setupDocument(FOO_MISSING_CLOSE_TEXT);
179     final File file = tempFile();
180     doc.saveFile(new FileSelector(file));
181    
182     CompileShouldFailListener listener = new CompileShouldFailListener();
183     _model.addListener(listener);
184     
185     doc.startCompile();
186     listener.waitCompileDone();
187     assertCompileErrorsPresent(_name(), true);
188     listener.checkCompileOccurred();
189
190     File compiled = classForJava(file, "DrJavaTestFoo");
191     assertTrue(_name() + "Class file exists after compile?!", !compiled.exists());
192     _model.removeListener(listener);
193   }
194
195   /** Puts an otherwise valid package statement inside a class declaration. This better not work! */
196   public void testCompileWithPackageStatementInsideClass() throws BadLocationException JavaDoc, IOException,
197     InterruptedException JavaDoc {
198     
199     // Create temp file
200
File baseTempDir = tempDirectory();
201     File subdir = new File(baseTempDir, "a");
202     File fooFile = new File(subdir, "DrJavaTestFoo.java");
203     File compiled = classForJava(fooFile, "DrJavaTestFoo");
204
205     // Now make subdirectory a
206
subdir.mkdir();
207
208     // Save the footext to DrJavaTestFoo.java in the subdirectory
209
OpenDefinitionsDocument doc = setupDocument(FOO_PACKAGE_INSIDE_CLASS);
210     doc.saveFileAs(new FileSelector(fooFile));
211
212     // do compile -- should fail since package decl is not valid!
213
CompileShouldFailListener listener = new CompileShouldFailListener();
214     _model.addListener(listener);
215     
216     doc.startCompile();
217     listener.waitCompileDone();
218
219     listener.checkCompileOccurred();
220     assertCompileErrorsPresent(_name(), true);
221     assertTrue(_name() + "Class file exists after failed compile", !compiled.exists());
222
223     // check that model.resetCompilerErrors works
224
_model.getCompilerModel().resetCompilerErrors();
225     CompilerErrorModel cem = _model.getCompilerModel().getCompilerErrorModel();
226     assertEquals("CompilerErrorModel has errors after reset", 0, cem.getNumErrors());
227     _model.removeListener(listener);
228   }
229   
230    
231
232   /** Tests the compiler errors have the correct line numbers.
233    * TODO: rewrite this test for the new error model interface
234    */

235   public void testCompileFailsCorrectLineNumbers() throws BadLocationException JavaDoc, IOException, InterruptedException JavaDoc {
236     File aDir = new File(_tempDir, "a");
237     File bDir = new File(_tempDir, "b");
238     aDir.mkdir();
239     bDir.mkdir();
240     OpenDefinitionsDocument doc = setupDocument(FOO_PACKAGE_AFTER_IMPORT);
241     final File file = new File(aDir, "DrJavaTestFoo.java");
242     doc.saveFile(new FileSelector(file));
243     OpenDefinitionsDocument doc2 = setupDocument(BAR_MISSING_SEMI_TEXT_MULTIPLE_LINES);
244     final File file2 = new File(bDir, "DrJavaTestBar.java");
245     doc2.saveFile(new FileSelector(file2));
246     
247    
248     // do compile -- should fail since package decl is not valid!
249
CompileShouldFailListener listener = new CompileShouldFailListener();
250     _model.addListener(listener);
251
252     CompilerModel cm = _model.getCompilerModel();
253     cm.compileAll();
254     listener.waitCompileDone();
255     
256     assertCompileErrorsPresent(_name(), true);
257     assertEquals("Should have 2 compiler errors", 2, _model.getCompilerModel().getNumErrors());
258     listener.checkCompileOccurred();
259     _model.removeListener(listener);
260
261     CompilerErrorModel cme = cm.getCompilerErrorModel();
262     assertEquals("Should have had two errors", 2, cme.getNumErrors());
263
264     CompilerError ce1 = cme.getError(0);
265     CompilerError ce2 = cme.getError(1);
266     assertEquals("first doc should have an error", file.getCanonicalFile(), ce1.file().getCanonicalFile());
267     assertEquals("second doc should have an error", file2.getCanonicalFile(), ce2.file().getCanonicalFile());
268
269     Position JavaDoc p1 = cme.getPosition(ce1);
270     Position JavaDoc p2 = cme.getPosition(ce2);
271     assertTrue("location of first error should be between 20 and 29 inclusive (line 2), but was " + p1.getOffset(),
272                p1.getOffset() <= 20 && p1.getOffset() <= 29);
273     assertTrue("location of error should be after 34 (line 3 or 4)", p2.getOffset() >= 34);
274   }
275 }
276
Popular Tags