01: package org.jzonic.webtester;
02:
03: import java.io.File;
04: import org.jzonic.webtester.writer.WebTestResultConsoleWriter;
05: import org.jzonic.webtester.writer.WebTestResultFileWriter;
06: import org.jzonic.webtester.writer.WebTestResultWriter;
07:
08: /**
09: * Command line tool to run either a single test or a set of
10: * testcases. It will also save the results using a defined writer.
11: *
12: * @author Mecky
13: */
14: public class WebTestSuiteRunner {
15:
16: public static void main(String[] args) {
17: if (args.length == 0) {
18: printUsage();
19: System.exit(0);
20: }
21: WebTestSuiteRunner runner = new WebTestSuiteRunner();
22: runner.process(args);
23: }
24:
25: /**
26: * @param args
27: */
28: private void process(String[] args) {
29: String html = getArgument("-h", args);
30: String output = getArgument("-o", args);
31: String file = getArgument("-f", args);
32: if (file != null) {
33: WebTestFileReader reader = new WebTestFileReader();
34: WebTestResult result = reader.parseFile(file);
35: WebTestResultWriter writer = null;
36: if (output == null) {
37: writer = new WebTestResultConsoleWriter();
38: } else {
39: writer = new WebTestResultFileWriter(output);
40: }
41: writer.generateResult(result);
42: }
43: String dir = getArgument("-d", args);
44: if (dir != null) {
45: File theDir = new File(dir);
46: if (theDir.isDirectory()) {
47: String[] files = theDir.list();
48: WebTestFileReader reader = new WebTestFileReader();
49: WebTestSuiteResult results = reader.runSuite(theDir,
50: files);
51: WebTestResultWriter writer = null;
52: if (output == null) {
53: writer = new WebTestResultConsoleWriter();
54: } else {
55: writer = new WebTestResultFileWriter(output);
56: }
57: writer.generateResult(results);
58: } else {
59: System.out.println(dir + " is not a directory");
60: printUsage();
61: }
62: }
63: }
64:
65: private String getArgument(String arg, String[] args) {
66: String ret = null;
67: int pos = -1;
68: for (int i = 0; i < args.length; i++) {
69: if (args[i].equals(arg)) {
70: pos = i;
71: }
72: }
73: if (pos != -1 && args.length >= pos + 1) {
74: ret = args[pos + 1];
75: }
76: return ret;
77: }
78:
79: /**
80: *
81: */
82: private static void printUsage() {
83: System.out.println("Usage:");
84: System.out.println("WebTestSuiteRunner -[dfoh] xxx");
85: System.out
86: .println("-d directoryname: will read in every file in the directory and add the file as tests to the suite");
87: System.out
88: .println("-f filename: will read in a file and add the file as tests to the suite");
89: System.out
90: .println("-o filename: writes the result to the defined file");
91: }
92: }
|