KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > guards > PositionBoundsTest


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.modules.editor.guards;
20 import javax.swing.text.BadLocationException JavaDoc;
21 import javax.swing.text.Position JavaDoc;
22 import javax.swing.text.StyledDocument JavaDoc;
23 import junit.framework.TestCase;
24 import org.netbeans.api.editor.guards.Editor;
25 import org.netbeans.api.editor.guards.GuardUtils;
26 import org.netbeans.modules.editor.guards.GuardedSectionsImpl;
27 import org.openide.text.NbDocument;
28
29 /**
30  *
31  * @author Jan Pokorsky
32  */

33 public class PositionBoundsTest extends TestCase {
34     
35     private Editor editor;
36     private GuardedSectionsImpl guardsImpl;
37     
38     /** Creates a new instance of PositionBoundsTest */
39     public PositionBoundsTest(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     protected void setUp() throws Exception JavaDoc {
44         this.editor = new Editor();
45         this.guardsImpl = new GuardedSectionsImpl(this.editor);
46         GuardUtils.initManager(this.editor, this.guardsImpl);
47     }
48     
49     public void testCreatePositionBounds() throws BadLocationException JavaDoc {
50         editor.doc.insertString(0, "_acd", null);
51         
52         // test create position bounds
53
PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl);
54         assertEquals("start", 1, bounds.getBegin().getOffset());
55         assertEquals("end", 3, bounds.getEnd().getOffset());
56         assertEquals("getText", editor.doc.getText(1, 2), bounds.getText());
57         assertEquals("getText2", "ac", bounds.getText());
58     }
59     
60     public void testChangesInPositionBounds() throws BadLocationException JavaDoc {
61         editor.doc.insertString(0, "_acd", null);
62         
63         // test create position bounds
64
PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl);
65         editor.doc.insertString(2, "b", null);
66         assertEquals("start", 1, bounds.getBegin().getOffset());
67         assertEquals("end", 4, bounds.getEnd().getOffset());
68         assertEquals("getText", editor.doc.getText(1, 3), bounds.getText());
69         assertEquals("getText2", "abc", bounds.getText());
70     }
71     
72     public void testSetText() throws BadLocationException JavaDoc {
73         editor.doc.insertString(0, "_abcd", null);
74         PositionBounds bounds = PositionBounds.create(1, 4, guardsImpl);
75         // test position bounds content changes; doc="_abcd"; pb="abc"
76
bounds.setText("xy");
77         assertEquals("start", 1, bounds.getBegin().getOffset());
78         assertEquals("end", 3, bounds.getEnd().getOffset());
79         assertEquals("getText", "xy", bounds.getText());
80         assertEquals("doc length", "_xyd".length(), editor.doc.getLength());
81     }
82     
83     public void testInsertionBeforeBounds() throws BadLocationException JavaDoc {
84         editor.doc.insertString(0, "_xyd", null);
85         PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl);
86         // test insertion before bounds; doc="_xyd"; pb="xy"
87
editor.doc.insertString(1, "a", null);
88         assertEquals("start", 2, bounds.getBegin().getOffset());
89         assertEquals("end", 4, bounds.getEnd().getOffset());
90         assertEquals("getText", "xy", bounds.getText());
91         assertEquals("doc length", "_axyd".length(), editor.doc.getLength());
92     }
93     
94     public void testSetEmptyText() throws BadLocationException JavaDoc {
95         editor.doc.insertString(0, "_axyd", null);
96         PositionBounds bounds = PositionBounds.create(2, 4, guardsImpl);
97         
98         // test cleaning position bounds; doc="_axyd"; pb="xy"
99
bounds.setText("");
100         assertEquals("start", 2, bounds.getBegin().getOffset());
101         assertEquals("end", 2, bounds.getEnd().getOffset());
102         assertEquals("getText", "", bounds.getText());
103         assertEquals("doc length", "_ad".length(), editor.doc.getLength());
104         
105         bounds.setText("xy");
106         assertEquals("start", 2, bounds.getBegin().getOffset());
107         assertEquals("end", 4, bounds.getEnd().getOffset());
108         assertEquals("getText", "xy", bounds.getText());
109         assertEquals("doc length", "_axyd".length(), editor.doc.getLength());
110     }
111     
112     public void testDocumentClean() throws BadLocationException JavaDoc {
113         editor.doc.insertString(0, "_acd", null);
114         PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl);
115         
116         editor.doc.remove(0, editor.doc.getLength());
117         assertEquals("start", 0, bounds.getBegin().getOffset());
118         assertEquals("end", 0, bounds.getEnd().getOffset());
119         assertEquals("getText", "", bounds.getText());
120     }
121         
122     public void testComplexSetText() throws BadLocationException JavaDoc {
123         editor.doc.insertString(0, "_acd", null);
124         
125         // test create position bounds
126
PositionBounds bounds = PositionBounds.create(1, 3, guardsImpl);
127         assertEquals("start", 1, bounds.getBegin().getOffset());
128         assertEquals("end", 3, bounds.getEnd().getOffset());
129         assertEquals("getText", editor.doc.getText(1, 2), bounds.getText());
130         assertEquals("getText2", "ac", bounds.getText());
131         
132         // test document changes inside the position bounds
133
editor.doc.insertString(2, "b", null);
134         assertEquals("start", 1, bounds.getBegin().getOffset());
135         assertEquals("end", 4, bounds.getEnd().getOffset());
136         assertEquals("getText", editor.doc.getText(1, 3), bounds.getText());
137         assertEquals("getText2", "abc", bounds.getText());
138         
139         // test position bounds content changes; doc="_abcd"; pb="abc"
140
bounds.setText("xy");
141         assertEquals("start", 1, bounds.getBegin().getOffset());
142         assertEquals("end", 3, bounds.getEnd().getOffset());
143         assertEquals("getText", "xy", bounds.getText());
144         assertEquals("doc length", "_xyd".length(), editor.doc.getLength());
145         
146         // test insertion before bounds; doc="_xyd"; pb="xy"
147
editor.doc.insertString(1, "a", null);
148         assertEquals("start", 2, bounds.getBegin().getOffset());
149         assertEquals("end", 4, bounds.getEnd().getOffset());
150         assertEquals("getText", "xy", bounds.getText());
151         assertEquals("doc length", "_axyd".length(), editor.doc.getLength());
152         
153         // test cleaning position bounds; doc="_axyd"; pb="xy"
154
bounds.setText("");
155         assertEquals("start", 2, bounds.getBegin().getOffset());
156         assertEquals("end", 2, bounds.getEnd().getOffset());
157         assertEquals("getText", "", bounds.getText());
158         assertEquals("doc length", "_ad".length(), editor.doc.getLength());
159         
160         // test cleaning document
161
bounds.setText("xy");
162         assertEquals("start", 2, bounds.getBegin().getOffset());
163         assertEquals("end", 4, bounds.getEnd().getOffset());
164         assertEquals("getText", "xy", bounds.getText());
165         assertEquals("doc length", "_axyd".length(), editor.doc.getLength());
166         
167         editor.doc.remove(0, editor.doc.getLength());
168         assertEquals("start", 0, bounds.getBegin().getOffset());
169         assertEquals("end", 0, bounds.getEnd().getOffset());
170         assertEquals("getText", "", bounds.getText());
171         
172     }
173     
174     public void testSetTextWithGuardMarks() throws Throwable JavaDoc {
175         final Throwable JavaDoc[] ts = new Throwable JavaDoc[1];
176         NbDocument.runAtomic(editor.doc, new Runnable JavaDoc() {
177             public void run() {
178                 try {
179                     doTestSetTextWithGuardMarks();
180                 } catch (Throwable JavaDoc ex) {
181                     ts[0] = ex;
182                 }
183             }
184         });
185         if (ts[0] != null) {
186             throw ts[0];
187         }
188     }
189     
190     private void doTestSetTextWithGuardMarks() throws BadLocationException JavaDoc {
191         StyledDocument JavaDoc doc = editor.doc;
192         doc.insertString(0, "abcdef", null);
193         Position JavaDoc p = doc.createPosition(1);
194         assertTrue(!GuardUtils.isGuarded(doc, 1));
195         NbDocument.markGuarded(doc, 1, 3);
196         assertTrue(GuardUtils.isGuarded(doc, 1));
197         
198         doc.insertString(1, "x", null);
199         assertEquals(2, p.getOffset());
200         assertTrue(GuardUtils.isGuarded(doc, 2));
201         assertTrue(!GuardUtils.isGuarded(doc, 1));
202         
203         doc.insertString(4, "x", null);
204         assertEquals(2, p.getOffset());
205         assertTrue(GuardUtils.isGuarded(doc, 4));
206         assertTrue(GuardUtils.isGuarded(doc, 3));
207         assertTrue(GuardUtils.isGuarded(doc, 5));
208         assertTrue(GuardUtils.isGuarded(doc, 2));
209         assertTrue(!GuardUtils.isGuarded(doc, 1));
210         GuardUtils.dumpGuardedAttr(doc);
211         
212         doc.remove(1, 1);
213         assertEquals(1, p.getOffset());
214     }
215     
216 }
217
Popular Tags