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.test.web;
043:
044: import java.io.File;
045: import java.io.FilenameFilter;
046: import java.lang.reflect.Constructor;
047: import java.util.Enumeration;
048: import junit.framework.Test;
049: import org.netbeans.api.project.ProjectInformation;
050: import org.netbeans.api.project.ProjectUtils;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.junit.NbTestSuite;
053: import org.netbeans.junit.ide.ProjectSupport;
054: import org.netbeans.api.project.Project;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057:
058: /**
059: *
060: * @author ms113234
061: */
062: public class RecurrentSuiteFactory {
063: private static boolean debug = true;
064:
065: public static Test createSuite(Class clazz, File projectsDir,
066: FileObjectFilter filter) {
067: String clazzName = clazz.getName();
068: NbTestSuite suite = new NbTestSuite(clazzName);
069: try {
070: //get list of projects to be used for testing
071: File[] projects = projectsDir
072: .listFiles(new FilenameFilter() {
073: // filter out non-project folders
074: public boolean accept(File dir, String fileName) {
075: return !fileName.equals("CVS");
076: }
077: });
078: debug("RecurrentSuiteFactory");
079: debug("Projects dir: " + projectsDir);
080: if (projects != null) {
081: for (int i = 0; i < projects.length; i++) {
082: debug("Prj Folder: " + projects[i].getName());
083: Project project = (Project) ProjectSupport
084: .openProject(projects[i]);
085: // not a project
086: if (project == null) {
087: debug("WW: Not a project!!!");
088: continue;
089: }
090: ProjectInformation projectInfo = ProjectUtils
091: .getInformation(project);
092: // find recursively all test.*[.jsp|.jspx|.jspf|.html] files in
093: // the web/ folder
094:
095: // TODO check if the project is of current version and if necessery update it.
096: // enables transparent update, see: org.netbeans.modules.web.project.UpdateHelper
097: // System.setProperty("webproject.transparentUpdate", "true");
098:
099: FileObject prjDir = project.getProjectDirectory();
100: Enumeration fileObjs = prjDir.getChildren(true);
101:
102: while (fileObjs.hasMoreElements()) {
103: FileObject fo = (FileObject) fileObjs
104: .nextElement();
105: if (filter.accept(fo)) {
106: String testName = projectInfo.getName()
107: + "_"
108: + FileUtil.getRelativePath(prjDir,
109: fo).replaceAll("[/.]", "_");
110: Constructor cnstr = clazz
111: .getDeclaredConstructor(new Class[] {
112: String.class,
113: FileObject.class });
114: NbTestCase test = (NbTestCase) cnstr
115: .newInstance(new Object[] {
116: testName, fo });
117: suite.addTest(test);
118: }
119: }
120: }
121: }
122: } catch (Exception ex) {
123: ex.printStackTrace(System.out);
124: }
125: return suite;
126: }
127:
128: private static void debug(Object msg) {
129: if (!debug)
130: return;
131: System.err.println("[debug] " + msg);
132: }
133: }
|