KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > compiler > CompilerErrorModelTest


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 /** This class tests the internal functionality of CompilerErrorModel using a dummy implementation of the
35  * IGetDocuments interface.
36  *
37  * @version $Id: CompilerErrorModelTest.java 4031 2006-11-15 22:09:06Z rcartwright $
38  */

39 package edu.rice.cs.drjava.model.compiler;
40
41 import java.io.File JavaDoc;
42 import java.io.IOException JavaDoc;
43 import javax.swing.text.Position JavaDoc;
44
45 import junit.framework.TestCase;
46 import edu.rice.cs.drjava.model.*;
47 import edu.rice.cs.drjava.DrJavaTestCase;
48 import edu.rice.cs.util.OperationCanceledException;
49
50 /** Tests the CompilerErrorModel.
51  * @version $Id: CompilerErrorModelTest.java 4031 2006-11-15 22:09:06Z rcartwright $
52  */

53 public final class CompilerErrorModelTest extends DrJavaTestCase {
54   private File JavaDoc[] files;
55   private String JavaDoc[] texts;
56   private TestDocGetter getter; // subclass of DummyGlobalModel
57
private CompilerError[] errors;
58   private CompilerErrorModel model;
59   
60   /** Tests CompilerErrorModel setup code with no compiler errors. */
61   public void testConstructNoErrors() {
62     getter = new TestDocGetter();
63     model = new CompilerErrorModel(new CompilerError[0], getter);
64     
65     // We successfully built the model, now test the basics.
66
assertEquals("Should have no compiler errors.", 0, model.getNumErrors());
67     assertEquals("Should have 0 warnings" , 0, model.getNumWarnings());
68     assertEquals("Should have 0 compiler errors" , 0, model.getNumCompErrors());
69     assertTrue("hasOnlyWarnings should return true.", model.hasOnlyWarnings());
70   }
71   
72   /** Tests CompilerErrorModel setup code with only warnings without files. Also tests hasOnlyWarnings logic. */
73   public void testConstructOnlyWarnings() {
74     getter = new TestDocGetter();
75     errors = new CompilerError[] {
76       new CompilerError("Test warning without File", true),
77       new CompilerError("Test warning without File", true)
78     };
79     model = new CompilerErrorModel(errors, getter);
80     
81     // We successfully built the model, now test the basics.
82
assertEquals("Should have 2 errors.", 2, model.getNumErrors());
83     assertEquals("Should have 2 warnings" , 2, model.getNumWarnings());
84     assertEquals("Should have 0 compiler errors" , 0, model.getNumCompErrors());
85     assertTrue("hasOnlyWarnings should return true.", model.hasOnlyWarnings());
86   }
87   
88   /** Tests CompilerErrorModel setup code with only errors without files. */
89   public void testConstructDoclessErrors() {
90     getter = new TestDocGetter();
91     errors = new CompilerError[] {
92       new CompilerError("Test error without File", false),
93       new CompilerError("Test warning without File", true),
94       new CompilerError("Test error without File", false)
95     };
96     
97     CompilerError[] copy = new CompilerError[errors.length];
98     for (int i = 0; i < errors.length; i++) copy[i] = errors[i];
99     model = new CompilerErrorModel(copy, getter);
100     
101     // We successfully built the model, now test the basics.
102
assertEquals("Should have 3 compiler errors.", 3, model.getNumErrors());
103     assertEquals("Should have 1 warning" , 1, model.getNumWarnings());
104     assertEquals("Should have 2 compiler errors" , 2, model.getNumCompErrors());
105 // System.out.println(model.getError(0) + "\n" + model.getError(1) + "\n" + model.getError(2));
106
assertEquals("Errors should be sorted.", errors[1], model.getError(2));
107     assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings());
108   }
109   
110   /** Tests CompilerErrorModel setup code with one file and only errors without line numbers. */
111   public void testConstructOneDocWithoutLineNums() {
112     setupDoc();
113     errors = new CompilerError[] {
114       new CompilerError(files[0], "Test error with File", false),
115       new CompilerError(files[0], "Test warning with File", true),
116       new CompilerError(files[0], "Test error with File", false)
117     };
118     
119     CompilerError[] copy = new CompilerError[errors.length];
120     for (int i = 0; i < errors.length; i++) copy[i] = errors[i];
121     model = new CompilerErrorModel(copy, getter);
122     
123     // We successfully built the model, now test the basics.
124
assertEquals("Should have 3 compiler errors.", 3, model.getNumErrors());
125     assertEquals("Should have 1 warning" , 1, model.getNumWarnings());
126     assertEquals("Should have 2 compiler errors" , 2, model.getNumCompErrors());
127     assertEquals("Errors should be sorted.", errors[1], model.getError(2));
128     assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings());
129   }
130   
131   /** Tests CompilerErrorModel setup code with one file and only errors with line numbers. */
132   public void testConstructOneDocWithLineNums() {
133     setupDoc();
134     errors = new CompilerError[] {
135       new CompilerError(files[0], 2, 0, "Test error with File and line", false),
136       new CompilerError(files[0], 1, 0, "Test warning with File and line", true),
137       new CompilerError(files[0], 3, 0, "Test error with File and line", false),
138       new CompilerError(files[0], 1, 0, "Test error with File and line", false)
139     };
140     
141     CompilerError[] copy = new CompilerError[errors.length];
142     for (int i = 0; i < errors.length; i++) copy[i] = errors[i];
143     model = new CompilerErrorModel(copy, getter);
144     
145     // We successfully built the model, now test the basics.
146
assertEquals("Should have 4 compiler errors.", 4, model.getNumErrors());
147     assertEquals("Should have 1 warning" , 1, model.getNumWarnings());
148     assertEquals("Should have compiler errors" , 3, model.getNumCompErrors());
149     assertEquals("Errors should be sorted.", errors[3], model.getError(0));
150     assertEquals("Errors should be sorted.", errors[1], model.getError(1));
151     assertEquals("Errors should be sorted.", errors[0], model.getError(2));
152     assertEquals("Errors should be sorted.", errors[2], model.getError(3));
153     assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings());
154   }
155   
156   /** Tests CompilerErrorModel setup code with one file and errors both with and without line numbers. */
157   public void testConstructOneDocWithBoth() {
158     setupDoc();
159     errors = new CompilerError[] {
160       new CompilerError(files[0], 2, 0, "Test error with File and line", false),
161       new CompilerError(files[0], "Test warning with File (no line)", true),
162       new CompilerError(files[0], 3, 0, "Test error with File and line", false),
163       new CompilerError("Test error without File or line", false),
164       new CompilerError(files[0], 3, 0, "Test warning with File and line", true),
165       new CompilerError(files[0], "Test error with File (no line)", false),
166       new CompilerError(files[0], 1, 0, "Test error with File and line", false)
167     };
168     
169     CompilerError[] copy = new CompilerError[errors.length];
170     for (int i = 0; i < errors.length; i++) copy[i] = errors[i];
171     model = new CompilerErrorModel(copy, getter);
172     
173     // We successfully built the model, now test the basics.
174
assertEquals("Should have 7 compiler errors.", 7, model.getNumErrors());
175     assertEquals("Should have 2 warnings" , 2, model.getNumWarnings());
176     assertEquals("Should have 5 compiler errors" , 5, model.getNumCompErrors());
177     assertEquals("Errors should be sorted.", errors[3], model.getError(0));
178     assertEquals("Errors should be sorted.", errors[5], model.getError(1));
179     assertEquals("Errors should be sorted.", errors[1], model.getError(2));
180     assertEquals("Errors should be sorted.", errors[6], model.getError(3));
181     assertEquals("Errors should be sorted.", errors[0], model.getError(4));
182     assertEquals("Errors should be sorted.", errors[2], model.getError(5));
183     assertEquals("Errors should be sorted.", errors[4], model.getError(6));
184     assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings());
185   }
186   
187   /** Tests CompilerErrorModel setup code with several files and only errors without line numbers. */
188   public void testConstructManyDocsWithoutLineNums() {
189     setupDocs();
190     errors = new CompilerError[] {
191       new CompilerError(files[0], "Test error with File", false),
192       new CompilerError(files[2], "Test warning with File", true),
193       new CompilerError(files[4], "Test warning with File", true),
194       new CompilerError(files[1], "Test error with File", false),
195       new CompilerError(files[3], "Test warning with File", true),
196       new CompilerError(files[3], "Test error with File", false),
197       new CompilerError(files[4], "Test error with File", false),
198       new CompilerError(files[0], "Test error with File", false)
199     };
200     
201     CompilerError[] copy = new CompilerError[errors.length];
202     for (int i = 0; i < errors.length; i++) copy[i] = errors[i];
203     model = new CompilerErrorModel(copy, getter);
204     
205     // We successfully built the model, now test the basics.
206
assertEquals("Should have 8 compiler errors.", 8, model.getNumErrors());
207     assertEquals("Should have 3 warnings" , 3, model.getNumWarnings());
208     assertEquals("Should have 5 compiler errors" , 5, model.getNumCompErrors());
209     assertEquals("Errors should be sorted.", errors[0], model.getError(0));
210     assertEquals("Errors should be sorted.", errors[7], model.getError(1));
211     assertEquals("Errors should be sorted.", errors[3], model.getError(2));
212     assertEquals("Errors should be sorted.", errors[1], model.getError(3));
213     assertEquals("Errors should be sorted.", errors[5], model.getError(4));
214     assertEquals("Errors should be sorted.", errors[4], model.getError(5));
215     assertEquals("Errors should be sorted.", errors[6], model.getError(6));
216     assertEquals("Errors should be sorted.", errors[2], model.getError(7));
217     assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings());
218   }
219   
220   /** Tests CompilerErrorModel setup code with several files and only errors with line numbers. */
221   public void testConstructManyDocsWithLineNums() {
222     setupDocs();
223     errors = new CompilerError[] {
224       new CompilerError(files[0], 2, 0, "Test error with File", false),
225       new CompilerError(files[2], 3, 0, "Test warning with File", true),
226       new CompilerError(files[4], 1, 0, "Test warning with File", true),
227       new CompilerError(files[1], 2, 0, "Test error with File", false),
228       new CompilerError(files[2], 2, 0, "Test warning with File", true),
229       new CompilerError(files[3], 3, 0, "Test error with File", false),
230       new CompilerError(files[4], 3, 0, "Test error with File", false),
231       new CompilerError(files[0], 1, 0, "Test error with File", false)
232     };
233     
234     CompilerError[] copy = new CompilerError[errors.length];
235     for (int i = 0; i < errors.length; i++) copy[i] = errors[i];
236     model = new CompilerErrorModel(copy, getter);
237     
238     // We successfully built the model, now test the basics.
239
assertEquals("Should have 8 compiler errors.", 8, model.getNumErrors());
240     assertEquals("Should have 3 warnings" , 3, model.getNumWarnings());
241     assertEquals("Should have 5 compiler errors" , 5, model.getNumCompErrors());
242     assertEquals("Errors should be sorted.", errors[7], model.getError(0));
243     assertEquals("Errors should be sorted.", errors[0], model.getError(1));
244     assertEquals("Errors should be sorted.", errors[3], model.getError(2));
245     assertEquals("Errors should be sorted.", errors[4], model.getError(3));
246     assertEquals("Errors should be sorted.", errors[1], model.getError(4));
247     assertEquals("Errors should be sorted.", errors[5], model.getError(5));
248     assertEquals("Errors should be sorted.", errors[2], model.getError(6));
249     assertEquals("Errors should be sorted.", errors[6], model.getError(7));
250     assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings());
251   }
252   
253   /** Tests CompilerErrorModel setup code with several files and errors both with and without line numbers. */
254   public void testConstructManyDocsWithBoth() {
255     fullSetup();
256     
257     // We successfully built the model, now test the basics.
258
assertEquals("Should have 15 compiler errors.", 15, model.getNumErrors());
259     assertEquals("Should have 6 warnings" , 6, model.getNumWarnings());
260     assertEquals("Should have 9 compiler errors" , 9, model.getNumCompErrors());
261     assertEquals("Errors should be sorted.", errors[0], model.getError(0));
262     assertEquals("Errors should be sorted.", errors[14], model.getError(1));
263     assertEquals("Errors should be sorted.", errors[12], model.getError(2));
264     assertEquals("Errors should be sorted.", errors[7], model.getError(3));
265     assertEquals("Errors should be sorted.", errors[6], model.getError(4));
266     assertEquals("Errors should be sorted.", errors[8], model.getError(5));
267     assertEquals("Errors should be sorted.", errors[2], model.getError(6));
268     assertEquals("Errors should be sorted.", errors[13], model.getError(7));
269     assertEquals("Errors should be sorted.", errors[4], model.getError(8));
270     assertEquals("Errors should be sorted.", errors[9], model.getError(9));
271     assertEquals("Errors should be sorted.", errors[10], model.getError(10));
272     assertEquals("Errors should be sorted.", errors[11], model.getError(11));
273     assertEquals("Errors should be sorted.", errors[3], model.getError(12));
274     assertEquals("Errors should be sorted.", errors[5], model.getError(13));
275     assertEquals("Errors should be sorted.", errors[1], model.getError(14));
276     assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings());
277   }
278   
279   /** Tests CompilerErrorModel.getPosition(CompilerError). */
280   public void testGetPosition() {
281     fullSetup();
282     
283     Position JavaDoc pos = model.getPosition(errors[1]);
284     assertEquals("Incorrect error Position.", 125, pos.getOffset());
285     pos = model.getPosition(errors[5]);
286     assertEquals("Incorrect error Position.", 38, pos.getOffset());
287   }
288   
289   /** Tests CompilerErrorModel.getErrorAtOffset(int). */
290   public void testGetErrorAtOffset() throws IOException JavaDoc, OperationCanceledException {
291     fullSetup();
292     
293     OpenDefinitionsDocument doc = getter.getDocumentForFile(files[4]);
294     assertEquals("Wrong error at given offset.", errors[1],
295                  model.getErrorAtOffset(doc, 125));
296     doc = getter.getDocumentForFile(files[4]);
297     assertEquals("Wrong error at given offset.", errors[5],
298                  model.getErrorAtOffset(doc, 38));
299   }
300   
301   /** Tests CompilerErrorModel.hasErrorsWithPositions(OpenDefinitionsDocument). */
302   public void testHasErrorsWithPositions() throws IOException JavaDoc, OperationCanceledException {
303     fullSetup();
304     
305     // Doc with errors
306
OpenDefinitionsDocument doc = getter.getDocumentForFile(files[4]);
307     assertTrue("File should have errors with lines.", model.hasErrorsWithPositions(doc));
308     
309     // Same doc with a different (but equivalent) file name
310
doc.setFile(new File JavaDoc("/tmp/./nowhere5"));
311     assertTrue("Same file should have errors with lines.", model.hasErrorsWithPositions(doc));
312     
313     // Doc without errors
314
doc = getter.getDocumentForFile(files[1]);
315     assertTrue("File shouldn't have errors with lines.", !model.hasErrorsWithPositions(doc));
316   }
317   
318   public void testErrorsInMultipleDocuments() throws IOException JavaDoc, OperationCanceledException {
319     files = new File JavaDoc[] {
320       new File JavaDoc("/tmp/nowhere1"),
321       new File JavaDoc("/tmp/nowhere2")
322     };
323     texts = new String JavaDoc[] {
324       "kfgkasjg\n" + "faijskgisgj\n" + "sifjsidgjsd\n",
325       "isdjfdi\n" + "jfa"
326     };
327     getter = new TestDocGetter(files, texts);
328     
329     errors = new CompilerError[] {
330       new CompilerError(files[1], 0, 0, "Test error with File", false),
331       new CompilerError(files[0], 0, 0, "Test error with File", false)
332     };
333     model = new CompilerErrorModel(errors, getter);
334     model.getErrorAtOffset(getter.getDocumentForFile(files[0]), 25);
335     String JavaDoc temp = texts[0];
336     texts[0] = texts[1];
337     texts[1] = temp;
338     getter = new TestDocGetter(files, texts);
339     errors = new CompilerError[] {
340       new CompilerError(files[0], 0, 0, "Test error with File", false),
341       new CompilerError(files[1], 2, 0, "Test error with File", false)
342     };
343     model = new CompilerErrorModel(errors, getter);
344     model.getErrorAtOffset(getter.getDocumentForFile(files[0]), 10);
345   }
346   
347   /** Setup for test cases with one document. */
348   private void setupDoc() {
349     files = new File JavaDoc[] { new File JavaDoc("/tmp/nowhere") };
350     texts = new String JavaDoc[] {
351       "This is a block of test text.\n" + "It doesn't matter what goes in here.\n" +
352                  "But it does matter if it is manipulated properly!\n"};
353     getter = new TestDocGetter(files, texts);
354   }
355   
356   /** Setup for test cases with several documents. */
357   private void setupDocs() {
358     files = new File JavaDoc[] {
359       new File JavaDoc("/tmp/nowhere1"),
360       new File JavaDoc("/tmp/nowhere2"),
361       new File JavaDoc("/tmp/nowhere3"),
362       new File JavaDoc("/tmp/nowhere4"),
363       new File JavaDoc("/tmp/nowhere5")
364     };
365     texts = new String JavaDoc[] {
366       "This is the first block of test text.\n" + "It doesn't matter what goes in here.\n" +
367                  "But it does matter if it is manipulated properly!\n",
368       "This is the second block of test text.\n" + "It doesn't matter what goes in here.\n" +
369                  "But it does matter if it is manipulated properly!\n",
370       "This is the third block of test text.\n" + "It doesn't matter what goes in here.\n" +
371                  "But it does matter if it is manipulated properly!\n",
372       "This is the fourth block of test text.\n" + "It doesn't matter what goes in here.\n" +
373                  "But it does matter if it is manipulated properly!\n",
374       "This is the fifth block of test text.\n" + "It doesn't matter what goes in here.\n" +
375                  "But it does matter if it is manipulated properly!\n" };
376     getter = new TestDocGetter(files, texts);
377   }
378   
379   /** Extra setup for test cases with several documents. */
380   private void fullSetup() {
381     setupDocs();
382     errors = new CompilerError[] {
383       new CompilerError(files[0], "Test error with File (no line)", false),
384       new CompilerError(files[4], 3, 0, "Test error with File", false),
385       new CompilerError(files[2], "Test warning with File (no line)", true),
386       new CompilerError(files[4], "Test warning with File (no line)", true),
387       new CompilerError(files[2], 3, 0, "Test warning with File", true),
388       new CompilerError(files[4], 1, 0, "Test warning with File", true),
389       new CompilerError(files[1], "Test warning with File (no line)", true),
390       new CompilerError(files[1], "Test error with File (no line)", false),
391       new CompilerError(files[2], "Test error with File (no line)", false),
392       new CompilerError(files[3], "Test error with File (no line)", false),
393       new CompilerError(files[3], 3, 0, "Test error with File", false),
394       new CompilerError(files[4], "Test error with File (no line)", false),
395       new CompilerError(files[0], 2, 0, "Test error with File", false),
396       new CompilerError(files[2], 2, 0, "Test warning with File", true),
397       new CompilerError(files[0], 1, 0, "Test error with File", false)
398     };
399         
400     CompilerError[] copy = new CompilerError[errors.length];
401     for (int i = 0; i < errors.length; i++) copy[i] = errors[i];
402     model = new CompilerErrorModel(copy, getter);
403   }
404 }
405
Popular Tags