1 33 34 39 package edu.rice.cs.drjava.model.compiler; 40 41 import java.io.File ; 42 import java.io.IOException ; 43 import javax.swing.text.Position ; 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 53 public final class CompilerErrorModelTest extends DrJavaTestCase { 54 private File [] files; 55 private String [] texts; 56 private TestDocGetter getter; private CompilerError[] errors; 58 private CompilerErrorModel model; 59 60 61 public void testConstructNoErrors() { 62 getter = new TestDocGetter(); 63 model = new CompilerErrorModel(new CompilerError[0], getter); 64 65 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 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 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 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 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 assertEquals("Errors should be sorted.", errors[1], model.getError(2)); 107 assertTrue("hasOnlyWarnings should return false.", !model.hasOnlyWarnings()); 108 } 109 110 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 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 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 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 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 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 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 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 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 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 254 public void testConstructManyDocsWithBoth() { 255 fullSetup(); 256 257 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 280 public void testGetPosition() { 281 fullSetup(); 282 283 Position 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 290 public void testGetErrorAtOffset() throws IOException , 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 302 public void testHasErrorsWithPositions() throws IOException , OperationCanceledException { 303 fullSetup(); 304 305 OpenDefinitionsDocument doc = getter.getDocumentForFile(files[4]); 307 assertTrue("File should have errors with lines.", model.hasErrorsWithPositions(doc)); 308 309 doc.setFile(new File ("/tmp/./nowhere5")); 311 assertTrue("Same file should have errors with lines.", model.hasErrorsWithPositions(doc)); 312 313 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 , OperationCanceledException { 319 files = new File [] { 320 new File ("/tmp/nowhere1"), 321 new File ("/tmp/nowhere2") 322 }; 323 texts = new String [] { 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 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 348 private void setupDoc() { 349 files = new File [] { new File ("/tmp/nowhere") }; 350 texts = new String [] { 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 357 private void setupDocs() { 358 files = new File [] { 359 new File ("/tmp/nowhere1"), 360 new File ("/tmp/nowhere2"), 361 new File ("/tmp/nowhere3"), 362 new File ("/tmp/nowhere4"), 363 new File ("/tmp/nowhere5") 364 }; 365 texts = new String [] { 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 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 |