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: package org.netbeans.test.web;
042:
043: import java.io.File;
044: import java.io.FilenameFilter;
045: import java.lang.reflect.Constructor;
046: import java.util.Enumeration;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import junit.framework.Test;
050: import org.netbeans.api.project.ProjectInformation;
051: import org.netbeans.api.project.ProjectUtils;
052: import org.netbeans.junit.NbTestCase;
053: import org.netbeans.junit.NbTestSuite;
054: import org.netbeans.junit.ide.ProjectSupport;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.jellytools.Bundle;
057: import org.netbeans.jellytools.NbDialogOperator;
058: import org.netbeans.jellytools.ProjectsTabOperator;
059: import org.netbeans.jellytools.nodes.Node;
060: import org.netbeans.jemmy.operators.JComboBoxOperator;
061: import org.netbeans.jemmy.operators.JDialogOperator;
062: import org.netbeans.jemmy.operators.JTreeOperator;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileUtil;
065:
066: /**
067: *
068: * @author ms113234
069: */
070: public class RecurrentSuiteFactory {
071:
072: private static boolean debug = true;
073:
074: public static Test createSuite(Class clazz, File projectsDir,
075: FileObjectFilter filter) {
076: String clazzName = clazz.getName();
077: NbTestSuite suite = new NbTestSuite(clazzName);
078: try {
079: //get list of projects to be used for testing
080: File[] projects = projectsDir
081: .listFiles(new FilenameFilter() {
082: // filter out non-project folders
083: public boolean accept(File dir, String fileName) {
084: return !fileName.equals("CVS");
085: }
086: });
087: debug("RecurrentSuiteFactory");
088: debug("Projects dir: " + projectsDir);
089: if (projects != null) {
090: for (int i = 0; i < projects.length; i++) {
091: debug("Prj Folder: " + projects[i].getName());
092: Project project = (Project) ProjectSupport
093: .openProject(projects[i]);
094: // not a project
095: if (project == null) {
096: debug("WW: Not a project!!!");
097: continue;
098: }
099: resolveServer(projects[i].getName());
100: ProjectInformation projectInfo = ProjectUtils
101: .getInformation(project);
102: // find recursively all test.*[.jsp|.jspx|.jspf|.html] files in
103: // the web/ folder
104:
105: // TODO check if the project is of current version and if necessery update it.
106: // enables transparent update, see: org.netbeans.modules.web.project.UpdateHelper
107: // System.setProperty("webproject.transparentUpdate", "true");
108:
109: FileObject prjDir = project.getProjectDirectory();
110: Enumeration fileObjs = prjDir.getChildren(true);
111:
112: while (fileObjs.hasMoreElements()) {
113: FileObject fo = (FileObject) fileObjs
114: .nextElement();
115: if (filter.accept(fo)) {
116: String testName = projectInfo.getName()
117: + "_"
118: + FileUtil.getRelativePath(prjDir,
119: fo).replaceAll("[/.]", "_");
120: Constructor cnstr = clazz
121: .getDeclaredConstructor(new Class[] {
122: String.class,
123: FileObject.class });
124: NbTestCase test = (NbTestCase) cnstr
125: .newInstance(new Object[] {
126: testName, fo });
127: suite.addTest(test);
128: }
129: }
130: }
131: }
132: } catch (Exception ex) {
133: ex.printStackTrace(System.out);
134: }
135: return suite;
136: }
137:
138: private static void debug(Object msg) {
139: if (!debug) {
140: return;
141: }
142: System.err.println("[debug] " + msg);
143: }
144:
145: private static void resolveServer(String projectName) {
146: ProjectSupport.waitScanFinished();
147: Logger log = Logger.getLogger(RecurrentSuiteFactory.class
148: .getName());
149: String openProjectTitle = Bundle.getString(
150: "org.netbeans.modules.j2ee.common.ui.Bundle",
151: "MSG_Broken_Server_Title");
152: if (JDialogOperator.findJDialog(openProjectTitle, true, true) != null) {
153: new NbDialogOperator(openProjectTitle).close();
154: log.info("Resolving server");
155: // open project properties
156: ProjectsTabOperator.invoke()
157: .getProjectRootNode(projectName).properties();
158: // "Project Properties"
159: String projectPropertiesTitle = Bundle
160: .getStringTrimmed(
161: "org.netbeans.modules.web.project.ui.customizer.Bundle",
162: "LBL_Customizer_Title");
163: NbDialogOperator propertiesDialogOper = new NbDialogOperator(
164: projectPropertiesTitle);
165: // select "Run" category
166: new Node(new JTreeOperator(propertiesDialogOper), "Run")
167: .select();
168: // set default server
169: new JComboBoxOperator(propertiesDialogOper)
170: .setSelectedIndex(0);
171: propertiesDialogOper.ok();
172: // if setting default server, it scans server jars; otherwise it continues immediatelly
173: ProjectSupport.waitScanFinished();
174: }
175: String editPropertiesTitle = "Edit Project Properties";
176: while (JDialogOperator.findJDialog(editPropertiesTitle, true,
177: true) != null) {
178: new NbDialogOperator(editPropertiesTitle).cancel();
179: log.info("Closing buildscript regeneration");
180: try {
181: Thread.sleep(5000);
182: } catch (InterruptedException exc) {
183: log.log(Level.INFO, "interrupt exception", exc);
184: }
185: }
186: }
187: }
|