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: /** This class tests the internal functionality of CompilerErrorModel using a dummy implementation of the
038: * IGetDocuments interface.
039: *
040: * @version $Id: CompilerErrorModelTest.java 4255 2007-08-28 19:17:37Z mgricken $
041: */package edu.rice.cs.drjava.model.compiler;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import javax.swing.text.Position;
046:
047: import junit.framework.TestCase;
048: import edu.rice.cs.drjava.model.*;
049: import edu.rice.cs.drjava.DrJavaTestCase;
050: import edu.rice.cs.util.OperationCanceledException;
051:
052: /** Tests the CompilerErrorModel.
053: * @version $Id: CompilerErrorModelTest.java 4255 2007-08-28 19:17:37Z mgricken $
054: */
055: public final class CompilerErrorModelTest extends DrJavaTestCase {
056: private File[] files;
057: private String[] texts;
058: private TestDocGetter getter; // subclass of DummyGlobalModel
059: private CompilerError[] errors;
060: private CompilerErrorModel model;
061:
062: /** Tests CompilerErrorModel setup code with no compiler errors. */
063: public void testConstructNoErrors() {
064: getter = new TestDocGetter();
065: model = new CompilerErrorModel(new CompilerError[0], getter);
066:
067: // We successfully built the model, now test the basics.
068: assertEquals("Should have no compiler errors.", 0, model
069: .getNumErrors());
070: assertEquals("Should have 0 warnings", 0, model
071: .getNumWarnings());
072: assertEquals("Should have 0 compiler errors", 0, model
073: .getNumCompErrors());
074: assertTrue("hasOnlyWarnings should return true.", model
075: .hasOnlyWarnings());
076: }
077:
078: /** Tests CompilerErrorModel setup code with only warnings without files. Also tests hasOnlyWarnings logic. */
079: public void testConstructOnlyWarnings() {
080: getter = new TestDocGetter();
081: errors = new CompilerError[] {
082: new CompilerError("Test warning without File", true),
083: new CompilerError("Test warning without File", true) };
084: model = new CompilerErrorModel(errors, getter);
085:
086: // We successfully built the model, now test the basics.
087: assertEquals("Should have 2 errors.", 2, model.getNumErrors());
088: assertEquals("Should have 2 warnings", 2, model
089: .getNumWarnings());
090: assertEquals("Should have 0 compiler errors", 0, model
091: .getNumCompErrors());
092: assertTrue("hasOnlyWarnings should return true.", model
093: .hasOnlyWarnings());
094: }
095:
096: /** Tests CompilerErrorModel setup code with only errors without files. */
097: public void testConstructDoclessErrors() {
098: getter = new TestDocGetter();
099: errors = new CompilerError[] {
100: new CompilerError("Test error without File", false),
101: new CompilerError("Test warning without File", true),
102: new CompilerError("Test error without File", false) };
103:
104: CompilerError[] copy = new CompilerError[errors.length];
105: for (int i = 0; i < errors.length; i++)
106: copy[i] = errors[i];
107: model = new CompilerErrorModel(copy, getter);
108:
109: // We successfully built the model, now test the basics.
110: assertEquals("Should have 3 compiler errors.", 3, model
111: .getNumErrors());
112: assertEquals("Should have 1 warning", 1, model.getNumWarnings());
113: assertEquals("Should have 2 compiler errors", 2, model
114: .getNumCompErrors());
115: // System.out.println(model.getError(0) + "\n" + model.getError(1) + "\n" + model.getError(2));
116: assertEquals("Errors should be sorted.", errors[1], model
117: .getError(2));
118: assertTrue("hasOnlyWarnings should return false.", !model
119: .hasOnlyWarnings());
120: }
121:
122: /** Tests CompilerErrorModel setup code with one file and only errors without line numbers. */
123: public void testConstructOneDocWithoutLineNums() {
124: setupDoc();
125: errors = new CompilerError[] {
126: new CompilerError(files[0], "Test error with File",
127: false),
128: new CompilerError(files[0], "Test warning with File",
129: true),
130: new CompilerError(files[0], "Test error with File",
131: false) };
132:
133: CompilerError[] copy = new CompilerError[errors.length];
134: for (int i = 0; i < errors.length; i++)
135: copy[i] = errors[i];
136: model = new CompilerErrorModel(copy, getter);
137:
138: // We successfully built the model, now test the basics.
139: assertEquals("Should have 3 compiler errors.", 3, model
140: .getNumErrors());
141: assertEquals("Should have 1 warning", 1, model.getNumWarnings());
142: assertEquals("Should have 2 compiler errors", 2, model
143: .getNumCompErrors());
144: assertEquals("Errors should be sorted.", errors[1], model
145: .getError(2));
146: assertTrue("hasOnlyWarnings should return false.", !model
147: .hasOnlyWarnings());
148: }
149:
150: /** Tests CompilerErrorModel setup code with one file and only errors with line numbers. */
151: public void testConstructOneDocWithLineNums() {
152: setupDoc();
153: errors = new CompilerError[] {
154: new CompilerError(files[0], 2, 0,
155: "Test error with File and line", false),
156: new CompilerError(files[0], 1, 0,
157: "Test warning with File and line", true),
158: new CompilerError(files[0], 3, 0,
159: "Test error with File and line", false),
160: new CompilerError(files[0], 1, 0,
161: "Test error with File and line", false) };
162:
163: CompilerError[] copy = new CompilerError[errors.length];
164: for (int i = 0; i < errors.length; i++)
165: copy[i] = errors[i];
166: model = new CompilerErrorModel(copy, getter);
167:
168: // We successfully built the model, now test the basics.
169: assertEquals("Should have 4 compiler errors.", 4, model
170: .getNumErrors());
171: assertEquals("Should have 1 warning", 1, model.getNumWarnings());
172: assertEquals("Should have compiler errors", 3, model
173: .getNumCompErrors());
174: assertEquals("Errors should be sorted.", errors[3], model
175: .getError(0));
176: assertEquals("Errors should be sorted.", errors[1], model
177: .getError(1));
178: assertEquals("Errors should be sorted.", errors[0], model
179: .getError(2));
180: assertEquals("Errors should be sorted.", errors[2], model
181: .getError(3));
182: assertTrue("hasOnlyWarnings should return false.", !model
183: .hasOnlyWarnings());
184: }
185:
186: /** Tests CompilerErrorModel setup code with one file and errors both with and without line numbers. */
187: public void testConstructOneDocWithBoth() {
188: setupDoc();
189: errors = new CompilerError[] {
190: new CompilerError(files[0], 2, 0,
191: "Test error with File and line", false),
192: new CompilerError(files[0],
193: "Test warning with File (no line)", true),
194: new CompilerError(files[0], 3, 0,
195: "Test error with File and line", false),
196: new CompilerError("Test error without File or line",
197: false),
198: new CompilerError(files[0], 3, 0,
199: "Test warning with File and line", true),
200: new CompilerError(files[0],
201: "Test error with File (no line)", false),
202: new CompilerError(files[0], 1, 0,
203: "Test error with File and line", false) };
204:
205: CompilerError[] copy = new CompilerError[errors.length];
206: for (int i = 0; i < errors.length; i++)
207: copy[i] = errors[i];
208: model = new CompilerErrorModel(copy, getter);
209:
210: // We successfully built the model, now test the basics.
211: assertEquals("Should have 7 compiler errors.", 7, model
212: .getNumErrors());
213: assertEquals("Should have 2 warnings", 2, model
214: .getNumWarnings());
215: assertEquals("Should have 5 compiler errors", 5, model
216: .getNumCompErrors());
217: assertEquals("Errors should be sorted.", errors[3], model
218: .getError(0));
219: assertEquals("Errors should be sorted.", errors[5], model
220: .getError(1));
221: assertEquals("Errors should be sorted.", errors[1], model
222: .getError(2));
223: assertEquals("Errors should be sorted.", errors[6], model
224: .getError(3));
225: assertEquals("Errors should be sorted.", errors[0], model
226: .getError(4));
227: assertEquals("Errors should be sorted.", errors[2], model
228: .getError(5));
229: assertEquals("Errors should be sorted.", errors[4], model
230: .getError(6));
231: assertTrue("hasOnlyWarnings should return false.", !model
232: .hasOnlyWarnings());
233: }
234:
235: /** Tests CompilerErrorModel setup code with several files and only errors without line numbers. */
236: public void testConstructManyDocsWithoutLineNums() {
237: setupDocs();
238: errors = new CompilerError[] {
239: new CompilerError(files[0], "Test error with File",
240: false),
241: new CompilerError(files[2], "Test warning with File",
242: true),
243: new CompilerError(files[4], "Test warning with File",
244: true),
245: new CompilerError(files[1], "Test error with File",
246: false),
247: new CompilerError(files[3], "Test warning with File",
248: true),
249: new CompilerError(files[3], "Test error with File",
250: false),
251: new CompilerError(files[4], "Test error with File",
252: false),
253: new CompilerError(files[0], "Test error with File",
254: false) };
255:
256: CompilerError[] copy = new CompilerError[errors.length];
257: for (int i = 0; i < errors.length; i++)
258: copy[i] = errors[i];
259: model = new CompilerErrorModel(copy, getter);
260:
261: // We successfully built the model, now test the basics.
262: assertEquals("Should have 8 compiler errors.", 8, model
263: .getNumErrors());
264: assertEquals("Should have 3 warnings", 3, model
265: .getNumWarnings());
266: assertEquals("Should have 5 compiler errors", 5, model
267: .getNumCompErrors());
268: assertEquals("Errors should be sorted.", errors[0], model
269: .getError(0));
270: assertEquals("Errors should be sorted.", errors[7], model
271: .getError(1));
272: assertEquals("Errors should be sorted.", errors[3], model
273: .getError(2));
274: assertEquals("Errors should be sorted.", errors[1], model
275: .getError(3));
276: assertEquals("Errors should be sorted.", errors[5], model
277: .getError(4));
278: assertEquals("Errors should be sorted.", errors[4], model
279: .getError(5));
280: assertEquals("Errors should be sorted.", errors[6], model
281: .getError(6));
282: assertEquals("Errors should be sorted.", errors[2], model
283: .getError(7));
284: assertTrue("hasOnlyWarnings should return false.", !model
285: .hasOnlyWarnings());
286: }
287:
288: /** Tests CompilerErrorModel setup code with several files and only errors with line numbers. */
289: public void testConstructManyDocsWithLineNums() {
290: setupDocs();
291: errors = new CompilerError[] {
292: new CompilerError(files[0], 2, 0,
293: "Test error with File", false),
294: new CompilerError(files[2], 3, 0,
295: "Test warning with File", true),
296: new CompilerError(files[4], 1, 0,
297: "Test warning with File", true),
298: new CompilerError(files[1], 2, 0,
299: "Test error with File", false),
300: new CompilerError(files[2], 2, 0,
301: "Test warning with File", true),
302: new CompilerError(files[3], 3, 0,
303: "Test error with File", false),
304: new CompilerError(files[4], 3, 0,
305: "Test error with File", false),
306: new CompilerError(files[0], 1, 0,
307: "Test error with File", false) };
308:
309: CompilerError[] copy = new CompilerError[errors.length];
310: for (int i = 0; i < errors.length; i++)
311: copy[i] = errors[i];
312: model = new CompilerErrorModel(copy, getter);
313:
314: // We successfully built the model, now test the basics.
315: assertEquals("Should have 8 compiler errors.", 8, model
316: .getNumErrors());
317: assertEquals("Should have 3 warnings", 3, model
318: .getNumWarnings());
319: assertEquals("Should have 5 compiler errors", 5, model
320: .getNumCompErrors());
321: assertEquals("Errors should be sorted.", errors[7], model
322: .getError(0));
323: assertEquals("Errors should be sorted.", errors[0], model
324: .getError(1));
325: assertEquals("Errors should be sorted.", errors[3], model
326: .getError(2));
327: assertEquals("Errors should be sorted.", errors[4], model
328: .getError(3));
329: assertEquals("Errors should be sorted.", errors[1], model
330: .getError(4));
331: assertEquals("Errors should be sorted.", errors[5], model
332: .getError(5));
333: assertEquals("Errors should be sorted.", errors[2], model
334: .getError(6));
335: assertEquals("Errors should be sorted.", errors[6], model
336: .getError(7));
337: assertTrue("hasOnlyWarnings should return false.", !model
338: .hasOnlyWarnings());
339: }
340:
341: /** Tests CompilerErrorModel setup code with several files and errors both with and without line numbers. */
342: public void testConstructManyDocsWithBoth() {
343: fullSetup();
344:
345: // We successfully built the model, now test the basics.
346: assertEquals("Should have 15 compiler errors.", 15, model
347: .getNumErrors());
348: assertEquals("Should have 6 warnings", 6, model
349: .getNumWarnings());
350: assertEquals("Should have 9 compiler errors", 9, model
351: .getNumCompErrors());
352: assertEquals("Errors should be sorted.", errors[0], model
353: .getError(0));
354: assertEquals("Errors should be sorted.", errors[14], model
355: .getError(1));
356: assertEquals("Errors should be sorted.", errors[12], model
357: .getError(2));
358: assertEquals("Errors should be sorted.", errors[7], model
359: .getError(3));
360: assertEquals("Errors should be sorted.", errors[6], model
361: .getError(4));
362: assertEquals("Errors should be sorted.", errors[8], model
363: .getError(5));
364: assertEquals("Errors should be sorted.", errors[2], model
365: .getError(6));
366: assertEquals("Errors should be sorted.", errors[13], model
367: .getError(7));
368: assertEquals("Errors should be sorted.", errors[4], model
369: .getError(8));
370: assertEquals("Errors should be sorted.", errors[9], model
371: .getError(9));
372: assertEquals("Errors should be sorted.", errors[10], model
373: .getError(10));
374: assertEquals("Errors should be sorted.", errors[11], model
375: .getError(11));
376: assertEquals("Errors should be sorted.", errors[3], model
377: .getError(12));
378: assertEquals("Errors should be sorted.", errors[5], model
379: .getError(13));
380: assertEquals("Errors should be sorted.", errors[1], model
381: .getError(14));
382: assertTrue("hasOnlyWarnings should return false.", !model
383: .hasOnlyWarnings());
384: }
385:
386: /** Tests CompilerErrorModel.getPosition(CompilerError). */
387: public void testGetPosition() {
388: fullSetup();
389:
390: Position pos = model.getPosition(errors[1]);
391: assertEquals("Incorrect error Position.", 125, pos.getOffset());
392: pos = model.getPosition(errors[5]);
393: assertEquals("Incorrect error Position.", 38, pos.getOffset());
394: }
395:
396: /** Tests CompilerErrorModel.getErrorAtOffset(int). */
397: public void testGetErrorAtOffset() throws IOException,
398: OperationCanceledException {
399: fullSetup();
400:
401: OpenDefinitionsDocument doc = getter
402: .getDocumentForFile(files[4]);
403: assertEquals("Wrong error at given offset.", errors[1], model
404: .getErrorAtOffset(doc, 125));
405: doc = getter.getDocumentForFile(files[4]);
406: assertEquals("Wrong error at given offset.", errors[5], model
407: .getErrorAtOffset(doc, 38));
408: }
409:
410: /** Tests CompilerErrorModel.hasErrorsWithPositions(OpenDefinitionsDocument). */
411: public void testHasErrorsWithPositions() throws IOException,
412: OperationCanceledException {
413: fullSetup();
414:
415: // Doc with errors
416: OpenDefinitionsDocument doc = getter
417: .getDocumentForFile(files[4]);
418: assertTrue("File should have errors with lines.", model
419: .hasErrorsWithPositions(doc));
420:
421: // Same doc with a different (but equivalent) file name
422: doc.setFile(new File("/tmp/./nowhere5"));
423: assertTrue("Same file should have errors with lines.", model
424: .hasErrorsWithPositions(doc));
425:
426: // Doc without errors
427: doc = getter.getDocumentForFile(files[1]);
428: assertTrue("File shouldn't have errors with lines.", !model
429: .hasErrorsWithPositions(doc));
430: }
431:
432: public void testErrorsInMultipleDocuments() throws IOException,
433: OperationCanceledException {
434: files = new File[] { new File("/tmp/nowhere1"),
435: new File("/tmp/nowhere2") };
436: texts = new String[] {
437: "kfgkasjg\n" + "faijskgisgj\n" + "sifjsidgjsd\n",
438: "isdjfdi\n" + "jfa" };
439: getter = new TestDocGetter(files, texts);
440:
441: errors = new CompilerError[] {
442: new CompilerError(files[1], 0, 0,
443: "Test error with File", false),
444: new CompilerError(files[0], 0, 0,
445: "Test error with File", false) };
446: model = new CompilerErrorModel(errors, getter);
447: model.getErrorAtOffset(getter.getDocumentForFile(files[0]), 25);
448: String temp = texts[0];
449: texts[0] = texts[1];
450: texts[1] = temp;
451: getter = new TestDocGetter(files, texts);
452: errors = new CompilerError[] {
453: new CompilerError(files[0], 0, 0,
454: "Test error with File", false),
455: new CompilerError(files[1], 2, 0,
456: "Test error with File", false) };
457: model = new CompilerErrorModel(errors, getter);
458: model.getErrorAtOffset(getter.getDocumentForFile(files[0]), 10);
459: }
460:
461: /** Setup for test cases with one document. */
462: private void setupDoc() {
463: files = new File[] { new File("/tmp/nowhere") };
464: texts = new String[] { "This is a block of test text.\n"
465: + "It doesn't matter what goes in here.\n"
466: + "But it does matter if it is manipulated properly!\n" };
467: getter = new TestDocGetter(files, texts);
468: }
469:
470: /** Setup for test cases with several documents. */
471: private void setupDocs() {
472: files = new File[] { new File("/tmp/nowhere1"),
473: new File("/tmp/nowhere2"), new File("/tmp/nowhere3"),
474: new File("/tmp/nowhere4"), new File("/tmp/nowhere5") };
475: texts = new String[] {
476: "This is the first block of test text.\n"
477: + "It doesn't matter what goes in here.\n"
478: + "But it does matter if it is manipulated properly!\n",
479: "This is the second block of test text.\n"
480: + "It doesn't matter what goes in here.\n"
481: + "But it does matter if it is manipulated properly!\n",
482: "This is the third block of test text.\n"
483: + "It doesn't matter what goes in here.\n"
484: + "But it does matter if it is manipulated properly!\n",
485: "This is the fourth block of test text.\n"
486: + "It doesn't matter what goes in here.\n"
487: + "But it does matter if it is manipulated properly!\n",
488: "This is the fifth block of test text.\n"
489: + "It doesn't matter what goes in here.\n"
490: + "But it does matter if it is manipulated properly!\n" };
491: getter = new TestDocGetter(files, texts);
492: }
493:
494: /** Extra setup for test cases with several documents. */
495: private void fullSetup() {
496: setupDocs();
497: errors = new CompilerError[] {
498: new CompilerError(files[0],
499: "Test error with File (no line)", false),
500: new CompilerError(files[4], 3, 0,
501: "Test error with File", false),
502: new CompilerError(files[2],
503: "Test warning with File (no line)", true),
504: new CompilerError(files[4],
505: "Test warning with File (no line)", true),
506: new CompilerError(files[2], 3, 0,
507: "Test warning with File", true),
508: new CompilerError(files[4], 1, 0,
509: "Test warning with File", true),
510: new CompilerError(files[1],
511: "Test warning with File (no line)", true),
512: new CompilerError(files[1],
513: "Test error with File (no line)", false),
514: new CompilerError(files[2],
515: "Test error with File (no line)", false),
516: new CompilerError(files[3],
517: "Test error with File (no line)", false),
518: new CompilerError(files[3], 3, 0,
519: "Test error with File", false),
520: new CompilerError(files[4],
521: "Test error with File (no line)", false),
522: new CompilerError(files[0], 2, 0,
523: "Test error with File", false),
524: new CompilerError(files[2], 2, 0,
525: "Test warning with File", true),
526: new CompilerError(files[0], 1, 0,
527: "Test error with File", false) };
528:
529: CompilerError[] copy = new CompilerError[errors.length];
530: for (int i = 0; i < errors.length; i++)
531: copy[i] = errors[i];
532: model = new CompilerErrorModel(copy, getter);
533: }
534: }
|