KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > web > folding > JspFoldingTest


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 org.netbeans.test.web.folding;
21
22 import java.awt.Toolkit JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintStream JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.util.Vector JavaDoc;
33 import javax.swing.JEditorPane JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import javax.swing.Timer JavaDoc;
36 import javax.swing.text.BadLocationException JavaDoc;
37 import org.netbeans.api.editor.fold.FoldHierarchy;
38 import org.netbeans.api.editor.fold.FoldHierarchyEvent;
39 import org.netbeans.api.editor.fold.FoldHierarchyListener;
40 import org.netbeans.api.project.Project;
41 import org.netbeans.api.project.ProjectManager;
42 import org.netbeans.editor.BaseDocument;
43 import org.netbeans.editor.TokenContext;
44 import org.netbeans.editor.TokenContextPath;
45 import org.netbeans.editor.TokenID;
46 import org.netbeans.editor.TokenItem;
47 import org.netbeans.editor.ext.html.HTMLSyntax;
48 import org.netbeans.editor.ext.java.JavaSyntax;
49 import org.netbeans.junit.NbTestCase;
50 import org.netbeans.modules.web.core.syntax.folding.JspFoldUtils;
51
52 import org.openide.ErrorManager;
53 import org.openide.cookies.EditorCookie;
54 import org.openide.filesystems.FileObject;
55 import org.openide.filesystems.FileUtil;
56 import org.openide.loaders.DataObject;
57
58 /** Basic jsp folding tests.
59  *
60  * @author mf100882
61  */

62 public class JspFoldingTest extends NbTestCase implements FoldHierarchyListener, ActionListener JavaDoc {
63     
64     private FileObject webDir = null;
65     private Timer JavaDoc timer = null;
66     private Object JavaDoc lock = new Object JavaDoc();
67     
68     public JspFoldingTest() {
69         super("jspfoldingtest");
70     }
71     
72     public void setUp() throws IOException JavaDoc {
73         File JavaDoc datadir = getDataDir();
74         File JavaDoc projectDir = new File JavaDoc(datadir, "TestWebApplication");
75         FileObject projectFO = FileUtil.toFileObject(projectDir);
76         webDir = projectFO.getFileObject("web");
77     }
78     
79     public void tearDown() {
80         compareReferenceFiles();
81     }
82     
83     //--------- test methods -----------
84

85     public void testHTMLFolds() throws IOException JavaDoc, BadLocationException JavaDoc, InterruptedException JavaDoc {
86         FileObject testFO = webDir.getFileObject("testHTMLtags","jsp");
87         dumpFoldHierarchyFor(testFO);
88     }
89     
90     public void testDirectiveFolds() throws IOException JavaDoc, BadLocationException JavaDoc, InterruptedException JavaDoc {
91         FileObject testFO = webDir.getFileObject("testDirectives","jsp");
92         dumpFoldHierarchyFor(testFO);
93     }
94     
95     public void testScriptlets() throws IOException JavaDoc, BadLocationException JavaDoc, InterruptedException JavaDoc {
96         FileObject testFO = webDir.getFileObject("testScriptlets","jsp");
97         dumpFoldHierarchyFor(testFO);
98     }
99     
100     public void testComments() throws IOException JavaDoc, BadLocationException JavaDoc, InterruptedException JavaDoc {
101         FileObject testFO = webDir.getFileObject("testComments","jsp");
102         dumpFoldHierarchyFor(testFO);
103     }
104     
105     public void testMix() throws IOException JavaDoc, BadLocationException JavaDoc, InterruptedException JavaDoc {
106         FileObject testFO = webDir.getFileObject("testMix","jsp");
107         dumpFoldHierarchyFor(testFO);
108     }
109     
110     //<<< --------- test methods ----------
111

112     private void dumpFoldHierarchyFor(FileObject fo) throws IOException JavaDoc, BadLocationException JavaDoc, InterruptedException JavaDoc {
113         try {
114             //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "executing " + fo.getNameExt());
115
DataObject dobj = DataObject.find(fo);
116             
117             //open the file in editor
118
EditorCookie ec = (EditorCookie)dobj.getCookie(EditorCookie.class);
119             EditorCookie.Observable eco = (EditorCookie.Observable)dobj.getCookie(EditorCookie.Observable.class);
120             
121             //wait for editor to be opened
122
final PropertyChangeListener JavaDoc pcl = new PropertyChangeListener JavaDoc() {
123                 public void propertyChange(PropertyChangeEvent JavaDoc evt) {
124                     if (evt.getPropertyName().equals(EditorCookie.Observable.PROP_OPENED_PANES)) {
125                         synchronized(lock) {
126                             lock.notifyAll();
127                             //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "pane opened");
128
}
129                     }
130                 }
131             };
132             
133             eco.addPropertyChangeListener(pcl);
134             
135             //open the document
136
BaseDocument doc = (BaseDocument)ec.openDocument();
137             ec.open();
138             
139             //and wait for the editor pane to show up
140
synchronized(lock) {
141                 JEditorPane JavaDoc[] panes = getOpenedPanes(ec);
142                 if(panes == null || panes.length == 0) lock.wait();
143             }
144             
145             eco.removePropertyChangeListener(pcl);
146             
147             
148             //get fold hierarchy for the editor component
149
JEditorPane JavaDoc editor = getOpenedPanes(ec)[0];
150             FoldHierarchy fh = FoldHierarchy.get(editor);
151             
152             //I have to wait some time for the foldhierarchy to be built by the foldmanager
153
//so I am listening in hirarchy changes for some time.
154
fh.addFoldHierarchyListener(this);
155             
156             //start timeout timer
157
timer = new Timer JavaDoc(10000, this);
158             timer.setRepeats(false);
159             timer.start();
160             
161             //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "waiting for hierarchy");
162
//wait for the hirerchy to be created
163
synchronized(lock) {
164                 lock.wait();
165             }
166             //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "unlocked");
167
fh.removeFoldHierarchyListener(this);
168             
169             //put hierarchy structure into ref stream
170
JspFoldUtils.printFolds(fh, getRef());
171             
172             //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "closing");
173
ec.close();
174             //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "closed");
175
}catch(Throwable JavaDoc t) {
176             ErrorManager.getDefault().notify(t);
177         }
178     }
179     
180     public JEditorPane JavaDoc[] getOpenedPanes(final EditorCookie ec) {
181         if(SwingUtilities.isEventDispatchThread()) return ec.getOpenedPanes();
182         else {
183             final Vector JavaDoc v = new Vector JavaDoc();
184             try {
185                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
186                     public void run() {
187                         v.add(ec.getOpenedPanes());
188                     }
189                 });
190             }catch(InvocationTargetException JavaDoc ite) {
191                 //do nothing
192
}catch(InterruptedException JavaDoc ie) {
193                 //do nothing
194
}
195             if(v.size() == 1) return (JEditorPane JavaDoc[])v.get(0);
196             else return null;
197         }
198     }
199     
200     public void foldHierarchyChanged(FoldHierarchyEvent evt) {
201         //restart timer - postpone the timout - for 1 second only
202
timer.setInitialDelay(1000);
203         timer.restart();
204         //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "hierarchy changed");
205
}
206     
207     public void actionPerformed(ActionEvent JavaDoc evt) {
208         //timeout elapsed
209
synchronized(lock) {
210             lock.notifyAll();
211         }
212         //ErrorManager.getDefault().log(ErrorManager.EXCEPTION, "timer action performed.");
213
}
214     
215 }
216
Popular Tags