1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileReader ; 26 import java.io.IOException ; 27 import java.io.PrintStream ; 28 import java.io.PrintWriter ; 29 import java.io.StringWriter ; 30 import java.util.ArrayList ; 31 import java.util.HashSet ; 32 import java.util.List ; 33 import java.util.Properties ; 34 import java.util.Set ; 35 import java.util.StringTokenizer ; 36 import java.util.TreeSet ; 37 import org.apache.tools.ant.BuildException; 38 import org.apache.tools.ant.Task; 39 40 42 46 public class FixTestDependencies extends Task { 47 49 Set <ModuleListParser.Entry> cachedEntries ; 50 51 52 public FixTestDependencies() { 53 } 54 55 56 String cnb; 57 58 File projectXmlFile; 59 public void setProjectXml(File projectXml) { 60 projectXmlFile = projectXml; 61 62 } 63 File propertiesFile; 64 public void setPropertiesFile(File properties) { 65 this.propertiesFile = properties; 66 } 67 public void execute() throws BuildException { 68 try { 70 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 prjFis = new FileInputStream (projectXmlFile); 77 try { 78 prjFis.read(xmlBytes); 79 } finally { 80 prjFis.close(); 81 } 82 String xml = new String (xmlBytes); 83 String oldXsd = "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/2"; 84 int xsdIndex = xml.indexOf(oldXsd); 85 if (xsdIndex != -1 || testFix) { 86 String part1 = xml.substring(0,xsdIndex + oldXsd.length() - 1); 88 String 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 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 if (xml.contains("<test-dependencies>") && !testFix) { 109 log("<test-dependencies> already exists."); 111 log("update only schema version"); 112 PrintStream ps = new PrintStream (projectXmlFile); 113 ps.print(xml); 114 ps.close(); 115 return ; 116 } 117 Set <ModuleListParser.Entry> entries = getModuleList(projectType); 118 Set <String > allCnbs = getCNBsFromEntries(entries); 119 121 123 Set <String > compileCNB = new TreeSet <String >(); 126 Set <String > compileTestCNB = new TreeSet <String >(); 127 Set <String > runtimeCNB = new TreeSet <String >(); 128 Set <String > runtimeTestCNB = new TreeSet <String >(); 129 130 Properties 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 []{"test.unit.cp","test.unit.cp.extra","test.unit.run.cp","test.unit.run.cp.extra"}); 136 137 StringWriter writer = new StringWriter (); 138 PrintWriter buffer = new PrintWriter (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.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 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 []{"test.qa-functional.cp","test.qa-functional.cp","test.qa-functional.runtime.cp","test.qa-functional.runtime.extra"}); 176 177 String 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 resultXml = new StringBuffer (); 185 resultXml.append(xml.substring(0,moduleDepEnd)); 186 resultXml.append(writer.toString()); 187 if (xml.charAt(moduleDepEnd) == '\r') { 189 moduleDepEnd++; 190 } 191 resultXml.append(xml.substring(moduleDepEnd + 1, xml.length())); 192 if (!testFix) { 193 PrintStream ps = new PrintStream (projectXmlFile); 194 ps.print(resultXml); 195 ps.close(); 196 } else { 197 System.out.println(resultXml); 198 } 199 } 200 201 } catch (IOException ex) { 202 throw new BuildException(ex); 203 } 204 205 } 207 208 private Set <ModuleListParser.Entry> getModuleList(final int projectType) throws IOException { 209 if (cachedEntries == null ) { 210 @SuppressWarnings ("unchecked") 212 ModuleListParser listParser = new ModuleListParser(getProject().getProperties(), projectType, getProject()); 213 return listParser.findAll(); 214 } else { 215 return cachedEntries; 217 } 218 } 219 220 private Set <String > getCNBsFromEntries(Set <ModuleListParser.Entry> entries) { 221 Set <String > cnbs = new HashSet <String >(); 222 for (ModuleListParser.Entry e : entries) { 223 cnbs.add(e.getCnb()); 224 } 225 return cnbs; 226 } 227 229 void readCodeNameBases(Set <String > compileCNB, 230 Set <String > testsCNB, 231 Properties projectPropertis, 232 String property, 233 Set <String > allCnbs, 234 Set <ModuleListParser.Entry> entries) { 235 String prop = projectPropertis.getProperty(property); 236 StringBuffer newProp = new StringBuffer (); 237 if (prop != null) { 238 StringTokenizer tokenizer = new StringTokenizer (prop,";:\\"); 239 while(tokenizer.hasMoreTokens()) { 240 String token = tokenizer.nextToken().trim(); 241 boolean found = false; 242 if (token.length() > 1) { 243 int lastSlash = token.lastIndexOf("/"); 244 int lastDot = token.lastIndexOf("."); 245 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 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 name = token.substring(lastSlash + 1, token.length()); 262 String wrapCNB = null; 264 for (ModuleListParser.Entry entry : entries) { 265 File extensions [] = entry.getClassPathExtensions(); 266 if (extensions != null) { 267 for (File f : extensions) { 268 if (f.getPath().endsWith( name)) { 269 if (wrapCNB != null) { 270 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 } else { 288 int prjEnd = token.indexOf("/build/test/unit/class"); 289 if (prjEnd != -1) { 290 int firstSlash = token.indexOf("/"); 292 if (firstSlash != -1 && firstSlash + 1 < prjEnd) { 293 String prjFolder = token.substring(firstSlash + 1, prjEnd); 294 String 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 if (found == false) { 307 if (newProp.length() > 0) { 308 newProp.append(":"); 309 } 310 token = token.replace(File.separatorChar,'/'); 312 newProp.append(token); 313 } 314 } 315 } projectPropertis.setProperty(property,newProp.toString()); 317 } 318 } 319 320 private void addDependencies(PrintWriter buffer, Set <String > moduleCNB, Set <String > testCNB, boolean compile, boolean recursive) { 321 for (String cnb : moduleCNB) { 322 addDependency(buffer,cnb,compile,recursive,testCNB.contains(cnb)); 323 } 324 } 325 326 private void addDependency(PrintWriter buffer, String 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 getTestProperties() throws IOException { 343 if (propertiesFile == null || !propertiesFile.isFile()) { 344 throw new BuildException("Property file doesn't exist"); 345 } 346 Properties props = new Properties (); 347 FileInputStream fis = new FileInputStream (propertiesFile); 348 try { 349 props.load(fis); 350 } finally { 351 fis.close(); 352 } 353 return props; 354 } 355 356 358 private String getCNBForFolder(String prjFolder, Set <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 projectProperties,String names[]) { 367 try { 368 369 BufferedReader reader = new BufferedReader (new FileReader (propertiesFile)); 371 List <String > lines = new ArrayList <String >(); 372 String line = null; 373 while ((line = reader.readLine()) != null) { 374 lines.add(line); 375 } 376 reader.close(); 377 378 for (String propName : names) { 380 String value = projectProperties.getProperty(propName); 381 lines = replaceProperty(propName,value,lines); 382 } 383 PrintStream ps = new PrintStream (propertiesFile); 385 for (String l : lines) { 386 ps.println(l); 387 } 388 ps.close(); 389 390 } catch (IOException ioe) { 391 throw new BuildException(ioe); 392 } 393 394 } 395 396 private List <String > replaceProperty(String name, String value, List <String > lines) { 397 List <String > retLines = new ArrayList <String >(); 398 for (int i = 0 ; i < lines.size() ; i++) { 399 String line = lines.get(i); 400 String trimmedLine = line.trim(); 401 int eqIdx = trimmedLine.indexOf("="); 402 if (eqIdx != -1) { 403 String pName = line.substring(0,eqIdx).trim(); 404 if (pName.equals(name)) { 405 for (; i < lines.size() && lines.get(i).trim().endsWith("\\") ; i++) ; 407 if (value != null && !value.trim().equals("")) { 409 retLines.add(name + "=" + value); 410 } 411 continue; 412 } 413 } 414 retLines.add(line); 416 } 417 return retLines; 418 } 419 420 } 421 | Popular Tags |