KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > source > SourceUtilsTest


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.modules.j2ee.common.source;
21
22 import com.sun.source.tree.*;
23 import com.sun.source.util.*;
24 import java.io.IOException JavaDoc;
25 import javax.lang.model.element.*;
26 import org.netbeans.api.java.source.CancellableTask;
27 import org.netbeans.api.java.source.CompilationController;
28 import org.netbeans.api.java.source.JavaSource;
29 import org.netbeans.junit.MockServices;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  *
36  * @author Andrei Badea, Martin Adamek
37  */

38 public class SourceUtilsTest extends NbTestCase {
39
40     private FileObject testFO;
41
42     public SourceUtilsTest(String JavaDoc testName) {
43         super(testName);
44     }
45
46     protected void setUp() throws Exception JavaDoc {
47         MockServices.setServices(FakeJavaDataLoaderPool.class, RepositoryImpl.class);
48
49         clearWorkDir();
50         FileObject workDir = FileUtil.toFileObject(getWorkDir());
51         testFO = workDir.createData("TestClass.java");
52     }
53
54     public void testNewInstance() throws Exception JavaDoc {
55         TestUtilities.copyStringToFileObject(testFO,
56                 "package foo;" +
57                 "public class TestClass {" +
58                 "}");
59         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
60             public void run(CompilationController controller) throws Exception JavaDoc {
61                 controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
62                 TypeElement typeElement = controller.getElements().getTypeElement("foo.TestClass");
63                 SourceUtils srcUtils = SourceUtils.newInstance(controller, typeElement);
64                 assertSame(typeElement, srcUtils.getTypeElement());
65                 assertEquals(controller.getTrees().getTree(typeElement), srcUtils.getClassTree());
66
67                 ClassTree classTree = (ClassTree)controller.getCompilationUnit().getTypeDecls().get(0);
68                 srcUtils = SourceUtils.newInstance(controller, classTree);
69                 assertSame(classTree, srcUtils.getClassTree());
70                 TreePath classTreePath = controller.getTrees().getPath(controller.getCompilationUnit(), classTree);
71                 typeElement = (TypeElement)controller.getTrees().getElement(classTreePath);
72                 assertEquals(typeElement, srcUtils.getTypeElement());
73
74                 srcUtils = SourceUtils.newInstance(controller);
75                 assertSame(srcUtils.getTypeElement(), typeElement);
76                 assertSame(srcUtils.getClassTree(), classTree);
77             }
78         });
79     }
80
81     public void testPhase() throws Exception JavaDoc {
82         TestUtilities.copyStringToFileObject(testFO,
83                 "package foo;" +
84                 "public class TestClass {" +
85                 "}");
86         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
87             public void run(CompilationController controller) throws Exception JavaDoc {
88                 SourceUtils srcUtils = SourceUtils.newInstance(controller);
89                 assertEquals(JavaSource.Phase.ELEMENTS_RESOLVED, controller.getPhase());
90             }
91         });
92     }
93
94     public void testMainTypeElement() throws Exception JavaDoc {
95         TestUtilities.copyStringToFileObject(testFO,
96                 "package foo;" +
97                 "public class TestClass {" +
98                 "}" +
99                 "class AnotherClass {" +
100                 "}");
101         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
102             public void run(CompilationController controller) throws IOException JavaDoc {
103                 TypeElement typeElement = SourceUtils.newInstance(controller).getTypeElement();
104                 assertTrue(typeElement.getQualifiedName().contentEquals("foo.TestClass"));
105             }
106         });
107
108         TestUtilities.copyStringToFileObject(testFO,
109                 "package foo;" +
110                 "public class AnotherClass {" +
111                 "}");
112         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
113             public void run(CompilationController controller) throws IOException JavaDoc {
114                 assertNull(SourceUtils.newInstance(controller));
115             }
116         });
117     }
118
119     public void testGetNoArgConstructor() throws Exception JavaDoc {
120         TestUtilities.copyStringToFileObject(testFO,
121                 "package foo;" +
122                 "public class TestClass {" +
123                 " public TestClass() {" +
124                 " }" +
125                 "}");
126         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
127             public void run(CompilationController controller) throws Exception JavaDoc {
128                 SourceUtils srcUtils = SourceUtils.newInstance(controller);
129                 ExecutableElement constructor = srcUtils.getNoArgConstructor();
130                 assertNotNull(constructor);
131                 assertFalse(controller.getElementUtilities().isSynthetic(constructor));
132             }
133         });
134
135         TestUtilities.copyStringToFileObject(testFO,
136                 "package foo;" +
137                 "public class TestClass {" +
138                 "}");
139         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
140             public void run(CompilationController controller) throws Exception JavaDoc {
141                 SourceUtils srcUtils = SourceUtils.newInstance(controller);
142                 assertNull(srcUtils.getNoArgConstructor());
143             }
144         });
145     }
146
147     public void testIsSubtype() throws Exception JavaDoc {
148         TestUtilities.copyStringToFileObject(testFO,
149                 "package foo;" +
150                 "public class TestClass implements java.io.Serializable {" +
151                 "}");
152         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
153             public void run(CompilationController controller) throws Exception JavaDoc {
154                 SourceUtils srcUtils = SourceUtils.newInstance(controller);
155                 assertTrue(srcUtils.isSubtype("java.io.Serializable"));
156                 assertFalse(srcUtils.isSubtype("java.lang.Cloneable"));
157             }
158         });
159     }
160
161     public void testIsSubtypeGenerics() throws Exception JavaDoc {
162         TestUtilities.copyStringToFileObject(testFO,
163                 "package foo;" +
164                 "import java.util.Enumeration;" +
165                 "public class TestClass implements Enumeration<String> {" +
166                 " public boolean hasMoreElement() {" +
167                 " return false;" +
168                 " }" +
169                 " public String nextElement() {" +
170                 " return null;" +
171                 " }" +
172                 "}");
173         runUserActionTask(testFO, new AbstractTask<CompilationController>() {
174             public void run(CompilationController controller) throws Exception JavaDoc {
175                 SourceUtils srcUtils = SourceUtils.newInstance(controller);
176                 assertTrue(srcUtils.isSubtype("java.util.Enumeration<String>"));
177                 assertFalse(srcUtils.isSubtype("java.util.Enumeration<Object>"));
178             }
179         });
180     }
181
182     private static void runUserActionTask(FileObject javaFile, CancellableTask<CompilationController> taskToTest) throws Exception JavaDoc {
183         JavaSource javaSource = JavaSource.forFileObject(javaFile);
184         javaSource.runUserActionTask(taskToTest, true);
185     }
186 }
187
Popular Tags