KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > editor > guards > GuardedSectionManagerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.editor.guards;
20
21 import java.beans.PropertyVetoException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.text.BadLocationException JavaDoc;
26 import javax.swing.text.Position JavaDoc;
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 /**
33  *
34  * @author Jan Pokorsky
35  */

36 public class GuardedSectionManagerTest extends TestCase {
37     
38     private Editor editor;
39     private GuardedSectionManager guards;
40     
41     /** Creates a new instance of GuardedSectionManagerTest */
42     public GuardedSectionManagerTest(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
56     }
57     
58     public void testSimpleSection() throws BadLocationException JavaDoc {
59         System.out.println("-- testSimpleSection -------------");
60         
61 // System.out.println("...init document");
62
editor.doc.insertString(0, "aaa", null);
63 // GuardUtils.dumpDocument(editor.doc);
64

65 // System.out.println("...create simple section on 0");
66
Position JavaDoc 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 JavaDoc ss1End = ss1.getEndPosition();
73 // GuardUtils.print(ss1);
74
// GuardUtils.dumpDocument(editor.doc);
75
// GuardUtils.dumpGuardedAttr(editor.doc);
76
// posinions verification
77
GuardUtils.verifyPositions(this, ss1, ss1StartI + 1, ss1StartI + 2);
78         GuardUtils.verifyGuardAttr(this, editor.doc, ss1);
79         
80         
81         Position JavaDoc ss2Start = editor.doc.createPosition(ss1End.getOffset() + 1);
82         int ss2StartI = ss2Start.getOffset();
83 // System.out.println("...create simple section on " + ss2StartI);
84
SimpleSection ss2 = guards.createSimpleSection(ss2Start, "ss2");
85         assertTrue("ss2.valid", ss2.isValid());
86         assertEquals("ss2.name", "ss2", ss2.getName());
87         
88 // GuardUtils.print(ss1);
89
// GuardUtils.print(ss2);
90
// GuardUtils.dumpDocument(editor.doc);
91
// GuardUtils.dumpGuardedAttr(editor.doc);
92
// posinions verification
93
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         // test insertion to document beginning
99
// System.out.println("...insert text to beginning");
100
String JavaDoc txt = "X\n";
101 // String txt = "text before sections\n";
102
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.print(ss1);
108
// GuardUtils.print(ss2);
109
// GuardUtils.dumpDocument(editor.doc);
110
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         // test adding text to section
117
// System.out.println("...set text to ss1");
118
txt = "XX";
119 // txt = "ss1 simple text\n";
120
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 // GuardUtils.print(ss1);
129
// GuardUtils.print(ss2);
130
// GuardUtils.dumpDocument(editor.doc);
131
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.dumpGuardedAttr(editor.doc);
137
GuardUtils.verifyGuardAttr(this, editor.doc, ss1);
138         GuardUtils.verifyGuardAttr(this, editor.doc, ss2);
139         
140 // System.out.println("...set text to ss2");
141
ss2.setText("XX");
142 // GuardUtils.print(ss1);
143
// GuardUtils.print(ss2);
144
// GuardUtils.dumpDocument(editor.doc);
145
GuardUtils.verifyGuardAttr(this, editor.doc, ss1);
146         GuardUtils.verifyGuardAttr(this, editor.doc, ss2);
147
148 // System.out.println("...delete ss1");
149
ss1.deleteSection();
150 // GuardUtils.print(ss1);
151
// GuardUtils.print(ss2);
152
// GuardUtils.dumpDocument(editor.doc);
153
// GuardUtils.dumpGuardedAttr(editor.doc);
154
assertTrue("ss1.valid", !ss1.isValid());
155         GuardUtils.verifyGuardAttr(this, editor.doc, ss2);
156     }
157     
158     public void testDeleteSimpleSection() throws BadLocationException JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
230         System.out.println("-- testInteriorSection -------------");
231         
232 // System.out.println("...init document");
233
editor.doc.insertString(0, "aaa", null);
234 // GuardUtils.dumpDocument(editor.doc);
235

236 // System.out.println("...create interior section on 0");
237
Position JavaDoc 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 JavaDoc s1End = s1.getEndPosition();
244 // GuardUtils.print(s1);
245
// GuardUtils.dumpDocument(editor.doc);
246
// GuardUtils.dumpGuardedAttr(editor.doc);
247
// posinions verification
248
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 JavaDoc s2Start = editor.doc.createPosition(s1End.getOffset() + 1);
254         int s2StartI = s2Start.getOffset();
255 // System.out.println("...create interior section on " + s2StartI);
256
InteriorSection s2 = guards.createInteriorSection(s2Start, "s2");
257         assertTrue("s2.valid", s2.isValid());
258         assertEquals("s2.name", "s2", s2.getName());
259         
260 // GuardUtils.print(s1);
261
// GuardUtils.print(s2);
262
// GuardUtils.dumpDocument(editor.doc);
263
// posinions verification
264
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         // test insertion to document beginning
272
// System.out.println("...insert text to beginning");
273
String JavaDoc 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.print(s1);
291
// GuardUtils.print(s2);
292
// GuardUtils.dumpDocument(editor.doc);
293
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         // test adding content to section
309
s1HeaderBegin = s1.getImpl().getHeaderBounds().getBegin().getOffset();
310 // GuardUtils.dumpGuardedAttr(editor.doc);
311
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 JavaDoc s1Header = "HEADER";
324         String JavaDoc s1Body = "BODY";
325         String JavaDoc s1Footer = "FOOTER";
326         s1.setHeader(s1Header);
327         s1.setBody(s1Body);
328         s1.setFooter(s1Footer);
329 // GuardUtils.print(s1);
330
// GuardUtils.print(s2);
331
// GuardUtils.dumpDocument(editor.doc);
332
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 // GuardUtils.dumpGuardedAttr(editor.doc);
339

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 // GuardUtils.print(s1);
354
// GuardUtils.print(s2);
355
// GuardUtils.dumpDocument(editor.doc);
356
// System.out.println("...set text to ss1");
357
// txt = "ss1 simple text\n";
358
// s1StartI = s1.getStartPosition().getOffset();
359
// s2StartI = s2.getStartPosition().getOffset();
360
// s2EndI = s2.getEndPosition().getOffset();
361
// s1.setBody(txt);
362
// GuardUtils.dumpDocument(editor.doc);
363
// assertEquals("s1.setText", txt, s1.getText());
364
// GuardUtils.verifyPositions(this, s1, s1StartI, s1StartI + txt.length());
365
// GuardUtils.verifyPositions(this, s2, s2StartI + txt.length(), s2EndI + txt.length());
366

367         // delete interior section
368
s1.deleteSection();
369 // GuardUtils.print(s1);
370
// GuardUtils.print(s2);
371
// GuardUtils.dumpDocument(editor.doc);
372
assertTrue("ss1.valid", !s1.isValid());
373         GuardUtils.verifyGuardAttr(this, editor.doc, s2);
374
375     }
376     
377     public void testManagement() throws BadLocationException JavaDoc {
378         System.out.println("testManagement");
379         
380         List JavaDoc<GuardedSection> wanted = new ArrayList JavaDoc<GuardedSection>();
381         
382         // add simple section
383
String JavaDoc 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         // add simple section
392
String JavaDoc 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         // add interior section
404
String JavaDoc 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         // insert simple section after gs1
417
String JavaDoc 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         // remove gs4
431
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         // add gs4
441
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         // remove gs1
454
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         // remove gs3
464
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         // remove gs2
473
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         // remove gs4
481
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     // test rename sections
490

491     public void testRenameSimpleSection() throws BadLocationException JavaDoc, PropertyVetoException JavaDoc {
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         // set the same name
501
ss1.setName("ssNewName");
502         
503         SimpleSection ss2 = guards.createSimpleSection(
504                 editor.doc.createPosition(ss1.getEndPosition().getOffset() + 1),
505                 "ss2");
506         
507         // rename to existing name
508
try {
509             ss1.setName("ss2");
510             fail("accepted already existing name");
511         } catch (PropertyVetoException JavaDoc ex) {
512             assertTrue("valid", ss1.isValid());
513             assertEquals("name", "ssNewName", ss1.getName());
514         }
515     }
516     
517     public void testRenameInteriorSection() throws BadLocationException JavaDoc, PropertyVetoException JavaDoc {
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         // set the same name
527
is1.setName("isNewName");
528         
529         InteriorSection is2 = guards.createInteriorSection(
530                 editor.doc.createPosition(is1.getEndPosition().getOffset() + 1),
531                 "is2");
532         
533         // rename to existing name
534
try {
535             is1.setName("is2");
536             fail("accepted already existing name");
537         } catch (PropertyVetoException JavaDoc ex) {
538             assertTrue("valid", is1.isValid());
539             assertEquals("name", "isNewName", is1.getName());
540         }
541     }
542     
543     public void testCreateSimpleSection() throws BadLocationException JavaDoc {
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 JavaDoc t) {
554             assertEquals("wrong exception", IllegalArgumentException JavaDoc.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 JavaDoc t) {
563             assertEquals("wrong exception", IllegalArgumentException JavaDoc.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 JavaDoc {
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 JavaDoc t) {
583             assertEquals("wrong exception", IllegalArgumentException JavaDoc.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 JavaDoc t) {
592             assertEquals("wrong exception", IllegalArgumentException JavaDoc.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 JavaDoc {
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)); // end of header \n
608
assertTrue(!GuardUtils.isGuarded(editor.doc, 4)); // start of body
609
assertTrue(!GuardUtils.isGuarded(editor.doc, 5)); // end of body
610
assertTrue(GuardUtils.isGuarded(editor.doc, 6)); // start of footer
611

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)); // end of header \n
615
assertTrue(!GuardUtils.isGuarded(editor.doc, 4)); // start of body X
616
assertTrue(!GuardUtils.isGuarded(editor.doc, 5)); // body
617
assertTrue(!GuardUtils.isGuarded(editor.doc, 6)); // end of body
618
assertTrue(GuardUtils.isGuarded(editor.doc, 7)); // start of footer
619

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 JavaDoc 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"))); // end of header \n
629
assertTrue(!GuardUtils.isGuarded(editor.doc, content.indexOf("XBODY"))); // start of body X
630
assertTrue(!GuardUtils.isGuarded(editor.doc, content.indexOf("BODY"))); // body
631
assertTrue(!GuardUtils.isGuarded(editor.doc, content.indexOf("\nFOOTER"))); // end of body
632
assertTrue(GuardUtils.isGuarded(editor.doc, content.indexOf("FOOTER"))); // start of footer
633
}
634     
635     private void verifyGuards(Iterable JavaDoc<GuardedSection> wanted, Iterable JavaDoc<GuardedSection> queried) {
636         Iterator JavaDoc<GuardedSection> itWanted = wanted.iterator();
637         Iterator JavaDoc<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