1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.StringReader ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.Collections ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Properties ; 33 import java.util.Set ; 34 import java.util.StringTokenizer ; 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 import org.apache.tools.ant.*; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.NodeList ; 41 import org.xml.sax.EntityResolver ; 42 import org.xml.sax.InputSource ; 43 import org.xml.sax.SAXException ; 44 45 65 public class TestDistFilter extends Task { 66 public static final String TYPE_ALL = "all"; 67 public static final String TYPE_UNIT = "unit"; 68 public static final String TYPE_QA_FUNCTIONAL = "qa-functional"; 69 70 public static final String HARNESS_JUNIT = "junit"; 71 public static final String HARNESS_XTEST = "xtest"; 72 73 private File testDistDir; 74 Set <TestConf> possibleTests = new HashSet <TestConf>(); 75 private String testtype = TYPE_ALL; 78 private String harness = HARNESS_XTEST; 79 private String attribs ; 81 private String testListProperty; 82 private String requiredModules; 83 86 88 private static class TestConf { 89 File moduleDir; 90 boolean unit; 91 TestConf(File moduleDir,boolean unit) { 92 this.moduleDir = moduleDir; 93 this.unit = unit; 94 } 95 96 public int hashCode() { 97 return moduleDir.hashCode(); 98 } 99 public boolean equals(Object obj) { 100 return (obj instanceof TestConf) && moduleDir.equals(((TestConf)obj).moduleDir); 101 } 102 103 104 107 boolean matchAttribs(String harness,String attribs) { 108 Element config; 109 try { 110 config = getConfig(); 111 } catch (SAXException ex) { 112 throw new BuildException("Error in parsing " + getConfigFile(),ex); 113 } catch (ParserConfigurationException ex) { 114 throw new BuildException("Error in parsing " + getConfigFile(),ex); 115 } catch (IOException ex) { 116 throw new BuildException("Error in parsing " + getConfigFile(),ex); 117 } 118 if (config == null) { 119 return false; 120 } 121 boolean junit = HARNESS_JUNIT.equals(harness); 122 NodeList elements = config.getElementsByTagName("testbag"); 123 for (int n = 0 ; n < elements.getLength() ; n++) { 124 Element testbag = (Element ) elements.item(n); 125 if (junit && "ide".equals(testbag.getAttribute("executor"))) { 126 continue; 127 } 128 if (testAttr(testbag.getAttribute("testattribs"),attribs)) { 129 return true; 130 } 131 } 132 return false; 133 } 134 private static boolean testAttr(String xmlAttr,String userAttr) { 135 if (userAttr == null) { 136 return true; 137 } 138 if (xmlAttr == null) { 139 return false; 140 } 141 StringTokenizer tokenizer = new StringTokenizer (xmlAttr,"&|, "); 142 while (tokenizer.hasMoreTokens()) { 143 String token = tokenizer.nextToken().trim(); 144 if (token.equals(userAttr)) { 145 return true; 146 } 147 } 148 return false; 149 } 150 File getModuleDir() { 151 return moduleDir; 152 } 153 154 private File getConfigFile () { 155 String name = (unit) ? "cfg-unit.xml" : "cfg-qa-functional.xml"; 156 return new File (getModuleDir(),name); 157 } 158 private Element getConfig() throws ParserConfigurationException , SAXException , IOException { 159 File xml = getConfigFile(); 160 if (!xml.exists()) { 161 return null; 162 } 163 return getDocumentBuilder().parse(xml).getDocumentElement(); 164 } 165 166 } 167 168 169 public void execute() throws BuildException { 170 possibleTests.clear(); 171 if (getTestListProperty() == null) { 172 throw new BuildException("Param testlistproperty is not defined."); 173 } 174 if (getTestDistDir() == null || !getTestDistDir().exists()) { 175 throw new BuildException("Param testdistdir is not defined."); 176 } 177 if ("".equals(attribs)) { 178 attribs = null; 179 } 180 String tt = getTesttype(); 181 if (getHarness().equals(HARNESS_JUNIT)) { 182 findCodeTests(HARNESS_JUNIT,TYPE_UNIT,getAttribs()); 183 } else { 184 if (tt.equals(TYPE_QA_FUNCTIONAL) || tt.equals(TYPE_ALL)) { 185 findCodeTests(HARNESS_XTEST,TYPE_QA_FUNCTIONAL,getAttribs()); 186 } 187 if (tt.equals(TYPE_UNIT) || tt.equals(TYPE_ALL)) { 188 findCodeTests(HARNESS_XTEST,TYPE_UNIT,getAttribs()); 189 } 190 } 191 define(getTestListProperty(),getTestList()); 192 } 193 195 private String getTestList() { 196 StringBuffer path = new StringBuffer (); 197 for (Iterator it = possibleTests.iterator() ; it.hasNext() ; ) { 198 TestConf tc = (TestConf)it.next(); 199 if (!matchRequiredModule(tc.getModuleDir())) { 200 continue; 201 } 202 if (path.length() > 0) { 203 path.append(':'); 204 } 205 path.append(tc.getModuleDir().getAbsolutePath()); 206 } 207 return path.toString(); 208 } 209 private void define(String prop, String val) { 210 log("Setting " + prop + "=" + val, Project.MSG_VERBOSE); 211 String old = getProject().getProperty(prop); 212 if (old != null && !old.equals(val)) { 213 getProject().log("Warning: " + prop + " was already set to " + old, Project.MSG_WARN); 214 } 215 getProject().setNewProperty(prop, val); 216 } 217 218 219 public String getTesttype() { 220 return testtype; 221 } 222 223 public void setTesttype(String testtype) { 224 this.testtype = testtype; 225 } 226 227 public String getHarness() { 228 return harness; 229 } 230 231 public void setHarness(String harness) { 232 this.harness = harness; 233 } 234 235 public String getAttribs() { 236 return attribs; 237 } 238 239 public void setAttribs(String attribs) { 240 this.attribs = attribs; 241 } 242 243 public String getTestListProperty() { 244 return testListProperty; 245 } 246 247 public void setTestListProperty(String testListProperty) { 248 this.testListProperty = testListProperty; 249 } 250 251 private void findCodeTests(String harness, String type, String string) { 252 List tests = getTestList(type); 253 for (int i = 0 ; i < tests.size() ; i++) { 254 TestConf test = (TestConf)tests.get(i); 255 if (test.matchAttribs(harness,attribs)) { 256 possibleTests.add(test); 257 } 258 } 259 } 260 261 private List getTestList(String testtype) { 262 File root = new File (getTestDistDir(),testtype); 263 List <TestConf> testList = new ArrayList <TestConf>(); 264 if (!root.exists()) { 265 return Collections.EMPTY_LIST; 266 } 267 File clusters[] = root.listFiles(); 268 for (int c = 0 ; c < clusters.length ; c++) { 269 File cluster = clusters[c]; 270 if (cluster.isDirectory()) { 271 File modules[] = cluster.listFiles(); 272 for (int m = 0 ; m < modules.length ; m++) { 273 File module = modules[m]; 274 if (module.isDirectory()) { 275 testList.add(new TestConf(module,testtype.equals(TYPE_UNIT))); 276 } 277 } 278 } 279 } 280 return testList; 281 } 282 283 284 private static DocumentBuilder db; 287 private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { 288 if (db == null) { 289 db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 290 db.setEntityResolver(new EntityResolver () { 291 public InputSource resolveEntity(String publicId, String systemId) throws SAXException ,IOException { 292 return new InputSource (new StringReader ("")); 293 } 294 295 }); 296 } 297 return db; 298 } 299 300 public File getTestDistDir() { 301 return testDistDir; 302 } 303 304 public void setTestDistDir(File testDistDir) { 305 this.testDistDir = testDistDir; 306 } 307 308 public String getRequiredModules() { 309 return requiredModules; 310 } 311 312 public void setRequiredModules(String requiredModules) { 313 this.requiredModules = requiredModules; 314 } 315 316 private boolean matchRequiredModule(File path) { 317 if (requiredModules == null || requiredModules.trim().length() == 0) { 318 return true; 319 } 320 File pfile = new File (path,"test.properties"); 321 if (pfile.exists()) { 322 Properties props = new Properties (); 323 try { 324 FileInputStream fis = new FileInputStream (pfile); 325 try { 326 props.load(fis); 327 328 String runCp = props.getProperty("test.unit.run.cp"); 329 if (runCp != null) { 330 String paths[] = runCp.split(":"); 331 Set reqModules = getRequiredModulesSet(); 332 if (reqModules.size() == 0) { 333 return true; 334 } 335 for (int i = 0 ; i < paths.length ; i++) { 336 String p = paths[i]; 337 int lastSlash = p.lastIndexOf('/'); 338 if (lastSlash != -1) { 339 p = p.substring(lastSlash + 1); 340 } 341 if (reqModules.contains(p)) { 342 return true; 343 } 344 } 345 } 346 } finally { 347 fis.close(); 348 } 349 } catch(IOException ioe){ 350 throw new BuildException(ioe); 351 } 352 } 353 return false; 354 } 355 356 private Set <String > getRequiredModulesSet() { 357 String names[] = getRequiredModules().split(","); 358 return new HashSet <String >(Arrays.asList(names)); 359 } 360 } 361 | Popular Tags |