001: package org.jzonic.webtester;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.io.IOException;
007: import java.util.Vector;
008:
009: import org.jzonic.webtester.commands.WebTestNodeResult;
010:
011: /**
012: * @author Mecky
013: */
014: public class WebTestFileReader {
015:
016: public WebTestFileReader() {
017: }
018:
019: public WebTestSuiteResult runSuite(File theDir, String[] names) {
020: WebTestSuite suite = new WebTestSuite("Test");
021: WebTestFileReader reader = new WebTestFileReader();
022: for (int i = 0; i < names.length; i++) {
023: if (names[i].endsWith(".test")) {
024: suite.addWebTest(reader.parse(theDir + "/" + names[i],
025: theDir.getPath()));
026: }
027: }
028: return suite.runTestSuite();
029: }
030:
031: private WebTester parseInclude(String line, String theDir,
032: WebTester tester, Vector used)
033: throws IllegalArgumentException {
034: int pos = line.indexOf(" ");
035: if (pos != -1 && pos < line.length()) {
036: String filename = line.substring(pos + 1).trim();
037: if (used.indexOf(theDir + "/" + filename) == -1) {
038: used.add(theDir + "/" + filename);
039: if ((theDir != null) && (theDir.length() > 1)) {
040: filename = theDir + "/" + filename;
041: }
042: File file = new File(filename);
043: if (file.exists()) {
044: try {
045: BufferedReader in = new BufferedReader(
046: new FileReader(filename));
047: String str;
048: while ((str = in.readLine()) != null) {
049: str = str.trim();
050: if (str.startsWith("@include")
051: && str.length() > 1) {
052: tester = parseInclude(str, theDir,
053: tester, used);
054: } else if (!str.startsWith("#")) {
055: tester.parseLine(str);
056: }
057: }
058: in.close();
059: } catch (IOException e) {
060: e.printStackTrace();
061: }
062: }
063: } else {
064: throw new IllegalArgumentException(
065: "Recursive include found in: " + theDir + "/"
066: + filename);
067: }
068: }
069: return tester;
070:
071: }
072:
073: private WebTestResult prepareFileNotFoundTestResult(String filename) {
074: return prepareErrorResult(filename, "File: " + filename
075: + " not found");
076: }
077:
078: private WebTestResult prepareRecursiveInclude(String filename,
079: String txt) {
080: return prepareErrorResult(filename, txt);
081: }
082:
083: private WebTestResult prepareErrorResult(String filename, String txt) {
084: WebTestResult result = new WebTestResult(filename);
085: result.setSuccess(false);
086: WebTestNodeResult node = new WebTestNodeResult("error");
087: node.setSuccess(false);
088: node.setErrorMessage(txt);
089: result.setErrorNode(node);
090: return result;
091: }
092:
093: public WebTestResult parseFile(String name) {
094: File file = new File(name);
095: if (file.exists()) {
096: try {
097: WebTester tester = parse(name);
098: return tester.runTest();
099: } catch (Exception e) {
100: return prepareRecursiveInclude(name, e.getMessage());
101: }
102: } else {
103: return prepareFileNotFoundTestResult(name);
104: }
105: }
106:
107: public WebTester parse(String name) {
108: return parse(name, "");
109: }
110:
111: public WebTester parse(String name, String theDir)
112: throws IllegalArgumentException {
113: Vector used = new Vector();
114: WebTester tester = new WebTester(name);
115: try {
116: BufferedReader in = new BufferedReader(new FileReader(name));
117: String str;
118: while ((str = in.readLine()) != null) {
119: str = str.trim();
120: if (str.startsWith("@include") && str.length() > 1) {
121: tester = parseInclude(str, theDir, tester, used);
122: } else if (!str.startsWith("#")) {
123: tester.parseLine(str);
124: }
125: }
126: in.close();
127: } catch (IOException e) {
128: e.printStackTrace();
129: }
130:
131: return tester;
132: }
133: }
|