1 19 20 package org.netbeans.modules.editor.hints; 21 22 import java.io.OutputStream ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import javax.swing.text.BadLocationException ; 28 import javax.swing.text.Document ; 29 import org.netbeans.editor.BaseDocument; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.modules.editor.highlights.spi.Highlight; 32 import org.netbeans.modules.editor.hints.AnnotationHolder.Attacher; 33 import org.netbeans.spi.editor.hints.ErrorDescription; 34 import org.netbeans.spi.editor.hints.ErrorDescriptionFactory; 35 import org.netbeans.spi.editor.hints.ErrorDescriptionTestSupport; 36 import org.netbeans.spi.editor.hints.Fix; 37 import org.netbeans.spi.editor.hints.Severity; 38 import org.openide.cookies.EditorCookie; 39 import org.openide.filesystems.FileLock; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileSystem; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.loaders.DataObject; 44 import org.openide.text.Annotation; 45 46 50 public class AnnotationHolderTest extends NbTestCase { 51 52 private FileObject file; 53 private Document doc; 54 private EditorCookie ec; 55 56 57 public AnnotationHolderTest(String name) { 58 super(name); 59 } 60 61 @Override 62 protected void setUp() throws Exception { 63 FileSystem fs = FileUtil.createMemoryFileSystem(); 64 65 file = fs.getRoot().createData("test.txt"); 66 67 writeIntoFile(file, "01234567890123456789"); 68 69 DataObject od = DataObject.find(file); 70 71 ec = od.getCookie(EditorCookie.class); 72 doc = ec.openDocument(); 73 } 74 75 public void testComputeHighlightsOneLayer1() throws Exception { 76 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 3); 77 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 5, 6); 78 79 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 80 List <Highlight> highlights = new ArrayList <Highlight>(); 81 82 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 83 84 assertEquals(2, highlights.size()); 85 86 Highlight h1 = highlights.get(0); 87 Highlight h2 = highlights.get(1); 88 89 assertEquals(1, h1.getStart()); 90 assertEquals(3, h1.getEnd()); 91 assertEquals(5, h2.getStart()); 92 assertEquals(6, h2.getEnd()); 93 } 94 95 public void testComputeHighlightsOneLayer2() throws Exception { 96 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 7); 97 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 5, 6); 98 99 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 100 List <Highlight> highlights = new ArrayList <Highlight>(); 101 102 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 103 104 assertEquals(1, highlights.size()); 105 106 Highlight h1 = highlights.get(0); 107 108 assertEquals(1, h1.getStart()); 109 assertEquals(7, h1.getEnd()); 110 } 111 112 public void testComputeHighlightsOneLayer3() throws Exception { 113 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 5); 114 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 3, 7); 115 116 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 117 List <Highlight> highlights = new ArrayList <Highlight>(); 118 119 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 120 121 assertEquals(1, highlights.size()); 122 123 Highlight h1 = highlights.get(0); 124 125 assertEquals(1, h1.getStart()); 126 assertEquals(7, h1.getEnd()); 127 } 128 129 public void testComputeHighlightsOneLayer4() throws Exception { 130 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5); 131 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "2", file, 1, 7); 132 133 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 134 List <Highlight> highlights = new ArrayList <Highlight>(); 135 136 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 137 138 assertEquals(1, highlights.size()); 139 140 Highlight h1 = highlights.get(0); 141 142 assertEquals(1, h1.getStart()); 143 assertEquals(7, h1.getEnd()); 144 } 145 146 public void testComputeHighlightsTwoLayers1() throws Exception { 147 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 3); 148 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 5, 7); 149 150 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 151 List <Highlight> highlights = new ArrayList <Highlight>(); 152 153 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 154 155 assertEquals(2, highlights.size()); 156 157 Highlight h1 = highlights.get(0); 158 Highlight h2 = highlights.get(1); 159 160 assertEquals(1, h1.getStart()); 161 assertEquals(3, h1.getEnd()); 162 assertEquals(5, h2.getStart()); 163 assertEquals(7, h2.getEnd()); 164 } 165 166 public void testComputeHighlightsTwoLayers2() throws Exception { 167 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 1, 7); 168 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 3, 5); 169 170 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 171 List <Highlight> highlights = new ArrayList <Highlight>(); 172 173 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 174 175 assertEquals(1, highlights.size()); 176 177 Highlight h1 = highlights.get(0); 178 179 assertEquals(1, h1.getStart()); 180 assertEquals(7, h1.getEnd()); 181 182 assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring()); 183 } 184 185 public void testComputeHighlightsTwoLayers3() throws Exception { 186 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5); 187 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 4, 7); 188 189 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 190 List <Highlight> highlights = new ArrayList <Highlight>(); 191 192 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 193 194 assertEquals(2, highlights.size()); 195 196 Highlight h1 = highlights.get(0); 197 Highlight h2 = highlights.get(1); 198 199 assertEquals(3, h1.getStart()); 200 assertEquals(5, h1.getEnd()); 201 assertEquals(6, h2.getStart()); 202 assertEquals(7, h2.getEnd()); 203 204 assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring()); 205 assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h2.getColoring()); 206 } 207 208 public void testComputeHighlightsTwoLayers4() throws Exception { 209 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5); 210 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 4); 211 212 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 213 List <Highlight> highlights = new ArrayList <Highlight>(); 214 215 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 216 217 assertEquals(2, highlights.size()); 218 219 Highlight h1 = highlights.get(0); 220 Highlight h2 = highlights.get(1); 221 222 assertEquals(3, h1.getStart()); 223 assertEquals(5, h1.getEnd()); 224 assertEquals(1, h2.getStart()); 225 assertEquals(2, h2.getEnd()); 226 227 assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring()); 228 assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h2.getColoring()); 229 } 230 231 public void testComputeHighlightsTwoLayers5() throws Exception { 232 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5); 233 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 7); 234 235 List <ErrorDescription> errors = Arrays.asList(ed1, ed2); 236 List <Highlight> highlights = new ArrayList <Highlight>(); 237 238 AnnotationHolder.computeHighlights(doc, 0, errors, highlights); 239 240 assertEquals(3, highlights.size()); 241 242 Highlight h1 = highlights.get(0); 243 Highlight h2 = highlights.get(1); 244 Highlight h3 = highlights.get(2); 245 246 assertEquals(3, h1.getStart()); 247 assertEquals(5, h1.getEnd()); 248 assertEquals(1, h3.getStart()); 249 assertEquals(2, h3.getEnd()); 250 assertEquals(6, h2.getStart()); 251 assertEquals(7, h2.getEnd()); 252 253 assertEquals(AnnotationHolder.COLORINGS.get(Severity.ERROR), h1.getColoring()); 254 assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h2.getColoring()); 255 assertEquals(AnnotationHolder.COLORINGS.get(Severity.WARNING), h3.getColoring()); 256 } 257 258 public void testNullSpan() throws Exception { 259 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5); 260 ErrorDescription ed2 = ErrorDescriptionTestSupport.createErrorDescription(file, "", Severity.DISABLED, ErrorDescriptionFactory.lazyListForFixes(Collections.<Fix>emptyList()), null); 261 ErrorDescription ed3 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 7); 262 263 ec.open(); 264 265 AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed1, ed2, ed3)); 266 267 ec.close(); 268 } 269 270 public void testComputeSeverity() throws Exception { 271 ErrorDescription ed1 = ErrorDescriptionFactory.createErrorDescription(Severity.ERROR, "1", file, 3, 5); 272 ErrorDescription ed2 = ErrorDescriptionFactory.createErrorDescription(Severity.HINT, "2", file, 1, 7); 273 ErrorDescription ed3 = ErrorDescriptionFactory.createErrorDescription(Severity.WARNING, "2", file, 1, 7); 274 ErrorDescription ed4 = ErrorDescriptionFactory.createErrorDescription(Severity.VERIFIER, "2", file, 1, 7); 275 276 ec.open(); 277 278 class AttacherImpl implements Attacher { 279 private ParseErrorAnnotation annotation; 280 public void attachAnnotation(int line, ParseErrorAnnotation a) throws BadLocationException { 281 if (line == 0) { 282 this.annotation = a; 283 } 284 } 285 public void detachAnnotation(Annotation a) {} 286 } 287 288 AttacherImpl impl = new AttacherImpl(); 289 290 AnnotationHolder.getInstance(file).attacher = impl; 291 292 AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed1, ed2, ed3)); 293 294 assertEquals(Severity.ERROR, impl.annotation.getSeverity()); 295 296 AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed2, ed3)); 297 298 assertEquals(Severity.WARNING, impl.annotation.getSeverity()); 299 300 AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed2)); 301 302 assertEquals(Severity.HINT, impl.annotation.getSeverity()); 303 304 AnnotationHolder.getInstance(file).setErrorDescriptions("foo", Arrays.asList(ed2, ed4)); 305 306 assertEquals(Severity.VERIFIER, impl.annotation.getSeverity()); 307 308 ec.close(); 309 } 310 311 @Override 312 protected boolean runInEQ() { 313 return true; 314 } 315 316 private void writeIntoFile(FileObject file, String what) throws Exception { 317 FileLock lock = file.lock(); 318 OutputStream out = file.getOutputStream(lock); 319 320 try { 321 out.write(what.getBytes()); 322 } finally { 323 out.close(); 324 lock.releaseLock(); 325 } 326 } 327 328 } 329 | Popular Tags |