1 19 20 package org.netbeans.modules.web.core; 21 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.tree.CompilationUnitTree; 24 import java.io.File ; 25 import java.io.IOException ; 26 import javax.lang.model.element.TypeElement; 27 import org.netbeans.api.java.source.CancellableTask; 28 import org.netbeans.api.java.source.CompilationController; 29 import org.netbeans.api.java.source.JavaSource; 30 import static org.netbeans.api.java.source.JavaSource.Phase; 31 import org.netbeans.modules.j2ee.common.source.SourceUtils; 32 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileUtil; 35 36 import org.netbeans.junit.NbTestCase; 37 38 import org.netbeans.modules.web.core.test.TestUtil; 39 40 44 public class WebInjectionTargetQueryImplementationTest extends NbTestCase { 45 46 private String serverID; 47 private FileObject ordinaryClass; 48 private FileObject fileSubclass; 49 private FileObject directServletSubclass; 50 private FileObject secondLevelServletSubclass; 51 52 public WebInjectionTargetQueryImplementationTest(String testName) { 53 super(testName); 54 } 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 59 TestUtil.makeScratchDir(this); 60 serverID = TestUtil.registerSunAppServer(this); 61 } 62 63 protected void tearDown() throws Exception { 64 serverID = null; 65 ordinaryClass = null; 66 fileSubclass = null; 67 directServletSubclass = null; 68 secondLevelServletSubclass = null; 69 70 super.tearDown(); 71 } 72 73 76 public void testIsInjectionTarget() { 77 System.out.println("isInjectionTarget"); 78 79 final boolean[] result = {false}; 80 final WebInjectionTargetQueryImplementation instance = new WebInjectionTargetQueryImplementation(); 81 82 File f = new File (getDataDir().getAbsolutePath(), "projects/WebApplication_j2ee14"); 84 FileObject projdir = FileUtil.toFileObject(f); 85 86 ordinaryClass = projdir.getFileObject("src/java/org/test/NewClass.java"); 87 fileSubclass = projdir.getFileObject("src/java/org/test/FileSubclass.java"); 88 directServletSubclass = projdir.getFileObject("src/java/org/test/NewServlet.java"); 89 secondLevelServletSubclass = projdir.getFileObject("src/java/org/test/NewServletSubclass.java"); 90 91 CancellableTask task = new CancellableTask<CompilationController>() { 92 public void run(CompilationController controller) throws IOException { 93 controller.toPhase(Phase.ELEMENTS_RESOLVED); 94 CompilationUnitTree cut = controller.getCompilationUnit(); 95 ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); 96 SourceUtils srcUtils = SourceUtils.newInstance(controller, clazz); 97 TypeElement thisTypeEl = srcUtils.getTypeElement(); 98 result[0] = instance.isInjectionTarget(controller, thisTypeEl); 99 } 100 public void cancel() {} 101 }; 102 try { 103 JavaSource javaSrc = JavaSource.forFileObject(ordinaryClass); 104 javaSrc.runUserActionTask(task, true); 105 assertEquals(false, result[0]); 106 javaSrc = JavaSource.forFileObject(fileSubclass); 107 javaSrc.runUserActionTask(task, true); 108 assertEquals(false, result[0]); 109 javaSrc = JavaSource.forFileObject(directServletSubclass); 110 javaSrc.runUserActionTask(task, true); 111 assertEquals(false, result[0]); 112 javaSrc = JavaSource.forFileObject(secondLevelServletSubclass); 113 javaSrc.runUserActionTask(task, true); 114 assertEquals(false, result[0]); 115 116 f = new File (getDataDir().getAbsolutePath(), "projects/WebApplication_jee5"); 118 projdir = FileUtil.toFileObject(f); 119 120 ordinaryClass = projdir.getFileObject("src/java/org/test/NewClass.java"); 121 fileSubclass = projdir.getFileObject("src/java/org/test/FileSubclass.java"); 122 directServletSubclass = projdir.getFileObject("src/java/org/test/NewServlet.java"); 123 secondLevelServletSubclass = projdir.getFileObject("src/java/org/test/NewServletSubclass.java"); 124 125 javaSrc = JavaSource.forFileObject(ordinaryClass); 126 javaSrc.runUserActionTask(task, true); 127 assertEquals(false, result[0]); 128 javaSrc = JavaSource.forFileObject(fileSubclass); 129 javaSrc.runUserActionTask(task, true); 130 assertEquals(false, result[0]); 131 javaSrc = JavaSource.forFileObject(directServletSubclass); 132 javaSrc.runUserActionTask(task, true); 133 assertEquals(true, result[0]); 134 javaSrc = JavaSource.forFileObject(secondLevelServletSubclass); 135 javaSrc.runUserActionTask(task, true); 136 assertEquals(true, result[0]); 137 } catch (IOException ex) { 138 ex.printStackTrace(); 139 System.out.println("ex="+ex); 140 throw new AssertionError (ex); 141 } 142 } 143 144 } 145 | Popular Tags |