1 19 20 package org.netbeans.modules.editor.errorstripe; 21 22 import java.awt.BorderLayout ; 23 import java.util.Arrays ; 24 import javax.swing.JFrame ; 25 import javax.swing.JScrollPane ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.SortedMap ; 32 import javax.swing.JEditorPane ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.Position ; 35 import org.netbeans.editor.AnnotationDesc; 36 import org.netbeans.editor.AnnotationType; 37 import org.netbeans.editor.AnnotationTypes; 38 import org.netbeans.editor.BaseDocument; 39 import org.netbeans.editor.BaseKit; 40 import org.netbeans.editor.Utilities; 41 import org.netbeans.junit.NbTestCase; 42 import org.netbeans.modules.editor.errorstripe.caret.CaretMarkProviderCreator; 43 import org.netbeans.modules.editor.errorstripe.privatespi.Mark; 44 import org.netbeans.modules.editor.errorstripe.privatespi.Status; 45 import org.netbeans.spi.editor.errorstripe.UpToDateStatus; 46 import org.netbeans.modules.editor.options.AnnotationTypeProcessor; 47 import org.netbeans.modules.editor.options.BaseOptions; 48 import org.netbeans.modules.editor.plain.PlainKit; 49 import org.openide.filesystems.FileObject; 50 import org.openide.filesystems.Repository; 51 52 56 public class AnnotationViewDataImplTest extends NbTestCase { 57 58 public AnnotationViewDataImplTest(String testName) { 59 super(testName); 60 } 61 62 protected void setUp() throws Exception { 63 UnitUtilities.prepareTest(new String [] {"/org/netbeans/modules/editor/resources/annotations-test-layer.xml", 64 "/org/netbeans/modules/editor/plain/resources/layer.xml", 65 "/org/netbeans/modules/editor/errorstripe/test-layer.xml"}, 66 new Object [0]); 67 BaseKit.getKit(PlainKit.class); 68 BaseOptions.findObject(BaseOptions.class, true); 69 70 AnnotationTypes.getTypes().registerLoader(new AnnotationsLoader()); 71 CaretMarkProviderCreator.switchOff = true; 72 } 73 74 public void testGetMainMarkForBlock() throws Exception { 75 JEditorPane editor = new JEditorPane (); 76 77 editor.setEditorKit(BaseKit.getKit(PlainKit.class)); 78 79 BaseDocument bd = (BaseDocument) editor.getDocument(); 80 81 bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null); 82 83 TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}); 84 TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}); 85 TestMark mark3 = new TestMark(Status.STATUS_WARNING, null, null, new int[] {2, 4}); 86 87 AnnotationDesc test1 = new TestAnnotationDesc(bd, bd.createPosition(7), "test-annotation-1"); 88 AnnotationDesc test2 = new TestAnnotationDesc(bd, bd.createPosition(8), "test-annotation-2"); 89 AnnotationDesc test3 = new TestAnnotationDesc(bd, bd.createPosition(8), "test-annotation-8"); 90 AnnotationDesc test4 = new TestAnnotationDesc(bd, bd.createPosition(9), "test-annotation-8"); 91 92 bd.getAnnotations().addAnnotation(test1); 93 bd.getAnnotations().addAnnotation(test2); 94 bd.getAnnotations().addAnnotation(test3); 95 bd.getAnnotations().addAnnotation(test4); 96 97 List marks1 = Arrays.asList(new Mark[]{mark1, mark2, mark3}); 98 List marks2 = Arrays.asList(new Mark[]{mark1, mark3}); 99 List marks3 = Arrays.asList(new Mark[]{mark2, mark3}); 100 List marks4 = Arrays.asList(new Mark[]{mark1, mark2}); 101 List marks5 = Arrays.asList(new Mark[]{mark3}); 102 103 TestMarkProvider provider = new TestMarkProvider(marks1, UpToDateStatus.UP_TO_DATE_OK); 104 TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault(); 105 106 creator.setProvider(provider); 107 108 AnnotationView aView = new AnnotationView(editor); 109 AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData(); 110 111 assertEquals(mark1, data.getMainMarkForBlock(2, 2)); 112 assertEquals(mark1, data.getMainMarkForBlock(2, 3)); 113 assertEquals(mark1, data.getMainMarkForBlock(2, 4)); 114 assertEquals(mark1, data.getMainMarkForBlock(2, 6)); 115 assertEquals(mark3, data.getMainMarkForBlock(3, 6)); 116 assertEquals(mark3, data.getMainMarkForBlock(3, 3)); 117 assertEquals(null, data.getMainMarkForBlock(6, 6)); 118 assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 7).getStatus()); 119 assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus()); 120 bd.getAnnotations().activateNextAnnotation(8); 121 assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus()); 122 bd.getAnnotations().activateNextAnnotation(8); 123 assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus()); 124 assertNull(data.getMainMarkForBlock(9, 9)); 125 assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 9).getStatus()); 126 127 provider.setMarks(marks2); 128 129 bd.getAnnotations().removeAnnotation(test3); 130 131 assertEquals(mark1, data.getMainMarkForBlock(2, 2)); 132 assertEquals(mark1, data.getMainMarkForBlock(2, 3)); 133 assertEquals(mark1, data.getMainMarkForBlock(2, 4)); 134 assertEquals(mark1, data.getMainMarkForBlock(2, 6)); 135 assertEquals(mark3, data.getMainMarkForBlock(3, 6)); 136 assertEquals(mark3, data.getMainMarkForBlock(3, 3)); 137 assertEquals(null, data.getMainMarkForBlock(6, 6)); 138 139 assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 7).getStatus()); 140 assertEquals(Status.STATUS_WARNING, data.getMainMarkForBlock(8, 8).getStatus()); 141 assertNull(data.getMainMarkForBlock(9, 9)); 142 assertEquals(Status.STATUS_ERROR, data.getMainMarkForBlock(7, 9).getStatus()); 143 144 provider.setMarks(marks3); 145 146 assertEquals(mark3, data.getMainMarkForBlock(2, 2)); 147 assertEquals(mark3, data.getMainMarkForBlock(2, 3)); 148 assertEquals(mark3, data.getMainMarkForBlock(2, 4)); 149 assertEquals(mark3, data.getMainMarkForBlock(2, 6)); 150 assertEquals(mark3, data.getMainMarkForBlock(3, 6)); 151 assertEquals(mark3, data.getMainMarkForBlock(3, 3)); 152 assertEquals(null, data.getMainMarkForBlock(6, 6)); 153 154 provider.setMarks(marks4); 155 156 assertEquals(mark1, data.getMainMarkForBlock(2, 2)); 157 assertEquals(mark1, data.getMainMarkForBlock(2, 3)); 158 assertEquals(mark1, data.getMainMarkForBlock(2, 4)); 159 assertEquals(mark1, data.getMainMarkForBlock(2, 6)); 160 assertEquals(null, data.getMainMarkForBlock(3, 6)); 161 assertEquals(null, data.getMainMarkForBlock(3, 3)); 162 assertEquals(null, data.getMainMarkForBlock(6, 6)); 163 164 provider.setMarks(marks5); 165 166 assertEquals(mark3, data.getMainMarkForBlock(2, 2)); 167 assertEquals(mark3, data.getMainMarkForBlock(2, 3)); 168 assertEquals(mark3, data.getMainMarkForBlock(2, 4)); 169 assertEquals(mark3, data.getMainMarkForBlock(2, 6)); 170 assertEquals(mark3, data.getMainMarkForBlock(3, 6)); 171 assertEquals(mark3, data.getMainMarkForBlock(3, 3)); 172 assertEquals(null, data.getMainMarkForBlock(6, 6)); 173 } 174 175 public void testComputeTotalStatus() throws Exception { 176 JFrame f = new JFrame (); 177 JEditorPane editor = new JEditorPane (); 178 179 editor.setEditorKit(BaseKit.getKit(PlainKit.class)); 180 181 AnnotationView aView = new AnnotationView(editor); 182 AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData(); 183 184 f.getContentPane().setLayout(new BorderLayout ()); 185 f.getContentPane().add(new JScrollPane (editor), BorderLayout.CENTER); 186 f.getContentPane().add(aView, BorderLayout.EAST); 187 188 f.setSize(500, 500); 189 190 f.setVisible(true); 191 192 BaseDocument bd = (BaseDocument) editor.getDocument(); 193 194 bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null); 195 196 Position start = bd.createPosition(Utilities.getRowStartFromLineOffset(bd, 2)); 197 198 AnnotationDesc a1 = new AnnotationTestUtilities.TestAnnotationDesc1(bd, start); 199 AnnotationDesc a2 = new AnnotationTestUtilities.TestAnnotationDesc2(bd, start); 200 201 bd.getAnnotations().addAnnotation(a1); 202 bd.getAnnotations().addAnnotation(a2); 203 204 assertEquals(Status.STATUS_ERROR, data.computeTotalStatus()); 205 206 bd.getAnnotations().activateNextAnnotation(2); 207 208 assertEquals(Status.STATUS_ERROR, data.computeTotalStatus()); 209 210 f.setVisible(false); 211 } 212 213 public void testMarkUpdates() { 214 JEditorPane editor = new JEditorPane (); 215 216 editor.setEditorKit(BaseKit.getKit(PlainKit.class)); 217 218 TestMark mark1 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}); 219 TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}); 220 TestMark mark3 = new TestMark(Status.STATUS_OK, null, null, new int[] {4, 6}); 221 222 List marks = Arrays.asList(new Mark[]{mark1, mark2}); 223 List marksOnlyFirst = Arrays.asList(new Mark[]{mark1}); 224 List marksOnlySecond = Arrays.asList(new Mark[]{mark2}); 225 List marksFirstAndThird = Arrays.asList(new Mark[]{mark1, mark3}); 226 227 TestMarkProvider provider = new TestMarkProvider(marks, UpToDateStatus.UP_TO_DATE_OK); 228 TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault(); 229 230 creator.setProvider(provider); 231 232 AnnotationView aView = new AnnotationView(editor); 233 AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData(); 234 235 List mergedMarks; 236 SortedMap map; 237 238 mergedMarks = data.getMergedMarks(); 239 240 assertEquals(marks, mergedMarks); 241 242 map = data.getMarkMap(); 243 244 assertEquals(1, map.size()); 245 assertEquals(marks, map.get(map.firstKey())); 246 247 provider.setMarks(marksOnlyFirst); 248 249 mergedMarks = data.getMergedMarks(); 250 251 assertEquals(marksOnlyFirst, mergedMarks); 252 253 map = data.getMarkMap(); 254 255 assertEquals(1, map.size()); 256 assertEquals(marksOnlyFirst, map.get(map.firstKey())); 257 258 provider.setMarks(marksFirstAndThird); 259 260 mergedMarks = data.getMergedMarks(); 261 262 assertEquals(marksFirstAndThird, mergedMarks); 263 264 map = data.getMarkMap(); 265 266 assertEquals(4, map.size()); 267 assertEquals(new HashSet (Arrays.asList(new Integer [] {new Integer (2), new Integer (4), new Integer (5), new Integer (6)})), map.keySet()); 268 assertEquals(Arrays.asList(new Mark[] {mark1}), map.get(new Integer (2))); 269 assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer (4))); 270 assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer (5))); 271 assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer (6))); 272 273 provider.setMarks(Collections.EMPTY_LIST); 274 275 mergedMarks = data.getMergedMarks(); 276 277 assertEquals(Collections.EMPTY_LIST, mergedMarks); 278 279 map = data.getMarkMap(); 280 281 assertEquals(0, map.size()); 282 283 provider.setMarks(marksFirstAndThird); 284 285 mergedMarks = data.getMergedMarks(); 286 287 assertEquals(marksFirstAndThird, mergedMarks); 288 289 map = data.getMarkMap(); 290 291 assertEquals(4, map.size()); 292 assertEquals(new HashSet (Arrays.asList(new Integer [] {new Integer (2), new Integer (4), new Integer (5), new Integer (6)})), map.keySet()); 293 assertEquals(Arrays.asList(new Mark[] {mark1}), map.get(new Integer (2))); 294 assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer (4))); 295 assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer (5))); 296 assertEquals(Arrays.asList(new Mark[] {mark3}), map.get(new Integer (6))); 297 } 298 299 public void testMarkPriorities() throws Exception { 300 JEditorPane editor = new JEditorPane (); 301 302 editor.setEditorKit(BaseKit.getKit(PlainKit.class)); 303 304 BaseDocument bd = (BaseDocument) editor.getDocument(); 305 306 bd.insertString(0, "\n\n\n\n\n\n\n\n\n\n", null); 307 308 TestMark mark1 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}, 99); 310 TestMark mark2 = new TestMark(Status.STATUS_OK, null, null, new int[] {2, 2}, 10); 311 TestMark mark3 = new TestMark(Status.STATUS_OK, null, null, new int[] {3, 4}, 5); 312 313 TestMark mark4 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}, 1000); 314 TestMark mark5 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {2, 2}, 100); 315 TestMark mark6 = new TestMark(Status.STATUS_ERROR, null, null, new int[] {3, 4}, 50); 316 317 List marks1 = Arrays.asList(new Mark[]{mark1, mark2, mark3}); 318 List marks2 = Arrays.asList(new Mark[]{mark2, mark1, mark3}); 319 List marks3 = Arrays.asList(new Mark[]{mark1, mark2, mark3, mark4, mark5, mark6}); 320 321 TestMarkProvider provider = new TestMarkProvider(marks1, UpToDateStatus.UP_TO_DATE_OK); 322 TestMarkProviderCreator creator = TestMarkProviderCreator.getDefault(); 323 324 creator.setProvider(provider); 325 326 AnnotationView aView = new AnnotationView(editor); 327 AnnotationViewDataImpl data = (AnnotationViewDataImpl) aView.getData(); 328 329 assertEquals(mark2, data.getMainMarkForBlock(2, 2)); 330 assertEquals(mark3, data.getMainMarkForBlock(2, 3)); 331 assertEquals(mark3, data.getMainMarkForBlock(3, 4)); 332 333 assertEquals(null, data.getMainMarkForBlock(6, 6)); 334 335 provider.setMarks(marks2); 336 337 assertEquals(mark2, data.getMainMarkForBlock(2, 2)); 338 assertEquals(mark3, data.getMainMarkForBlock(2, 3)); 339 assertEquals(mark3, data.getMainMarkForBlock(3, 4)); 340 341 assertEquals(null, data.getMainMarkForBlock(6, 6)); 342 343 provider.setMarks(marks3); 344 345 assertEquals(mark5, data.getMainMarkForBlock(2, 2)); 346 assertEquals(mark6, data.getMainMarkForBlock(2, 3)); 347 assertEquals(mark6, data.getMainMarkForBlock(3, 4)); 348 349 assertEquals(null, data.getMainMarkForBlock(6, 6)); 350 351 provider.setMarks(Collections.EMPTY_LIST); 352 353 AnnotationDesc test1 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-1"); 355 AnnotationDesc test2 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-2"); 356 AnnotationDesc test3 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-3"); 357 AnnotationDesc test4 = new TestAnnotationDesc(bd, bd.createPosition(2), "test-annotation-priority-4"); 358 359 bd.getAnnotations().addAnnotation(test1); 360 bd.getAnnotations().addAnnotation(test2); 361 362 assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); 363 364 bd.getAnnotations().activateNextAnnotation(2); 365 366 assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); 367 368 bd.getAnnotations().activateNextAnnotation(2); 369 370 bd.getAnnotations().addAnnotation(test3); 371 bd.getAnnotations().addAnnotation(test4); 372 373 assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); 374 375 bd.getAnnotations().activateNextAnnotation(2); 376 377 assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); 378 379 bd.getAnnotations().activateNextAnnotation(2); 380 381 assertEquals(test4, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); 382 383 List marks4 = Arrays.asList(new Mark[]{mark1}); 385 386 provider.setMarks(marks4); 387 388 bd.getAnnotations().removeAnnotation(test2); 389 bd.getAnnotations().removeAnnotation(test3); 390 bd.getAnnotations().removeAnnotation(test4); 391 392 assertEquals(mark1, data.getMainMarkForBlock(2, 2)); 393 394 bd.getAnnotations().addAnnotation(test2); 395 396 assertEquals(test2, ((AnnotationMark) data.getMainMarkForBlock(2, 2)).getAnnotationDesc()); 397 } 398 399 protected boolean runInEQ() { 400 return true; 401 } 402 403 static class TestAnnotationDesc extends AnnotationDesc { 404 405 private BaseDocument doc; 406 private Position position; 407 private String type; 408 409 public TestAnnotationDesc (BaseDocument bd, Position position, String type) { 410 super(position.getOffset(), -1); 411 this.doc = bd; 412 this.position = position; 413 this.type = type; 414 } 415 416 public String getShortDescription() { 417 return getAnnotationType(); 418 } 419 420 public String getAnnotationType() { 421 return type; 422 } 423 424 public int getOffset() { 425 return position.getOffset(); 426 } 427 428 public int getLine() { 429 try { 430 return Utilities.getLineOffset(doc, getOffset()); 431 } catch (BadLocationException e) { 432 IllegalStateException exc = new IllegalStateException (); 433 434 exc.initCause(e); 435 436 throw exc; 437 } 438 } 439 440 } 441 442 static final class AnnotationsLoader implements AnnotationTypes.Loader { 443 444 public void loadTypes() { 445 try { 446 Map typesInstances = new HashMap (); 447 FileObject typesFolder = Repository.getDefault().getDefaultFileSystem().findResource("Editors/AnnotationTypes"); 448 FileObject[] types = typesFolder.getChildren(); 449 450 for (int cntr = 0; cntr < types.length; cntr++) { 451 AnnotationTypeProcessor proc = new AnnotationTypeProcessor(); 452 453 System.err.println("CCC:" + types[cntr].getNameExt()); 454 if (types[cntr].getName().startsWith("testAnnotation") && "xml".equals(types[cntr].getExt())) { 455 proc.attachTo(types[cntr]); 456 AnnotationType type = (AnnotationType) proc.instanceCreate(); 457 typesInstances.put(type.getName(), type); 458 } 459 } 460 461 AnnotationTypes.getTypes().setTypes(typesInstances); 462 } catch (Exception e) { 463 e.printStackTrace(); 464 } 465 } 466 467 public void loadSettings() { 468 } 469 470 public void saveType(AnnotationType type) { 471 } 472 473 public void saveSetting(String settingName, Object value) { 474 } 475 476 } 477 478 } 479 | Popular Tags |