1 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 ; 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 38 public class SourceUtilsTest extends NbTestCase { 39 40 private FileObject testFO; 41 42 public SourceUtilsTest(String testName) { 43 super(testName); 44 } 45 46 protected void setUp() throws Exception { 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 { 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 { 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 { 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 { 88 SourceUtils srcUtils = SourceUtils.newInstance(controller); 89 assertEquals(JavaSource.Phase.ELEMENTS_RESOLVED, controller.getPhase()); 90 } 91 }); 92 } 93 94 public void testMainTypeElement() throws Exception { 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 { 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 { 114 assertNull(SourceUtils.newInstance(controller)); 115 } 116 }); 117 } 118 119 public void testGetNoArgConstructor() throws Exception { 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 { 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 { 141 SourceUtils srcUtils = SourceUtils.newInstance(controller); 142 assertNull(srcUtils.getNoArgConstructor()); 143 } 144 }); 145 } 146 147 public void testIsSubtype() throws Exception { 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 { 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 { 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 { 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 { 183 JavaSource javaSource = JavaSource.forFileObject(javaFile); 184 javaSource.runUserActionTask(taskToTest, true); 185 } 186 } 187 | Popular Tags |