1 19 package org.netbeans.api.editor.guards; 20 21 import java.beans.PropertyVetoException ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import javax.swing.text.BadLocationException ; 26 import javax.swing.text.Position ; 27 import junit.framework.TestCase; 28 import org.netbeans.api.editor.guards.Editor; 29 import org.netbeans.api.editor.guards.GuardedSection; 30 import org.netbeans.api.editor.guards.GuardedSectionManager; 31 32 36 public class GuardedSectionManagerTest extends TestCase { 37 38 private Editor editor; 39 private GuardedSectionManager guards; 40 41 42 public GuardedSectionManagerTest(String testName) { 43 super(testName); 44 } 45 46 protected void setUp() throws Exception { 47 editor = new Editor(); 48 49 GuardUtils.initManager(editor); 50 51 guards = GuardedSectionManager.getInstance(editor.doc); 52 assertNotNull("missing manager", guards); 53 } 54 55 protected void tearDown() throws Exception { 56 } 57 58 public void testSimpleSection() throws BadLocationException { 59 System.out.println("-- testSimpleSection -------------"); 60 61 editor.doc.insertString(0, "aaa", null); 63 65 Position ss1Start = editor.doc.createPosition(0); 67 int ss1StartI = ss1Start.getOffset(); 68 SimpleSection ss1 = guards.createSimpleSection(ss1Start, "ss1"); 69 assertTrue("ss1.valid", ss1.isValid()); 70 assertEquals("ss1.name", "ss1", ss1.getName()); 71 72 Position ss1End = ss1.getEndPosition(); 73 GuardUtils.verifyPositions(this, ss1, ss1StartI + 1, ss1StartI + 2); 78 GuardUtils.verifyGuardAttr(this, editor.doc, ss1); 79 80 81 Position ss2Start = editor.doc.createPosition(ss1End.getOffset() + 1); 82 int ss2StartI = ss2Start.getOffset(); 83 SimpleSection ss2 = guards.createSimpleSection(ss2Start, "ss2"); 85 assertTrue("ss2.valid", ss2.isValid()); 86 assertEquals("ss2.name", "ss2", ss2.getName()); 87 88 GuardUtils.verifyPositions(this, ss1, ss1StartI + 1, ss1StartI + 2); 94 GuardUtils.verifyPositions(this, ss2, ss2StartI + 1, ss2StartI + 2); 95 GuardUtils.verifyGuardAttr(this, editor.doc, ss1); 96 GuardUtils.verifyGuardAttr(this, editor.doc, ss2); 97 98 String txt = "X\n"; 101 ss1StartI = ss1.getStartPosition().getOffset(); 103 int ss1EndI = ss1.getEndPosition().getOffset(); 104 ss2StartI = ss2.getStartPosition().getOffset(); 105 int ss2EndI = ss2.getEndPosition().getOffset(); 106 editor.doc.insertString(0, txt, null); 107 GuardUtils.verifyPositions(this, ss1, ss1StartI + txt.length(), ss1EndI + txt.length()); 111 GuardUtils.verifyPositions(this, ss2, ss2StartI + txt.length(), ss2EndI + txt.length()); 112 GuardUtils.verifyGuardAttr(this, editor.doc, ss1); 113 GuardUtils.verifyGuardAttr(this, editor.doc, ss2); 114 115 116 txt = "XX"; 119 ss1StartI = ss1.getStartPosition().getOffset(); 121 ss1EndI = ss1.getEndPosition().getOffset(); 122 ss2StartI = ss2.getStartPosition().getOffset(); 123 ss2EndI = ss2.getEndPosition().getOffset(); 124 assertTrue(ss1EndI < ss2StartI); 125 126 ss1.setText(txt); 127 128 assertEquals("ss1.setText", txt, ss1.getText()); 132 GuardUtils.verifyPositions(this, ss1, ss1StartI, ss1StartI + txt.length()); 133 GuardUtils.verifyPositions(this, ss2, 134 ss1.getEndPosition().getOffset() + 2, 135 ss1.getEndPosition().getOffset() + 2 + 1); 136 GuardUtils.verifyGuardAttr(this, editor.doc, ss1); 138 GuardUtils.verifyGuardAttr(this, editor.doc, ss2); 139 140 ss2.setText("XX"); 142 GuardUtils.verifyGuardAttr(this, editor.doc, ss1); 146 GuardUtils.verifyGuardAttr(this, editor.doc, ss2); 147 148 ss1.deleteSection(); 150 assertTrue("ss1.valid", !ss1.isValid()); 155 GuardUtils.verifyGuardAttr(this, editor.doc, ss2); 156 } 157 158 public void testDeleteSimpleSection() throws BadLocationException { 159 System.out.println("-- testDeleteSimpleSection -------------"); 160 161 editor.doc.insertString(0, "aaa", null); 162 SimpleSection ss = guards.createSimpleSection(editor.doc.createPosition(1), "ss1"); 163 164 assertEquals("ss1.content", "a\n \naa", editor.doc.getText(0, editor.doc.getLength())); 165 ss.deleteSection(); 166 assertEquals("ss1.content2", "aaa", editor.doc.getText(0, editor.doc.getLength())); 167 assertTrue("valid", !ss.isValid()); 168 } 169 170 public void testRemoveSimpleSection() throws BadLocationException { 171 System.out.println("-- testRemoveSimpleSection -------------"); 172 173 editor.doc.insertString(0, "aaa", null); 174 SimpleSection ss = guards.createSimpleSection(editor.doc.createPosition(1), "ss1"); 175 176 assertEquals("ss1.content", "a\n \naa", editor.doc.getText(0, editor.doc.getLength())); 177 assertTrue("'\\n'", !GuardUtils.isGuarded(editor.doc, 1)); 178 assertTrue("' '", GuardUtils.isGuarded(editor.doc, 2)); 179 assertTrue("'\\n'", GuardUtils.isGuarded(editor.doc, 3)); 180 assertTrue("'a'", !GuardUtils.isGuarded(editor.doc, 4)); 181 ss.removeSection(); 182 assertEquals("ss1.content2", "a\n \naa", editor.doc.getText(0, editor.doc.getLength())); 183 assertTrue("valid", !ss.isValid()); 184 assertTrue("'\\n'", !GuardUtils.isGuarded(editor.doc, 1)); 185 assertTrue("' '", !GuardUtils.isGuarded(editor.doc, 2)); 186 assertTrue("'\\n'", !GuardUtils.isGuarded(editor.doc, 3)); 187 assertTrue("'a'", !GuardUtils.isGuarded(editor.doc, 4)); 188 } 189 190 public void testDeleteInteriorSection() throws BadLocationException { 191 System.out.println("-- testDeleteInteriorSection -------------"); 192 193 editor.doc.insertString(0, "aaa", null); 194 InteriorSection is = guards.createInteriorSection(editor.doc.createPosition(1), "is1"); 195 196 assertEquals("is1.content", "a\n \n \n \naa", editor.doc.getText(0, editor.doc.getLength())); 197 is.deleteSection(); 198 assertEquals("ss1.content2", "aaa", editor.doc.getText(0, editor.doc.getLength())); 199 assertTrue("valid", !is.isValid()); 200 } 201 202 public void testRemoveInteriorSection() throws BadLocationException { 203 System.out.println("-- testRemoveInteriorSection -------------"); 204 205 editor.doc.insertString(0, "aaa", null); 206 InteriorSection is = guards.createInteriorSection(editor.doc.createPosition(1), "is1"); 207 208 assertEquals("is1.content", "a\n \n \n \naa", editor.doc.getText(0, editor.doc.getLength())); 209 assertTrue("'\\n'", !GuardUtils.isGuarded(editor.doc, 1)); 210 assertTrue("header.' '", GuardUtils.isGuarded(editor.doc, 2)); 211 assertTrue("header.'\\n'", GuardUtils.isGuarded(editor.doc, 3)); 212 assertTrue("body.' '", !GuardUtils.isGuarded(editor.doc, 4)); 213 assertTrue("body.'\\n'", !GuardUtils.isGuarded(editor.doc, 5)); 214 assertTrue("footer.' '", GuardUtils.isGuarded(editor.doc, 6)); 215 assertTrue("footer.'\\n'", GuardUtils.isGuarded(editor.doc, 7)); 216 assertTrue("'a'", !GuardUtils.isGuarded(editor.doc, 8)); 217 is.removeSection(); 218 assertEquals("is1.content2", "a\n \n \n \naa", editor.doc.getText(0, editor.doc.getLength())); 219 assertTrue("'\\n'", !GuardUtils.isGuarded(editor.doc, 1)); 220 assertTrue("header.' '", !GuardUtils.isGuarded(editor.doc, 2)); 221 assertTrue("header.'\\n'", !GuardUtils.isGuarded(editor.doc, 3)); 222 assertTrue("body.' '", !GuardUtils.isGuarded(editor.doc, 4)); 223 assertTrue("body.'\\n'", !GuardUtils.isGuarded(editor.doc, 5)); 224 assertTrue("footer.' '", !GuardUtils.isGuarded(editor.doc, 6)); 225 assertTrue("footer.'\\n'", !GuardUtils.isGuarded(editor.doc, 7)); 226 assertTrue("'a'", !GuardUtils.isGuarded(editor.doc, 8)); 227 } 228 229 public void testInteriorSection() throws BadLocationException { 230 System.out.println("-- testInteriorSection -------------"); 231 232 editor.doc.insertString(0, "aaa", null); 234 236 Position s1Start = editor.doc.createPosition(0); 238 int s1StartI = s1Start.getOffset(); 239 InteriorSection s1 = guards.createInteriorSection(s1Start, "s1"); 240 assertTrue("s1.valid", s1.isValid()); 241 assertEquals("s1.name", "s1", s1.getName()); 242 243 Position s1End = s1.getEndPosition(); 244 GuardUtils.verifyPositions(this, s1, s1StartI + 1, s1StartI + 6); 249 GuardUtils.verifyPositions(this, s1, s1StartI + 1, s1StartI + 2, s1StartI + 3, s1StartI + 4, s1StartI + 5, s1StartI + 6); 250 GuardUtils.verifyGuardAttr(this, editor.doc, s1); 251 252 253 Position s2Start = editor.doc.createPosition(s1End.getOffset() + 1); 254 int s2StartI = s2Start.getOffset(); 255 InteriorSection s2 = guards.createInteriorSection(s2Start, "s2"); 257 assertTrue("s2.valid", s2.isValid()); 258 assertEquals("s2.name", "s2", s2.getName()); 259 260 GuardUtils.verifyPositions(this, s1, s1StartI + 1, s1StartI + 6); 265 GuardUtils.verifyPositions(this, s1, s1StartI + 1, s1StartI + 2, s1StartI + 3, s1StartI + 4, s1StartI + 5, s1StartI + 6); 266 GuardUtils.verifyPositions(this, s2, s2StartI + 1, s2StartI + 6); 267 GuardUtils.verifyPositions(this, s2, s2StartI + 1, s2StartI + 2, s2StartI + 3, s2StartI + 4, s2StartI + 5, s2StartI + 6); 268 GuardUtils.verifyGuardAttr(this, editor.doc, s1); 269 GuardUtils.verifyGuardAttr(this, editor.doc, s2); 270 271 String txt = "text before sections\n"; 274 int s1HeaderBegin = s1.getImpl().getHeaderBounds().getBegin().getOffset(); 275 int s1HeaderEnd = s1.getImpl().getHeaderBounds().getEnd().getOffset(); 276 int s1BodyBegin = s1.getImpl().getBodyBounds().getBegin().getOffset(); 277 int s1BodyEnd = s1.getImpl().getBodyBounds().getEnd().getOffset(); 278 int s1FooterBegin = s1.getImpl().getFooterBounds().getBegin().getOffset(); 279 int s1FooterEnd = s1.getImpl().getFooterBounds().getEnd().getOffset(); 280 int s2HeaderBegin = s2.getImpl().getHeaderBounds().getBegin().getOffset(); 281 int s2HeaderEnd = s2.getImpl().getHeaderBounds().getEnd().getOffset(); 282 int s2BodyBegin = s2.getImpl().getBodyBounds().getBegin().getOffset(); 283 int s2BodyEnd = s2.getImpl().getBodyBounds().getEnd().getOffset(); 284 int s2FooterBegin = s2.getImpl().getFooterBounds().getBegin().getOffset(); 285 int s2FooterEnd = s2.getImpl().getFooterBounds().getEnd().getOffset(); 286 s1StartI = s1.getStartPosition().getOffset(); 287 int s1EndI = s1.getEndPosition().getOffset(); 288 289 editor.doc.insertString(0, txt, null); 290 GuardUtils.verifyPositions(this, s1, s1StartI + txt.length(), s1EndI + txt.length()); 294 GuardUtils.verifyPositions(this, s1, 295 s1HeaderBegin + txt.length(), s1HeaderEnd + txt.length(), 296 s1BodyBegin + txt.length(), s1BodyEnd + txt.length(), 297 s1FooterBegin + txt.length(), s1FooterEnd + txt.length() 298 ); 299 GuardUtils.verifyPositions(this, s2, 300 s2HeaderBegin + txt.length(), s2HeaderEnd + txt.length(), 301 s2BodyBegin + txt.length(), s2BodyEnd + txt.length(), 302 s2FooterBegin + txt.length(), s2FooterEnd + txt.length() 303 ); 304 GuardUtils.verifyGuardAttr(this, editor.doc, s1); 305 GuardUtils.verifyGuardAttr(this, editor.doc, s2); 306 307 308 s1HeaderBegin = s1.getImpl().getHeaderBounds().getBegin().getOffset(); 310 assertTrue("is not guarded", GuardUtils.isGuarded(editor.doc, s1HeaderBegin)); 312 s1HeaderEnd = s1.getImpl().getHeaderBounds().getEnd().getOffset(); 313 s1BodyBegin = s1.getImpl().getBodyBounds().getBegin().getOffset(); 314 s1BodyEnd = s1.getImpl().getBodyBounds().getEnd().getOffset(); 315 s1FooterBegin = s1.getImpl().getFooterBounds().getBegin().getOffset(); 316 s1FooterEnd = s1.getImpl().getFooterBounds().getEnd().getOffset(); 317 s2HeaderBegin = s2.getImpl().getHeaderBounds().getBegin().getOffset(); 318 s2HeaderEnd = s2.getImpl().getHeaderBounds().getEnd().getOffset(); 319 s2BodyBegin = s2.getImpl().getBodyBounds().getBegin().getOffset(); 320 s2BodyEnd = s2.getImpl().getBodyBounds().getEnd().getOffset(); 321 s2FooterBegin = s2.getImpl().getFooterBounds().getBegin().getOffset(); 322 s2FooterEnd = s2.getImpl().getFooterBounds().getEnd().getOffset(); 323 String s1Header = "HEADER"; 324 String s1Body = "BODY"; 325 String s1Footer = "FOOTER"; 326 s1.setHeader(s1Header); 327 s1.setBody(s1Body); 328 s1.setFooter(s1Footer); 329 GuardUtils.verifyPositions(this, s1, 333 s1HeaderBegin, s1HeaderEnd + s1Header.length() - 1, 334 s1BodyBegin + s1Header.length() - 1, s1BodyEnd + s1Header.length() + s1Body.length() - 2, 335 s1FooterBegin + s1Header.length() + s1Body.length() - 2, s1FooterEnd + s1Header.length() + s1Body.length() + s1Footer.length() - 3 336 ); 337 338 340 int length = s1Header.length() + s1Body.length() + s1Footer.length() - 3; 341 GuardUtils.verifyPositions(this, s2, 342 s2HeaderBegin + length, s2HeaderEnd + length, 343 s2BodyBegin + length, s2BodyEnd + length, 344 s2FooterBegin + length, s2FooterEnd + length 345 ); 346 GuardUtils.verifyGuardAttr(this, editor.doc, s1); 347 GuardUtils.verifyGuardAttr(this, editor.doc, s2); 348 349 350 s2.setHeader("HEADER2"); 351 s2.setBody("BODY2"); 352 s2.setFooter("FOOTER2"); 353 367 s1.deleteSection(); 369 assertTrue("ss1.valid", !s1.isValid()); 373 GuardUtils.verifyGuardAttr(this, editor.doc, s2); 374 375 } 376 377 public void testManagement() throws BadLocationException { 378 System.out.println("testManagement"); 379 380 List <GuardedSection> wanted = new ArrayList <GuardedSection>(); 381 382 String gs1Name = "gs1"; 384 GuardedSection gs1 = guards.createSimpleSection(editor.doc.createPosition(0), gs1Name); 385 assertNotNull(gs1Name, gs1); 386 assertEquals("gs1 not found", gs1, guards.findSimpleSection(gs1Name)); 387 assertNull("wrong section type", guards.findInteriorSection(gs1Name)); 388 wanted.add(gs1); 389 verifyGuards(wanted, guards.getGuardedSections()); 390 391 String gs2Name = "gs2"; 393 GuardedSection gs2 = guards.createSimpleSection( 394 editor.doc.createPosition(gs1.getEndPosition().getOffset() + 1), 395 gs2Name); 396 assertNotNull(gs2Name, gs2); 397 assertEquals("gs2 not found", gs2, guards.findSimpleSection(gs2Name)); 398 assertNull("wrong section type", guards.findInteriorSection(gs2Name)); 399 assertEquals("gs1 not found", gs1, guards.findSimpleSection(gs1Name)); 400 wanted.add(gs2); 401 verifyGuards(wanted, guards.getGuardedSections()); 402 403 String gs3Name = "gs3"; 405 GuardedSection gs3 = guards.createInteriorSection( 406 editor.doc.createPosition(gs2.getEndPosition().getOffset() + 1), 407 gs3Name); 408 assertNotNull(gs3Name, gs3); 409 assertEquals("gs3 not found", gs3, guards.findInteriorSection(gs3Name)); 410 assertNull("wrong section type", guards.findSimpleSection(gs3Name)); 411 assertEquals("gs1 not found", gs1, guards.findSimpleSection(gs1Name)); 412 assertEquals("gs2 not found", gs2, guards.findSimpleSection(gs2Name)); 413 wanted.add(gs3); 414 verifyGuards(wanted, guards.getGuardedSections()); 415 416 String gs4Name = "gs4"; 418 GuardedSection gs4 = guards.createSimpleSection( 419 editor.doc.createPosition(gs1.getEndPosition().getOffset() + 1), 420 gs4Name); 421 assertNotNull(gs4Name, gs4); 422 assertEquals("gs4 not found", gs4, guards.findSimpleSection(gs4Name)); 423 assertNull("wrong section type", guards.findInteriorSection(gs4Name)); 424 assertEquals("gs1 not found", gs1, guards.findSimpleSection(gs1Name)); 425 assertEquals("gs2 not found", gs2, guards.findSimpleSection(gs2Name)); 426 assertEquals("gs3 not found", gs3, guards.findInteriorSection(gs3Name)); 427 wanted.add(1, gs4); 428 verifyGuards(wanted, guards.getGuardedSections()); 429 430 gs4.deleteSection(); 432 assertNull("gs4 found", guards.findSimpleSection(gs4Name)); 433 assertNull("wrong section type", guards.findInteriorSection(gs4Name)); 434 assertEquals("gs1 not found", gs1, guards.findSimpleSection(gs1Name)); 435 assertEquals("gs2 not found", gs2, guards.findSimpleSection(gs2Name)); 436 assertEquals("gs3 not found", gs3, guards.findInteriorSection(gs3Name)); 437 wanted.remove(gs4); 438 verifyGuards(wanted, guards.getGuardedSections()); 439 440 gs4 = guards.createSimpleSection( 442 editor.doc.createPosition(gs1.getEndPosition().getOffset() + 1), 443 gs4Name); 444 assertNotNull(gs4Name, gs4); 445 assertEquals("gs4 not found", gs4, guards.findSimpleSection(gs4Name)); 446 assertNull("wrong section type", guards.findInteriorSection(gs4Name)); 447 assertEquals("gs1 not found", gs1, guards.findSimpleSection(gs1Name)); 448 assertEquals("gs2 not found", gs2, guards.findSimpleSection(gs2Name)); 449 assertEquals("gs3 not found", gs3, guards.findInteriorSection(gs3Name)); 450 wanted.add(1, gs4); 451 verifyGuards(wanted, guards.getGuardedSections()); 452 453 gs1.deleteSection(); 455 assertNull("gs1 found", guards.findSimpleSection(gs1Name)); 456 assertNull("wrong section type", guards.findInteriorSection(gs1Name)); 457 assertEquals("gs2 not found", gs2, guards.findSimpleSection(gs2Name)); 458 assertEquals("gs3 not found", gs3, guards.findInteriorSection(gs3Name)); 459 assertEquals("gs4 not found", gs4, guards.findSimpleSection(gs4Name)); 460 wanted.remove(gs1); 461 verifyGuards(wanted, guards.getGuardedSections()); 462 463 gs3.deleteSection(); 465 assertNull("gs3 found", guards.findSimpleSection(gs3Name)); 466 assertNull("wrong section type", guards.findInteriorSection(gs3Name)); 467 assertEquals("gs2 not found", gs2, guards.findSimpleSection(gs2Name)); 468 assertEquals("gs4 not found", gs4, guards.findSimpleSection(gs4Name)); 469 wanted.remove(gs3); 470 verifyGuards(wanted, guards.getGuardedSections()); 471 472 gs2.deleteSection(); 474 assertNull("gs2 found", guards.findSimpleSection(gs2Name)); 475 assertNull("wrong section type", guards.findInteriorSection(gs2Name)); 476 assertEquals("gs4 not found", gs4, guards.findSimpleSection(gs4Name)); 477 wanted.remove(gs2); 478 verifyGuards(wanted, guards.getGuardedSections()); 479 480 gs4.deleteSection(); 482 assertNull("gs4 found", guards.findSimpleSection(gs4Name)); 483 assertNull("wrong section type", guards.findInteriorSection(gs4Name)); 484 wanted.remove(gs4); 485 verifyGuards(wanted, guards.getGuardedSections()); 486 487 } 488 489 491 public void testRenameSimpleSection() throws BadLocationException , PropertyVetoException { 492 System.out.println("-- testRenameSimpleSection -------------"); 493 494 editor.doc.insertString(0, "aaa", null); 495 SimpleSection ss1 = guards.createSimpleSection(editor.doc.createPosition(1), "ss1"); 496 assertEquals("name", "ss1", ss1.getName()); 497 ss1.setName("ssNewName"); 498 assertTrue("valid", ss1.isValid()); 499 assertEquals("new name", "ssNewName", ss1.getName()); 500 ss1.setName("ssNewName"); 502 503 SimpleSection ss2 = guards.createSimpleSection( 504 editor.doc.createPosition(ss1.getEndPosition().getOffset() + 1), 505 "ss2"); 506 507 try { 509 ss1.setName("ss2"); 510 fail("accepted already existing name"); 511 } catch (PropertyVetoException ex) { 512 assertTrue("valid", ss1.isValid()); 513 assertEquals("name", "ssNewName", ss1.getName()); 514 } 515 } 516 517 public void testRenameInteriorSection() throws BadLocationException , PropertyVetoException { 518 System.out.println("-- testRenameInteriorSection -------------"); 519 520 editor.doc.insertString(0, "aaa", null); 521 InteriorSection is1 = guards.createInteriorSection(editor.doc.createPosition(1), "is1"); 522 assertEquals("name", "is1", is1.getName()); 523 is1.setName("isNewName"); 524 assertTrue("valid", is1.isValid()); 525 assertEquals("new name", "isNewName", is1.getName()); 526 is1.setName("isNewName"); 528 529 InteriorSection is2 = guards.createInteriorSection( 530 editor.doc.createPosition(is1.getEndPosition().getOffset() + 1), 531 "is2"); 532 533 try { 535 is1.setName("is2"); 536 fail("accepted already existing name"); 537 } catch (PropertyVetoException ex) { 538 assertTrue("valid", is1.isValid()); 539 assertEquals("name", "isNewName", is1.getName()); 540 } 541 } 542 543 public void testCreateSimpleSection() throws BadLocationException { 544 System.out.println("-- testCreateSimpleSection -------------"); 545 546 editor.doc.insertString(0, "aaa", null); 547 SimpleSection ss1 = guards.createSimpleSection(editor.doc.createPosition(1), "ss1"); 548 assertEquals("doc.content", "a\n \naa", editor.doc.getText(0, editor.doc.getLength())); 549 550 try { 551 guards.createSimpleSection(editor.doc.createPosition(2), "ss2"); 552 fail("section created inside another section!"); 553 } catch (Throwable t) { 554 assertEquals("wrong exception", IllegalArgumentException .class, t.getClass()); 555 } 556 557 try { 558 guards.createSimpleSection( 559 editor.doc.createPosition(ss1.getEndPosition().getOffset() + 1), 560 "ss1"); 561 fail("section with duplicate name created!"); 562 } catch (Throwable t) { 563 assertEquals("wrong exception", IllegalArgumentException .class, t.getClass()); 564 } 565 566 guards.createSimpleSection( 567 editor.doc.createPosition(ss1.getEndPosition().getOffset() + 1), 568 "ss2"); 569 assertEquals("doc.content", "a\n \n\n \naa", editor.doc.getText(0, editor.doc.getLength())); 570 } 571 572 public void testCreateInteriorSection() throws BadLocationException { 573 System.out.println("-- testCreateInteriorSection -------------"); 574 575 editor.doc.insertString(0, "aaa", null); 576 InteriorSection is1 = guards.createInteriorSection(editor.doc.createPosition(1), "is1"); 577 assertEquals("doc.content", "a\n \n \n \naa", editor.doc.getText(0, editor.doc.getLength())); 578 579 try { 580 guards.createInteriorSection(editor.doc.createPosition(4), "is2"); 581 fail("section created inside another section!"); 582 } catch (Throwable t) { 583 assertEquals("wrong exception", IllegalArgumentException .class, t.getClass()); 584 } 585 586 try { 587 guards.createInteriorSection( 588 editor.doc.createPosition(is1.getEndPosition().getOffset() + 1), 589 "is1"); 590 fail("section with duplicate name created!"); 591 } catch (Throwable t) { 592 assertEquals("wrong exception", IllegalArgumentException .class, t.getClass()); 593 } 594 595 guards.createInteriorSection( 596 editor.doc.createPosition(is1.getEndPosition().getOffset() + 1), 597 "is2"); 598 assertEquals("doc.content", "a\n \n \n \n\n \n \n \naa", editor.doc.getText(0, editor.doc.getLength())); 599 } 600 601 public void testModifyInteriorSection() throws BadLocationException { 602 System.out.println("-- testModifyInteriorSection -------------"); 603 604 editor.doc.insertString(0, "aaa", null); 605 InteriorSection is1 = guards.createInteriorSection(editor.doc.createPosition(1), "is1"); 606 assertEquals("doc.content", "a\n \n \n \naa", editor.doc.getText(0, editor.doc.getLength())); 607 assertTrue(GuardUtils.isGuarded(editor.doc, 3)); assertTrue(!GuardUtils.isGuarded(editor.doc, 4)); assertTrue(!GuardUtils.isGuarded(editor.doc, 5)); assertTrue(GuardUtils.isGuarded(editor.doc, 6)); 612 editor.doc.insertString(4, "X", null); 613 assertEquals("doc.content", "a\n \nX \n \naa", editor.doc.getText(0, editor.doc.getLength())); 614 assertTrue(GuardUtils.isGuarded(editor.doc, 3)); assertTrue(!GuardUtils.isGuarded(editor.doc, 4)); assertTrue(!GuardUtils.isGuarded(editor.doc, 5)); assertTrue(!GuardUtils.isGuarded(editor.doc, 6)); assertTrue(GuardUtils.isGuarded(editor.doc, 7)); 620 is1.setHeader("HEADER"); 621 is1.setFooter("FOOTER"); 622 is1.setBody("BODY"); 623 assertEquals("doc.content", "a\nHEADER\nBODY\nFOOTER\naa", editor.doc.getText(0, editor.doc.getLength())); 624 625 String content = "a\nHEADER\nBODY\nFOOTER\naa"; 626 editor.doc.insertString(content.indexOf("BODY"), "X", null); 627 content = "a\nHEADER\nXBODY\nFOOTER\naa"; 628 assertTrue(GuardUtils.isGuarded(editor.doc, content.indexOf("\nXBODY"))); assertTrue(!GuardUtils.isGuarded(editor.doc, content.indexOf("XBODY"))); assertTrue(!GuardUtils.isGuarded(editor.doc, content.indexOf("BODY"))); assertTrue(!GuardUtils.isGuarded(editor.doc, content.indexOf("\nFOOTER"))); assertTrue(GuardUtils.isGuarded(editor.doc, content.indexOf("FOOTER"))); } 634 635 private void verifyGuards(Iterable <GuardedSection> wanted, Iterable <GuardedSection> queried) { 636 Iterator <GuardedSection> itWanted = wanted.iterator(); 637 Iterator <GuardedSection> itQueried = queried.iterator(); 638 for(int i = 0; ; i++) { 639 if (itWanted.hasNext()) { 640 assertTrue(i + ": missing guard" , itQueried.hasNext()); 641 assertEquals(i + ": wrong guard", itWanted.next(), itQueried.next()); 642 } else { 643 assertFalse(i + ": extra guard ", itQueried.hasNext()); 644 break; 645 } 646 } 647 } 648 } 649 | Popular Tags |