KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > FixTestDependencies


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.nbbuild;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.PrintStream JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.TreeSet JavaDoc;
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.Task;
39
40 // XXX should use DOM, not text manipulation
41

42 /**
43  * Moves classpath from properties to project.xml
44  * @author pzajac
45  */

46 public class FixTestDependencies extends Task {
47     /** entries for unit testing in order to avoid scanning modules
48      */

49     Set JavaDoc<ModuleListParser.Entry> cachedEntries ;
50     
51     /** Creates a new instance of FixTestClassPath */
52     public FixTestDependencies() {
53     }
54     
55     
56     String JavaDoc cnb;
57     
58     File JavaDoc projectXmlFile;
59     public void setProjectXml(File JavaDoc projectXml) {
60         projectXmlFile = projectXml;
61         
62     }
63     File JavaDoc propertiesFile;
64     public void setPropertiesFile(File JavaDoc properties) {
65         this.propertiesFile = properties;
66     }
67     public void execute() throws BuildException {
68         // read project xml
69
try {
70             // test mode doesn't override project.xml
71
boolean testFix = getProject().getProperty("test.fix.dependencies") != null;
72             if (projectXmlFile == null || !projectXmlFile.isFile()) {
73                 throw new BuildException("project.xml file doesn't exist.");
74             }
75             byte xmlBytes [] = new byte[(int)projectXmlFile.length()];
76             FileInputStream JavaDoc prjFis = new FileInputStream JavaDoc(projectXmlFile);
77             try {
78                 prjFis.read(xmlBytes);
79             } finally {
80                 prjFis.close();
81             }
82             String JavaDoc xml = new String JavaDoc(xmlBytes);
83             String JavaDoc oldXsd = "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2";
84             int xsdIndex = xml.indexOf(oldXsd);
85             if (xsdIndex != -1 || testFix) {
86                 // increase schema version
87
String JavaDoc part1 = xml.substring(0,xsdIndex + oldXsd.length() - 1);
88                 String JavaDoc part2 = xml.substring(xsdIndex + oldXsd.length(), xml.length());
89                 xml = part1 + "3" + part2;
90                 
91                 int projectType = ParseProjectXml.TYPE_NB_ORG;
92                 if (xml.contains("<suite-component/>")) {
93                     projectType = ParseProjectXml.TYPE_SUITE;
94                 } else if (xml.contains("<standalone/>")) {
95                     projectType = ParseProjectXml.TYPE_STANDALONE;
96                 }
97                 //grrr
98
int typeStart = xml.indexOf("<code-name-base>");
99                 int typeEnd = xml.indexOf("</code-name-base>");
100                 if (typeStart <= 0 || typeEnd <= 0 || typeEnd <= typeStart) {
101                     throw new BuildException("Parsing of project.xml failed.");
102                 }
103                 cnb = xml.substring(typeStart + "<code-name-base>".length(), typeEnd).trim();
104                 if (cnb.length() <= 0) {
105                     throw new BuildException("Invalid codename base:" + cnb);
106                 }
107                 // test if project.xml contains test-deps
108
if (xml.contains("<test-dependencies>") && !testFix) {
109                     // yes -> exit
110
log("<test-dependencies> already exists.");
111                     log("update only schema version");
112                     PrintStream JavaDoc ps = new PrintStream JavaDoc(projectXmlFile);
113                     ps.print(xml);
114                     ps.close();
115                     return ;
116                 }
117                 Set JavaDoc<ModuleListParser.Entry> entries = getModuleList(projectType);
118                 Set JavaDoc<String JavaDoc> allCnbs = getCNBsFromEntries(entries);
119                 // read properties
120

121                 // remove modules and test from properties and put it to project.xml
122

123                 // unittest
124
//
125
Set JavaDoc<String JavaDoc> compileCNB = new TreeSet JavaDoc<String JavaDoc>();
126                 Set JavaDoc<String JavaDoc> compileTestCNB = new TreeSet JavaDoc<String JavaDoc>();
127                 Set JavaDoc<String JavaDoc> runtimeCNB = new TreeSet JavaDoc<String JavaDoc>();
128                 Set JavaDoc<String JavaDoc> runtimeTestCNB = new TreeSet JavaDoc<String JavaDoc>();
129
130                 Properties JavaDoc projectProperties = getTestProperties();
131                 readCodeNameBases(compileCNB,compileTestCNB,projectProperties,"test.unit.cp",allCnbs,entries);
132                 readCodeNameBases(compileCNB,compileTestCNB,projectProperties,"test.unit.cp.extra",allCnbs,entries);
133                 readCodeNameBases(runtimeCNB,runtimeTestCNB,projectProperties,"test.unit.run.cp",allCnbs,entries);
134                 readCodeNameBases(runtimeCNB,runtimeTestCNB,projectProperties,"test.unit.run.cp.extra",allCnbs,entries);
135                 updateProperties(projectProperties,new String JavaDoc[]{"test.unit.cp","test.unit.cp.extra","test.unit.run.cp","test.unit.run.cp.extra"});
136
137                 StringWriter JavaDoc writer = new StringWriter JavaDoc();
138                 PrintWriter JavaDoc buffer = new PrintWriter JavaDoc(writer);
139                 buffer.println("");
140                 buffer.println(" <test-dependencies>");
141                 buffer.println(" <test-type>");
142                 buffer.println(" <name>unit</name>");
143                 addDependency(buffer,cnb,true,true,false);
144
145                 runtimeCNB.removeAll(compileCNB);
146                 //compileCNB.removeAll(runtimeCNB);
147
compileCNB.addAll(compileTestCNB);
148                 runtimeTestCNB.removeAll(compileTestCNB);
149                 runtimeCNB.addAll(runtimeTestCNB);
150                 addDependencies(buffer,compileCNB,compileTestCNB,true,false);
151                 addDependencies(buffer,runtimeCNB,runtimeTestCNB,false,false);
152                 buffer.println(" </test-type>");
153
154                 // qa functional tests
155
compileCNB.clear();
156                 runtimeCNB.clear();
157                 compileTestCNB.clear();
158                 runtimeTestCNB.clear();
159
160                 readCodeNameBases(compileCNB,compileTestCNB,projectProperties,"test.qa-functional.cp",allCnbs,entries);
161                 readCodeNameBases(compileCNB,compileTestCNB,projectProperties,"test.qa-functional.cp.extra",allCnbs,entries);
162
163                 readCodeNameBases(runtimeCNB,runtimeTestCNB,projectProperties,"test.qa-functional.runtime.cp",allCnbs,entries);
164                 readCodeNameBases(runtimeCNB,runtimeTestCNB,projectProperties,"test.qa-functional.runtime.extra",allCnbs,entries);
165                 if (!compileTestCNB.isEmpty() || !compileCNB.isEmpty() || !runtimeTestCNB.isEmpty() || !runtimeCNB.isEmpty()) {
166                     buffer.println(" <test-type>");
167                     buffer.println(" <name>qa-functional</name>");
168
169                     addDependencies(buffer,compileCNB,compileTestCNB,true,false);
170                     addDependencies(buffer,runtimeCNB,runtimeTestCNB,false,false);
171                     buffer.println(" </test-type>");
172                 }
173                 
174                 buffer.println(" </test-dependencies>");
175                 updateProperties(projectProperties,new String JavaDoc[]{"test.qa-functional.cp","test.qa-functional.cp","test.qa-functional.runtime.cp","test.qa-functional.runtime.extra"});
176
177                 // merge project properties
178
String JavaDoc MODULE_DEP_END = "</module-dependencies>";
179                 int moduleDepEnd = xml.indexOf(MODULE_DEP_END);
180                 if (moduleDepEnd == -1) {
181                     throw new BuildException("No module dependency found.");
182                 }
183                 moduleDepEnd += MODULE_DEP_END.length();
184                 StringBuffer JavaDoc resultXml = new StringBuffer JavaDoc();
185                 resultXml.append(xml.substring(0,moduleDepEnd));
186                 resultXml.append(writer.toString());
187                 // windows
188
if (xml.charAt(moduleDepEnd) == '\r') {
189                     moduleDepEnd++;
190                 }
191                 resultXml.append(xml.substring(moduleDepEnd + 1, xml.length()));
192                 if (!testFix) {
193                    PrintStream JavaDoc ps = new PrintStream JavaDoc(projectXmlFile);
194                    ps.print(resultXml);
195                    ps.close();
196                 } else {
197                     System.out.println(resultXml);
198                 }
199             }
200             
201         } catch (IOException JavaDoc ex) {
202             throw new BuildException(ex);
203         }
204         
205         // store project.properties and project.xml
206
}
207
208     private Set JavaDoc<ModuleListParser.Entry> getModuleList(final int projectType) throws IOException JavaDoc {
209         if (cachedEntries == null ) {
210           // scan for all modules
211
@SuppressWarnings JavaDoc("unchecked")
212             ModuleListParser listParser = new ModuleListParser(getProject().getProperties(), projectType, getProject());
213             return listParser.findAll();
214         } else {
215             // used by FixTestDependenciesTest
216
return cachedEntries;
217         }
218     }
219     
220     private Set JavaDoc<String JavaDoc> getCNBsFromEntries(Set JavaDoc<ModuleListParser.Entry> entries) {
221         Set JavaDoc<String JavaDoc> cnbs = new HashSet JavaDoc<String JavaDoc>();
222         for (ModuleListParser.Entry e : entries) {
223             cnbs.add(e.getCnb());
224         }
225         return cnbs;
226     }
227     /** parses all codenamebases from path
228      */

229      void readCodeNameBases(Set JavaDoc<String JavaDoc> compileCNB,
230             Set JavaDoc <String JavaDoc> testsCNB,
231             Properties JavaDoc projectPropertis,
232             String JavaDoc property,
233             Set JavaDoc <String JavaDoc> allCnbs,
234             Set JavaDoc <ModuleListParser.Entry> entries) {
235         String JavaDoc prop = projectPropertis.getProperty(property);
236         StringBuffer JavaDoc newProp = new StringBuffer JavaDoc();
237         if (prop != null) {
238             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(prop,";:\\");
239             while(tokenizer.hasMoreTokens()) {
240                 String JavaDoc token = tokenizer.nextToken().trim();
241                 boolean found = false;
242                 if (token.length() > 1) {
243                     int lastSlash = token.lastIndexOf("/");
244                     int lastDot = token.lastIndexOf(".");
245                     // check if the file is module
246
//
247
// the boot.jar is org.netbeans.bootstrap
248
if (token.endsWith("lib/boot.jar")) {
249                          compileCNB.add("org.netbeans.bootstrap");
250                          found = true;
251                     } else if (token.endsWith("core/core.jar")) {
252                         compileCNB.add("org.netbeans.core.startup");
253                         found = true;
254                     } else if (lastSlash != -1 && lastDot != -1 && lastSlash + 1< lastDot ) {
255                         String JavaDoc codeBaseName = token.substring(lastSlash + 1, lastDot);
256                         codeBaseName = codeBaseName.replace('-','.');
257                         if (allCnbs.contains(codeBaseName)) {
258                             compileCNB.add(codeBaseName);
259                             found = true;
260                         } else {
261                             String JavaDoc name = token.substring(lastSlash + 1, token.length());
262                             // check if the file is wrapped library
263
String JavaDoc wrapCNB = null;
264                             for (ModuleListParser.Entry entry : entries) {
265                                   File JavaDoc extensions [] = entry.getClassPathExtensions();
266                                   if (extensions != null) {
267                                       for (File JavaDoc f : extensions) {
268                                           if (f.getPath().endsWith( name)) {
269                                               if (wrapCNB != null) {
270                                                 // collision
271
found = false;
272                                                   System.out.println("wrapped? " + entry.getCnb() + " -> " + token + " = " + f);
273                                               } else {
274                                                   wrapCNB = entry.getCnb();
275                                                   found = true;
276                                               }
277                                           }
278                                       }
279                                   }
280
281                             }
282                             if (found && wrapCNB != null && allCnbs.contains(wrapCNB)) {
283                                   compileCNB.add(wrapCNB);
284                             }
285                          }
286                         // check if the dependency is dependency on test
287
} else {
288                         int prjEnd = token.indexOf("/build/test/unit/class");
289                         if (prjEnd != -1) {
290                             // get the project folder
291
int firstSlash = token.indexOf("/");
292                             if (firstSlash != -1 && firstSlash + 1 < prjEnd) {
293                                 String JavaDoc prjFolder = token.substring(firstSlash + 1, prjEnd);
294                                 String JavaDoc codebaseName = getCNBForFolder(prjFolder,entries);
295                                 if (codebaseName == null) {
296                                     log("No code name base found for file " + token);
297                                 } else {
298                                     testsCNB.add(codebaseName);
299                                     found = true;
300                                 }
301                             }
302                         }
303                     }
304                     // check if the file is file
305
//
306
if (found == false) {
307                         if (newProp.length() > 0) {
308                             newProp.append(":");
309                         }
310                         // windows platform
311
token = token.replace(File.separatorChar,'/');
312                         newProp.append(token);
313                     }
314                 }
315             } // while
316
projectPropertis.setProperty(property,newProp.toString());
317         }
318     }
319     
320     private void addDependencies(PrintWriter JavaDoc buffer, Set JavaDoc<String JavaDoc> moduleCNB, Set JavaDoc<String JavaDoc> testCNB, boolean compile, boolean recursive) {
321         for (String JavaDoc cnb : moduleCNB) {
322             addDependency(buffer,cnb,compile,recursive,testCNB.contains(cnb));
323         }
324     }
325     
326     private void addDependency(PrintWriter JavaDoc buffer, String JavaDoc cnb, boolean compile, boolean recursive, boolean test) {
327         buffer.println(" <test-dependency>");
328         buffer.println(" <code-name-base>" + cnb + "</code-name-base>");
329         if (recursive) {
330             buffer.println(" <recursive/>");
331         }
332         if (compile) {
333             buffer.println(" <compile-dependency/>");
334         }
335         if (test) {
336             buffer.println(" <test/>");
337         }
338         buffer.println(" </test-dependency>");
339         
340     }
341     
342     private Properties JavaDoc getTestProperties() throws IOException JavaDoc {
343         if (propertiesFile == null || !propertiesFile.isFile()) {
344             throw new BuildException("Property file doesn't exist");
345         }
346         Properties JavaDoc props = new Properties JavaDoc();
347         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(propertiesFile);
348         try {
349             props.load(fis);
350         } finally {
351             fis.close();
352         }
353         return props;
354     }
355     
356     /** @return codeNameBase of project for relative folder prjFolder
357      */

358     private String JavaDoc getCNBForFolder(String JavaDoc prjFolder, Set JavaDoc<ModuleListParser.Entry> entries) {
359         for (ModuleListParser.Entry elem : entries) {
360             if (prjFolder.equals(elem.getNetbeansOrgPath())) {
361                 return elem.getCnb();
362             }
363         }
364         return null;
365     }
366     private void updateProperties(Properties JavaDoc projectProperties,String JavaDoc names[]) {
367         try {
368             
369             // read properties
370
BufferedReader JavaDoc reader = new BufferedReader JavaDoc (new FileReader JavaDoc(propertiesFile));
371             List JavaDoc<String JavaDoc> lines = new ArrayList JavaDoc<String JavaDoc>();
372             String JavaDoc line = null;
373             while ((line = reader.readLine()) != null) {
374                 lines.add(line);
375             }
376             reader.close();
377             
378             // merge properties
379
for (String JavaDoc propName : names) {
380                String JavaDoc value = projectProperties.getProperty(propName);
381                lines = replaceProperty(propName,value,lines);
382             }
383             // store properties
384
PrintStream JavaDoc ps = new PrintStream JavaDoc(propertiesFile);
385             for (String JavaDoc l : lines) {
386                 ps.println(l);
387             }
388             ps.close();
389             
390         } catch (IOException JavaDoc ioe) {
391             throw new BuildException(ioe);
392         }
393         
394     }
395
396     private List JavaDoc<String JavaDoc> replaceProperty(String JavaDoc name, String JavaDoc value, List JavaDoc<String JavaDoc> lines) {
397         List JavaDoc<String JavaDoc> retLines = new ArrayList JavaDoc<String JavaDoc>();
398         for (int i = 0 ; i < lines.size() ; i++) {
399             String JavaDoc line = lines.get(i);
400             String JavaDoc trimmedLine = line.trim();
401             int eqIdx = trimmedLine.indexOf("=");
402             if (eqIdx != -1) {
403                 String JavaDoc pName = line.substring(0,eqIdx).trim();
404                 if (pName.equals(name)) {
405                     // skip property
406
for (; i < lines.size() && lines.get(i).trim().endsWith("\\") ; i++) ;
407                     // append new property
408
if (value != null && !value.trim().equals("")) {
409                         retLines.add(name + "=" + value);
410                     }
411                     continue;
412                 }
413             }
414             // either empty line, comment or other property
415
retLines.add(line);
416         }
417         return retLines;
418     }
419
420 }
421
Popular Tags