KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > universe > TestEntry


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.modules.apisupport.project.universe;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.modules.apisupport.project.NbModuleProject;
32 import org.netbeans.modules.apisupport.project.Util;
33 import org.netbeans.spi.project.SubprojectProvider;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36
37 /**
38  * Representation of jarfile with tests
39  */

40 public final class TestEntry {
41     
42     private static final String JavaDoc JAR_NAME = "tests.jar"; // NOI18N
43
private static final String JavaDoc QA_FUNCTIONAL = "qa-functional"; // NOI18N
44
private static final String JavaDoc UNIT = "unit"; // NOI18N;
45
/** Hardcoded location of testdistribution relatively to nb cvs. */
46     private static final String JavaDoc TEST_DIST_DIR = "nbbuild/build/testdist"; // NOI18N;
47
private final String JavaDoc codeNameBase;
48     private final boolean unit;
49     private final String JavaDoc cluster;
50     private final File JavaDoc jarFile;
51     
52     /**
53      * Creates a new instance of TestEntry
54      */

55     private TestEntry(File JavaDoc jarFile,String JavaDoc codeNameBase,boolean unit,String JavaDoc cluster) {
56         this.jarFile = jarFile;
57         this.codeNameBase = codeNameBase;
58         this.unit = unit;
59         this.cluster = cluster;
60         
61     }
62     /**
63      * get TestEntry for jarfile with tests
64      *
65      * @param jarFile input file with tests
66      * @return null when the file is not jarfile with tests
67      */

68     public static TestEntry get(File JavaDoc jarFile) {
69         // testtype/cluster/codenamebase/testsjar
70
String JavaDoc path = jarFile.getPath().replace(File.separatorChar,'/');
71         if (path.endsWith(JAR_NAME)) {
72             String JavaDoc tokens[] = path.split("/");
73             int len = tokens.length;
74             if (len > 3 ) {
75                String JavaDoc cnb = tokens[len - 2].replace('-','.') ;
76                String JavaDoc cluster = tokens[len - 3];
77                String JavaDoc testType = tokens[len - 4];
78                boolean unit = true;
79                if (!testType.equals(UNIT)) {
80                    if (testType.equals(QA_FUNCTIONAL)) {
81                        unit = false;
82                    } else {
83                        return null;
84                    }
85                }
86                return new TestEntry(jarFile,cnb,unit,cluster);
87             }
88         }
89         return null;
90     }
91     
92     public String JavaDoc getCodeNameBase() {
93         return codeNameBase;
94     }
95
96     public boolean isUnit() {
97         return unit;
98     }
99
100     public String JavaDoc getCluster() {
101         // set default cluster for modules in module suite
102
return (cluster == null) ? "cluster" : cluster; // NOI18N
103
}
104
105     public File JavaDoc getJarFile() {
106         return jarFile;
107     }
108     
109     /** Get root folder of binary tests distribution.
110      */

111     public File JavaDoc getTestDistRoot() {
112         return getJarFile().getParentFile().getParentFile().getParentFile().getParentFile();
113     }
114     
115     /** Get source dir with tests.
116      * @return null if source dir was not located
117      */

118     public URL JavaDoc getSrcDir() throws IOException JavaDoc {
119         String JavaDoc nborgPath = getNetBeansOrgPath();
120         if (nborgPath != null) {
121             return new File JavaDoc(getNBCVSRoot(),nborgPath).toURI().toURL();
122         }
123         File JavaDoc prjDir = getTestDistRoot();
124         // find parent when dir was not created
125
while(!prjDir.exists()) {
126             prjDir = prjDir.getParentFile();
127             if (prjDir == null) {
128                 // parent doesn't exist
129
return null;
130             }
131         }
132         Project prj = FileOwnerQuery.getOwner(FileUtil.toFileObject(prjDir));
133         if (prj != null) {
134             // ModuleSuite
135
SubprojectProvider subprojects = (SubprojectProvider) prj.getLookup().lookup(SubprojectProvider.class);
136             if (subprojects != null) {
137                 Set JavaDoc<? extends Project> projects = subprojects.getSubprojects();
138                 for (Iterator JavaDoc it = projects.iterator() ; it.hasNext();) {
139                     Project p = (Project)it.next();
140                     if (p instanceof NbModuleProject) {
141                         NbModuleProject nbm = (NbModuleProject) p;
142                         if (nbm != null && nbm.getCodeNameBase().equals(getCodeNameBase())) {
143                             FileObject file = (isUnit()) ? nbm.getTestSourceDirectory() : nbm.getFunctionalTestSourceDirectory();
144                             if (file != null) {
145                                 return file.getURL();
146                             }
147                         }
148                     }
149                 }
150             }
151         }
152         return null;
153     }
154     
155     File JavaDoc getNBCVSRoot() {
156         File JavaDoc rootDir = getTestDistRoot();
157         String JavaDoc path = rootDir.getAbsolutePath().replace(File.separatorChar,'/');
158         File JavaDoc nbcvs = null;
159         // hardcoded location of testdistribution relatively to nb cvs
160
if (path.endsWith(TEST_DIST_DIR)) {
161             nbcvs = rootDir.getParentFile().getParentFile().getParentFile();
162         }
163         return nbcvs;
164     }
165     
166     public String JavaDoc getNetBeansOrgPath () throws IOException JavaDoc {
167         File JavaDoc nbcvs = getNBCVSRoot();
168         if (nbcvs != null && ModuleList.isNetBeansOrg(nbcvs) ) {
169             ModuleList list = ModuleList.getModuleList(new File JavaDoc(nbcvs,"core")); // NOI18N
170
ModuleEntry entry = list.getEntry(codeNameBase);
171             if (entry == null) {
172                 return null;
173             }
174             return entry.getNetBeansOrgPath() + "/test/" + getTestType() + "/src";
175         }
176         return null;
177     }
178
179     public String JavaDoc getTestType() {
180         return (isUnit()) ? UNIT : QA_FUNCTIONAL;
181     }
182
183     /**
184      * Get project for TestEntry
185      * @return null when project was not found
186      */

187     public Project getProject() {
188         try {
189             URL JavaDoc url = getSrcDir();
190             if (url != null) {
191                 URI JavaDoc uri = url.toURI();
192                 if (uri != null) {
193                     return FileOwnerQuery.getOwner(uri);
194                 }
195             }
196         } catch (IOException JavaDoc ex) {
197             Util.err.notify(ex);
198         } catch (URISyntaxException JavaDoc ex) {
199             Util.err.notify(ex);
200         }
201         return null;
202     }
203     
204 }
205
Popular Tags