KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java_code_folding > JavaFoldsTest


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
20 package java_code_folding;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import javax.swing.text.AbstractDocument JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import org.netbeans.jellytools.Bundle;
32 import org.netbeans.jellytools.EditorOperator;
33 import org.netbeans.jellytools.EditorWindowOperator;
34 import lib.EditorTestCase;
35 import code_folding.CodeFoldingTest;
36 import org.netbeans.api.editor.fold.Fold;
37 import org.netbeans.api.editor.fold.FoldHierarchy;
38 import org.netbeans.jemmy.operators.JEditorPaneOperator;
39 import org.netbeans.jemmy.operators.JTextComponentOperator;
40
41 /**
42  * Test behavior of java code folds.
43  * Sample file contains java folds. Test creates a fold hierarchy dump
44  * and compares it with golden file.
45  * Then test simply moves a caret
46  * position and tries to invoke expand or collapse action.
47  * At the end collapse all and expand all action is tested.
48  * Note: static imports block are not a subject of testing yet [PENDING]
49  * @author Martin Roskanin
50  */

51   public class JavaFoldsTest extends JavaCodeFoldingTest {
52
53     // private PrintStream wrapper for System.out
54
PrintStream JavaDoc systemOutPSWrapper = new PrintStream JavaDoc(System.out);
55     
56     JTextComponent JavaDoc target;
57     EditorOperator editor;
58     int index = 0;
59       
60       
61     /** Creates a new instance of Main */
62     public JavaFoldsTest(String JavaDoc testMethodName) {
63         super(testMethodName);
64     }
65
66     
67     
68     private String JavaDoc getIndexAsString(){
69         String JavaDoc ret = String.valueOf(index);
70         if (ret.length() == 1) ret = "0" + ret;
71         return ret;
72     }
73     
74     private String JavaDoc getRefFileName(){
75         return this.getName()+getIndexAsString()+".ref"; //NOI18N
76
}
77     
78     private String JavaDoc getGoldenFileName(){
79         return this.getName()+getIndexAsString()+".pass"; //NOI18N
80
}
81     
82     private String JavaDoc getDiffFileName(){
83         return this.getName()+getIndexAsString()+".diff"; //NOI18N
84
}
85     
86     // hashtable holding all already used logs and correspondig printstreams
87
private Hashtable JavaDoc logStreamTable = null;
88     
89     private PrintStream JavaDoc getFileLog(String JavaDoc logName) throws IOException JavaDoc {
90         OutputStream JavaDoc outputStream;
91         FileOutputStream JavaDoc fileOutputStream;
92         
93         if ((logStreamTable == null)|(hasTestMethodChanged())) {
94             // we haven't used logging capability - create hashtables
95
logStreamTable = new Hashtable JavaDoc();
96             //System.out.println("Created new hashtable");
97
} else {
98             if (logStreamTable.containsKey(logName)) {
99                 //System.out.println("Getting stream from cache:"+logName);
100
return (PrintStream JavaDoc)logStreamTable.get(logName);
101             }
102         }
103         // we didn't used this log, so let's create it
104
FileOutputStream JavaDoc fileLog = new FileOutputStream JavaDoc(new File JavaDoc(getWorkDir(),logName));
105         PrintStream JavaDoc printStreamLog = new PrintStream JavaDoc(fileLog,true);
106         logStreamTable.put(logName,printStreamLog);
107         //System.out.println("Created new stream:"+logName);
108
return printStreamLog;
109     }
110     
111     private String JavaDoc lastTestMethod=null;
112     
113     private boolean hasTestMethodChanged() {
114         if (!this.getName().equals(lastTestMethod)) {
115             lastTestMethod=this.getName();
116             return true;
117         } else {
118             return false;
119         }
120     }
121     
122     public PrintStream JavaDoc getRef() {
123         String JavaDoc refFilename = getRefFileName();
124         try {
125             return getFileLog(refFilename);
126         } catch (IOException JavaDoc ioe) {
127             // canot get ref file - return system.out
128
//System.err.println("Test method "+this.getName()+" - cannot open ref file:"+refFilename
129
// +" - defaulting to System.out and failing test");
130
fail("Could not open reference file: "+refFilename);
131             return systemOutPSWrapper;
132         }
133     }
134     
135     private void compareFoldHierarchyDump(){
136         String JavaDoc viewDumpHierarchy = foldHierarchyToString(target);
137         ref(viewDumpHierarchy);
138         compareReferenceFiles(getRefFileName(), getGoldenFileName(), getDiffFileName());
139         index++;
140     }
141     
142     
143     
144     
145     public void testJavaFolds(){
146         openDefaultProject();
147         openDefaultSampleFile();
148         
149         editor = getDefaultSampleEditorOperator();
150         JTextComponentOperator text = new JTextComponentOperator(editor);
151         target = (JTextComponent JavaDoc)text.getSource();
152         
153         // wait max. 6 second for code folding initialization
154
waitForFolding(target, 6000);
155
156         //00 compare fold hierarchy dump against golden file
157
compareFoldHierarchyDump();
158     
159         //01 collapse initial comment fold. Caret is inside fold
160
//* Initial Comme|nt Fold
161
collapseFoldAtCaretPosition(editor, 2, 17);
162         compareFoldHierarchyDump();
163     
164         //02 collapse import section fold. Caret is at the fold start offset
165
//|import javax.swing.JApplet;
166
collapseFoldAtCaretPosition(editor, 9, 1);
167         compareFoldHierarchyDump();
168     
169         //03 collapse Outer Class Javadoc Fold. Caret is at the end fold guarded position
170
//*/|
171
collapseFoldAtCaretPosition(editor, 17, 4);
172         compareFoldHierarchyDump();
173     
174         //04 collapse One Line Field Javadoc Fold. Caret - before the fold
175
//| /** One Line Field Javadoc Fold*/
176
collapseFoldAtCaretPosition(editor, 20, 1);
177         compareFoldHierarchyDump();
178     
179         //05 collapse Multi-line Field Favadoc Fold. Caret - before the fold
180
// |/**
181
collapseFoldAtCaretPosition(editor, 23, 5);
182         compareFoldHierarchyDump();
183
184         //06 collapse One-line Constructor Javadoc Fold
185
// /** One-line Constructor Javadoc Fold |*/
186
collapseFoldAtCaretPosition(editor, 28, 43);
187         compareFoldHierarchyDump();
188     
189         //07 collapse One-line Constructor Fold
190
// public testJavaFolds() {} //One-line Const|ructor Fold
191
collapseFoldAtCaretPosition(editor, 29, 47);
192         compareFoldHierarchyDump();
193     
194         //08
195
//| * Multi-line Constructor Javadoc Fold
196
collapseFoldAtCaretPosition(editor, 32, 1);
197         compareFoldHierarchyDump();
198     
199         //09
200
// public testJavaFolds(String s) |{ //Multi-line Constructor Fold
201
collapseFoldAtCaretPosition(editor, 34, 36);
202         compareFoldHierarchyDump();
203     
204         //10
205
// /**| One-line Method Javadoc Fold */
206
collapseFoldAtCaretPosition(editor, 40, 8);
207         compareFoldHierarchyDump();
208     
209         //11
210
// public void methodOne(){|} // One-line Method Fold
211
collapseFoldAtCaretPosition(editor, 41, 29);
212         compareFoldHierarchyDump();
213     
214         //12
215
// * Multi-line Meth|od Javadoc Fold
216
collapseFoldAtCaretPosition(editor, 44, 24);
217         compareFoldHierarchyDump();
218     
219         //13 Multi-line Method Fold
220
// } |
221
collapseFoldAtCaretPosition(editor, 48, 7);
222         compareFoldHierarchyDump();
223     
224         //14 multi folds on one line. Collapsing middle method
225
// public void firstMethod(){} public void secondM|ethod(){} public void thirdMethod(){}
226
collapseFoldAtCaretPosition(editor, 50, 53);
227         compareFoldHierarchyDump();
228     
229         //15 multi folds on one line. Expanding middle method
230
// public void firstMethod(){} public void secondM|ethod()[...] public void thirdMethod(){}
231
expandFoldAtCaretPosition(editor, 50, 53);
232         compareFoldHierarchyDump();
233     
234         //16 multi folds on one line. Collapsing middle method
235
// public void firstMethod(){} | public void secondMethod(){} public void thirdMethod(){}
236
collapseFoldAtCaretPosition(editor, 50, 33);
237         compareFoldHierarchyDump();
238     
239         //17 multi folds on one line. Expandinging middle method
240
// public void firstMethod(){} | public void secondMethod()[...] public void thirdMethod(){}
241
expandFoldAtCaretPosition(editor, 50, 33);
242         compareFoldHierarchyDump();
243     
244         //18 multi folds on one line. Collapsing middle method
245
// public void firstMethod(){} public void secondMethod(){}| public void thirdMethod(){}
246
collapseFoldAtCaretPosition(editor, 50, 62);
247         compareFoldHierarchyDump();
248     
249         //19 multi folds on one line. Expandinging middle method
250
// public void firstMethod(){} public void secondMethod()[...]| public void thirdMethod(){}
251
expandFoldAtCaretPosition(editor, 50, 62);
252         compareFoldHierarchyDump();
253     
254         //20 multi folds on one line. Collapsing first method
255
// public void firstMethod(){|} public void secondMethod(){} public void thirdMethod(){}
256
collapseFoldAtCaretPosition(editor, 50, 31);
257         compareFoldHierarchyDump();
258     
259         //21 multi folds on one line. Expanding first method
260
// public void firstMethod()[...]| public void secondMethod(){} public void thirdMethod(){}
261
expandFoldAtCaretPosition(editor, 50, 32);
262         compareFoldHierarchyDump();
263
264         //22 multi folds on one line. Collapsing first method
265
// public void firstMethod(){}| public void secondMethod(){} public void thirdMethod(){}
266
collapseFoldAtCaretPosition(editor, 50, 32);
267         compareFoldHierarchyDump();
268
269         //23 multi folds on one line. Expanding first method
270
//| public void firstMethod()[...] public void secondMethod(){} public void thirdMethod(){}
271
expandFoldAtCaretPosition(editor, 50, 1);
272         compareFoldHierarchyDump();
273
274         //24 multi folds on one line. Collapsing third method
275
// public void firstMethod(){} public void secondMethod(){} |public void thirdMethod(){}
276
collapseFoldAtCaretPosition(editor, 50, 63);
277         compareFoldHierarchyDump();
278
279         //25 multi folds on one line. Expanding third method
280
// public void firstMethod(){} public void secondMethod(){} |public void thirdMethod()[...]
281
expandFoldAtCaretPosition(editor, 50, 63);
282         compareFoldHierarchyDump();
283
284         //26 multi folds on one line. Collapsing third method
285
// public void firstMethod(){} public void secondMethod(){} public void thirdMethod(){}|
286
collapseFoldAtCaretPosition(editor, 50, 90);
287         compareFoldHierarchyDump();
288
289         //27 multi folds on one line. Expanding third method
290
// public void firstMethod(){} public void secondMethod(){} public void thirdMethod()[...]|
291
expandFoldAtCaretPosition(editor, 50, 90);
292         compareFoldHierarchyDump();
293
294         //28 collapse One-line InnerClass Javadoc Fold
295
// One-line Inn|erClass Javadoc Fold
296
collapseFoldAtCaretPosition(editor, 52, 21);
297         compareFoldHierarchyDump();
298     
299         //29 collapse One-line InnerClass Fold
300
//| public static class InnerClassOne{}
301
collapseFoldAtCaretPosition(editor, 53, 1);
302         compareFoldHierarchyDump();
303     
304         //30 collapse Multi-line InnerClass Javadoc Fold
305
//| /**
306
collapseFoldAtCaretPosition(editor, 55, 1);
307         compareFoldHierarchyDump();
308
309         //31 collapse Multi-line InnerClass Fold
310
//public static class InnerClassTwo{ //Multi-line InnerClass Fold|
311
collapseFoldAtCaretPosition(editor, 58, 68);
312         compareFoldHierarchyDump();
313     
314         //------------------- Inner Class tests -------------------------
315
//---------------------------------------------------------------
316

317         //32 collapse Multi-line InnerClass Fold
318
//| public static class InnerClassThree{
319
collapseFoldAtCaretPosition(editor, 61, 1);
320         compareFoldHierarchyDump();
321     
322         //33 expand Multi-line InnerClass Fold
323
//| public static class InnerClassThree[...]
324
expandFoldAtCaretPosition(editor, 61, 1);
325         compareFoldHierarchyDump();
326     
327         //34 collapse One Line InnerClass Field Javadoc Fold
328
// /** One Line InnerClass Field Javadoc Fold*/|
329
collapseFoldAtCaretPosition(editor, 62, 53);
330         compareFoldHierarchyDump();
331
332         //35 collapse Multi-line InnerClass Field Javadoc Fold
333
// /**|
334
collapseFoldAtCaretPosition(editor, 65, 12);
335         compareFoldHierarchyDump();
336     
337         //36 collapse One-line InnerClass Constructor Javadoc Fold
338
// |/** One-line InnerClass Constructor Javadoc Fold */
339
collapseFoldAtCaretPosition(editor, 70, 9);
340         compareFoldHierarchyDump();
341     
342         //37 collapse One-line InnerClass Constructor Fold
343
// public InnerClassThree() {}| //One-line InnerClass Constructor Fold
344
collapseFoldAtCaretPosition(editor, 71, 36);
345         compareFoldHierarchyDump();
346
347         //38 collapse Multi-line InnerClass Constructor Javadoc Fold
348
// |*/
349
collapseFoldAtCaretPosition(editor, 75, 10);
350         compareFoldHierarchyDump();
351     
352         //39 collapse Multi-line InnerClass Constructor Fold
353
// applet = new |JApplet();
354
collapseFoldAtCaretPosition(editor, 78, 26);
355         compareFoldHierarchyDump();
356
357         //40 collapse One-line InnerClass Method Javadoc Fold
358
// /**| One-line InnerClass Method Javadoc Fold */
359
collapseFoldAtCaretPosition(editor, 82, 12);
360         compareFoldHierarchyDump();
361
362         //41 collapse One-line InnerClass Method Fold
363
// public void methodOne(){}| // One-line InnerClass Method Fold
364
collapseFoldAtCaretPosition(editor, 83, 34);
365         compareFoldHierarchyDump();
366
367         //42 collapse Multi-line InnerClass Method Javadoc Fold
368
// *|/
369
collapseFoldAtCaretPosition(editor, 87, 11);
370         compareFoldHierarchyDump();
371     
372         //43 collapse Multi-line InnerClass Method Fold
373
// }|
374
collapseFoldAtCaretPosition(editor, 90, 10);
375         compareFoldHierarchyDump();
376     
377         //44 collapse Whole InnerClass, the caret is just one characted behind method fold endOffset
378
// } |
379
collapseFoldAtCaretPosition(editor, 90, 11);
380         compareFoldHierarchyDump();
381     
382         //45 expand Whole InnerClass
383
// public static class InnerClassThree[...]|
384
expandFoldAtCaretPosition(editor, 93, 6);
385         compareFoldHierarchyDump();
386
387         //46 collapse first method
388
//| public void firstMethod(){} public void secondMethod(){} public void thirdMethod(){}
389
collapseFoldAtCaretPosition(editor, 92, 1);
390         compareFoldHierarchyDump();
391
392         //47 expand first method
393
//| public void firstMethod()[...] public void secondMethod(){} public void thirdMethod(){}
394
expandFoldAtCaretPosition(editor, 92, 1);
395         compareFoldHierarchyDump();
396
397         //48 collapse second method
398
// public void firstMethod(){} | public void secondMethod(){} public void thirdMethod(){}
399
collapseFoldAtCaretPosition(editor, 92, 37);
400         compareFoldHierarchyDump();
401
402         //49 expand second method
403
// public void firstMethod(){} | public void secondMethod()[...] public void thirdMethod(){}
404
expandFoldAtCaretPosition(editor, 92, 37);
405         compareFoldHierarchyDump();
406     
407         //51 collapse third method
408
// public void firstMethod(){} public void secondMethod(){} public void thirdMe|thod(){}
409
collapseFoldAtCaretPosition(editor, 92, 86);
410         compareFoldHierarchyDump();
411
412         //51 expand second method
413
// public void firstMethod(){} public void secondMethod(){} public void thirdMe|thod()[...]
414
expandFoldAtCaretPosition(editor, 92, 86);
415         compareFoldHierarchyDump();
416     
417         //52 collapse all three methods
418
// public void firstMethod(){|} public void second|Method(){} public void thirdMethod(){}|
419
collapseFoldAtCaretPosition(editor, 92, 35);
420         collapseFoldAtCaretPosition(editor, 92, 56);
421         collapseFoldAtCaretPosition(editor, 92, 94);
422         compareFoldHierarchyDump();
423
424         
425         
426         //---------------------------------------------------
427
// Inner class in Inner Class testing
428

429         //53 collapse a method javadoc
430
// */|
431
collapseFoldAtCaretPosition(editor, 131, 16);
432         compareFoldHierarchyDump();
433         
434         //54 collapse a method
435
// public void method|Two(){ // Multi-line InnerClassInInnerClass Method Fold
436
collapseFoldAtCaretPosition(editor, 132, 31);
437         compareFoldHierarchyDump();
438         
439         //55 multi line methods
440
// public vo|id firstMethod(){} public void secondMethod(){|} public void thirdMethod(){}|
441
collapseFoldAtCaretPosition(editor, 136, 22);
442         collapseFoldAtCaretPosition(editor, 136, 69);
443         collapseFoldAtCaretPosition(editor, 136, 98);
444         compareFoldHierarchyDump();
445
446         
447         //--------------------------------------------------------------
448
// 56 Collapse All Folds
449
collapseAllFolds(editor);
450         compareFoldHierarchyDump();
451         
452         //--------------------------------------------------------------
453
// 57 Collapse All Folds
454
expandAllFolds(editor);
455         compareFoldHierarchyDump();
456         
457         closeFileWithDiscard();
458
459     }
460     
461 }
462
Popular Tags