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 org.netbeans.test.java.gui.parser;
043:
044: import com.sun.source.tree.CompilationUnitTree;
045: import com.sun.source.tree.Tree;
046: import com.sun.source.util.TreePath;
047: import java.awt.event.KeyEvent;
048: import org.netbeans.junit.*;
049: import org.netbeans.jellytools.*;
050: import org.netbeans.jellytools.nodes.*;
051: import org.netbeans.jellytools.actions.*;
052: import java.io.*;
053: import java.util.ArrayList;
054: import java.util.List;
055: import javax.lang.model.element.Element;
056: import org.netbeans.api.java.source.CancellableTask;
057: import org.netbeans.api.java.source.CompilationController;
058: import org.netbeans.api.java.source.JavaSource;
059: import org.netbeans.api.java.source.JavaSource.Phase;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062:
063: /**
064: * Tests Java Parser.
065: * @author Roman Strobl, Jiri Prox
066: */
067: public class ParserTest extends JellyTestCase {
068:
069: /** Switch to generate golden files */
070: private boolean generateGoledenFiles = false;
071:
072: // name of sample project
073: private static final String TEST_PROJECT_NAME = "default";
074:
075: // name of sample package
076: private static String testPackageName;
077:
078: // name of sample class
079: private static String testClassName;
080:
081: // workdir, default /tmp, changed to NBJUnit workdir during test
082: private String workDir = "/tmp";
083:
084: private static String projectDir;
085:
086: private File classPathWorkDir;
087:
088: private FileObject artefact;
089:
090: private FileObject testFileObject;
091:
092: /**
093: * Adds tests into the test suite.
094: * @return suite
095: */
096: public static NbTestSuite suite() {
097: NbTestSuite suite = new NbTestSuite();
098: suite.addTest(new ParserTest("testCreateInnerClass"));
099: suite.addTest(new ParserTest("testCreateInnerInterface"));
100: suite.addTest(new ParserTest("testCreateConstructor"));
101: suite.addTest(new ParserTest("testCreateField"));
102: suite.addTest(new ParserTest("testCreateMethod"));
103: suite.addTest(new ParserTest("testCreateOuterClass"));
104: suite.addTest(new ParserTest("testCreateOuterInterface"));
105: return suite;
106: }
107:
108: /**
109: * Main method for standalone execution.
110: * @param args the command line arguments
111: */
112: public static void main(String[] args) {
113: junit.textui.TestRunner.run(suite());
114: }
115:
116: /**
117: * Sets up logging facilities.
118: */
119: public void setUp() {
120: System.out.println("######## " + getName() + " #######");
121: try {
122: File wd = getWorkDir();
123: workDir = wd.toString();
124: } catch (IOException e) {
125: }
126: classPathWorkDir = new File(getDataDir(), ("projects."
127: + TEST_PROJECT_NAME + ".src").replace('.',
128: File.separatorChar));
129: artefact = FileUtil.toFileObject(classPathWorkDir);
130: testPackageName = getClass().getPackage().getName() + "."
131: + getClass().getSimpleName();
132: testClassName = getName();
133: getLog().println(
134: "testPackage=" + testPackageName + " testClass="
135: + testClassName);
136: testFileObject = artefact
137: .getFileObject((testPackageName + "." + testClassName)
138: .replace(".", "/")
139: + ".java");
140: }
141:
142: public void tearDown() {
143: System.out.println("######## " + getName() + " #######");
144: if (generateGoledenFiles)
145: fail("Passive mode");
146: }
147:
148: /**
149: * Creates a new instance of ParserTest
150: * @param testName name of test
151: */
152: public ParserTest(String testName) {
153: super (testName);
154: }
155:
156: private void dumpStructure(JavaSource js, final PrintStream ps,
157: final String text) throws IOException {
158: CancellableTask task = new CancellableTask<CompilationController>() {
159: ElementVisitor scanner;
160:
161: public void cancel() {
162: scanner.cancel();
163: }
164:
165: public void run(CompilationController parameter)
166: throws Exception {
167: parameter.toPhase(Phase.RESOLVED);
168: CompilationUnitTree cuTree = parameter
169: .getCompilationUnit();
170: List<? extends Tree> typeDecls = cuTree.getTypeDecls();
171: TreePath cuPath = new TreePath(cuTree);
172: List<Element> elements = new ArrayList<Element>(
173: typeDecls.size());
174: for (Tree t : typeDecls) {
175: TreePath p = new TreePath(cuPath, t);
176: Element e = parameter.getTrees().getElement(p);
177: if (e != null) {
178: elements.add(e);
179: }
180: }
181: scanner = new ElementVisitor(parameter, text);
182: for (Element element : elements) {
183: scanner.scan(element, ps);
184: }
185: }
186:
187: };
188: js.runModificationTask(task).commit();
189: }
190:
191: public File getRefFile(int n) throws IOException {
192: File f = new File(getWorkDir(), getName() + n + ".ref");
193: return f;
194: }
195:
196: public File getGoldenFile(int n) {
197: return super .getGoldenFile(getName() + n + ".pass");
198: }
199:
200: public File getNewGoldenFile(int n) {
201: String fileName = "data/goldenfiles/"
202: + testPackageName.replace('.', '/') + "/"
203: + testClassName + n + ".pass";
204: File f = new File(getDataDir().getParentFile().getParentFile()
205: .getParentFile(), fileName);
206: f.getParentFile().mkdirs();
207: return f;
208: }
209:
210: public void compareGoldenFile(int n) {
211: if (generateGoledenFiles) {
212: try {
213: File nGolden = getNewGoldenFile(n);
214: File ref = getRefFile(n);
215: BufferedReader br = new BufferedReader(new FileReader(
216: ref));
217: FileWriter fw = new FileWriter(nGolden);
218: String line;
219: while ((line = br.readLine()) != null)
220: fw.write(line + "\n");
221: fw.close();
222: br.close();
223: } catch (IOException ioe) {
224: fail(ioe.getMessage());
225: }
226: } else {
227: try {
228: assertFile(getRefFile(n), getGoldenFile(n), new File(
229: getWorkDir(), getName() + n + ".diff"));
230: } catch (IOException ioe) {
231: fail(ioe.getMessage());
232: }
233: }
234: }
235:
236: private EditorOperator getDefaultEditor() {
237: Node pn = new ProjectsTabOperator()
238: .getProjectRootNode(TEST_PROJECT_NAME);
239: pn.select();
240:
241: Node n = new Node(pn, org.netbeans.jellytools.Bundle.getString(
242: "org.netbeans.modules.java.j2seproject.Bundle",
243: "NAME_src.dir")
244: + "|" + testPackageName + "|" + testClassName);
245:
246: n.select();
247: new OpenAction().perform();
248:
249: EditorOperator editor = new EditorOperator(testClassName);
250: return editor;
251: }
252:
253: private void save() {
254: SaveAction saveAction = new SaveAction();
255: MainWindowOperator.getDefault().menuBar().showMenuItem(
256: saveAction.getMenuPath());
257: MainWindowOperator.getDefault().pushKey(KeyEvent.VK_ESCAPE);
258: saveAction.perform();
259: }
260:
261: /**
262: * Tests parsing of inner class.
263: */
264: public void testCreateInnerClass() throws IOException {
265: EditorOperator editor = getDefaultEditor();
266: editor
267: .insert(
268: " public class InnerClass {\n int a;\n }\n",
269: 18, 1);
270: save();
271: JavaSource js = JavaSource.forFileObject(testFileObject);
272: FileWriter fw = new FileWriter(getRefFile(1));
273: FileOutputStream fos = new FileOutputStream(getRefFile(1));
274: PrintStream ps = new PrintStream(fos);
275: dumpStructure(js, ps, editor.getText());
276: compareGoldenFile(1);
277: }
278:
279: /**
280: * Tests parsing of inner interface.
281: */
282: public void testCreateInnerInterface() throws IOException {
283: EditorOperator editor = getDefaultEditor();
284: editor
285: .insert(
286: "public interface Iface {\n String method();\n }\n",
287: 19, 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: }
296:
297: /**
298: * Tests parsing of constructor.
299: */
300: public void testCreateConstructor() throws IOException {
301: EditorOperator editor = getDefaultEditor();
302: editor.insert(
303: "public testCreateConstructor(String param) { }\n", 20,
304: 1);
305: save();
306: JavaSource js = JavaSource.forFileObject(testFileObject);
307: FileWriter fw = new FileWriter(getRefFile(1));
308: FileOutputStream fos = new FileOutputStream(getRefFile(1));
309: PrintStream ps = new PrintStream(fos);
310: dumpStructure(js, ps, editor.getText());
311: compareGoldenFile(1);
312: editor.deleteLine(20);
313: fos = new FileOutputStream(getRefFile(2));
314: ps = new PrintStream(fos);
315: dumpStructure(js, ps, editor.getText());
316: compareGoldenFile(2);
317: }
318:
319: /**
320: * Tests parsing of a field.
321: */
322: public void testCreateField() throws IOException {
323: EditorOperator editor = getDefaultEditor();
324: editor.insert("String field;\n", 21, 1);
325: save();
326: JavaSource js = JavaSource.forFileObject(testFileObject);
327: FileWriter fw = new FileWriter(getRefFile(1));
328: FileOutputStream fos = new FileOutputStream(getRefFile(1));
329: PrintStream ps = new PrintStream(fos);
330: dumpStructure(js, ps, editor.getText());
331: compareGoldenFile(1);
332: editor.deleteLine(21);
333: fos = new FileOutputStream(getRefFile(2));
334: ps = new PrintStream(fos);
335: dumpStructure(js, ps, editor.getText());
336: compareGoldenFile(2);
337:
338: }
339:
340: /**
341: * Tests parsing of a method.
342: */
343: public void testCreateMethod() throws IOException {
344:
345: EditorOperator editor = getDefaultEditor();
346: editor.insert("private int method(Object obj) { return 1; }\n",
347: 22, 1);
348: save();
349: JavaSource js = JavaSource.forFileObject(testFileObject);
350: FileWriter fw = new FileWriter(getRefFile(1));
351: FileOutputStream fos = new FileOutputStream(getRefFile(1));
352: PrintStream ps = new PrintStream(fos);
353: dumpStructure(js, ps, editor.getText());
354: compareGoldenFile(1);
355: editor.deleteLine(22);
356: fos = new FileOutputStream(getRefFile(2));
357: ps = new PrintStream(fos);
358: dumpStructure(js, ps, editor.getText());
359: compareGoldenFile(2);
360: }
361:
362: /**
363: * Tests parsing of outer class.
364: */
365: public void testCreateOuterClass() throws IOException {
366:
367: EditorOperator editor = getDefaultEditor();
368: editor.insert("class Outer { }\n", 23, 1);
369: save();
370: JavaSource js = JavaSource.forFileObject(testFileObject);
371: FileWriter fw = new FileWriter(getRefFile(1));
372: FileOutputStream fos = new FileOutputStream(getRefFile(1));
373: PrintStream ps = new PrintStream(fos);
374: dumpStructure(js, ps, editor.getText());
375: compareGoldenFile(1);
376: editor.deleteLine(23);
377: fos = new FileOutputStream(getRefFile(2));
378: ps = new PrintStream(fos);
379: dumpStructure(js, ps, editor.getText());
380: compareGoldenFile(2);
381:
382: }
383:
384: /**
385: * Tests parsing of outer interface.
386: */
387: public void testCreateOuterInterface() throws IOException {
388:
389: EditorOperator editor = getDefaultEditor();
390: editor.insert("interface Iface{ }\n", 24, 1);
391: save();
392: JavaSource js = JavaSource.forFileObject(testFileObject);
393: FileWriter fw = new FileWriter(getRefFile(1));
394: FileOutputStream fos = new FileOutputStream(getRefFile(1));
395: PrintStream ps = new PrintStream(fos);
396: dumpStructure(js, ps, editor.getText());
397: compareGoldenFile(1);
398: editor.deleteLine(23);
399: fos = new FileOutputStream(getRefFile(2));
400: ps = new PrintStream(fos);
401: dumpStructure(js, ps, editor.getText());
402: compareGoldenFile(2);
403:
404: }
405:
406: }
|