// to force loading of components if not loaded
ComponentsLoader.getInstance();
// dynamically create VerbsTestAPI class with verbs methods
TestAPI testAPI = TestAPIImpl.getInstance();
Collection<String> registeredComponents = testAPI.getRegisteredComponents();
if (engine != null) {
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
if (bindings != null) {
bindings.clear();
}
}
globalBindings = engine.createBindings();
globalBindings.put(ScriptEngine.FILENAME, "embedded_jython");
globalBindings.put("logger", scriptLogger);
globalBindings.put("Status", ScriptTestResultStatus.class);
try {
// Declare __TestAPIWrapper class, of which the testAPI variable will be an instance
String code =
"import sys as __sys\n" +
"from sets import Set as __Set\n" +
"from com.qspin.qtaste.testsuite import QTasteException, QTasteTestFailException, QTasteDataException\n" +
"import com.qspin.qtaste.testsuite.impl.JythonTestScript.ScriptTestResultStatus as Status\n" +
"class ComponentNotPresentException(Exception):\n" +
" pass\n" +
"class StepsException(Exception):\n" +
" pass\n" +
"class ImportTestScriptException(Exception):\n" +
" pass\n" +
"class __TestAPIWrapper:\n" +
" def __init__(self, testScript):\n" +
" self.testScript = testScript\n";
code +=
// new-style test api - direct method call
" def __invoke(self, method, arguments):\n" +
" self.testScript.logInvoke(method.im_self, method.__name__, str(arguments)[1:-1-(len(arguments)==1)])\n" +
" try:\n" +
" return method(*arguments)\n" +
" except TypeError, e:\n" +
" raise QTasteDataException('Invalid argument(s): ' + str(e))\n" +
" def stopTest(self, status, message):\n" +
" if status == Status.FAIL:\n" +
" raise QTasteTestFailException(message)\n" +
" elif status == Status.NOT_AVAILABLE:\n" +
" raise QTasteDataException(message)\n" +
" else:\n" +
" raise SyntaxError('Invalid status argument')\n";
// add get<Component>() methods to the __TestAPIWrapper class
for (String component : registeredComponents) {
code += " def get" + component + "(self, **keywords):\n" +
" component = self.testScript.getComponent('" + component + "', keywords)\n" +
" return __TestAPIWrapper." + component + "Wrapper(self, component)\n";
// declare the <Component>Wrapper class, of which the objects returned
// by get<Component>() methods will be instances
code += " class " + component + "Wrapper:\n" +
" def __init__(self, testAPI, component):\n" +
" self.testAPI = testAPI\n" +
" self.component = component\n" +
" def __nonzero__(self):\n" +
" return self.component\n";
// add verbs methods to the ComponentWrapper class
Collection<String> verbs = testAPI.getRegisteredVerbs(component);
for (String verb : verbs) {
code += " def " + verb + "(self, *arguments, **keywords):\n" +
" if self.component:\n";
code += " return self.testAPI._TestAPIWrapper__invoke(self.component." + verb + ", arguments)\n";