diff -Nu /home/hannes/helma/modules/jala/util/Test/code/Global/jala.Test.js modules/jala/test/jala.Test.js --- /home/hannes/helma/modules/jala/util/Test/code/Global/jala.Test.js 2008-04-17 20:46:14.000000000 +0200 +++ modules/jala/test/jala.Test.js 2008-05-09 22:29:20.000000000 +0200 @@ -23,26 +23,21 @@ /** - * @fileoverview Fields and methods of the jala.Test class. + * @fileoverview Fields and methods of the Test class. */ - -// Define the global namespace for Jala modules -if (!global.jala) { - global.jala = {}; -} - /** * HelmaLib dependencies */ -app.addRepository("modules/core/String.js"); -app.addRepository("modules/helma/Http.js"); +importModule("core.string"); +importModule("helma.http"); +importModule("helma.file"); +importModule("helma.rhino"); /** * Jala dependencies */ -app.addRepository(getProperty("jala.dir", "modules/jala") + - "/code/Database.js"); +// importModule("jala.database"); /** * Constructs a new Test instance. @@ -52,7 +47,7 @@ * @param {Number} capacity The capacity of the cache * @constructor */ -jala.Test = function() { +Test = function() { /** * Contains the number of tests that were executed @@ -99,14 +94,14 @@ * @type String * @final */ -jala.Test.PASSED = "passed"; +Test.PASSED = "passed"; /** * Constant indicating "failed" status * @type String * @final */ -jala.Test.FAILED = "failed"; +Test.FAILED = "failed"; /** * Helper method useable for displaying a value @@ -114,7 +109,7 @@ * @returns The value rendered as string * @type String */ -jala.Test.valueToString = function(val) { +Test.valueToString = function(val) { res.push(); if (val === null) { res.write("null"); @@ -155,14 +150,17 @@ * @returns The directory containing the test files * @type helma.File */ -jala.Test.getTestsDirectory = function() { +Test.getTestsDirectory = function() { var dir; + if (true) { + dir = new helma.file.File("/home/hannes/svn/helma-ng/modules/tests"); + } else if (getProperty("jala.testDir") != null) { - dir = new helma.File(getProperty("jala.testDir")); + dir = new helma.file.File(getProperty("jala.testDir")); } if (!dir || !dir.exists()) { - var appDir = new helma.File(app.dir); - dir = new helma.File(appDir.getParent(), "tests"); + var appDir = new helma.file.File(app.dir); + dir = new helma.file.File(appDir.getParent(), "tests"); if (!dir.exists()) return null; } @@ -175,9 +173,9 @@ * @returns An array containing the names of all test files * @type Array */ -jala.Test.getTestFiles = function() { +Test.getTestFiles = function() { var dir; - if ((dir = jala.Test.getTestsDirectory()) != null) { + if ((dir = Test.getTestsDirectory()) != null) { return dir.list(/.*\.js/).sort(); } return null; @@ -189,10 +187,10 @@ * @returns The test file * @type helma.File */ -jala.Test.getTestFile = function(fileName) { - var dir = jala.Test.getTestsDirectory(); +Test.getTestFile = function(fileName) { + var dir = Test.getTestsDirectory(); if (dir != null) { - return new helma.File(dir, fileName); + return new helma.file.File(dir, fileName); } return null; }; @@ -204,10 +202,10 @@ * the expected amount, false otherwise. * @type Boolean */ -jala.Test.evalArguments = function(args, argsExpected) { +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"); + throw new Test.ArgumentsException("Insufficient arguments passed to assertion function"); } return; }; @@ -221,7 +219,7 @@ * @returns True if the arguments contain a comment, false otherwise. * @type Boolean */ -jala.Test.argsContainComment = function(args, argsExpected) { +Test.argsContainComment = function(args, argsExpected) { return !(args.length <= argsExpected || (args.length == argsExpected + 1 && typeof(args[0]) != "string")) }; @@ -234,8 +232,8 @@ * @returns The comment, if existing. Null otherwise. * @type String */ -jala.Test.getComment = function(args, argsExpected) { - if (jala.Test.argsContainComment(args, argsExpected)) +Test.getComment = function(args, argsExpected) { + if (Test.argsContainComment(args, argsExpected)) return args[0]; return null; }; @@ -251,8 +249,8 @@ * @returns The non-comment value, or null. * @type Object */ -jala.Test.getValue = function(args, argsExpected, idx) { - return jala.Test.argsContainComment(args, argsExpected) ? args[idx+1] : args[idx]; +Test.getValue = function(args, argsExpected, idx) { + return Test.argsContainComment(args, argsExpected) ? args[idx+1] : args[idx]; }; @@ -263,7 +261,7 @@ * @returns The parsed stack trace * @type String */ -jala.Test.getStackTrace = function(trace) { +Test.getStackTrace = function(trace) { /** * Private method for filtering out only JS parts of the stack trace * @param {Object} name @@ -288,11 +286,11 @@ fileName = el.getFileName(); lineNumber = el.getLineNumber(); if (fileName != null && lineNumber > -1 && accept(fileName)) { - if (fileName.endsWith(res.meta.currentTest)) { + if (fileName.endsWith(res.currentTest)) { inTrace = true; } if (inTrace == true) { - // ignore all trace lines that refer to jala.Test + // ignore all trace lines that refer to Test if (fileName.endsWith("jala.Test.js")) { break; } @@ -312,34 +310,33 @@ * @type helma.scripting.rhino.GlobalObject * @private */ -jala.Test.getTestScope = function() { +Test.getTestScope = function() { // create a new vanilla global object - var engine = Packages.helma.scripting.rhino.RhinoEngine.getRhinoEngine(); - var scope = new Packages.helma.scripting.rhino.GlobalObject(engine.getCore(), - app.__app__, true); + var engine = helma.rhino.getRhinoEngine(); + var scope = engine.createThreadScope(helma.rhino.getRhinoContext()) // put the necessary global objects into the scope - scope.root = root; + // scope.root = root; scope.session = session; scope.req = req; scope.res = res; - scope.path = path; + // scope.path = path; // define global assertion functions - for (var i in jala.Test.prototype) { + for (var i in Test.prototype) { if (i.indexOf("assert") == 0) { - scope[i] = jala.Test.prototype[i]; + scope[i] = Test.prototype[i]; } } // add global include method scope.include = function(file) { - jala.Test.include(scope, file); + Test.include(scope, file); return; }; // instantiate a global HttpClient - scope.httpClient = new jala.Test.HttpClient(); + scope.httpClient = new Test.HttpClient(); // instantiate the test database manager - scope.testDatabases = new jala.Test.DatabaseMgr(); + scope.testDatabases = new Test.DatabaseMgr(); // instantiate the smtp server - scope.smtpServer = new jala.Test.SmtpServer(); + scope.smtpServer = new Test.SmtpServer(); return scope; }; @@ -350,7 +347,7 @@ * @param {Object} scope The scope in which the file should be evaluated * @param {String} fileName The name of the file to include, including the path */ -jala.Test.include = function(scope, file) { +Test.include = function(scope, file) { var file = new helma.File(file); var fileName = file.getName(); if (file.canRead() && file.exists()) { @@ -374,13 +371,13 @@ * @returns A newly created Exception instance * @constructor */ -jala.Test.Exception = function Exception() { +Test.Exception = function Exception() { return this; }; /** @ignore */ -jala.Test.Exception.prototype.toString = function() { - return "[jala.Test.Exception: " + this.message + "]"; +Test.Exception.prototype.toString = function() { + return "[Test.Exception: " + this.message + "]"; }; /** @@ -392,18 +389,18 @@ * @returns A newly created TestException instance * @constructor */ -jala.Test.TestException = function TestException(comment, message) { +Test.TestException = function TestException(comment, message) { this.functionName = null; this.comment = comment; this.message = message; - this.stackTrace = jala.Test.getStackTrace(); + this.stackTrace = Test.getStackTrace(); return this; }; -jala.Test.TestException.prototype = new jala.Test.Exception(); +Test.TestException.prototype = new Test.Exception(); /** @ignore */ -jala.Test.TestException.prototype.toString = function() { - return "[jala.Test.TestException in " + this.functionName + +Test.TestException.prototype.toString = function() { + return "[Test.TestException in " + this.functionName + ": " + this.message + "]"; }; @@ -415,17 +412,17 @@ * @returns A newly created ArgumentsException instance * @constructor */ -jala.Test.ArgumentsException = function ArgumentsException(message) { +Test.ArgumentsException = function ArgumentsException(message) { this.functionName = null; this.message = message; - this.stackTrace = jala.Test.getStackTrace(); + this.stackTrace = Test.getStackTrace(); return this; }; -jala.Test.ArgumentsException.prototype = new jala.Test.Exception(); +Test.ArgumentsException.prototype = new Test.Exception(); /** @ignore */ -jala.Test.ArgumentsException.prototype.toString = function() { - return "[jala.Test.ArgumentsException in " + this.functionName + +Test.ArgumentsException.prototype.toString = function() { + return "[Test.ArgumentsException in " + this.functionName + ": " + this.message + "]"; }; @@ -439,7 +436,7 @@ * @returns A newly created EvaluatorException instance * @constructor */ -jala.Test.EvaluatorException = function EvaluatorException(message, exception) { +Test.EvaluatorException = function EvaluatorException(message, exception) { this.functionName = null; this.message = null; this.stackTrace = null; @@ -458,7 +455,7 @@ if (exception.rhinoException != null) { var e = exception.rhinoException; this.message += e.details(); - this.stackTrace = jala.Test.getStackTrace(e.getStackTrace()); + this.stackTrace = Test.getStackTrace(e.getStackTrace()); } else if (exception instanceof Error) { this.message = exception.message; } @@ -471,11 +468,11 @@ return this; }; -jala.Test.EvaluatorException.prototype = new jala.Test.Exception(); +Test.EvaluatorException.prototype = new Test.Exception(); /** @ignore */ -jala.Test.EvaluatorException.prototype.toString = function() { - return "[jala.Test.EvaluatorException: " + this.message + "]"; +Test.EvaluatorException.prototype.toString = function() { + return "[Test.EvaluatorException: " + this.message + "]"; }; @@ -493,10 +490,10 @@ * @returns A new TestResult instance * @constructor */ -jala.Test.TestResult = function(testFileName) { +Test.TestResult = function(testFileName) { this.name = testFileName; this.elapsed = 0; - this.status = jala.Test.PASSED; + this.status = Test.PASSED; this.log = []; return this; }; @@ -511,7 +508,7 @@ * @returns A new TestFunctionResult instance * @constructor */ -jala.Test.TestFunctionResult = function(functionName, startTime) { +Test.TestFunctionResult = function(functionName, startTime) { this.functionName = functionName; this.elapsed = (new Date()) - startTime; return this; @@ -528,18 +525,18 @@ * Executes a single test file * @param {helma.File} testFile The file containing the test to run */ -jala.Test.prototype.executeTest = function(testFile) { +Test.prototype.executeTest = function(testFile) { var testFileName = testFile.getName(); - // store the name of the current test in res.meta.currentTest + // store the name of the current test in res.currentTest // as this is needed in getStackTrace - res.meta.currentTest = testFileName; + res.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); + var testResult = new Test.TestResult(testFileName); try { // instantiate a new test scope - var scope = jala.Test.getTestScope(scope); + var scope = Test.getTestScope(scope); // evaluate the test file in the per-thread which is garbage // collected at the end of the test run and prevents the application // scope from being polluted @@ -560,20 +557,20 @@ try { functionName = scope.tests[i]; if (!scope[functionName] || scope[functionName].constructor != Function) { - throw new jala.Test.EvaluatorException("Test function '" + + throw new Test.EvaluatorException("Test function '" + functionName + "' is not defined."); } testResult.log.push(this.executeTestFunction(functionName, scope)); } catch (e) { this.testsFailed += 1; - testResult.status = jala.Test.FAILED; + testResult.status = Test.FAILED; e.functionName = functionName; testResult.log.push(e); } } } catch (e) { this.testsFailed += 1; - testResult.status = jala.Test.FAILED; + testResult.status = Test.FAILED; testResult.log.push(e); } finally { // call any existing "cleanup" method @@ -583,13 +580,13 @@ } } catch (e) { this.testsFailed += 1; - testResult.status = jala.Test.FAILED; - testResult.log.push(new jala.Test.EvaluatorException(e)); + testResult.status = Test.FAILED; + testResult.log.push(new Test.EvaluatorException(e)); } finally { // exit the js context created above cx.exit(); - // clear res.meta.currentTest - res.meta.currentTest = null; + // clear res.currentTest + res.currentTest = null; } testResult.elapsed = (new Date()) - start; this.results.push(testResult); @@ -602,23 +599,23 @@ * @param {helma.scripting.rhino.GlobalObject} scope The scope to execute * the test method in */ -jala.Test.prototype.executeTestFunction = function(functionName, scope) { - // store the name of the current function in res.meta.currentTestFunction - res.meta.currentTestFunction = functionName; +Test.prototype.executeTestFunction = function(functionName, scope) { + // store the name of the current function in res.currentTestFunction + res.currentTestFunction = functionName; var start = new Date(); try { scope[functionName](); this.functionsPassed += 1; - return new jala.Test.TestFunctionResult(functionName, start); + return new Test.TestFunctionResult(functionName, start); } catch (e) { - if (!(e instanceof jala.Test.Exception)) { - e = new jala.Test.EvaluatorException(e); + if (!(e instanceof Test.Exception)) { + e = new Test.EvaluatorException(e); } this.functionsFailed += 1; throw e; } finally { - // clear res.meta.currentFunction - res.meta.currentTestFunction = null; + // clear res.currentFunction + res.currentTestFunction = null; } }; @@ -628,10 +625,10 @@ * or an array containing the names of several function files that should * be executed. */ -jala.Test.prototype.execute = function(what) { +Test.prototype.execute = function(what) { var self = this; var executeTest = function(fileName) { - var file = jala.Test.getTestFile(fileName); + var file = Test.getTestFile(fileName); if (file != null && file.exists()) { self.testsRun += 1; self.executeTest(file); @@ -649,15 +646,15 @@ }; /** @ignore */ -jala.Test.prototype.toString = function() { - return "[jala.Test]"; +Test.prototype.toString = function() { + return "[Test]"; }; /** * Renders the results of all tests done by this test instance * to response. */ -jala.Test.prototype.renderResults = function() { +Test.prototype.renderResults = function() { if (this.results.length > 0) { for (var i=0;i 0) { var fileName, skinParam; for (var i=0;i Jala Unit Test Runner -" /> +