001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.core;
043:
044: import com.sun.source.tree.ClassTree;
045: import com.sun.source.tree.CompilationUnitTree;
046: import java.io.File;
047: import java.io.IOException;
048: import javax.lang.model.element.TypeElement;
049: import org.netbeans.api.java.source.CancellableTask;
050: import org.netbeans.api.java.source.CompilationController;
051: import org.netbeans.api.java.source.JavaSource;
052: import org.netbeans.modules.j2ee.core.api.support.java.SourceUtils;
053: import static org.netbeans.api.java.source.JavaSource.Phase;
054:
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057:
058: import org.netbeans.junit.NbTestCase;
059:
060: import org.netbeans.modules.web.core.test.TestUtil;
061:
062: /**
063: *
064: * @author Radko Najman
065: */
066: public class WebInjectionTargetQueryImplementationTest extends
067: NbTestCase {
068:
069: private String serverID;
070: private FileObject ordinaryClass;
071: private FileObject fileSubclass;
072: private FileObject directServletSubclass;
073: private FileObject secondLevelServletSubclass;
074:
075: public WebInjectionTargetQueryImplementationTest(String testName) {
076: super (testName);
077: }
078:
079: protected void setUp() throws Exception {
080: super .setUp();
081:
082: TestUtil.makeScratchDir(this );
083: serverID = TestUtil.registerSunAppServer(this );
084: }
085:
086: protected void tearDown() throws Exception {
087: serverID = null;
088: ordinaryClass = null;
089: fileSubclass = null;
090: directServletSubclass = null;
091: secondLevelServletSubclass = null;
092:
093: super .tearDown();
094: }
095:
096: /**
097: * Test of isInjectionTarget method, of class org.netbeans.modules.web.core.WebInjectionTargetQueryImplementation.
098: */
099: public void testIsInjectionTarget() {
100: System.out.println("isInjectionTarget");
101:
102: final boolean[] result = { false };
103: final WebInjectionTargetQueryImplementation instance = new WebInjectionTargetQueryImplementation();
104:
105: //J2EE 1.4 project
106: File f = new File(getDataDir().getAbsolutePath(),
107: "projects/WebApplication_j2ee14");
108: FileObject projdir = FileUtil.toFileObject(f);
109:
110: ordinaryClass = projdir
111: .getFileObject("src/java/org/test/NewClass.java");
112: fileSubclass = projdir
113: .getFileObject("src/java/org/test/FileSubclass.java");
114: directServletSubclass = projdir
115: .getFileObject("src/java/org/test/NewServlet.java");
116: secondLevelServletSubclass = projdir
117: .getFileObject("src/java/org/test/NewServletSubclass.java");
118:
119: CancellableTask task = new CancellableTask<CompilationController>() {
120: public void run(CompilationController controller)
121: throws IOException {
122: controller.toPhase(Phase.ELEMENTS_RESOLVED);
123: TypeElement this TypeEl = SourceUtils
124: .getPublicTopLevelElement(controller);
125: result[0] = instance.isInjectionTarget(controller,
126: this TypeEl);
127: }
128:
129: public void cancel() {
130: }
131: };
132: try {
133: JavaSource javaSrc = JavaSource
134: .forFileObject(ordinaryClass);
135: javaSrc.runUserActionTask(task, true);
136: assertEquals(false, result[0]);
137: javaSrc = JavaSource.forFileObject(fileSubclass);
138: javaSrc.runUserActionTask(task, true);
139: assertEquals(false, result[0]);
140: javaSrc = JavaSource.forFileObject(directServletSubclass);
141: javaSrc.runUserActionTask(task, true);
142: assertEquals(false, result[0]);
143: javaSrc = JavaSource
144: .forFileObject(secondLevelServletSubclass);
145: javaSrc.runUserActionTask(task, true);
146: assertEquals(false, result[0]);
147:
148: //Java EE 5 project
149: f = new File(getDataDir().getAbsolutePath(),
150: "projects/WebApplication_jee5");
151: projdir = FileUtil.toFileObject(f);
152:
153: ordinaryClass = projdir
154: .getFileObject("src/java/org/test/NewClass.java");
155: fileSubclass = projdir
156: .getFileObject("src/java/org/test/FileSubclass.java");
157: directServletSubclass = projdir
158: .getFileObject("src/java/org/test/NewServlet.java");
159: secondLevelServletSubclass = projdir
160: .getFileObject("src/java/org/test/NewServletSubclass.java");
161:
162: javaSrc = JavaSource.forFileObject(ordinaryClass);
163: javaSrc.runUserActionTask(task, true);
164: assertEquals(false, result[0]);
165: javaSrc = JavaSource.forFileObject(fileSubclass);
166: javaSrc.runUserActionTask(task, true);
167: assertEquals(false, result[0]);
168: javaSrc = JavaSource.forFileObject(directServletSubclass);
169: javaSrc.runUserActionTask(task, true);
170: assertEquals(true, result[0]);
171: javaSrc = JavaSource
172: .forFileObject(secondLevelServletSubclass);
173: javaSrc.runUserActionTask(task, true);
174: assertEquals(true, result[0]);
175: } catch (IOException ex) {
176: ex.printStackTrace();
177: System.out.println("ex=" + ex);
178: throw new AssertionError(ex);
179: }
180: }
181:
182: }
|