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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.web.jspparser;
041:
042: import java.io.File;
043: import java.util.Map;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046: import org.netbeans.api.java.classpath.ClassPath;
047: import org.netbeans.api.java.project.classpath.ProjectClassPathModifier;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.api.project.libraries.Library;
051: import org.netbeans.api.project.libraries.LibraryManager;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.modules.java.project.JavaAntLogger;
054: import org.netbeans.modules.web.api.webmodule.WebModule;
055: import org.netbeans.modules.web.jsps.parserapi.JspParserAPI;
056: import org.netbeans.modules.web.jsps.parserapi.JspParserFactory;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059: import org.openide.modules.ModuleInfo;
060: import org.openide.util.Lookup;
061:
062: /**
063: * Tests that need "full" IDE can be placed here.
064: * @author Tomas Mysik
065: */
066: public class IdeEnvironmentTest extends NbTestCase {
067:
068: public IdeEnvironmentTest(String testName) {
069: super (testName);
070: }
071:
072: @Override
073: protected void setUp() throws Exception {
074: super .setUp();
075:
076: clearWorkDir();
077:
078: File userdir = new File(getWorkDir(), "userdir");
079: FileUtil.createFolder(userdir);
080: System.setProperty("netbeans.user", userdir.getPath());
081:
082: File platformCluster = new File(Lookup.class
083: .getProtectionDomain().getCodeSource().getLocation()
084: .toURI()).getParentFile().getParentFile();
085: File ideCluster = new File(ProjectManager.class
086: .getProtectionDomain().getCodeSource().getLocation()
087: .toURI()).getParentFile().getParentFile();
088: File javaCluster = new File(JavaAntLogger.class
089: .getProtectionDomain().getCodeSource().getLocation()
090: .toURI()).getParentFile().getParentFile();
091: File enterCluster = new File(WebModule.class
092: .getProtectionDomain().getCodeSource().getLocation()
093: .toURI()).getParentFile().getParentFile();
094: System.setProperty("netbeans.home", platformCluster.getPath());
095: System.setProperty("netbeans.dirs", javaCluster.getPath()
096: + File.pathSeparator + enterCluster.getPath()
097: + File.pathSeparator + ideCluster.getPath());
098:
099: Logger.getLogger("org.netbeans.core.startup.ModuleList")
100: .setLevel(Level.OFF);
101:
102: // module system
103: Lookup.getDefault().lookup(ModuleInfo.class);
104: }
105:
106: // test for issue #70426
107: public void testGetTagLibMap70426() throws Exception {
108: // first make sure that the library is not present
109: removeLibrary("emptyWebProject", "jstl11");
110:
111: FileObject jspFo = TestUtil.getProjectFile(this ,
112: "emptyWebProject", "web/index.jsp");
113: WebModule wm = TestUtil.getWebModule(jspFo);
114: Map library = JspParserFactory.getJspParser().getTaglibMap(wm);
115: assertNull("The JSTL library should not be present.", library
116: .get("http://java.sun.com/jsp/jstl/fmt"));
117:
118: addLibrary("emptyWebProject", "jstl11");
119:
120: library = JspParserFactory.getJspParser().getTaglibMap(wm);
121: assertNotNull("The JSTL library should be present.", library
122: .get("http://java.sun.com/jsp/jstl/fmt"));
123:
124: // cleanup
125: removeLibrary("emptyWebProject", "jstl11");
126: }
127:
128: public void testAddedJarFile() throws Exception {
129: JspParserAPI jspParser = JspParserFactory.getJspParser();
130:
131: FileObject jspFo = TestUtil.getProjectFile(this ,
132: "emptyWebProject", "/web/index.jsp");
133: WebModule webModule = TestUtil.getWebModule(jspFo);
134:
135: Map<String, String[]> taglibMap1 = jspParser
136: .getTaglibMap(webModule);
137:
138: // add library
139: addLibrary("emptyWebProject", "junit");
140:
141: Map<String, String[]> taglibMap2 = jspParser
142: .getTaglibMap(webModule);
143:
144: String url1 = taglibMap1.get("http://java.sun.com/jstl/core")[0];
145: String url2 = taglibMap2.get("http://java.sun.com/jstl/core")[0];
146: assertNotNull(url1);
147: assertNotNull(url2);
148: assertNotSame("TagLibMaps should not be exactly the same",
149: url1, url2);
150: assertEquals("TagLibMaps should be equal", url1, url2);
151:
152: // cleanup
153: jspParser = null;
154: removeLibrary("emptyWebProject", "junit");
155: }
156:
157: public void testRemovedJarFile() throws Exception {
158: // init
159: addLibrary("emptyWebProject", "junit");
160:
161: JspParserAPI jspParser = JspParserFactory.getJspParser();
162:
163: FileObject jspFo = TestUtil.getProjectFile(this ,
164: "emptyWebProject", "/web/index.jsp");
165: WebModule webModule = TestUtil.getWebModule(jspFo);
166:
167: Map<String, String[]> taglibMap1 = jspParser
168: .getTaglibMap(webModule);
169:
170: // remove library
171: removeLibrary("emptyWebProject", "junit");
172:
173: Map<String, String[]> taglibMap2 = jspParser
174: .getTaglibMap(webModule);
175:
176: String url1 = taglibMap1.get("http://java.sun.com/jstl/core")[0];
177: String url2 = taglibMap2.get("http://java.sun.com/jstl/core")[0];
178: assertNotNull(url1);
179: assertNotNull(url2);
180: assertNotSame("TagLibMaps should not be exactly the same",
181: url1, url2);
182: assertEquals("TagLibMaps should be equal", url1, url2);
183: removeLibrary("emptyWebProject", "jstl11");
184: }
185:
186: private void removeLibrary(String projectFolderName,
187: String libraryName) throws Exception {
188: Library library = LibraryManager.getDefault().getLibrary(
189: libraryName);
190: assertNotNull("Library has to be found", library);
191: Project project = TestUtil.getProject(this , projectFolderName);
192: FileObject srcJava = project.getProjectDirectory()
193: .getFileObject("src/java");
194: ProjectClassPathModifier.removeLibraries(
195: new Library[] { library }, srcJava, ClassPath.COMPILE);
196: }
197:
198: private void addLibrary(String projectFolderName, String libraryName)
199: throws Exception {
200: Library library = LibraryManager.getDefault().getLibrary(
201: libraryName);
202: assertNotNull("Library has to be found", library);
203: Project project = TestUtil.getProject(this , projectFolderName);
204: FileObject srcJava = project.getProjectDirectory()
205: .getFileObject("src/java");
206: boolean added = ProjectClassPathModifier.addLibraries(
207: new Library[] { library }, srcJava, ClassPath.COMPILE);
208: assertTrue("Library should be added to the class path", added);
209: }
210: }
|