KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > web > RecurrentSuiteFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.test.web;
21
22 import java.io.File JavaDoc;
23 import java.io.FilenameFilter JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import junit.framework.Test;
27 import org.netbeans.api.project.ProjectInformation;
28 import org.netbeans.api.project.ProjectUtils;
29 import org.netbeans.junit.NbTestCase;
30 import org.netbeans.junit.NbTestSuite;
31 import org.netbeans.junit.ide.ProjectSupport;
32 import org.netbeans.api.project.Project;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35
36
37 /**
38  *
39  * @author ms113234
40  */

41 public class RecurrentSuiteFactory {
42     private static boolean debug = true;
43     
44     public static Test createSuite(Class JavaDoc clazz, File JavaDoc projectsDir, FileObjectFilter filter) {
45         String JavaDoc clazzName = clazz.getName();
46         NbTestSuite suite = new NbTestSuite(clazzName);
47         try {
48             //get list of projects to be used for testing
49
File JavaDoc[] projects = projectsDir.listFiles(new FilenameFilter JavaDoc() {
50                 // filter out non-project folders
51
public boolean accept(File JavaDoc dir, String JavaDoc fileName) {
52                     return !fileName.equals("CVS");
53                 }
54             });
55             debug("RecurrentSuiteFactory");
56             debug("Projects dir: " + projectsDir);
57             if (projects != null) {
58                 for(int i = 0; i < projects.length; i++) {
59                     debug("Prj Folder: " + projects[i].getName());
60                     Project project = (Project) ProjectSupport.openProject(projects[i]);
61                     // not a project
62
if (project == null) {
63                         debug("WW: Not a project!!!");
64                         continue;
65                     }
66                     ProjectInformation projectInfo = ProjectUtils.getInformation(project);
67                     // find recursively all test.*[.jsp|.jspx|.jspf|.html] files in
68
// the web/ folder
69

70                     // TODO check if the project is of current version and if necessery update it.
71
// enables transparent update, see: org.netbeans.modules.web.project.UpdateHelper
72
// System.setProperty("webproject.transparentUpdate", "true");
73

74                     FileObject prjDir = project.getProjectDirectory();
75                     Enumeration JavaDoc fileObjs = prjDir.getChildren(true);
76                     
77                     while (fileObjs.hasMoreElements()) {
78                         FileObject fo = (FileObject) fileObjs.nextElement();
79                         if (filter.accept(fo)) {
80                             String JavaDoc testName = projectInfo.getName() + "_"
81                                     + FileUtil.getRelativePath(prjDir, fo).replaceAll("[/.]", "_");
82                             Constructor JavaDoc cnstr = clazz.getDeclaredConstructor(new Class JavaDoc[] {String JavaDoc.class, FileObject.class});
83                             NbTestCase test = (NbTestCase) cnstr.newInstance(new Object JavaDoc[] {testName, fo});
84                             suite.addTest(test);
85                         }
86                     }
87                 }
88             }
89         } catch (Exception JavaDoc ex) {
90             ex.printStackTrace(System.out);
91         }
92         return suite;
93     }
94     
95     private static void debug(Object JavaDoc msg) {
96         if (!debug) return;
97         System.err.println("[debug] " + msg);
98     }
99 }
100
Popular Tags