001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package java_code_folding;
043:
044: import java.awt.event.KeyEvent;
045: import java.io.File;
046: import java.io.FileOutputStream;
047: import java.io.IOException;
048: import java.io.OutputStream;
049: import java.io.PrintStream;
050: import java.util.Hashtable;
051: import javax.swing.text.AbstractDocument;
052: import javax.swing.text.JTextComponent;
053: import org.netbeans.jellytools.Bundle;
054: import org.netbeans.jellytools.EditorOperator;
055: import org.netbeans.jellytools.EditorWindowOperator;
056: import lib.EditorTestCase;
057: import code_folding.CodeFoldingTest;
058: import junit.textui.TestRunner;
059: import org.netbeans.api.editor.fold.Fold;
060: import org.netbeans.api.editor.fold.FoldHierarchy;
061: import org.netbeans.jemmy.operators.JEditorPaneOperator;
062: import org.netbeans.jemmy.operators.JTextComponentOperator;
063:
064: /**
065: * Test behavior of java code folds.
066: * Sample file contains java folds. Test creates a fold hierarchy dump
067: * and compares it with golden file.
068: * Then test simply moves a caret
069: * position and tries to invoke expand or collapse action.
070: * At the end collapse all and expand all action is tested.
071: * Note: static imports block are not a subject of testing yet [PENDING]
072: * @author Martin Roskanin
073: */
074: public class JavaFoldsTest extends JavaCodeFoldingTest {
075:
076: // private PrintStream wrapper for System.out
077: PrintStream systemOutPSWrapper = new PrintStream(System.out);
078:
079: JTextComponent target;
080: EditorOperator editor;
081: int index = 0;
082:
083: /** Creates a new instance of Main */
084: public JavaFoldsTest(String testMethodName) {
085: super (testMethodName);
086: }
087:
088: private String getIndexAsString() {
089: String ret = String.valueOf(index);
090: if (ret.length() == 1)
091: ret = "0" + ret;
092: return ret;
093: }
094:
095: private String getRefFileName() {
096: return this .getName() + getIndexAsString() + ".ref"; //NOI18N
097: }
098:
099: private String getGoldenFileName() {
100: return this .getName() + getIndexAsString() + ".pass"; //NOI18N
101: }
102:
103: private String getDiffFileName() {
104: return this .getName() + getIndexAsString() + ".diff"; //NOI18N
105: }
106:
107: // hashtable holding all already used logs and correspondig printstreams
108: private Hashtable logStreamTable = null;
109:
110: private PrintStream getFileLog(String logName) throws IOException {
111: OutputStream outputStream;
112: FileOutputStream fileOutputStream;
113:
114: if ((logStreamTable == null) | (hasTestMethodChanged())) {
115: // we haven't used logging capability - create hashtables
116: logStreamTable = new Hashtable();
117: //System.out.println("Created new hashtable");
118: } else {
119: if (logStreamTable.containsKey(logName)) {
120: //System.out.println("Getting stream from cache:"+logName);
121: return (PrintStream) logStreamTable.get(logName);
122: }
123: }
124: // we didn't used this log, so let's create it
125: FileOutputStream fileLog = new FileOutputStream(new File(
126: getWorkDir(), logName));
127: PrintStream printStreamLog = new PrintStream(fileLog, true);
128: logStreamTable.put(logName, printStreamLog);
129: //System.out.println("Created new stream:"+logName);
130: return printStreamLog;
131: }
132:
133: private String lastTestMethod = null;
134:
135: private boolean hasTestMethodChanged() {
136: if (!this .getName().equals(lastTestMethod)) {
137: lastTestMethod = this .getName();
138: return true;
139: } else {
140: return false;
141: }
142: }
143:
144: public PrintStream getRef() {
145: String refFilename = getRefFileName();
146: try {
147: return getFileLog(refFilename);
148: } catch (IOException ioe) {
149: // canot get ref file - return system.out
150: //System.err.println("Test method "+this.getName()+" - cannot open ref file:"+refFilename
151: // +" - defaulting to System.out and failing test");
152: fail("Could not open reference file: " + refFilename);
153: return systemOutPSWrapper;
154: }
155: }
156:
157: private void compareFoldHierarchyDump() {
158: String viewDumpHierarchy = foldHierarchyToString(target);
159: ref(viewDumpHierarchy);
160: compareReferenceFiles(getRefFileName(), getGoldenFileName(),
161: getDiffFileName());
162: index++;
163: }
164:
165: public void testJavaFolds() {
166: openDefaultProject();
167: openDefaultSampleFile();
168:
169: editor = getDefaultSampleEditorOperator();
170: JTextComponentOperator text = new JTextComponentOperator(editor);
171: target = (JTextComponent) text.getSource();
172:
173: // wait max. 6 second for code folding initialization
174: waitForFolding(target, 6000);
175:
176: //00 compare fold hierarchy dump against golden file
177: compareFoldHierarchyDump();
178:
179: //01 collapse initial comment fold. Caret is inside fold
180: //* Initial Comme|nt Fold
181: collapseFoldAtCaretPosition(editor, 2, 17);
182: compareFoldHierarchyDump();
183:
184: //02 collapse import section fold. Caret is at the fold start offset
185: //|import javax.swing.JApplet;
186: collapseFoldAtCaretPosition(editor, 9, 1);
187: compareFoldHierarchyDump();
188:
189: //03 collapse Outer Class Javadoc Fold. Caret is at the end fold guarded position
190: //*/|
191: collapseFoldAtCaretPosition(editor, 17, 4);
192: compareFoldHierarchyDump();
193:
194: //04 collapse One Line Field Javadoc Fold. Caret - before the fold
195: //| /** One Line Field Javadoc Fold*/
196: collapseFoldAtCaretPosition(editor, 20, 1);
197: compareFoldHierarchyDump();
198:
199: //05 collapse Multi-line Field Favadoc Fold. Caret - before the fold
200: // |/**
201: collapseFoldAtCaretPosition(editor, 23, 5);
202: compareFoldHierarchyDump();
203:
204: //06 collapse One-line Constructor Javadoc Fold
205: // /** One-line Constructor Javadoc Fold |*/
206: collapseFoldAtCaretPosition(editor, 28, 43);
207: compareFoldHierarchyDump();
208:
209: //07 collapse One-line Constructor Fold
210: // public testJavaFolds() { } //One-line Const|ructor Fold
211: collapseFoldAtCaretPosition(editor, 29, 47);
212: compareFoldHierarchyDump();
213:
214: //08
215: //| * Multi-line Constructor Javadoc Fold
216: collapseFoldAtCaretPosition(editor, 32, 1);
217: compareFoldHierarchyDump();
218:
219: //09
220: // public testJavaFolds(String s) |{ //Multi-line Constructor Fold
221: collapseFoldAtCaretPosition(editor, 34, 36);
222: compareFoldHierarchyDump();
223:
224: //10
225: // /**| One-line Method Javadoc Fold */
226: collapseFoldAtCaretPosition(editor, 40, 8);
227: compareFoldHierarchyDump();
228:
229: //11
230: // public void methodOne(){|} // One-line Method Fold
231: collapseFoldAtCaretPosition(editor, 41, 29);
232: compareFoldHierarchyDump();
233:
234: //12
235: // * Multi-line Meth|od Javadoc Fold
236: collapseFoldAtCaretPosition(editor, 44, 24);
237: compareFoldHierarchyDump();
238:
239: //13 Multi-line Method Fold
240: // } |
241: collapseFoldAtCaretPosition(editor, 48, 7);
242: compareFoldHierarchyDump();
243:
244: //14 multi folds on one line. Collapsing middle method
245: // public void firstMethod(){ } public void secondM|ethod(){ } public void thirdMethod(){ }
246: collapseFoldAtCaretPosition(editor, 50, 53);
247: compareFoldHierarchyDump();
248:
249: //15 multi folds on one line. Expanding middle method
250: // public void firstMethod(){ } public void secondM|ethod()[...] public void thirdMethod(){ }
251: expandFoldAtCaretPosition(editor, 50, 53);
252: compareFoldHierarchyDump();
253:
254: //16 multi folds on one line. Collapsing middle method
255: // public void firstMethod(){ } | public void secondMethod(){ } public void thirdMethod(){ }
256: collapseFoldAtCaretPosition(editor, 50, 33);
257: compareFoldHierarchyDump();
258:
259: //17 multi folds on one line. Expandinging middle method
260: // public void firstMethod(){ } | public void secondMethod()[...] public void thirdMethod(){ }
261: expandFoldAtCaretPosition(editor, 50, 33);
262: compareFoldHierarchyDump();
263:
264: //18 multi folds on one line. Collapsing middle method
265: // public void firstMethod(){ } public void secondMethod(){ } |public void thirdMethod(){ }
266: collapseFoldAtCaretPosition(editor, 50, 65);
267: compareFoldHierarchyDump();
268:
269: //19 multi folds on one line. Expandinging middle method
270: // public void firstMethod(){ } public void secondMethod()[...] |public void thirdMethod(){ }
271: expandFoldAtCaretPosition(editor, 50, 65);
272: compareFoldHierarchyDump();
273:
274: //20 multi folds on one line. Collapsing first method
275: // public void firstMethod(){|} public void secondMethod(){ } public void thirdMethod(){ }
276: collapseFoldAtCaretPosition(editor, 50, 31);
277: compareFoldHierarchyDump();
278:
279: //21 multi folds on one line. Expanding first method
280: // public void firstMethod()[...]| public void secondMethod(){ } public void thirdMethod(){ }
281: expandFoldAtCaretPosition(editor, 50, 33);
282: compareFoldHierarchyDump();
283:
284: //22 multi folds on one line. Collapsing first method
285: // public void firstMethod(){ }| public void secondMethod(){ } public void thirdMethod(){ }
286: collapseFoldAtCaretPosition(editor, 50, 33);
287: compareFoldHierarchyDump();
288:
289: //23 multi folds on one line. Expanding first method
290: //| public void firstMethod()[...] public void secondMethod(){ } public void thirdMethod(){ }
291: expandFoldAtCaretPosition(editor, 50, 1);
292: compareFoldHierarchyDump();
293:
294: //24 multi folds on one line. Collapsing third method
295: // public void firstMethod(){ } public void secondMethod(){ } |public void thirdMethod(){ }
296: collapseFoldAtCaretPosition(editor, 50, 64);
297: compareFoldHierarchyDump();
298:
299: //25 multi folds on one line. Expanding third method
300: // public void firstMethod(){ } public void secondMethod(){ } |public void thirdMethod()[...]
301: expandFoldAtCaretPosition(editor, 50, 64);
302: compareFoldHierarchyDump();
303:
304: //26 multi folds on one line. Collapsing third method
305: // public void firstMethod(){ } public void secondMethod(){ } public void thirdMethod(){ }|
306: collapseFoldAtCaretPosition(editor, 50, 92);
307: compareFoldHierarchyDump();
308:
309: //27 multi folds on one line. Expanding third method
310: // public void firstMethod(){ } public void secondMethod(){ } public void thirdMethod()[...]|
311: expandFoldAtCaretPosition(editor, 50, 92);
312: compareFoldHierarchyDump();
313:
314: //28 collapse One-line InnerClass Javadoc Fold
315: // One-line Inn|erClass Javadoc Fold
316: collapseFoldAtCaretPosition(editor, 52, 21);
317: compareFoldHierarchyDump();
318:
319: //29 collapse One-line InnerClass Fold
320: //| public static class InnerClassOne{ }
321: collapseFoldAtCaretPosition(editor, 53, 1);
322: compareFoldHierarchyDump();
323:
324: //30 collapse Multi-line InnerClass Javadoc Fold
325: //| /**
326: collapseFoldAtCaretPosition(editor, 55, 1);
327: compareFoldHierarchyDump();
328:
329: //31 collapse Multi-line InnerClass Fold
330: //public static class InnerClassTwo{ //Multi-line InnerClass Fold|
331: collapseFoldAtCaretPosition(editor, 58, 68);
332: compareFoldHierarchyDump();
333:
334: //------------------- Inner Class tests -------------------------
335: //---------------------------------------------------------------
336:
337: //32 collapse Multi-line InnerClass Fold
338: //| public static class InnerClassThree{
339: collapseFoldAtCaretPosition(editor, 61, 1);
340: compareFoldHierarchyDump();
341:
342: //33 expand Multi-line InnerClass Fold
343: //| public static class InnerClassThree[...]
344: expandFoldAtCaretPosition(editor, 61, 1);
345: compareFoldHierarchyDump();
346:
347: //34 collapse One Line InnerClass Field Javadoc Fold
348: // /** One Line InnerClass Field Javadoc Fold*/|
349: collapseFoldAtCaretPosition(editor, 62, 53);
350: compareFoldHierarchyDump();
351:
352: //35 collapse Multi-line InnerClass Field Javadoc Fold
353: // /**|
354: collapseFoldAtCaretPosition(editor, 65, 12);
355: compareFoldHierarchyDump();
356:
357: //36 collapse One-line InnerClass Constructor Javadoc Fold
358: // |/** One-line InnerClass Constructor Javadoc Fold */
359: collapseFoldAtCaretPosition(editor, 70, 9);
360: compareFoldHierarchyDump();
361:
362: //37 collapse One-line InnerClass Constructor Fold
363: // public InnerClassThree() { }| //One-line InnerClass Constructor Fold
364: collapseFoldAtCaretPosition(editor, 71, 36);
365: compareFoldHierarchyDump();
366:
367: //38 collapse Multi-line InnerClass Constructor Javadoc Fold
368: // |*/
369: collapseFoldAtCaretPosition(editor, 75, 10);
370: compareFoldHierarchyDump();
371:
372: //39 collapse Multi-line InnerClass Constructor Fold
373: // applet = new |JApplet();
374: collapseFoldAtCaretPosition(editor, 78, 26);
375: compareFoldHierarchyDump();
376:
377: //40 collapse One-line InnerClass Method Javadoc Fold
378: // /**| One-line InnerClass Method Javadoc Fold */
379: collapseFoldAtCaretPosition(editor, 82, 12);
380: compareFoldHierarchyDump();
381:
382: //41 collapse One-line InnerClass Method Fold
383: // public void methodOne(){ }| // One-line InnerClass Method Fold
384: collapseFoldAtCaretPosition(editor, 83, 34);
385: compareFoldHierarchyDump();
386:
387: //42 collapse Multi-line InnerClass Method Javadoc Fold
388: // *|/
389: collapseFoldAtCaretPosition(editor, 87, 11);
390: compareFoldHierarchyDump();
391:
392: //43 collapse Multi-line InnerClass Method Fold
393: // }|
394: collapseFoldAtCaretPosition(editor, 90, 10);
395: compareFoldHierarchyDump();
396:
397: //44 collapse Whole InnerClass, the caret is just one characted behind method fold endOffset
398: // } |
399: collapseFoldAtCaretPosition(editor, 90, 11);
400: compareFoldHierarchyDump();
401:
402: //45 expand Whole InnerClass
403: // public static class InnerClassThree[...]|
404: expandFoldAtCaretPosition(editor, 93, 6);
405: compareFoldHierarchyDump();
406:
407: //46 collapse first method
408: //| public void firstMethod(){ } public void secondMethod(){ } public void thirdMethod(){ }
409: collapseFoldAtCaretPosition(editor, 92, 1);
410: compareFoldHierarchyDump();
411:
412: //47 expand first method
413: //| public void firstMethod()[...] public void secondMethod(){ } public void thirdMethod(){ }
414: expandFoldAtCaretPosition(editor, 92, 1);
415: compareFoldHierarchyDump();
416:
417: //48 collapse second method
418: // public void firstMethod(){ } | public void secondMethod(){ } public void thirdMethod(){ }
419: collapseFoldAtCaretPosition(editor, 92, 38);
420: compareFoldHierarchyDump();
421:
422: //49 expand second method
423: // public void firstMethod(){ } | public void secondMethod()[...] public void thirdMethod(){ }
424: expandFoldAtCaretPosition(editor, 92, 38);
425: compareFoldHierarchyDump();
426:
427: //51 collapse third method
428: // public void firstMethod(){ } public void secondMethod(){ } public void thirdMe|thod(){ }
429: collapseFoldAtCaretPosition(editor, 92, 86);
430: compareFoldHierarchyDump();
431:
432: //51 expand second method
433: // public void firstMethod(){ } public void secondMethod(){ } public void thirdMe|thod()[...]
434: expandFoldAtCaretPosition(editor, 92, 86);
435: compareFoldHierarchyDump();
436:
437: //52 collapse all three methods
438: // public void firstMethod(){|} public void second|Method(){ } public void thirdMethod(){ }|
439: collapseFoldAtCaretPosition(editor, 92, 35);
440: collapseFoldAtCaretPosition(editor, 92, 56);
441: collapseFoldAtCaretPosition(editor, 92, 94);
442: compareFoldHierarchyDump();
443:
444: //---------------------------------------------------
445: // Inner class in Inner Class testing
446:
447: //53 collapse a method javadoc
448: // */|
449: collapseFoldAtCaretPosition(editor, 131, 16);
450: compareFoldHierarchyDump();
451:
452: //54 collapse a method
453: // public void method|Two(){ // Multi-line InnerClassInInnerClass Method Fold
454: collapseFoldAtCaretPosition(editor, 132, 31);
455: compareFoldHierarchyDump();
456:
457: //55 multi line methods
458: // public vo|id firstMethod(){ } public void secondMethod(){|} public void thirdMethod(){ }|
459: collapseFoldAtCaretPosition(editor, 136, 22);
460: collapseFoldAtCaretPosition(editor, 136, 69);
461: collapseFoldAtCaretPosition(editor, 136, 98);
462: compareFoldHierarchyDump();
463:
464: //--------------------------------------------------------------
465: // 56 Collapse All Folds
466: collapseAllFolds(editor);
467: compareFoldHierarchyDump();
468:
469: //--------------------------------------------------------------
470: // 57 Collapse All Folds
471: expandAllFolds(editor);
472: compareFoldHierarchyDump();
473:
474: closeFileWithDiscard();
475:
476: }
477:
478: public static void main(String[] args) {
479: TestRunner.run(JavaFoldsTest.class);
480: }
481:
482: }
|