KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > BaseDocumentUnitTestCase


1
2 package org.netbeans.modules.editor;
3
4 import javax.swing.event.ChangeEvent JavaDoc;
5 import javax.swing.event.ChangeListener JavaDoc;
6 import javax.swing.event.DocumentEvent JavaDoc;
7 import javax.swing.event.DocumentListener JavaDoc;
8 import javax.swing.event.EventListenerList JavaDoc;
9 import javax.swing.text.BadLocationException JavaDoc;
10 import javax.swing.text.Caret JavaDoc;
11 import javax.swing.text.Document JavaDoc;
12 import javax.swing.text.EditorKit JavaDoc;
13 import org.netbeans.editor.BaseDocument;
14 import org.netbeans.editor.BaseKit;
15 import org.netbeans.junit.NbTestCase;
16
17 /**
18  * Support for creation of unit tests working with document.
19  *
20  * @author Miloslav Metelka
21  */

22 public class BaseDocumentUnitTestCase extends NbTestCase {
23     
24     private EditorKit JavaDoc editorKit;
25     
26     private String JavaDoc loadDocumentText;
27     
28     private BaseDocument doc;
29     
30     private Caret JavaDoc caret;
31     
32     private int loadCaretOffset = -1;
33     
34     
35     public BaseDocumentUnitTestCase(String JavaDoc testMethodName) {
36         super(testMethodName);
37         
38     }
39     
40     /**
41      * Set the text that the document obtained by {@link #getDocument()}
42      * would be loaded with.
43      *
44      * <p>
45      * The text is parsed and the first occurrence of "|" sets
46      * the caret offset which is available by {@link #getLoadCaretOffset()}.
47      * <br>
48      * The "|" itself is removed from the document text and subsequent
49      * calls to {@link #getLoadDocumentText()} do not contain it.
50      */

51     protected void setLoadDocumentText(String JavaDoc loadDocumentText) {
52         // [TODO] a more elaborated support could be done e.g. "||" to a real "|" etc.
53
loadCaretOffset = loadDocumentText.indexOf('|');
54         if (loadCaretOffset != -1) {
55             loadDocumentText = loadDocumentText.substring(0, loadCaretOffset)
56                 + loadDocumentText.substring(loadCaretOffset + 1);
57         }
58         
59         this.loadDocumentText = loadDocumentText;
60     }
61     
62     /**
63      * Get the text that the document obtained by {@link #getDocument()}
64      * would be loaded with.
65      *
66      * @return text to be loaded into the document or null if nothing
67      * should be loaded.
68      */

69     protected final String JavaDoc getLoadDocumentText() {
70         return loadDocumentText;
71     }
72     
73     /**
74      * Return caret offset based on the scanning of the text passed
75      * to {@link #setLoadDocumentText(String)}.
76      *
77      * @return valid caret offset or -1 if no caret offset was determined
78      * in the document text.
79      */

80     protected final int getLoadCaretOffset() {
81         return loadCaretOffset;
82     }
83     
84     /**
85      * Return the caret instance that can be used for testing.
86      * <br>
87      * The caret listens on document changes and its initial
88      * position is set to {@link #getLoadCaretOffset()}.
89      *
90      * @return caret instance.
91      */

92     protected synchronized final Caret JavaDoc getCaret () {
93         if (caret == null) {
94             caret = new CaretImpl(getDocument(), loadCaretOffset);
95         }
96         return caret;
97     }
98     
99     /**
100      * Get the offset of where the caret resides in the document.
101      */

102     protected final int getCaretOffset() {
103         return getCaret().getDot();
104     }
105     
106     /**
107      * Get the document that the test should work with.
108      * <br>
109      * If the document does not exist yet it will be created
110      * and loaded with the text from {@link #getLoadDocumentText()}.
111      */

112     protected synchronized BaseDocument getDocument() {
113         if (doc == null) {
114             doc = createAndLoadDocument();
115         }
116         return doc;
117     }
118     
119     /**
120      * Return text of the whole document.
121      * <br>
122      * The document is retrieved by {@link #getDocument()}.
123      */

124     protected String JavaDoc getDocumentText() {
125         try {
126             Document JavaDoc d = getDocument();
127             return d.getText(0, d.getLength());
128         } catch (BadLocationException JavaDoc ex) {
129             ex.printStackTrace(getLog());
130             fail(ex.getMessage());
131             return null; // should never be reached
132
}
133     }
134     
135     /**
136      * Assert whether the document available through {@link #getDocument()}
137      * has a content equal to <code>expectedText</code>.
138      */

139     protected void assertDocumentText(String JavaDoc msg, String JavaDoc expectedText) {
140         String JavaDoc docText = getDocumentText();
141         if (!docText.equals(expectedText)) {
142             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
143             sb.append(msg);
144             sb.append("\n----- expected text: -----\n");
145             appendDebugText(sb, expectedText);
146             sb.append("\n----- document text: -----\n");
147             appendDebugText(sb, docText);
148             sb.append("\n-----\n");
149
150             fail(sb.toString());
151         }
152     }
153     
154     protected final void appendDebugChar(StringBuffer JavaDoc sb, char ch) {
155         switch (ch) {
156             case '\n':
157                 sb.append("\\n\n");
158                 break;
159             case '\t':
160                 sb.append("\\t");
161                 break;
162
163             default:
164                 sb.append(ch);
165                 break;
166         }
167     }
168     
169     protected final void appendDebugText(StringBuffer JavaDoc sb, String JavaDoc text) {
170         for (int i = 0; i < text.length(); i++) {
171             appendDebugChar(sb, text.charAt(i));
172         }
173     }
174     
175     protected final String JavaDoc debugText(String JavaDoc text) {
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
177         appendDebugText(sb, text);
178         return sb.toString();
179     }
180
181     /**
182      * Assert whether the document available through {@link #getDocument()}
183      * has a content equal to <code>expectedText</code> and whether the caret
184      * position {@link #getCaretOffset()}
185      * indicated by "|" in the passed text is at the right place.
186      */

187     protected void assertDocumentTextAndCaret(String JavaDoc msg, String JavaDoc expectedTextAndCaretPipe) {
188         // [TODO] a more elaborated support could be done e.g. "||" to a real "|" etc.
189
int expectedCaretOffset = expectedTextAndCaretPipe.indexOf('|');
190         if (expectedCaretOffset == -1) { // caret position not indicated
191
fail("Caret position not indicated in '" + expectedTextAndCaretPipe + "'");
192         }
193
194         String JavaDoc expectedDocumentText= expectedTextAndCaretPipe.substring(0, expectedCaretOffset)
195             + expectedTextAndCaretPipe.substring(expectedCaretOffset + 1);
196
197         assertDocumentText(msg, expectedDocumentText);
198         if (expectedCaretOffset != getCaretOffset()) {
199             fail("caretOffset=" + getCaretOffset()
200                 + " but expectedCaretOffset=" + expectedCaretOffset
201                 + " in '" + expectedTextAndCaretPipe + "'"
202             );
203         }
204     }
205
206     /**
207      * Create editor kit instance to be returned
208      * by {@link #getEditorKit()}.
209      * <br>
210      * The returned editor kit should return
211      * <code>BaseDocument</code> instances
212      * from its {@link javax.swing.text.EditorKit.createDefaultDocument()}.
213      */

214     protected EditorKit JavaDoc createEditorKit() {
215         return BaseKit.getKit(BaseKit.class);
216     }
217     
218     /**
219      * Get the kit that should be used
220      * when creating the <code>BaseDocument</code>
221      * instance.
222      * <br>
223      * The editor kit instance is created in {@link #createEditorKit()}.
224      *
225      * @return editor kit instance.
226      */

227     public final EditorKit JavaDoc getEditorKit() {
228         if (editorKit == null) {
229             editorKit = createEditorKit();
230         }
231         return editorKit;
232     }
233     
234     private BaseDocument createAndLoadDocument() {
235         BaseDocument bd = (BaseDocument)getEditorKit().createDefaultDocument();
236
237         if (loadDocumentText != null) {
238             try {
239                 bd.insertString(0, loadDocumentText, null);
240             } catch (BadLocationException JavaDoc e) {
241                 e.printStackTrace(getLog());
242                 fail();
243             }
244         }
245         return bd;
246     }
247
248     class CaretImpl implements Caret JavaDoc, DocumentListener JavaDoc {
249         
250         private Document JavaDoc doc;
251         
252         private int dot;
253         
254         private int mark;
255         
256         private boolean visible = true;
257         
258         private boolean selectionVisible;
259         
260         private int blinkRate = 300;
261         
262         private EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
263         
264         private ChangeEvent JavaDoc changeEvent;
265         
266         CaretImpl(Document JavaDoc doc, int dot) {
267             this.doc = doc;
268             doc.addDocumentListener(this);
269             setDot(dot);
270         }
271
272         public void deinstall (javax.swing.text.JTextComponent JavaDoc c) {
273             fail("Not yet implemented");
274         }
275         
276         public void install (javax.swing.text.JTextComponent JavaDoc c) {
277             fail("Not yet implemented");
278         }
279         
280         public java.awt.Point JavaDoc getMagicCaretPosition () {
281             fail("Not yet implemented");
282             return null;
283         }
284         
285         public void setMagicCaretPosition (java.awt.Point JavaDoc p) {
286             fail("Not yet implemented");
287         }
288         
289         public int getDot () {
290             return dot;
291         }
292         
293         public int getMark () {
294             return mark;
295         }
296         
297        public void setDot (int dot) {
298             this.mark = this.dot;
299             changeCaretPosition(dot);
300         }
301         
302         public void moveDot (int dot) {
303             changeCaretPosition(dot);
304         }
305         
306         public int getBlinkRate () {
307             return blinkRate;
308         }
309         
310         public void setBlinkRate (int rate) {
311             this.blinkRate = blinkRate;
312         }
313         
314         public boolean isVisible () {
315             return visible;
316         }
317         
318         public void setVisible (boolean v) {
319             this.visible = visible;
320         }
321         
322         public boolean isSelectionVisible () {
323             return selectionVisible;
324         }
325         
326         public void setSelectionVisible (boolean v) {
327             this.selectionVisible = v;
328         }
329         
330         public void addChangeListener (ChangeListener JavaDoc l) {
331             listenerList.add(ChangeListener JavaDoc.class, l);
332         }
333         
334         public void removeChangeListener (ChangeListener JavaDoc l) {
335             listenerList.remove(ChangeListener JavaDoc.class, l);
336         }
337         
338         public void fireChangeListener() {
339             // Lazily create the event
340
if (changeEvent == null) {
341                 changeEvent = new ChangeEvent JavaDoc(this);
342             }
343
344             Object JavaDoc[] listeners = listenerList.getListenerList();
345             for (int i = 0; i < listeners.length; i++) {
346                 ((ChangeListener JavaDoc)listeners[i + 1]).stateChanged(changeEvent);
347             }
348         }
349         
350         public void paint (java.awt.Graphics JavaDoc g) {
351         }
352         
353         public void insertUpdate(DocumentEvent JavaDoc e) {
354             int offset = e.getOffset();
355             int length = e.getLength();
356             int newDot = dot;
357             short changed = 0;
358             if (newDot >= offset) {
359                 newDot += length;
360                 changed |= 1;
361             }
362             int newMark = mark;
363             if (newMark >= offset) {
364                 newMark += length;
365                 changed |= 2;
366             }
367
368             if (changed != 0) {
369                 if (newMark == newDot) {
370                     setDot(newDot);
371                     ensureValidPosition();
372                 } else {
373                     setDot(newMark);
374                     if (getDot() == newMark) {
375                         moveDot(newDot);
376                     }
377                     ensureValidPosition();
378                 }
379
380             }
381         }
382         
383         public void removeUpdate(DocumentEvent JavaDoc e) {
384             int offs0 = e.getOffset();
385             int offs1 = offs0 + e.getLength();
386             int newDot = dot;
387             if (newDot >= offs1) {
388                 newDot -= (offs1 - offs0);
389             } else if (newDot >= offs0) {
390                 newDot = offs0;
391             }
392             int newMark = mark;
393             if (newMark >= offs1) {
394                 newMark -= (offs1 - offs0);
395             } else if (newMark >= offs0) {
396                 newMark = offs0;
397             }
398             if (newMark == newDot) {
399                 setDot(newDot);
400                 ensureValidPosition();
401             } else {
402                 setDot(newMark);
403                 if (getDot() == newMark) {
404                     moveDot(newDot);
405                 }
406                 ensureValidPosition();
407             }
408         }
409         
410         public void changedUpdate(DocumentEvent JavaDoc e) {
411             
412         }
413
414         private void changeCaretPosition(int dot) {
415             if (this.dot != dot) {
416                 this.dot = dot;
417                 fireChangeListener();
418             }
419         }
420         
421        private void ensureValidPosition() {
422             int length = doc.getLength();
423             if (dot > length || mark > length) {
424                 setDot(length);
425             }
426         }
427
428     }
429     
430 }
431
Popular Tags