1 19 20 package org.openide.text; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.Arrays ; 26 import junit.framework.*; 27 28 import org.netbeans.junit.*; 29 30 import org.openide.util.Lookup; 31 import org.openide.util.lookup.*; 32 33 34 38 public class LineSetTest extends NbTestCase implements CloneableEditorSupport.Env { 39 40 private CloneableEditorSupport support; 41 42 private InstanceContent ic; 43 44 45 private String content = ""; 47 private boolean valid = true; 48 private boolean modified = false; 49 private java.util.Date date = new java.util.Date (); 50 private java.util.List propL = new java.util.ArrayList (); 51 private java.beans.VetoableChangeListener vetoL; 52 53 54 public LineSetTest(java.lang.String testName) { 55 super(testName); 56 } 57 58 public static void main(java.lang.String [] args) { 59 if (args.length == 1) { 60 junit.textui.TestRunner.run (new LineSetTest (args[0])); 61 } 62 junit.textui.TestRunner.run(suite()); 63 } 64 65 public static Test suite() { 66 TestSuite suite = new NbTestSuite(LineSetTest.class); 67 68 return suite; 69 } 70 71 72 protected void setUp () { 73 ic = new InstanceContent (); 74 support = new CES (this, new AbstractLookup (ic)); 75 ic.add (this); 76 } 77 78 public void testLineSetIsEmpty () throws Exception { 79 support.openDocument (); 80 81 Line.Set set = support.getLineSet(); 82 assertEquals ("empty means one line ;-)", 1, set.getLines().size ()); 83 } 84 85 public void testLineSetContainsTwoLinesWhenOpen () throws Exception { 86 content = "Ahoj\n"; 87 support.openDocument (); 88 89 Line.Set set = support.getLineSet(); 90 assertEquals ("Two lines", 2, set.getLines().size ()); 91 } 92 93 public void testLineNumberUpdatedWhenInsertedNewLine () throws Exception { 94 content = "Ahoj\n"; 95 javax.swing.text.Document doc = support.openDocument (); 96 97 Line.Set set = support.getLineSet (); 98 java.util.List lines = set.getLines (); 99 assertEquals ("Two lines", 2, lines.size ()); 100 101 Line line = set.getCurrent (1); 102 assertEquals ("Line number is one", 1, line.getLineNumber ()); 103 assertGetOriginal ("Original number is of course 1", set, line, 1); 104 105 doc.insertString (0, "New line\n", null); 106 107 assertEquals ("Line number is now 2", 2, line.getLineNumber ()); 108 109 Line whereIsOriginalLineOne = set.getOriginal (1); 110 assertEquals ("It is the same", line, whereIsOriginalLineOne); 111 assertEquals ("Line number is two", 2, whereIsOriginalLineOne.getLineNumber ()); 112 113 support.saveDocument (); 114 115 Line currentLineOne = support.getLineSet ().getOriginal (1); 116 assertEquals ("Well its line number is 1", 1, currentLineOne.getLineNumber ()); 117 assertFalse ("And it is not the same", currentLineOne.equals (line)); 118 119 Line currentLineTwo = support.getLineSet ().getOriginal (2); 120 assertEquals ("This is our original line", line, currentLineTwo); 121 assertGetOriginal ("Original number of the line was 1", set, line, 1); 122 123 assertEquals ("Original set still has two lines", 2, set.getLines ().size ()); 124 assertEquals ("Index of current line 1 is 0 in old set", 1, set.getLines ().indexOf (line)); 125 assertEquals ("even the line number is two", 2, line.getLineNumber ()); 126 java.util.List newLines = support.getLineSet ().getLines (); 127 assertEquals ("and the index in new set is two as well", 2, newLines.indexOf (line)); 128 129 } 130 131 public void testWhenInsertedNewLineAtTheLineItStaysTheSame () throws Exception { 132 content = "Ahoj\n"; 133 javax.swing.text.Document doc = support.openDocument (); 134 135 Line.Set set = support.getLineSet (); 136 137 Line line = set.getOriginal (0); 138 doc.insertString (0, "Hi\n", null); 139 140 assertEquals ("First line still stays the first", 0, line.getLineNumber ()); 141 } 142 143 public void testWhenInsertedNewLineAtTheLineItStaysTheSame2 () throws Exception { 144 content = "Ahoj\nJak se mas?\nChlape?\n"; 145 javax.swing.text.Document doc = support.openDocument (); 146 147 Line.Set set = support.getLineSet (); 148 149 Line line = set.getOriginal (1); 150 assertEquals("Text of second line", "Jak se mas?\n", line.getText ()); 151 assertEquals ("Line number is one", 1, line.getLineNumber()); 152 assertEquals ("New line at forth position", "\n", doc.getText (4, 1)); 153 doc.insertString (6, "Jarda\n", null); 154 155 assertEquals ("Second line still stays the second", 1, line.getLineNumber ()); 156 } 157 158 public void testJoinedLinesWillNeverPart () throws Exception { 161 content = "111\n2\n"; 162 javax.swing.text.Document doc = support.openDocument (); 163 164 Line.Set set = support.getLineSet (); 165 166 Line one = set.getOriginal (0); 167 Line two = set.getOriginal (1); 168 169 assertEquals ("New line after first line", "\n", doc.getText (3, 1)); 170 171 doc.remove (3, 1); 172 173 assertEquals ("Joined", one, two); 174 175 doc.insertString (2, "\n", null); 176 177 assertEquals ("They will not part", one, two); 178 179 assertEquals ("Line number is 0", 0, one.getLineNumber ()); 180 } 181 182 public void testGetLinesIndexOfDoesNotCreateAllLines () throws Exception { 183 content = "0\n1\n2\n3\n4\n"; 184 javax.swing.text.Document doc = support.openDocument (); 185 186 Line.Set set = support.getLineSet (); 187 Line two = set.getOriginal (2); 188 189 assertEquals ("Line index is two", 2, set.getLines ().indexOf (two)); 190 assertNumberOfLines (1, set); 191 192 assertEquals ("Really two", 2, new java.util.ArrayList (set.getLines ()).indexOf (two)); 193 } 194 195 public void testLinesAreNonMutable () throws Exception { 196 content = "0\n1\n2\n3\n4\n"; 197 javax.swing.text.Document doc = support.openDocument (); 198 199 assertNonmutable (support.getLineSet ().getLines ()); 200 } 201 202 public void testGetLinesIndexWorksAfterModifications () throws Exception { 203 content = "0\n1\n2\n3\n4\n"; 204 javax.swing.text.Document doc = support.openDocument (); 205 206 Line.Set set = support.getLineSet (); 207 208 int offset = 4; 209 assertEquals ("2 is on the second line", "2", doc.getText (4, 1)); 210 doc.insertString (offset, "x\n", null); 211 assertEquals ("x\n is on the second line", "x\n", doc.getText (4, 2)); 212 213 214 Line two = set.getOriginal (2); 215 assertEquals ("2\n is the line text", "2\n", two.getText ()); 216 217 assertEquals ("Line index is two", 2, set.getLines ().indexOf (two)); 218 assertNumberOfLines (1, set); 219 220 assertEquals ("Really two", 2, new java.util.ArrayList (set.getLines ()).indexOf (two)); 221 } 222 223 public void testWhatHappensWhenAskingForLineOutOfBounds () throws Exception { 224 content = "0"; 225 javax.swing.text.Document doc = support.openDocument (); 226 227 Line.Set set = support.getLineSet (); 228 229 try { 230 Line l = set.getCurrent (1); 231 fail ("Should thrown IndexOutOfBoundsException"); 232 } catch (IndexOutOfBoundsException ex) { 233 } 235 236 try { 237 Line n = set.getOriginal (1); 238 fail ("Should thrown IndexOutOfBoundsException"); 239 } catch (IndexOutOfBoundsException ex) { 240 } 242 try { 243 Line l = set.getCurrent (-1); 244 fail ("Should thrown IndexOutOfBoundsException"); 245 } catch (IndexOutOfBoundsException ex) { 246 } 248 249 try { 250 Line n = set.getOriginal (-1); 251 fail ("Should thrown IndexOutOfBoundsException"); 252 } catch (IndexOutOfBoundsException ex) { 253 } 255 256 Line l = set.getCurrent (0); 257 Line n = set.getOriginal (0); 258 259 assertNotNull (l); 260 assertNotNull (n); 261 262 assertEquals ("Lines are the same", l, n); 263 assertEquals ("Text is", "0", l.getText ()); 264 } 265 266 267 public void testGetLinesIndexWorksForNewlyAddedLines () throws Exception { 268 content = "0\n1\n2\n3\n4\n"; 269 javax.swing.text.Document doc = support.openDocument (); 270 271 Line.Set set = support.getLineSet (); 272 273 int offset = 4; 274 assertEquals ("2 is on the second line", "2", doc.getText (4, 1)); 275 doc.insertString (offset, "x\n", null); 276 assertEquals ("x\n is on the second line", "x\n", doc.getText (4, 2)); 277 278 279 Line two = set.getCurrent (2); 280 assertEquals ("x\n is the line text", "x\n", two.getText ()); 281 282 assertEquals ("Line index is -1 as it is not present", -1, set.getLines ().indexOf (two)); 283 assertNumberOfLines (2, set); 288 assertEquals ("Query on set works and does not produce new lines", 2, set.getOriginalLineNumber (two)); 289 assertNumberOfLines (2, set); 290 291 assertGetOriginal ("However if one asks the line set it works", set, two, 2); 293 assertEquals ("Really missing from the list", -1, new java.util.ArrayList (set.getLines ()).indexOf (two)); 294 } 295 296 297 public void testGetOriginalForLineFromDifferentLineSet () throws Exception { 298 content = "line 1\nline 2\n"; 299 javax.swing.text.Document doc = support.openDocument (); 300 301 Line.Set lineSet1 = support.getLineSet (); 302 Line line = lineSet1.getCurrent (1); 303 assertEquals ("line number is 1", 1, line.getLineNumber ()); 304 assertGetOriginal ("original line number is 1", lineSet1, line, 1); 305 doc.insertString (6, "\nnew line", null); 306 307 assertEquals ("line number is 2", 2, line.getLineNumber ()); 308 assertGetOriginal ("original line number is 1", lineSet1, line, 1); 309 310 support.saveDocument (); 311 312 assertEquals ("line number is 2", 2, line.getLineNumber ()); 313 assertGetOriginal ("original line number is 1", lineSet1, line, 1); 314 315 Line.Set lineSet2 = support.getLineSet (); 316 assertEquals ("line number is 2", 2, line.getLineNumber ()); 317 assertGetOriginal ("original line number is 1", lineSet1, line, 1); 318 assertGetOriginal ("original line number is 2", lineSet2, line, 2); 319 doc.insertString (6, "\nnew line", null); 320 assertEquals ("line number is 3", 3, line.getLineNumber ()); 321 assertGetOriginal ("original line number is 1", lineSet1, line, 1); 322 assertGetOriginal ("original line number is 2", lineSet2, line, 2); 323 support.saveDocument (); 324 assertEquals ("line number is 3", 3, line.getLineNumber ()); 325 assertGetOriginal ("original line number is 1", lineSet1, line, 1); 326 assertGetOriginal ("original line number is 2", lineSet2, line, 2); 327 } 328 329 330 public void testGetOriginalForLineFromDifferentLineSet2() throws Exception { 331 content = "line 1\nline 2\n"; 332 javax.swing.text.Document doc = 333 support.openDocument(); 334 335 Line.Set lineSet1 = support.getLineSet(); 336 337 doc.insertString(6, "\nnew line", null); 338 support.saveDocument(); 339 Line.Set lineSet2 = support.getLineSet(); 340 341 Line line = lineSet2.getCurrent(2); 342 343 assertEquals("line number is 2", 344 2, 345 line.getLineNumber() 346 ); 347 assertGetOriginal ("original line number is 1", lineSet1, line, 1); 348 assertGetOriginal("original line number is 2", lineSet2, line, 2); 349 350 doc.insertString(6, "\nnew line", null); 351 352 assertEquals("line number is 3", 3, line.getLineNumber()); 353 assertGetOriginal("original line number is 1", lineSet1, line, 1); 354 assertGetOriginal("original line number is 2", lineSet2, line, 2); 355 } 356 357 public void testThatNullIsNeverReturnedAsALine () throws Exception { 358 content = "line 1\nline 2\n"; 359 javax.swing.text.Document doc = 360 support.openDocument(); 361 362 Line.Set lineSet1 = support.getLineSet(); 363 364 support.close (); 365 366 Line l = lineSet1.getCurrent (0); 367 assertNotNull ("Some line needs to be returned", l); 368 } 369 370 public void testLineAndLinePartCannotGoOverTheEndOfLine () throws Exception { 371 content = "line 1\nline 2\n"; 372 javax.swing.text.Document doc = support.openDocument(); 373 374 Line.Set lineSet1 = support.getLineSet(); 375 Line first = lineSet1.getCurrent (0); 376 Line.Part part = first.createPart (0, 10); 377 assertEquals ("Line text is the same as text of the part", first.getText (), part.getText ()); 378 379 doc.remove (2, 2); 380 assertEquals ("Ends with \n", part.getText ().length () - 1, part.getText ().indexOf ("\n")); 381 assertEquals ("Line as well", first.getText ().length () - 1, first.getText ().indexOf ("\n")); 382 assertEquals ("Still the line text is the same as text of the part", first.getText (), part.getText ()); 383 } 384 385 public void testLineAndLinePartDoesNotThrowISEIssue53504 () throws Exception { 386 content = "line 1\nline 2\n"; 387 javax.swing.text.Document doc = support.openDocument(); 388 389 Line.Set lineSet1 = support.getLineSet(); 390 Line first = lineSet1.getCurrent (0); 391 Line.Part part = first.createPart (0, 10); 392 assertEquals ("Line text is the same as text of the part", first.getText (), part.getText ()); 393 394 doc.remove (2, doc.getLength () - 2); 395 if (!"li".equals (part.getText ())) { 396 fail ("part now contains just first two characters: " + part.getText ()); 397 } 398 assertEquals ("Still the line text is the same as text of the part", first.getText (), part.getText ()); 399 } 400 401 public void testCreatingLineLongerThanFileFailsIssue53504 () throws Exception { 402 content = "line 1"; 403 javax.swing.text.Document doc = support.openDocument(); 404 405 Line.Set lineSet1 = support.getLineSet(); 406 Line first = lineSet1.getCurrent (0); 407 Line.Part part = first.createPart (8, 10); 408 409 assertNotNull ("Part is returned", part); 410 assertEquals ("But it is of course empty", 0, part.getText ().length ()); 411 } 412 413 private void assertNumberOfLines (int cnt, Line.Set set) throws Exception { 414 class MF implements org.netbeans.junit.MemoryFilter { 415 private java.util.HashSet counted = new java.util.HashSet (); 416 public int cnt; 417 public boolean reject(Object obj) { 418 if (obj instanceof Line) { 419 Line l = (Line)obj; 420 if (counted.add (obj)) { 421 if (l.getLookup ().lookup (LineSetTest.class) == LineSetTest.this) { 422 cnt++; 423 } 424 } 425 } 426 return false; 427 } 428 } 429 430 MF mf = new MF (); 431 432 assertSize ( 434 "Just one line", 435 java.util.Collections.singleton (set), 436 Integer.MAX_VALUE, 437 mf 438 ); 439 440 if (mf.cnt > cnt) { 441 fail ("Only given number of instance of line created (" + cnt + ") but was: " + mf.cnt); 442 } 443 } 444 445 private static void assertGetOriginal (String s, Line.Set set, Line line, int expected) { 446 assertEquals (s + " - Overriden DocumentLine.Set.getOriginal as well", expected, set.getOriginalLineNumber (line)); 447 assertEquals (s + " - The default Line.Set.computeOriginal method works", expected, Line.Set.computeOriginal (set, line)); 448 } 449 450 private static void assertNonmutable (java.util.List <? extends Line> l) throws Exception { 451 465 466 try { 467 l.remove (new Object ()); 468 fail ("remove should fail"); 469 } catch (java.lang.UnsupportedOperationException ex) { 470 } 472 473 487 try { 488 l.removeAll (java.util.Collections.EMPTY_LIST); 489 fail ("removeAll should fail"); 490 } catch (java.lang.UnsupportedOperationException ex) { 491 } 493 494 try { 495 l.retainAll (java.util.Collections.EMPTY_LIST); 496 fail ("retainAll should fail"); 497 } catch (java.lang.UnsupportedOperationException ex) { 498 } 500 try { 501 l.set (0, null); 502 fail ("set should fail"); 503 } catch (java.lang.UnsupportedOperationException ex) { 504 } 506 } 507 508 512 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 513 propL.add (l); 514 } 515 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 516 propL.remove (l); 517 } 518 519 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 520 assertNull ("This is the first veto listener", vetoL); 521 vetoL = l; 522 } 523 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 524 assertEquals ("Removing the right veto one", vetoL, l); 525 vetoL = null; 526 } 527 528 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 529 return support; 530 } 531 532 public String getMimeType() { 533 return "text/plain"; 534 } 535 536 public java.util.Date getTime() { 537 return date; 538 } 539 540 public java.io.InputStream inputStream() throws java.io.IOException { 541 return new java.io.ByteArrayInputStream (content.getBytes ()); 542 } 543 public java.io.OutputStream outputStream() throws java.io.IOException { 544 class ContentStream extends java.io.ByteArrayOutputStream { 545 public void close () throws java.io.IOException { 546 super.close (); 547 content = new String (toByteArray ()); 548 } 549 } 550 551 return new ContentStream (); 552 } 553 554 public boolean isValid() { 555 return valid; 556 } 557 558 public boolean isModified() { 559 return modified; 560 } 561 562 public void markModified() throws java.io.IOException { 563 modified = true; 564 } 565 566 public void unmarkModified() { 567 modified = false; 568 } 569 570 571 private static final class CES extends CloneableEditorSupport { 572 public CES (Env env, Lookup l) { 573 super (env, l); 574 } 575 576 protected String messageName() { 577 return "Name"; 578 } 579 580 protected String messageOpened() { 581 return "Opened"; 582 } 583 584 protected String messageOpening() { 585 return "Opening"; 586 } 587 588 protected String messageSave() { 589 return "Save"; 590 } 591 592 protected String messageToolTip() { 593 return "ToolTip"; 594 } 595 596 } 597 } 598 | Popular Tags |