KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > core > WebInjectionTargetQueryImplementationTest


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.web.core;
21
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.CompilationUnitTree;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
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 /**
41  *
42  * @author Radko Najman
43  */

44 public class WebInjectionTargetQueryImplementationTest extends NbTestCase {
45     
46     private String JavaDoc serverID;
47     private FileObject ordinaryClass;
48     private FileObject fileSubclass;
49     private FileObject directServletSubclass;
50     private FileObject secondLevelServletSubclass;
51
52     public WebInjectionTargetQueryImplementationTest(String JavaDoc testName) {
53         super(testName);
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         
59         TestUtil.makeScratchDir(this);
60         serverID = TestUtil.registerSunAppServer(this);
61     }
62
63     protected void tearDown() throws Exception JavaDoc {
64         serverID = null;
65         ordinaryClass = null;
66         fileSubclass = null;
67         directServletSubclass = null;
68         secondLevelServletSubclass = null;
69         
70         super.tearDown();
71     }
72
73     /**
74      * Test of isInjectionTarget method, of class org.netbeans.modules.web.core.WebInjectionTargetQueryImplementation.
75      */

76     public void testIsInjectionTarget() {
77         System.out.println("isInjectionTarget");
78         
79         final boolean[] result = {false};
80         final WebInjectionTargetQueryImplementation instance = new WebInjectionTargetQueryImplementation();
81
82         //J2EE 1.4 project
83
File JavaDoc f = new File JavaDoc(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 JavaDoc {
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             //Java EE 5 project
117
f = new File JavaDoc(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 JavaDoc ex) {
138             ex.printStackTrace();
139             System.out.println("ex="+ex);
140             throw new AssertionError JavaDoc(ex);
141         }
142     }
143     
144 }
145
Popular Tags