01: package test;
02:
03: import java.io.File;
04: import java.util.ArrayList;
05: import java.util.List;
06: import java.util.Random;
07:
08: import org.testng.TestNG;
09: import org.testng.xml.XmlClass;
10: import org.testng.xml.XmlSuite;
11: import org.testng.xml.XmlTest;
12:
13: public class TestHelper {
14:
15: public static XmlSuite createSuite(String cls, String suiteName) {
16: XmlSuite result = new XmlSuite();
17: result.setName(suiteName);
18:
19: XmlTest test = new XmlTest(result);
20: test.setName("TmpTest");
21: List classes = new ArrayList();
22: classes.add(new XmlClass(cls));
23: test.setClassNames(classes);
24:
25: return result;
26: }
27:
28: public static TestNG createTestNG(String outputDir) {
29: return createTestNG(null, outputDir);
30: }
31:
32: public static TestNG createTestNG() {
33: return createTestNG(null, null);
34: }
35:
36: public static TestNG createTestNG(XmlSuite suite) {
37: return createTestNG(suite, System.getProperty("java.io.tmpdir"));
38: }
39:
40: public static TestNG createTestNG(XmlSuite suite, String outputDir) {
41: TestNG result = new TestNG();
42: if (suite != null) {
43: List suites = new ArrayList();
44: suites.add(suite);
45: result.setXmlSuites(suites);
46: }
47: if (outputDir == null) {
48: outputDir = createRandomDirectory().getAbsolutePath();
49: }
50: result.setOutputDirectory(outputDir);
51: result.setVerbose(0);
52:
53: return result;
54: }
55:
56: public static File createRandomDirectory() {
57: String dir = System.getProperty("java.io.tmpdir");
58: String name = "testng-tmp-"
59: + new Random(System.currentTimeMillis()).nextInt();
60: File result = new File(dir + File.separatorChar + name);
61: result.deleteOnExit();
62: result.mkdirs();
63:
64: return result;
65: }
66:
67: private static void ppp(String string) {
68: System.out.println("[TestHelper] " + string);
69: }
70:
71: }
|