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-2008 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.jspparser;
043:
044: import java.io.File;
045: import java.io.FileNotFoundException;
046: import java.io.FileOutputStream;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.util.Enumeration;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import java.util.zip.ZipEntry;
053: import java.util.zip.ZipFile;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.project.ProjectManager;
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.modules.java.project.JavaAntLogger;
058: import org.netbeans.modules.web.api.webmodule.WebModule;
059: import org.netbeans.modules.web.spi.webmodule.WebModuleFactory;
060: import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.modules.ModuleInfo;
064: import org.openide.util.Lookup;
065:
066: /**
067: *
068: * @author pj97932, Tomas Mysik
069: */
070: final class TestUtil {
071:
072: private TestUtil() {
073: }
074:
075: static void setup(NbTestCase test) throws Exception {
076:
077: test.clearWorkDir();
078:
079: File javaCluster = new File(JavaAntLogger.class
080: .getProtectionDomain().getCodeSource().getLocation()
081: .toURI()).getParentFile().getParentFile();
082: File enterCluster = new File(WebModule.class
083: .getProtectionDomain().getCodeSource().getLocation()
084: .toURI()).getParentFile().getParentFile();
085: System.setProperty("netbeans.dirs", javaCluster.getPath()
086: + File.pathSeparator + enterCluster.getPath());
087:
088: Logger.getLogger("org.netbeans.core.startup.ModuleList")
089: .setLevel(Level.OFF);
090:
091: Logger.getLogger("org.netbeans.modules.web.jspparser_ext")
092: .setLevel(Level.FINE);
093:
094: // module system
095: Lookup.getDefault().lookup(ModuleInfo.class);
096:
097: // unzip test project
098: TestUtil.getProject(test, "project3");
099: }
100:
101: static FileObject getFileInWorkDir(String path, NbTestCase test)
102: throws Exception {
103: File f = test.getDataDir();
104: FileObject workDirFO = FileUtil.toFileObject(f);
105: return FileUtil.createData(workDirFO, path);
106: }
107:
108: static WebModule getWebModule(FileObject fo) {
109: WebModule wm = WebModule.getWebModule(fo);
110: if (wm == null) {
111: return null;
112: }
113: FileObject wmRoot = wm.getDocumentBase();
114: if (fo == wmRoot || FileUtil.isParentOf(wmRoot, fo)) {
115: return WebModule.getWebModule(fo);
116: }
117: return null;
118: }
119:
120: static Project getProject(NbTestCase test, String projectFolderName)
121: throws Exception {
122: File f = getProjectAsFile(test, projectFolderName);
123: FileObject projectPath = FileUtil.toFileObject(f);
124: Project project = ProjectManager.getDefault().findProject(
125: projectPath);
126: NbTestCase.assertNotNull("Project should exist", project);
127: return project;
128: }
129:
130: static FileObject getProjectFile(NbTestCase test,
131: String projectFolderName, String filePath) throws Exception {
132: Project project = getProject(test, projectFolderName);
133: FileObject fo = project.getProjectDirectory().getFileObject(
134: filePath);
135: NbTestCase.assertNotNull("Project file should exist: "
136: + filePath, fo);
137:
138: return fo;
139: }
140:
141: static WebModule createWebModule(FileObject documentRoot) {
142: WebModuleImplementation webModuleImpl = new WebModuleImpl(
143: documentRoot);
144: return WebModuleFactory.createWebModule(webModuleImpl);
145: }
146:
147: static void copyFolder(FileObject source, FileObject dest)
148: throws IOException {
149: for (FileObject child : source.getChildren()) {
150: if (child.isFolder()) {
151: FileObject created = FileUtil.createFolder(dest, child
152: .getNameExt());
153: copyFolder(child, created);
154: } else {
155: FileUtil.copyFile(child, dest, child.getName(), child
156: .getExt());
157: }
158: }
159: }
160:
161: static File getProjectAsFile(NbTestCase test,
162: String projectFolderName) throws Exception {
163: File f = new File(test.getDataDir(), projectFolderName);
164: if (!f.exists()) {
165: // maybe it's zipped
166: File archive = new File(test.getDataDir(),
167: projectFolderName + ".zip");
168: unZip(archive, test.getDataDir());
169: }
170: NbTestCase.assertTrue("project directory has to exists: " + f,
171: f.exists());
172: return f;
173: }
174:
175: private static void unZip(File archive, File destination)
176: throws Exception {
177: if (!archive.exists()) {
178: throw new FileNotFoundException(archive
179: + " does not exist.");
180: }
181: ZipFile zipFile = new ZipFile(archive);
182: Enumeration<? extends ZipEntry> all = zipFile.entries();
183: while (all.hasMoreElements()) {
184: extractFile(zipFile, all.nextElement(), destination);
185: }
186: }
187:
188: private static void extractFile(ZipFile zipFile, ZipEntry e,
189: File destination) throws IOException {
190: String zipName = e.getName();
191: if (zipName.startsWith("/")) {
192: zipName = zipName.substring(1);
193: }
194: if (zipName.endsWith("/")) {
195: return;
196: }
197: int ix = zipName.lastIndexOf('/');
198: if (ix > 0) {
199: String dirName = zipName.substring(0, ix);
200: File d = new File(destination, dirName);
201: if (!(d.exists() && d.isDirectory())) {
202: if (!d.mkdirs()) {
203: NbTestCase.fail("Warning: unable to mkdir "
204: + dirName);
205: }
206: }
207: }
208: FileOutputStream os = new FileOutputStream(destination
209: .getAbsolutePath()
210: + "/" + zipName);
211: InputStream is = zipFile.getInputStream(e);
212: int n = 0;
213: byte[] buff = new byte[8192];
214: while ((n = is.read(buff)) > 0) {
215: os.write(buff, 0, n);
216: }
217: is.close();
218: os.close();
219: }
220: }
|