KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > TreeUtilitiesTest


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.source;
20
21 import com.sun.source.tree.BlockTree;
22 import com.sun.source.util.TreePath;
23 import java.io.File JavaDoc;
24 import org.netbeans.junit.NbTestCase;
25 import org.netbeans.modules.java.source.TestUtil;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileUtil;
28
29 /**
30  *
31  * @author Jan Lahoda
32  */

33 public class TreeUtilitiesTest extends NbTestCase {
34     
35     public TreeUtilitiesTest(String JavaDoc testName) {
36         super(testName);
37     }
38     
39     protected void setUp() throws Exception JavaDoc {
40         SourceUtilsTestUtil.prepareTest(new String JavaDoc[0], new Object JavaDoc[0]);
41         super.setUp();
42     }
43     
44     private CompilationInfo info;
45     
46     private void prepareTest(String JavaDoc filename, String JavaDoc code) throws Exception JavaDoc {
47         File JavaDoc work = TestUtil.createWorkFolder();
48         FileObject workFO = FileUtil.toFileObject(work);
49         
50         assertNotNull(workFO);
51         
52         FileObject sourceRoot = workFO.createFolder("src");
53         FileObject buildRoot = workFO.createFolder("build");
54         FileObject cache = workFO.createFolder("cache");
55         FileObject packageRoot = sourceRoot.createFolder("test");
56         
57         SourceUtilsTestUtil.prepareTest(sourceRoot, buildRoot, cache);
58         
59         FileObject testSource = packageRoot.createData(filename + ".java");
60         
61         assertNotNull(testSource);
62         
63         TestUtilities.copyStringToFile(FileUtil.toFile(testSource), code);
64         
65         JavaSource js = JavaSource.forFileObject(testSource);
66         
67         assertNotNull(js);
68         
69         info = SourceUtilsTestUtil.getCompilationInfo(js, JavaSource.Phase.RESOLVED);
70         
71         assertNotNull(info);
72     }
73
74     public void testIsSynthetic1() throws Exception JavaDoc {
75         prepareTest("Test", "package test; public class Test {public Test(){}}");
76         
77         TreePath tp = info.getTreeUtilities().pathFor(47);
78         BlockTree bt = (BlockTree) tp.getLeaf();
79         
80         tp = new TreePath(tp, bt.getStatements().get(0));
81         
82         assertTrue(info.getTreeUtilities().isSynthetic(tp));
83     }
84     
85     public void testIsSynthetic2() throws Exception JavaDoc {
86         prepareTest("Test", "package test; public class Test {public Test(){super();}}");
87         
88         TreePath tp = info.getTreeUtilities().pathFor(47);
89         BlockTree bt = (BlockTree) tp.getLeaf();
90         
91         tp = new TreePath(tp, bt.getStatements().get(0));
92         
93         assertFalse(info.getTreeUtilities().isSynthetic(tp));
94     }
95 }
96
Popular Tags