1 19 20 package org.netbeans.test.web.folding; 21 22 import java.awt.Toolkit ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.io.PrintStream ; 30 import java.lang.reflect.InvocationTargetException ; 31 import java.net.URL ; 32 import java.util.Vector ; 33 import javax.swing.JEditorPane ; 34 import javax.swing.SwingUtilities ; 35 import javax.swing.Timer ; 36 import javax.swing.text.BadLocationException ; 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 62 public class JspFoldingTest extends NbTestCase implements FoldHierarchyListener, ActionListener { 63 64 private FileObject webDir = null; 65 private Timer timer = null; 66 private Object lock = new Object (); 67 68 public JspFoldingTest() { 69 super("jspfoldingtest"); 70 } 71 72 public void setUp() throws IOException { 73 File datadir = getDataDir(); 74 File projectDir = new File (datadir, "TestWebApplication"); 75 FileObject projectFO = FileUtil.toFileObject(projectDir); 76 webDir = projectFO.getFileObject("web"); 77 } 78 79 public void tearDown() { 80 compareReferenceFiles(); 81 } 82 83 85 public void testHTMLFolds() throws IOException , BadLocationException , InterruptedException { 86 FileObject testFO = webDir.getFileObject("testHTMLtags","jsp"); 87 dumpFoldHierarchyFor(testFO); 88 } 89 90 public void testDirectiveFolds() throws IOException , BadLocationException , InterruptedException { 91 FileObject testFO = webDir.getFileObject("testDirectives","jsp"); 92 dumpFoldHierarchyFor(testFO); 93 } 94 95 public void testScriptlets() throws IOException , BadLocationException , InterruptedException { 96 FileObject testFO = webDir.getFileObject("testScriptlets","jsp"); 97 dumpFoldHierarchyFor(testFO); 98 } 99 100 public void testComments() throws IOException , BadLocationException , InterruptedException { 101 FileObject testFO = webDir.getFileObject("testComments","jsp"); 102 dumpFoldHierarchyFor(testFO); 103 } 104 105 public void testMix() throws IOException , BadLocationException , InterruptedException { 106 FileObject testFO = webDir.getFileObject("testMix","jsp"); 107 dumpFoldHierarchyFor(testFO); 108 } 109 110 112 private void dumpFoldHierarchyFor(FileObject fo) throws IOException , BadLocationException , InterruptedException { 113 try { 114 DataObject dobj = DataObject.find(fo); 116 117 EditorCookie ec = (EditorCookie)dobj.getCookie(EditorCookie.class); 119 EditorCookie.Observable eco = (EditorCookie.Observable)dobj.getCookie(EditorCookie.Observable.class); 120 121 final PropertyChangeListener pcl = new PropertyChangeListener () { 123 public void propertyChange(PropertyChangeEvent evt) { 124 if (evt.getPropertyName().equals(EditorCookie.Observable.PROP_OPENED_PANES)) { 125 synchronized(lock) { 126 lock.notifyAll(); 127 } 129 } 130 } 131 }; 132 133 eco.addPropertyChangeListener(pcl); 134 135 BaseDocument doc = (BaseDocument)ec.openDocument(); 137 ec.open(); 138 139 synchronized(lock) { 141 JEditorPane [] panes = getOpenedPanes(ec); 142 if(panes == null || panes.length == 0) lock.wait(); 143 } 144 145 eco.removePropertyChangeListener(pcl); 146 147 148 JEditorPane editor = getOpenedPanes(ec)[0]; 150 FoldHierarchy fh = FoldHierarchy.get(editor); 151 152 fh.addFoldHierarchyListener(this); 155 156 timer = new Timer (10000, this); 158 timer.setRepeats(false); 159 timer.start(); 160 161 synchronized(lock) { 164 lock.wait(); 165 } 166 fh.removeFoldHierarchyListener(this); 168 169 JspFoldUtils.printFolds(fh, getRef()); 171 172 ec.close(); 174 }catch(Throwable t) { 176 ErrorManager.getDefault().notify(t); 177 } 178 } 179 180 public JEditorPane [] getOpenedPanes(final EditorCookie ec) { 181 if(SwingUtilities.isEventDispatchThread()) return ec.getOpenedPanes(); 182 else { 183 final Vector v = new Vector (); 184 try { 185 SwingUtilities.invokeAndWait(new Runnable () { 186 public void run() { 187 v.add(ec.getOpenedPanes()); 188 } 189 }); 190 }catch(InvocationTargetException ite) { 191 }catch(InterruptedException ie) { 193 } 195 if(v.size() == 1) return (JEditorPane [])v.get(0); 196 else return null; 197 } 198 } 199 200 public void foldHierarchyChanged(FoldHierarchyEvent evt) { 201 timer.setInitialDelay(1000); 203 timer.restart(); 204 } 206 207 public void actionPerformed(ActionEvent evt) { 208 synchronized(lock) { 210 lock.notifyAll(); 211 } 212 } 214 215 } 216 | Popular Tags |