Global/jala.Test.js
Summary
Fields and methods of the jala.Test class.
|
Class Summary
|
| jala.Test |
Provides various methods for automated tests. |
| jala.Test.ArgumentsException |
Instances of this exception are thrown whenever an assertion
function is called with incorrect or insufficient arguments
|
| jala.Test.DatabaseMgr |
Instances of this class allow managing test databases
and switching a running application to an in-memory test
database to use within a unit test. |
| jala.Test.EvaluatorException |
Instances of this exception are thrown when attempt
to evaluate the test code fails
|
| jala.Test.Exception |
Base exception class
|
| jala.Test.HttpClient |
Instances of this class represent a http client useable for
testing, as any session cookies received by the tested application
are stored and used for subsequent requests, allowing simple "walkthrough"
tests. |
| jala.Test.TestException |
Instances of this exception are thrown whenever an
assertion function fails
|
| jala.Test.TestFunctionResult |
Instances of this class represent the result of the successful
execution of a single test function (failed executions will be represented
as Exceptions in the log of a test result). |
| jala.Test.TestResult |
Instances of this class represent the result of the execution
of a single test file
|
if (!global.jala) {
global.jala = {};
}
app.addRepository("modules/helma/Http.js");
app.addRepository("modules/jala/code/Database.js");
jala.Test = function() {
this.testsRun = 0;
this.testsFailed = 0;
this.functionsPassed = 0;
this.functionsFailed = 0;
this.results = [];
return this;
};
jala.Test.PASSED = "passed";
jala.Test.FAILED = "failed";
jala.Test.valueToString = function(val) {
res.push();
if (val === null) {
res.write("null");
} else if (val === undefined) {
res.write("undefined");
} else {
if (val.constructor && val.constructor == String) {
res.write('"' + val.toString() + '"');
} else {
res.write(val.toString());
}
res.write(" (");
if (val.constructor && val.constructor.name != null) {
res.write(val.constructor.name);
} else {
res.write(typeof(val));
}
res.write(")");
}
return res.pop();
};
jala.Test.getTestsDirectory = function() {
var dir;
if (getProperty("jala.testDir") != null) {
dir = new helma.File(getProperty("jala.testDir"));
}
if (!dir || !dir.exists()) {
var appDir = new helma.File(app.dir);
dir = new helma.File(appDir.getParent(), "tests");
if (!dir.exists())
return null;
}
return dir;
};
jala.Test.getTestFiles = function() {
var dir;
if ((dir = jala.Test.getTestsDirectory()) != null) {
return dir.list(/.*\.js/).sort();
}
return null;
};
jala.Test.getTestFile = function(fileName) {
var dir = jala.Test.getTestsDirectory();
if (dir != null) {
return new helma.File(dir, fileName);
}
return null;
};
jala.Test.evalArguments = function(args, argsExpected) {
if (!(args.length == argsExpected ||
(args.length == argsExpected + 1 && typeof(args[0]) == "string"))) {
throw new jala.Test.ArgumentsException("Insufficient arguments passed to assertion function");
}
return;
};
jala.Test.argsContainComment = function(args, argsExpected) {
return !(args.length == argsExpected
|| (args.length == argsExpected + 1 && typeof(args[0]) != "string"))
};
jala.Test.getComment = function(args, argsExpected) {
if (jala.Test.argsContainComment(args, argsExpected))
return args[0];
return null;
};
jala.Test.getValue = function(args, argsExpected, idx) {
return jala.Test.argsContainComment(args, argsExpected) ? args[idx+1] : args[idx];
};
jala.Test.getStackTrace = function(message) {
var ex = new Packages.org.mozilla.javascript.EvaluatorException(message || "");
ex.fillInStackTrace();
var trace = ex.getStackTrace();
var el, fileName, lineNumber;
var stack = [];
for (var i=0;i<trace.length;i++) {
el = trace[i];
if (!(fileName = el.getFileName()))
continue;
if ((lineNumber = el.getLineNumber()) == -1)
continue;
if (fileName.endsWith(".js") || fileName.endsWith(".hac") || fileName.endsWith(".hsp")) {
if (fileName.endsWith("jala.Test.js")) {
continue;
}
stack[stack.length] = "at " + fileName + ", line " + lineNumber;
if (fileName.endsWith(res.meta.currentTest)) {
break;
}
}
}
return stack.join("\n");
};
jala.Test.getTestScope = function() {
var engine = Packages.helma.scripting.rhino.RhinoEngine.getRhinoEngine();
var scope = new Packages.helma.scripting.rhino.GlobalObject(engine.getCore(),
app.__app__, true);
var framework = Packages.helma.framework;
scope.root = root;
scope.session = session;
scope.req = req;
scope.res = res;
scope.path = path;
for (var i in jala.Test.prototype) {
if (i.indexOf("assert") == 0) {
scope[i] = jala.Test.prototype[i];
}
}
scope.httpClient = new jala.Test.HttpClient();
scope.testDatabases = new jala.Test.DatabaseMgr();
return scope;
}
jala.Test.Exception = function Exception() {
return this;
};
jala.Test.Exception.prototype.toString = function() {
return "jala.Test.Exception: " + this.message + "]";
};
jala.Test.TestException = function TestException(comment, message) {
this.functionName = null;
this.comment = comment;
this.message = message;
this.stackTrace = jala.Test.getStackTrace(message);
return this;
};
jala.Test.TestException.prototype = new jala.Test.Exception();
jala.Test.TestException.prototype.toString = function() {
return "jala.Test.TestException in " + this.functionName +
": " + this.message;
};
jala.Test.ArgumentsException = function ArgumentsException(message) {
this.functionName = null;
this.message = message;
this.stackTrace = jala.Test.getStackTrace(message);
return this;
};
jala.Test.ArgumentsException.prototype = new jala.Test.Exception();
jala.Test.ArgumentsException.prototype.toString = function() {
return "jala.Test.ArgumentsException in " + this.functionName +
": " + this.message;
};
jala.Test.EvaluatorException = function EvaluatorException(message) {
this.message = message;
return this;
};
jala.Test.EvaluatorException.prototype = new jala.Test.Exception();
jala.Test.EvaluatorException.prototype.toString = function() {
return "jala.Test.EvaluatorException: " + this.message;
};
jala.Test.TestResult = function(testFileName) {
this.name = testFileName;
this.elapsed = 0;
this.status = jala.Test.PASSED;
this.log = [];
return this;
};
jala.Test.TestFunctionResult = function(functionName, startTime) {
this.functionName = functionName;
this.elapsed = (new Date()) - startTime;
return this;
};
jala.Test.prototype.executeTest = function(testFile) {
var testFileName = testFile.getName();
res.meta.currentTest = testFileName;
var cx = Packages.org.mozilla.javascript.Context.enter();
var code = new java.lang.String(testFile.readAll() || "");
var testResult = new jala.Test.TestResult(testFileName);
try {
var scope = jala.Test.getTestScope(scope);
cx.evaluateString(scope, code, testFileName, 1, null);
if (!scope.tests || scope.tests.constructor != Array || scope.tests.length == 0) {
throw "Please define an Array named 'tests' containing the names of the test functions to run";
}
var start = new Date();
try {
if (scope.setup != null && scope.setup instanceof Function) {
testResult.log[testResult.log.length] = this.executeTestFunction("setup", scope);
}
var functionName;
for (var i=0;i<scope.tests.length;i++) {
functionName = scope.tests[i];
if (!scope[functionName] || scope[functionName].constructor != Function) {
throw new jala.Test.EvaluatorException("Test function '" +
functionName + "' is not defined.");
}
testResult.log[testResult.log.length] = this.executeTestFunction(functionName, scope);
}
} catch (e) {
this.testsFailed += 1;
testResult.status = jala.Test.FAILED;
testResult.log[testResult.log.length] = e;
} finally {
if (scope.cleanup != null && scope.cleanup instanceof Function) {
testResult.log[testResult.log.length] = this.executeTestFunction("cleanup", scope);
}
}
} catch (e) {
this.testsFailed += 1;
testResult.status = jala.Test.FAILED;
testResult.log[testResult.log.length] = new jala.Test.EvaluatorException(e.toString());
} finally {
cx.exit();
res.meta.currentTest = null;
}
testResult.elapsed = (new Date()) - start;
this.results[this.results.length] = testResult;
return;
};
jala.Test.prototype.executeTestFunction = function(functionName, scope) {
res.meta.currentTestFunction = functionName;
var start = new Date();
try {
scope[functionName]();
this.functionsPassed += 1;
return new jala.Test.TestFunctionResult(functionName, start);
} catch (e) {
if (e instanceof jala.Test.Exception) {
e.functionName = functionName;
} else {
e = new jala.Test.EvaluatorException(e.toString());
}
this.functionsFailed += 1;
throw e;
} finally {
res.meta.currentTestFunction = null;
}
};
jala.Test.prototype.execute = function(what) {
var self = this;
var executeTest = function(fileName) {
var file = jala.Test.getTestFile(fileName);
if (file != null && file.exists()) {
self.testsRun += 1;
self.executeTest(file);
}
};
if (what instanceof Array) {
for (var i in what) {
executeTest(what[i]);
}
} else {
executeTest(what);
}
return;
};
jala.Test.prototype.toString = function() {
return "[jala.Test]";
};
jala.Test.prototype.renderResults = function() {
if (this.results.length > 0) {
for (var i=0;i<this.results.length;i++) {
this.renderResult(this.results[i]);
}
}
return;
};
jala.Test.prototype.renderResult = function(result) {
res.push();
var logItem;
for (var i=0;i<result.log.length;i++) {
logItem = result.log[i];
if (logItem instanceof jala.Test.Exception) {
renderSkin("jala.Test.log.failed", logItem);
} else {
renderSkin("jala.Test.log.passed", logItem);
}
}
var param = {
name: result.name,
elapsed: result.elapsed,
status: result.status,
log: res.pop()
}
renderSkin("jala.Test.result", param);
return;
};
jala.Test.prototype.list_macro = function() {
var list = jala.Test.getTestFiles();
if (list && list.length > 0) {
var fileName, skinParam;
for (var i=0;i<list.length;i++) {
fileName = list[i];
skinParam = {name: fileName};
if (req.data.test == fileName ||
(req.data.test_array && req.data.test_array.contains(fileName))) {
skinParam.checked = "checked";
}
renderSkin("jala.Test.item", skinParam);
}
}
return;
};
jala.Test.prototype.results_macro = function() {
this.renderResults();
return;
};
jala.Test.prototype.assertTrue = function assertTrue(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (typeof(value) != "boolean") {
throw new jala.Test.ArgumentsException("Invalid argument to assertTrue(boolean): " +
jala.Test.valueToString(value));
} else if (value !== true) {
throw new jala.Test.TestException(comment,
"assertTrue(boolean) called with argument " +
jala.Test.valueToString(value));
}
return;
};
jala.Test.prototype.assertFalse = function assertFalse(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (typeof(value) != "boolean") {
throw new jala.Test.ArgumentsException("Invalid argument to assertFalse(boolean): " +
jala.Test.valueToString(value));
} else if (value === true) {
throw new jala.Test.TestException(comment,
"assertFalse(boolean) called with argument " +
jala.Test.valueToString(value));
}
return;
};
jala.Test.prototype.assertEqual = function assertEqual(val1, val2) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value1 = jala.Test.getValue(arguments, argsExpected, 0);
var value2 = jala.Test.getValue(arguments, argsExpected, 1);
if (value1 !== value2) {
throw new jala.Test.TestException(comment,
"Expected " + jala.Test.valueToString(value1) +
" to be equal to " + jala.Test.valueToString(value2));
}
return;
};
jala.Test.prototype.assertNotEqual = function assertNotEqual(val1, val2) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var value1 = jala.Test.getValue(arguments, argsExpected, 0);
var value2 = jala.Test.getValue(arguments, argsExpected, 1);
var comment = jala.Test.getComment(arguments, argsExpected);
if (value1 === value2) {
throw new jala.Test.TestException(comment,
"Expected " + jala.Test.valueToString(value1) +
" to be not equal to " + jala.Test.valueToString(value2));
}
return;
};
jala.Test.prototype.assertNull = function assertNull(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (value !== null) {
throw new jala.Test.TestException(comment,
"Expected " + jala.Test.valueToString(value) +
" to be null");
}
return;
};
jala.Test.prototype.assertNotNull = function assertNotNull(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (value === null) {
throw new jala.Test.TestException(comment,
"Expected " + jala.Test.valueToString(value) +
" to be not null");
}
return;
};
jala.Test.prototype.assertUndefined = function assertUndefined(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (value !== undefined) {
throw new jala.Test.TestException(comment,
"Expected " + jala.Test.valueToString(value) +
" to be undefined");
}
return;
};
jala.Test.prototype.assertNotUndefined = function assertNotUndefined(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (value === undefined) {
throw new jala.Test.TestException(comment,
"Expected argument to be not undefined");
}
return;
};
jala.Test.prototype.assertNaN = function assertNaN(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (!isNaN(value)) {
throw new jala.Test.TestException(comment,
"Expected " + jala.Test.valueToString(value) +
" to be NaN");
}
return;
};
jala.Test.prototype.assertNotNaN = function assertNotNaN(val) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
if (isNaN(value)) {
throw new jala.Test.TestException(comment,
"Expected " + jala.Test.valueToString(value) +
" to be a number");
}
return;
};
jala.Test.prototype.assertStringContains = function assertStringContains(val, str) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
var pattern = jala.Test.getValue(arguments, argsExpected, 1);
if (pattern.constructor == String) {
if (value.indexOf(pattern) < 0) {
throw new jala.Test.TestException(comment,
"Expected string " + jala.Test.valueToString(pattern) +
" to be found in " + jala.Test.valueToString(value));
}
} else {
throw new jala.Test.ArgumentsException("Invalid argument to assertStringContains(string, string): " +
jala.Test.valueToString(pattern));
}
return;
};
jala.Test.prototype.assertMatch = function assertStringContains(val, rxp) {
var functionName = arguments.callee.name;
var argsExpected = arguments.callee.length;
jala.Test.evalArguments(arguments, argsExpected);
var comment = jala.Test.getComment(arguments, argsExpected);
var value = jala.Test.getValue(arguments, argsExpected, 0);
var exp = jala.Test.getValue(arguments, argsExpected, 1);
if (exp.constructor == RegExp) {
if (exp.test(value) == false) {
throw new jala.Test.TestException(comment,
"Expected pattern " + jala.Test.valueToString(exp) +
" to match " + jala.Test.valueToString(value));
}
} else {
throw new jala.Test.ArgumentsException("Invalid argument to assertMatch(string, regexp): " +
jala.Test.valueToString(pattern));
}
return;
};
jala.Test.HttpClient = function() {
var client = new helma.Http();
var cookie = null;
this.getClient = function() {
return client;
};
this.setCookie = function(c) {
cookie = c;
return;
};
this.getCookie = function() {
return cookie;
};
return this;
};
jala.Test.HttpClient.prototype.executeRequest = function(method, url, param) {
var client = this.getClient();
client.setMethod(method);
var cookie = this.getCookie();
if (cookie != null) {
client.setCookie(cookie.name, cookie.value);
}
client.setHeader("Cache-control", "no-cache,max-age=0");
client.setContent(param);
client.setFollowRedirects(false);
var result = client.getUrl(url);
if (result.cookie != null) {
this.setCookie(result.cookie);
}
if (result.location != null) {
result = this.executeRequest("GET", result.location);
}
return result;
};
jala.Test.HttpClient.prototype.getUrl = function(url, param) {
return this.executeRequest("GET", url, param);
};
jala.Test.HttpClient.prototype.submitForm = function(url, param) {
return this.executeRequest("POST", url, param);
};
jala.Test.HttpClient.prototype.toString = function() {
return "[jala.Test.HttpClient]";
};
jala.Test.DatabaseMgr = function() {
this.databases = {};
this.dbSourceProperties = {};
return this;
};
jala.Test.DatabaseMgr.prototype.startDatabase = function(dbSourceName, copyTables) {
try {
var testDb = new jala.db.RamDatabase(dbSourceName);
var dbSource = app.getDbSource(dbSourceName);
if (copyTables === true) {
testDb.copyTables(dbSource);
}
var oldProps = dbSource.switchProperties(testDb.getProperties());
this.databases[dbSourceName] = testDb;
this.dbSourceProperties[dbSourceName] = oldProps;
app.clearCache();
root.invalidate();
return testDb;
} catch (e) {
throw new jala.Test.EvaluatorException("Unable to switch to test database, reason: " + e);
}
};
jala.Test.DatabaseMgr.prototype.stopAll = function() {
res.commit();
try {
var testDb, dbSource;
for (var dbSourceName in this.databases) {
testDb = this.databases[dbSourceName];
dbSource = app.getDbSource(dbSourceName);
dbSource.switchProperties(this.dbSourceProperties[dbSourceName]);
testDb.shutdown();
}
app.clearCache();
root.invalidate();
} catch (e) {
throw new jala.Test.EvaluatorException("Unable to stop test databases, reason: " + e);
}
return;
};
Documentation generated by
JSDoc on Thu Mar 15 14:24:11 2007