001: package test.util;
002:
003: import java.io.BufferedReader;
004: import java.io.BufferedWriter;
005: import java.io.File;
006: import java.io.FileNotFoundException;
007: import java.io.FileReader;
008: import java.io.FileWriter;
009: import java.io.IOException;
010: import java.io.StringReader;
011:
012: import junit.extensions.TestSetup;
013: import junit.framework.Test;
014:
015: import org.apache.cactus.JspTestCase;
016:
017: /**
018: * Utility class which enables it to quickly write tests based on the execution of a JSP. Since we know the expected
019: * result, we can fetch this result and store it into a file, and then compare the next executions of the test against
020: * this file.
021: *
022: * @author Manuel Gay
023: * @version $Id: MakumbaJSPTest.java,v 1.1 25.09.2007 16:08:26 Manuel Exp $
024: */
025: public class MakumbaJspTestCase extends JspTestCase {
026:
027: private static final class Suite extends TestSetup {
028: private Suite(Test arg0) {
029: super (arg0);
030: }
031: }
032:
033: /**
034: * Compares a test output to its stored (expected) result. The method detects automatically the name of the test
035: * based on the invoking method, so all there's to do is to pass it the new result. TODO this should be much more
036: * verbose
037: *
038: * @param result
039: * the new result, from the currently running test
040: * @return <code>true</code> if this worked out, <code>false</code> otherwise.
041: * @throws FileNotFoundException in case the comparison basis file is not found, this indicates it
042: */
043: protected boolean compareTest(String result) throws Exception {
044:
045: boolean testOk = true;
046:
047: // first we retrieve the name of the method which calls us
048: String testName = new Throwable().fillInStackTrace()
049: .getStackTrace()[1].getMethodName();
050:
051: // based on the method name, we retrieve the file used as comparison basis
052:
053: File f = new File("classes/test/expected/" + testName + ".txt");
054:
055: if (!f.exists())
056: throw new Exception(
057: "Couldn't find the comparison file in classes/test/expected/"
058: + testName
059: + ".txt - create it first using the fetchValidTestResult(String result) method!");
060:
061: String fileIntoString = "";
062: BufferedReader fileIn = null;
063: BufferedReader stringIn = null;
064:
065: try {
066: fileIn = new BufferedReader(new FileReader(f));
067: stringIn = new BufferedReader(new StringReader(result));
068: String strFile = "";
069: String strStr = "";
070:
071: while ((strFile = fileIn.readLine()) != null) {
072: fileIntoString += strFile + "\n";
073: strStr = stringIn.readLine();
074: testOk = strFile.equals(strStr);
075: }
076:
077: } catch (IOException e) {
078: // TODO Auto-generated catch block
079: e.printStackTrace();
080: } finally {
081: try {
082: fileIn.close();
083: } catch (IOException e) {
084: // TODO Auto-generated catch block
085: e.printStackTrace();
086: }
087: }
088:
089: if (!testOk) {
090: System.out.println("************************ Test "
091: + testName + " failed! ************************");
092: System.out
093: .println("======================== Expected ========================");
094: System.out.println(fileIntoString);
095: System.out
096: .println("======================== Actual ========================");
097: System.out.println(result);
098:
099: }
100:
101: return testOk;
102: }
103:
104: /**
105: * Method that helps to fetch the result of a test, on the first run. Just pass it the expected result, it will
106: * store it automatically. Don't forget to remove it after the first time!
107: *
108: * @param output
109: * the result (HTML code) of the page that was ran correctly.
110: * @param record TODO
111: */
112: protected void fetchValidTestResult(String output, boolean record) {
113:
114: if (!record)
115: return;
116:
117: // first we retrieve the name of the method which calls us
118: String testName = new Throwable().fillInStackTrace()
119: .getStackTrace()[1].getMethodName();
120:
121: File f = new File("classes/test/expected/" + testName + ".txt");
122: if (!f.exists())
123: try {
124: f.createNewFile();
125: } catch (IOException e1) {
126: // TODO Auto-generated catch block
127: e1.printStackTrace();
128: }
129:
130: try {
131: BufferedWriter out = new BufferedWriter(new FileWriter(f));
132: out.write(output);
133: out.close();
134: } catch (IOException e) {
135: e.printStackTrace();
136: }
137: }
138: }
|