01: package test.failures;
02:
03: import java.io.BufferedReader;
04: import java.io.File;
05: import java.io.FileNotFoundException;
06: import java.io.FileReader;
07: import java.io.IOException;
08: import java.util.regex.Pattern;
09:
10: import org.testng.Assert;
11: import org.testng.TestNG;
12: import org.testng.reporters.FailedReporter;
13:
14: public class BaseFailuresTest {
15:
16: // protected TestNG run(Class[] classes, String outputDir) {
17: // return run(new TestNG(), classes, outputDir);
18: // }
19:
20: protected String getSuiteName() {
21: return "TmpSuite";
22: }
23:
24: protected TestNG run(TestNG result, Class[] classes,
25: String outputDir) {
26: result.setVerbose(0);
27: result.setOutputDirectory(outputDir);
28: result.setTestClasses(classes);
29: result.run();
30:
31: return result;
32: }
33:
34: /**
35: * @param f
36: * @param regexps
37: * @return true if the file contains at least one occurrence of each regexp
38: */
39: protected boolean containsRegularExpressions(File f,
40: String[] strRegexps) {
41: Pattern[] matchers = new Pattern[strRegexps.length];
42: boolean[] results = new boolean[strRegexps.length];
43: for (int i = 0; i < strRegexps.length; i++) {
44: matchers[i] = Pattern.compile(".*" + strRegexps[i] + ".*");
45: results[i] = false;
46: }
47:
48: try {
49: FileReader fr = new FileReader(f);
50: BufferedReader br = new BufferedReader(fr);
51: String line = br.readLine();
52: while (line != null) {
53: for (int i = 0; i < strRegexps.length; i++) {
54: if (matchers[i].matcher(line).matches())
55: results[i] = true;
56: }
57: line = br.readLine();
58: }
59: fr.close();
60: br.close();
61: } catch (FileNotFoundException e) {
62: e.printStackTrace();
63: return false;
64: } catch (IOException e) {
65: e.printStackTrace();
66: return false;
67: }
68:
69: for (int i = 0; i < results.length; i++) {
70: boolean result = results[i];
71: if (!result)
72: throw new AssertionError("Couldn't find "
73: + strRegexps[i]);
74: }
75:
76: return true;
77: }
78:
79: protected void verify(String outputDir, String[] expected) {
80: File f = new File(outputDir + File.separatorChar
81: + getSuiteName() + File.separator
82: + FailedReporter.TESTNG_FAILED_XML);
83: boolean passed = containsRegularExpressions(f, expected);
84: Assert.assertTrue(passed);
85:
86: File tmpDir = new File(outputDir);
87: tmpDir.delete();
88: }
89:
90: }
|