KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > java > gui > parser > ParserTest


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.java.gui.parser;
21
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.CompilationUnitTree;
24 import com.sun.source.tree.Tree;
25 import com.sun.source.tree.TreeVisitor;
26 import com.sun.source.util.TreePath;
27 import com.sun.source.util.TreePathScanner;
28 import com.sun.tools.javac.tree.TreeScanner;
29 import java.awt.event.KeyEvent JavaDoc;
30 import org.netbeans.junit.*;
31 import org.netbeans.jellytools.*;
32 import org.netbeans.jellytools.nodes.*;
33 import org.netbeans.jellytools.actions.*;
34 import org.netbeans.jemmy.JemmyProperties;
35 import org.netbeans.jemmy.TestOut;
36 import org.netbeans.test.java.gui.GuiUtilities;
37 import java.io.*;
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40 import javax.lang.model.element.Element;
41 import javax.lang.model.element.TypeElement;
42 import javax.lang.model.util.ElementFilter;
43 import javax.lang.model.util.Elements;
44 import junit.textui.TestRunner;
45 import org.netbeans.api.java.source.CancellableTask;
46 import org.netbeans.api.java.source.CompilationController;
47 import org.netbeans.api.java.source.CompilationInfo;
48 import org.netbeans.api.java.source.JavaSource;
49 import org.netbeans.api.java.source.JavaSource.Phase;
50 import org.openide.filesystems.FileObject;
51 import org.openide.filesystems.FileUtil;
52
53 /**
54  * Tests Java Parser.
55  * @author Roman Strobl, Jiri Prox
56  */

57 public class ParserTest extends JellyTestCase {
58     
59     /** Switch to generate golden files */
60     private boolean generateGoledenFiles = false;
61     
62     // name of sample project
63
private static final String JavaDoc TEST_PROJECT_NAME = "default";
64     
65     // name of sample package
66
private static String JavaDoc testPackageName;
67     
68     // name of sample class
69
private static String JavaDoc testClassName;
70     
71     
72     // workdir, default /tmp, changed to NBJUnit workdir during test
73
private String JavaDoc workDir = "/tmp";
74     
75     private static String JavaDoc projectDir;
76     
77     private File classPathWorkDir;
78     
79     private FileObject artefact;
80     
81     private FileObject testFileObject;
82     
83     /**
84      * Adds tests into the test suite.
85      * @return suite
86      */

87     public static NbTestSuite suite() {
88         NbTestSuite suite = new NbTestSuite();
89         suite.addTest(new ParserTest("testCreateInnerClass"));
90         suite.addTest(new ParserTest("testCreateInnerInterface"));
91         suite.addTest(new ParserTest("testCreateConstructor"));
92         suite.addTest(new ParserTest("testCreateField"));
93         suite.addTest(new ParserTest("testCreateMethod"));
94         suite.addTest(new ParserTest("testCreateOuterClass"));
95         suite.addTest(new ParserTest("testCreateOuterInterface"));
96         return suite;
97     }
98     
99     /**
100      * Main method for standalone execution.
101      * @param args the command line arguments
102      */

103     public static void main(String JavaDoc[] args) {
104         junit.textui.TestRunner.run(suite());
105     }
106     
107     /**
108      * Sets up logging facilities.
109      */

110     public void setUp() {
111         System.out.println("######## "+getName()+" #######");
112         try {
113             File wd = getWorkDir();
114             workDir = wd.toString();
115         } catch (IOException e) { }
116         classPathWorkDir=new File(getDataDir(), ("projects."+TEST_PROJECT_NAME+".src").replace('.', File.separatorChar));
117         artefact=FileUtil.toFileObject(classPathWorkDir);
118         testPackageName = getClass().getPackage().getName()+"."+getClass().getSimpleName();
119         testClassName = getName();
120         getLog().println("testPackage="+testPackageName+" testClass="+testClassName);
121         testFileObject = artefact.getFileObject((testPackageName + "." + testClassName).replace(".","/")+".java");
122     }
123     
124     public void tearDown() {
125         System.out.println("######## "+getName()+" #######");
126         if(generateGoledenFiles) fail("Passive mode");
127     }
128     
129     /**
130      * Creates a new instance of ParserTest
131      * @param testName name of test
132      */

133     public ParserTest(String JavaDoc testName) {
134         super(testName);
135     }
136     
137     private void dumpStructure(JavaSource js,final PrintStream ps,final String JavaDoc text) throws IOException {
138         CancellableTask task = new CancellableTask<CompilationController>() {
139             ElementVisitor scanner;
140             public void cancel() {
141                 scanner.cancel();
142             }
143             public void run(CompilationController parameter) throws Exception JavaDoc {
144                 parameter.toPhase(Phase.RESOLVED);
145                 CompilationUnitTree cuTree = parameter.getCompilationUnit();
146                 List JavaDoc<? extends Tree> typeDecls = cuTree.getTypeDecls();
147                 TreePath cuPath = new TreePath( cuTree );
148                 List JavaDoc<Element> elements = new ArrayList JavaDoc<Element>( typeDecls.size() );
149                 for( Tree t : typeDecls ) {
150                     TreePath p = new TreePath( cuPath, t );
151                     Element e = parameter.getTrees().getElement(p);
152                     if ( e != null ) {
153                         elements.add( e );
154                     }
155                 }
156                 scanner = new ElementVisitor(parameter,text);
157                 for (Element element : elements) {
158                     scanner.scan(element,ps);
159                 }
160             }
161             
162         };
163         js.runModificationTask(task).commit();
164     }
165     
166     public File getRefFile(int n) throws IOException {
167         File f = new File(getWorkDir(),getName()+n+".ref");
168         return f;
169     }
170     
171     public File getGoldenFile(int n) {
172         return super.getGoldenFile(getName()+n+".pass");
173     }
174     
175     public File getNewGoldenFile(int n) {
176         String JavaDoc fileName = "data/goldenfiles/"+testPackageName.replace('.', '/')+ "/" + testClassName + n + ".pass";
177         File f = new File(getDataDir().getParentFile().getParentFile().getParentFile(),fileName);
178         f.getParentFile().mkdirs();
179         return f;
180     }
181     
182     public void compareGoldenFile(int n) {
183         if(generateGoledenFiles) {
184             try {
185                 File nGolden = getNewGoldenFile(n);
186                 File ref = getRefFile(n);
187                 BufferedReader br = new BufferedReader(new FileReader(ref));
188                 FileWriter fw = new FileWriter(nGolden);
189                 String JavaDoc line;
190                 while((line=br.readLine())!=null) fw.write(line+"\n");
191                 fw.close();
192                 br.close();
193             } catch (IOException ioe) {
194                 fail(ioe.getMessage());
195             }
196         } else {
197             try {
198                 assertFile(getRefFile(n),getGoldenFile(n), new File(getWorkDir(),getName()+n+".diff"));
199             } catch (IOException ioe) {
200                 fail(ioe.getMessage());
201             }
202         }
203     }
204     
205     
206     
207     private EditorOperator getDefaultEditor() {
208         Node pn = new ProjectsTabOperator().getProjectRootNode(
209                 TEST_PROJECT_NAME);
210         pn.select();
211         
212         Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
213                 "org.netbeans.modules.java.j2seproject.Bundle",
214                 "NAME_src.dir")+"|"+testPackageName+"|"
215                 +testClassName);
216         
217         n.select();
218         new OpenAction().perform();
219         
220         EditorOperator editor = new EditorOperator(testClassName);
221         return editor;
222     }
223     
224     private void save() {
225         SaveAction saveAction = new SaveAction();
226         MainWindowOperator.getDefault().menuBar().showMenuItem(saveAction.getMenuPath());
227         MainWindowOperator.getDefault().pushKey(KeyEvent.VK_ESCAPE);
228         saveAction.perform();
229     }
230     
231     /**
232      * Tests parsing of inner class.
233      */

234     public void testCreateInnerClass() throws IOException {
235         EditorOperator editor = getDefaultEditor();
236         editor.insert(" public class InnerClass {\n int a;\n }\n", 18, 1);
237         save();
238         JavaSource js = JavaSource.forFileObject(testFileObject);
239         FileWriter fw = new FileWriter(getRefFile(1));
240         FileOutputStream fos = new FileOutputStream(getRefFile(1));
241         PrintStream ps = new PrintStream(fos);
242         dumpStructure(js,ps,editor.getText());
243         compareGoldenFile(1);
244     }
245     
246     
247     /**
248      * Tests parsing of inner interface.
249      */

250     public void testCreateInnerInterface() throws IOException {
251         EditorOperator editor = getDefaultEditor();
252         editor.insert("public interface Iface {\n String method();\n }\n", 19, 1);
253         save();
254         JavaSource js = JavaSource.forFileObject(testFileObject);
255         FileWriter fw = new FileWriter(getRefFile(1));
256         FileOutputStream fos = new FileOutputStream(getRefFile(1));
257         PrintStream ps = new PrintStream(fos);
258         dumpStructure(js,ps,editor.getText());
259         compareGoldenFile(1);
260     }
261     
262     /**
263      * Tests parsing of constructor.
264      */

265     public void testCreateConstructor() throws IOException {
266         EditorOperator editor = getDefaultEditor();
267         editor.insert("public testCreateConstructor(String param) { }\n", 20, 1);
268         save();
269         JavaSource js = JavaSource.forFileObject(testFileObject);
270         FileWriter fw = new FileWriter(getRefFile(1));
271         FileOutputStream fos = new FileOutputStream(getRefFile(1));
272         PrintStream ps = new PrintStream(fos);
273         dumpStructure(js,ps,editor.getText());
274         compareGoldenFile(1);
275         editor.deleteLine(20);
276         fos = new FileOutputStream(getRefFile(2));
277         ps = new PrintStream(fos);
278         dumpStructure(js,ps,editor.getText());
279         compareGoldenFile(2);
280     }
281     
282     /**
283      * Tests parsing of a field.
284      */

285     public void testCreateField() throws IOException {
286         EditorOperator editor = getDefaultEditor();
287         editor.insert("String field;\n", 21, 1);
288         save();
289         JavaSource js = JavaSource.forFileObject(testFileObject);
290         FileWriter fw = new FileWriter(getRefFile(1));
291         FileOutputStream fos = new FileOutputStream(getRefFile(1));
292         PrintStream ps = new PrintStream(fos);
293         dumpStructure(js,ps,editor.getText());
294         compareGoldenFile(1);
295         editor.deleteLine(21);
296         fos = new FileOutputStream(getRefFile(2));
297         ps = new PrintStream(fos);
298         dumpStructure(js,ps,editor.getText());
299         compareGoldenFile(2);
300         
301     }
302     
303     /**
304      * Tests parsing of a method.
305      */

306     public void testCreateMethod() throws IOException {
307         
308         EditorOperator editor = getDefaultEditor();
309         editor.insert("private int method(Object obj) { return 1; }\n", 22, 1);
310         save();
311         JavaSource js = JavaSource.forFileObject(testFileObject);
312         FileWriter fw = new FileWriter(getRefFile(1));
313         FileOutputStream fos = new FileOutputStream(getRefFile(1));
314         PrintStream ps = new PrintStream(fos);
315         dumpStructure(js,ps,editor.getText());
316         compareGoldenFile(1);
317         editor.deleteLine(22);
318         fos = new FileOutputStream(getRefFile(2));
319         ps = new PrintStream(fos);
320         dumpStructure(js,ps,editor.getText());
321         compareGoldenFile(2);
322     }
323     
324     /**
325      * Tests parsing of outer class.
326      */

327     public void testCreateOuterClass() throws IOException {
328         
329         EditorOperator editor = getDefaultEditor();
330         editor.insert("class Outer { }\n", 23, 1);
331         save();
332         JavaSource js = JavaSource.forFileObject(testFileObject);
333         FileWriter fw = new FileWriter(getRefFile(1));
334         FileOutputStream fos = new FileOutputStream(getRefFile(1));
335         PrintStream ps = new PrintStream(fos);
336         dumpStructure(js,ps,editor.getText());
337         compareGoldenFile(1);
338         editor.deleteLine(23);
339         fos = new FileOutputStream(getRefFile(2));
340         ps = new PrintStream(fos);
341         dumpStructure(js,ps,editor.getText());
342         compareGoldenFile(2);
343         
344     }
345     
346     /**
347      * Tests parsing of outer interface.
348      */

349     public void testCreateOuterInterface() throws IOException {
350         
351         EditorOperator editor = getDefaultEditor();
352         editor.insert("interface Iface{ }\n", 24, 1);
353         save();
354         JavaSource js = JavaSource.forFileObject(testFileObject);
355         FileWriter fw = new FileWriter(getRefFile(1));
356         FileOutputStream fos = new FileOutputStream(getRefFile(1));
357         PrintStream ps = new PrintStream(fos);
358         dumpStructure(js,ps,editor.getText());
359         compareGoldenFile(1);
360         editor.deleteLine(23);
361         fos = new FileOutputStream(getRefFile(2));
362         ps = new PrintStream(fos);
363         dumpStructure(js,ps,editor.getText());
364         compareGoldenFile(2);
365         
366         
367     }
368     
369     
370 }
371
Popular Tags