1 18 package org.apache.batik.test.xml; 19 20 import java.io.File ; 21 22 import java.net.URL ; 23 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import java.util.StringTokenizer ; 27 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.DocumentBuilder ; 30 31 import org.w3c.dom.Document ; 32 33 import org.apache.batik.test.DefaultTestReport; 34 import org.apache.batik.test.DefaultTestSuite; 35 import org.apache.batik.test.AbstractTest; 36 import org.apache.batik.test.TestReport; 37 import org.apache.batik.test.TestSuiteReport; 38 39 68 public class XMLTestSuiteRunnerValidator extends DefaultTestSuite { 69 public static final String ERROR_TEST_NOT_RUN 70 = "error.test.not.run"; 71 72 public static final String ERROR_EXTRA_TEST_RUN 73 = "error.extra.test.run"; 74 75 public static final String ENTRY_KEY_CONFIGURATION 76 = "entry.key.configuration"; 77 78 public static final String ENTRY_KEY_EXPECTED_RESULT 79 = "entry.key.expected.result"; 80 81 public static final String ENTRY_KEY_ACTUAL_RESULT 82 = "entry.key.actual.result"; 83 84 public static final String ENTRY_KEY_TEST_IDS_NOT_RUN 85 = "entry.key.test.ids.not.run"; 86 87 public static final String ENTRY_KEY_TEST_ID_NOT_EXPECTED 88 = "entry.key.test.id.not.expected"; 89 90 93 static final String dummyTestRun = 94 "test-resources/org/apache/batik/test/xml/dummyTestRun.xml"; 95 96 118 public XMLTestSuiteRunnerValidator() { 119 Object config[][] = { 120 {"", new String []{"all", 121 "all.A", "all.A.A1", "all.A.A2", "all.A.duplicatedId", "all.A.duplicatedId.1", "all.A.duplicatedId.2", 122 "all.A.AG", "all.A.AG.AG1", "all.A.AG.AG2", 123 "all.B", "all.B.B1", "all.B.B2", "all.B.B3", "all.B.duplicatedId"}}, 124 125 {"all.B.B3", new String [] {"all", "all.B", "all.B.B3"}}, 126 127 {"all.A", new String [] {"all", 128 "all.A", "all.A.A1", "all.A.A2", "all.A.duplicatedId", "all.A.duplicatedId.1", "all.A.duplicatedId.2", 129 "all.A.AG", "all.A.AG.AG1", "all.A.AG.AG2"}}, 130 131 {"all.B all.A.A1 all.A.A2", 132 new String [] {"all", 133 "all.B", "all.B.B1", "all.B.B2", "all.B.B3", "all.B.duplicatedId", 134 "all.A", "all.A.A1", "all.A.A2"}}, 135 136 {"duplicatedId", 137 new String [] {"all", 138 "all.A", "all.A.duplicatedId", "all.A.duplicatedId.1", "all.A.duplicatedId.2", 139 "all.B", "all.B.duplicatedId"}}, 140 141 {"AG", 142 new String [] {"all", 143 "all.A", 144 "all.A.AG", 145 "all.A.AG.AG1", "all.A.AG.AG2"}} 146 }; 147 148 for(int i=0; i<config.length; i++){ 149 addTest(new XMLTestSuiteRunnerTest(config[i])); 150 } 151 152 } 153 154 static class XMLTestSuiteRunnerTest extends AbstractTest { 155 158 protected String [] args; 159 160 164 protected HashSet ids; 165 protected String [] idsArray; 166 167 173 public XMLTestSuiteRunnerTest(Object config[]){ 174 StringTokenizer st = new StringTokenizer ((String )config[0], " "); 175 int nArgs = st.countTokens(); 176 args = new String [nArgs]; 177 for(int i=0; i<nArgs; i++){ 178 args[i] = st.nextToken(); 179 } 180 181 ids = new HashSet (); 182 for(int i=0; i<(config[1] != null? ((Object [])config[1]).length:0); i++){ 183 ids.add(((Object [])config[1])[i]); 184 } 185 idsArray = (String [])config[1]; 186 } 187 188 191 public TestReport runImpl() throws Exception { 192 Document doc = loadDummyTestRun(); 196 197 XMLTestSuiteRunner runner 201 = new XMLTestSuiteRunner(); 202 203 TestReport runReport 204 = runner.run(doc, args); 205 206 Set idSet = (Set )(ids.clone()); 210 String idNotExpected = checkTestReport(runReport, idSet); 211 212 if(idNotExpected == null){ 213 if(idSet.isEmpty()){ 214 return reportSuccess(); 215 } 216 else{ 217 DefaultTestReport report = new DefaultTestReport(this); 218 report.setErrorCode(ERROR_TEST_NOT_RUN); 219 report.addDescriptionEntry(ENTRY_KEY_CONFIGURATION, 220 arrayToString(args)); 221 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_RESULT, 222 arrayToString(idsArray)); 223 report.addDescriptionEntry(ENTRY_KEY_ACTUAL_RESULT, 224 reportIdsToString(runReport)); 225 report.addDescriptionEntry(ENTRY_KEY_TEST_IDS_NOT_RUN, 226 arrayToString(idSet.toArray())); 227 report.setPassed(false); 228 return report; 229 } 230 } 231 else{ 232 DefaultTestReport report = new DefaultTestReport(this); 233 report.setErrorCode(ERROR_EXTRA_TEST_RUN); 234 report.addDescriptionEntry(ENTRY_KEY_CONFIGURATION, 235 arrayToString(args)); 236 report.addDescriptionEntry(ENTRY_KEY_EXPECTED_RESULT, 237 arrayToString(idsArray)); 238 report.addDescriptionEntry(ENTRY_KEY_ACTUAL_RESULT, 239 reportIdsToString(runReport)); 240 report.addDescriptionEntry(ENTRY_KEY_TEST_ID_NOT_EXPECTED, 241 idNotExpected); 242 report.setPassed(false); 243 return report; 244 } 245 } 246 247 protected String arrayToString(Object [] array){ 248 StringBuffer sb = new StringBuffer (); 249 if(array != null){ 250 if(array.length > 0){ 251 sb.append(array[0]); 252 } 253 for(int i=1; i<array.length; i++){ 254 sb.append(", "); 255 sb.append(array[i].toString()); 256 } 257 } 258 return sb.toString(); 259 } 260 261 protected String reportIdsToString(TestReport r){ 262 StringBuffer sb = new StringBuffer (); 263 if(r != null){ 264 sb.append(r.getTest().getQualifiedId()); 265 if(r instanceof TestSuiteReport){ 266 TestReport[] c = ((TestSuiteReport)r).getChildrenReports(); 267 if(c != null){ 268 for(int i=0; i<c.length; i++){ 269 appendReportIds(c[i], sb); 270 } 271 } 272 } 273 } 274 else{ 275 sb.append("null"); 276 } 277 278 return sb.toString(); 279 } 280 281 protected void appendReportIds(TestReport r, StringBuffer sb){ 282 if(r != null){ 283 sb.append(", "); 284 sb.append(r.getTest().getQualifiedId()); 285 286 if(r instanceof TestSuiteReport){ 287 TestReport[] c = ((TestSuiteReport)r).getChildrenReports(); 288 if(c != null){ 289 for(int i=0; i<c.length; i++){ 290 appendReportIds(c[i], sb); 291 } 292 } 293 } 294 } 295 } 296 297 300 protected Document loadDummyTestRun() throws Exception { 301 DocumentBuilder docBuilder 302 = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 303 304 URL url = (new File (XMLTestSuiteRunnerValidator.dummyTestRun)).toURL(); 305 return docBuilder.parse(url.toString()); 306 307 } 308 309 315 protected String checkTestReport(TestReport report, 316 Set idSet){ 317 String id = report.getTest().getQualifiedId(); 318 if(!(idSet.contains(id))){ 319 return id; 320 } 321 322 idSet.remove(id); 323 324 if(report instanceof TestSuiteReport){ 328 TestReport[] childReports = ((TestSuiteReport)report).getChildrenReports(); 329 if(childReports != null){ 330 for(int i=0; i<childReports.length; i++){ 331 String idNotExpected 332 = checkTestReport(childReports[i], 333 idSet); 334 if(idNotExpected != null){ 335 return idNotExpected; 336 } 337 } 338 } 339 } 340 341 return null; 342 } 343 344 } 345 346 347 } 348 | Popular Tags |