use of org.mozilla.javascript.JavaScriptException in project cxf by apache.
the class JavascriptTestUtilities method rhinoCallExpectingException.
/**
* Call a Javascript function, identified by name, on a set of arguments. Optionally, expect it to throw
* an exception.
*
* @param expectingException
* @param functionName
* @param args
* @return
*/
public Object rhinoCallExpectingException(final Object expectingException, final String functionName, final Object... args) {
Object fObj = rhinoScope.get(functionName, rhinoScope);
if (!(fObj instanceof Function)) {
throw new RuntimeException("Missing test function " + functionName);
}
Function function = (Function) fObj;
try {
return function.call(rhinoContext, rhinoScope, rhinoScope, args);
} catch (RhinoException angryRhino) {
if (expectingException != null && angryRhino instanceof JavaScriptException) {
JavaScriptException jse = (JavaScriptException) angryRhino;
Assert.assertEquals(jse.getValue(), expectingException);
return null;
}
String trace = angryRhino.getScriptStackTrace();
Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace);
} catch (JavaScriptAssertionFailed assertion) {
Assert.fail(assertion.getMessage());
}
return null;
}
use of org.mozilla.javascript.JavaScriptException in project pentaho-kettle by pentaho.
the class ScriptValuesMetaMod method check.
public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space, Repository repository, IMetaStore metaStore) {
boolean error_found = false;
String error_message = "";
CheckResult cr;
Context jscx;
Scriptable jsscope;
Script jsscript;
jscx = ContextFactory.getGlobal().enterContext();
jsscope = jscx.initStandardObjects(null, false);
try {
jscx.setOptimizationLevel(Integer.valueOf(transMeta.environmentSubstitute(optimizationLevel)));
} catch (NumberFormatException nfe) {
error_message = "Error with optimization level. Could not convert the value of " + transMeta.environmentSubstitute(optimizationLevel) + " to an integer.";
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
} catch (IllegalArgumentException iae) {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, iae.getMessage(), stepMeta);
remarks.add(cr);
}
String strActiveStartScriptName = "";
String strActiveEndScriptName = "";
String strActiveScript = "";
String strActiveStartScript = "";
String strActiveEndScript = "";
// Building the Scripts
if (jsScripts.length > 0) {
for (int i = 0; i < jsScripts.length; i++) {
if (jsScripts[i].isTransformScript()) {
// strActiveScriptName =jsScripts[i].getScriptName();
strActiveScript = jsScripts[i].getScript();
} else if (jsScripts[i].isStartScript()) {
strActiveStartScriptName = jsScripts[i].getScriptName();
strActiveStartScript = jsScripts[i].getScript();
} else if (jsScripts[i].isEndScript()) {
strActiveEndScriptName = jsScripts[i].getScriptName();
strActiveEndScript = jsScripts[i].getScript();
}
}
}
if (prev != null && strActiveScript.length() > 0) {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ConnectedStepOK", String.valueOf(prev.size())), stepMeta);
remarks.add(cr);
// Adding the existing Scripts to the Context
for (int i = 0; i < getNumberOfJSScripts(); i++) {
Scriptable jsR = Context.toObject(jsScripts[i].getScript(), jsscope);
jsscope.put(jsScripts[i].getScriptName(), jsscope, jsR);
}
// Modification for Additional Script parsing
try {
if (getAddClasses() != null) {
for (int i = 0; i < getAddClasses().length; i++) {
Object jsOut = Context.javaToJS(getAddClasses()[i].getAddObject(), jsscope);
ScriptableObject.putProperty(jsscope, getAddClasses()[i].getJSName(), jsOut);
}
}
} catch (Exception e) {
error_message = ("Couldn't add JavaClasses to Context! Error:");
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
// Adding some default JavaScriptFunctions to the System
try {
Context.javaToJS(ScriptValuesAddedFunctions.class, jsscope);
((ScriptableObject) jsscope).defineFunctionProperties(ScriptValuesAddedFunctions.jsFunctionList, ScriptValuesAddedFunctions.class, ScriptableObject.DONTENUM);
} catch (Exception ex) {
error_message = "Couldn't add Default Functions! Error:" + Const.CR + ex.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
// Adding some Constants to the JavaScript
try {
jsscope.put("SKIP_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.SKIP_TRANSFORMATION));
jsscope.put("ABORT_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.ABORT_TRANSFORMATION));
jsscope.put("ERROR_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.ERROR_TRANSFORMATION));
jsscope.put("CONTINUE_TRANSFORMATION", jsscope, Integer.valueOf(ScriptValuesMod.CONTINUE_TRANSFORMATION));
} catch (Exception ex) {
error_message = "Couldn't add Transformation Constants! Error:" + Const.CR + ex.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
try {
ScriptValuesModDummy dummyStep = new ScriptValuesModDummy(prev, transMeta.getStepFields(stepMeta));
Scriptable jsvalue = Context.toObject(dummyStep, jsscope);
jsscope.put("_step_", jsscope, jsvalue);
Object[] row = new Object[prev.size()];
Scriptable jsRowMeta = Context.toObject(prev, jsscope);
jsscope.put("rowMeta", jsscope, jsRowMeta);
for (int i = 0; i < prev.size(); i++) {
ValueMetaInterface valueMeta = prev.getValueMeta(i);
Object valueData = null;
//
if (valueMeta.isDate()) {
valueData = new Date();
}
if (valueMeta.isString()) {
valueData = "test value test value test value test value test value " + "test value test value test value test value test value";
}
if (valueMeta.isInteger()) {
valueData = Long.valueOf(0L);
}
if (valueMeta.isNumber()) {
valueData = new Double(0.0);
}
if (valueMeta.isBigNumber()) {
valueData = BigDecimal.ZERO;
}
if (valueMeta.isBoolean()) {
valueData = Boolean.TRUE;
}
if (valueMeta.isBinary()) {
valueData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
row[i] = valueData;
if (isCompatible()) {
Value value = valueMeta.createOriginalValue(valueData);
Scriptable jsarg = Context.toObject(value, jsscope);
jsscope.put(valueMeta.getName(), jsscope, jsarg);
} else {
Scriptable jsarg = Context.toObject(valueData, jsscope);
jsscope.put(valueMeta.getName(), jsscope, jsarg);
}
}
// Add support for Value class (new Value())
Scriptable jsval = Context.toObject(Value.class, jsscope);
jsscope.put("Value", jsscope, jsval);
//
if (isCompatible()) {
Row v2Row = RowMeta.createOriginalRow(prev, row);
Scriptable jsV2Row = Context.toObject(v2Row, jsscope);
jsscope.put("row", jsscope, jsV2Row);
} else {
Scriptable jsRow = Context.toObject(row, jsscope);
jsscope.put("row", jsscope, jsRow);
}
} catch (Exception ev) {
error_message = "Couldn't add Input fields to Script! Error:" + Const.CR + ev.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
try {
// Checking for StartScript
if (strActiveStartScript != null && strActiveStartScript.length() > 0) {
/* Object startScript = */
jscx.evaluateString(jsscope, strActiveStartScript, "trans_Start", 1, null);
error_message = "Found Start Script. " + strActiveStartScriptName + " Processing OK";
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta);
remarks.add(cr);
}
} catch (Exception e) {
error_message = "Couldn't process Start Script! Error:" + Const.CR + e.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
try {
jsscript = jscx.compileString(strActiveScript, "script", 1, null);
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ScriptCompiledOK"), stepMeta);
remarks.add(cr);
try {
jsscript.exec(jscx, jsscope);
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ScriptCompiledOK2"), stepMeta);
remarks.add(cr);
if (fieldname.length > 0) {
StringBuilder message = new StringBuilder(BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.FailedToGetValues", String.valueOf(fieldname.length)) + Const.CR + Const.CR);
if (error_found) {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, message.toString(), stepMeta);
} else {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, message.toString(), stepMeta);
}
remarks.add(cr);
}
} catch (JavaScriptException jse) {
Context.exit();
error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotExecuteScript") + Const.CR + jse.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
} catch (Exception e) {
Context.exit();
error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotExecuteScript2") + Const.CR + e.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
// Checking End Script
try {
if (strActiveEndScript != null && strActiveEndScript.length() > 0) {
/* Object endScript = */
jscx.evaluateString(jsscope, strActiveEndScript, "trans_End", 1, null);
error_message = "Found End Script. " + strActiveEndScriptName + " Processing OK";
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, error_message, stepMeta);
remarks.add(cr);
}
} catch (Exception e) {
error_message = "Couldn't process End Script! Error:" + Const.CR + e.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
} catch (Exception e) {
Context.exit();
error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotCompileScript") + Const.CR + e.toString();
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
} else {
Context.exit();
error_message = BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.CouldNotGetFieldsFromPreviousStep");
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
remarks.add(cr);
}
// See if we have input streams leading to this step!
if (input.length > 0) {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.ConnectedStepOK2"), stepMeta);
remarks.add(cr);
} else {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "ScriptValuesMetaMod.CheckResult.NoInputReceived"), stepMeta);
remarks.add(cr);
}
}
use of org.mozilla.javascript.JavaScriptException in project pentaho-kettle by pentaho.
the class ScriptDialog method test.
private boolean test(boolean getvars, boolean popup) {
boolean retval = true;
StyledTextComp wScript = getStyledTextComp();
String scr = wScript.getText();
KettleException testException = null;
Context jscx;
Scriptable jsscope;
// Script jsscript;
// Making Refresh to get Active Script State
refreshScripts();
jscx = ContextFactory.getGlobal().enterContext();
jscx.setOptimizationLevel(-1);
jsscope = jscx.initStandardObjects(null, false);
// Adding the existing Scripts to the Context
for (int i = 0; i < folder.getItemCount(); i++) {
StyledTextComp sItem = getStyledTextComp(folder.getItem(i));
Scriptable jsR = Context.toObject(sItem.getText(), jsscope);
jsscope.put(folder.getItem(i).getText(), jsscope, jsR);
}
// Adding the Name of the Transformation to the Context
jsscope.put("_TransformationName_", jsscope, this.stepname);
try {
RowMetaInterface rowMeta = transMeta.getPrevStepFields(stepname);
if (rowMeta != null) {
ScriptDummy dummyStep = new ScriptDummy(rowMeta, transMeta.getStepFields(stepname));
Scriptable jsvalue = Context.toObject(dummyStep, jsscope);
jsscope.put("_step_", jsscope, jsvalue);
// Modification for Additional Script parsing
try {
if (input.getAddClasses() != null) {
for (int i = 0; i < input.getAddClasses().length; i++) {
Object jsOut = Context.javaToJS(input.getAddClasses()[i].getAddObject(), jsscope);
ScriptableObject.putProperty(jsscope, input.getAddClasses()[i].getJSName(), jsOut);
}
}
} catch (Exception e) {
testException = new KettleException(BaseMessages.getString(PKG, "ScriptDialog.CouldNotAddToContext", e.toString()));
retval = false;
}
// Adding some default JavaScriptFunctions to the System
try {
Context.javaToJS(ScriptAddedFunctions.class, jsscope);
((ScriptableObject) jsscope).defineFunctionProperties(jsFunctionList, ScriptAddedFunctions.class, ScriptableObject.DONTENUM);
} catch (Exception ex) {
testException = new KettleException(BaseMessages.getString(PKG, "ScriptDialog.CouldNotAddDefaultFunctions", ex.toString()));
retval = false;
}
// Adding some Constants to the JavaScript
try {
jsscope.put("SKIP_TRANSFORMATION", jsscope, Integer.valueOf(SKIP_TRANSFORMATION));
jsscope.put("ABORT_TRANSFORMATION", jsscope, Integer.valueOf(ABORT_TRANSFORMATION));
jsscope.put("ERROR_TRANSFORMATION", jsscope, Integer.valueOf(ERROR_TRANSFORMATION));
jsscope.put("CONTINUE_TRANSFORMATION", jsscope, Integer.valueOf(CONTINUE_TRANSFORMATION));
} catch (Exception ex) {
testException = new KettleException(BaseMessages.getString(PKG, "ScriptDialog.CouldNotAddTransformationConstants", ex.toString()));
retval = false;
}
try {
Object[] row = new Object[rowMeta.size()];
Scriptable jsRowMeta = Context.toObject(rowMeta, jsscope);
jsscope.put("rowMeta", jsscope, jsRowMeta);
for (int i = 0; i < rowMeta.size(); i++) {
ValueMetaInterface valueMeta = rowMeta.getValueMeta(i);
Object valueData = null;
//
if (valueMeta.isDate()) {
valueData = new Date();
}
if (valueMeta.isString()) {
valueData = "test value test value test value test value test " + "value test value test value test value test value test value";
}
if (valueMeta.isInteger()) {
valueData = Long.valueOf(0L);
}
if (valueMeta.isNumber()) {
valueData = new Double(0.0);
}
if (valueMeta.isBigNumber()) {
valueData = BigDecimal.ZERO;
}
if (valueMeta.isBoolean()) {
valueData = Boolean.TRUE;
}
if (valueMeta.isBinary()) {
valueData = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
}
if (valueMeta.isStorageBinaryString()) {
valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
}
row[i] = valueData;
Scriptable jsarg = Context.toObject(valueData, jsscope);
jsscope.put(valueMeta.getName(), jsscope, jsarg);
}
// OK, for these input values, we're going to allow the user to edit the default values...
// We are displaying a
// 2)
// Add support for Value class (new Value())
Scriptable jsval = Context.toObject(Value.class, jsscope);
jsscope.put("Value", jsscope, jsval);
// Add the old style row object for compatibility reasons...
//
Scriptable jsRow = Context.toObject(row, jsscope);
jsscope.put("row", jsscope, jsRow);
} catch (Exception ev) {
testException = new KettleException(BaseMessages.getString(PKG, "ScriptDialog.CouldNotAddInputFields", ev.toString()));
retval = false;
}
try {
// Checking for StartScript
if (strActiveStartScript != null && !folder.getSelection().getText().equals(strActiveStartScript) && strActiveStartScript.length() > 0) {
String strStartScript = getStyledTextComp(folder.getItem(getCTabPosition(strActiveStartScript))).getText();
/* Object startScript = */
jscx.evaluateString(jsscope, strStartScript, "trans_Start", 1, null);
}
} catch (Exception e) {
testException = new KettleException(BaseMessages.getString(PKG, "ScriptDialog.CouldProcessStartScript", e.toString()));
retval = false;
}
try {
Script evalScript = jscx.compileString(scr, "script", 1, null);
evalScript.exec(jscx, jsscope);
if (getvars) {
ScriptNode tree = parseVariables(jscx, jsscope, scr, "script", 1, null);
for (int i = 0; i < tree.getParamAndVarCount(); i++) {
String varname = tree.getParamOrVarName(i);
if (!varname.equalsIgnoreCase("row") && !varname.equalsIgnoreCase("trans_Status")) {
int type = ValueMetaInterface.TYPE_STRING;
int length = -1, precision = -1;
Object result = jsscope.get(varname, jsscope);
if (result != null) {
String classname = result.getClass().getName();
if (classname.equalsIgnoreCase("java.lang.Byte")) {
// MAX = 127
type = ValueMetaInterface.TYPE_INTEGER;
length = 3;
precision = 0;
} else if (classname.equalsIgnoreCase("java.lang.Integer")) {
// MAX = 2147483647
type = ValueMetaInterface.TYPE_INTEGER;
length = 9;
precision = 0;
} else if (classname.equalsIgnoreCase("java.lang.Long")) {
// MAX = 9223372036854775807
type = ValueMetaInterface.TYPE_INTEGER;
length = 18;
precision = 0;
} else if (classname.equalsIgnoreCase("java.lang.Double")) {
type = ValueMetaInterface.TYPE_NUMBER;
length = 16;
precision = 2;
} else if (classname.equalsIgnoreCase("org.mozilla.javascript.NativeDate") || classname.equalsIgnoreCase("java.util.Date")) {
type = ValueMetaInterface.TYPE_DATE;
} else if (classname.equalsIgnoreCase("java.lang.Boolean")) {
type = ValueMetaInterface.TYPE_BOOLEAN;
}
}
TableItem ti = new TableItem(wFields.table, SWT.NONE);
ti.setText(1, varname);
ti.setText(2, "");
ti.setText(3, ValueMetaFactory.getValueMetaName(type));
ti.setText(4, length >= 0 ? ("" + length) : "");
ti.setText(5, precision >= 0 ? ("" + precision) : "");
// If the variable name exists in the input, suggest to replace the value
//
ti.setText(6, (rowMeta.indexOfValue(varname) >= 0) ? YES_NO_COMBO[1] : YES_NO_COMBO[0]);
}
}
wFields.removeEmptyRows();
wFields.setRowNums();
wFields.optWidth(true);
}
// End Script!
} catch (EvaluatorException e) {
String position = "(" + e.lineNumber() + ":" + e.columnNumber() + ")";
String message = BaseMessages.getString(PKG, "ScriptDialog.Exception.CouldNotExecuteScript", position);
testException = new KettleException(message, e);
retval = false;
} catch (JavaScriptException e) {
String position = "(" + e.lineNumber() + ":" + e.columnNumber() + ")";
String message = BaseMessages.getString(PKG, "ScriptDialog.Exception.CouldNotExecuteScript", position);
testException = new KettleException(message, e);
retval = false;
} catch (Exception e) {
testException = new KettleException(BaseMessages.getString(PKG, "ScriptDialog.Exception.CouldNotExecuteScript2"), e);
retval = false;
}
} else {
testException = new KettleException(BaseMessages.getString(PKG, "ScriptDialog.Exception.CouldNotGetFields"));
retval = false;
}
if (popup) {
if (retval) {
if (!getvars) {
MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
mb.setMessage(BaseMessages.getString(PKG, "ScriptDialog.ScriptCompilationOK") + Const.CR);
mb.setText("OK");
mb.open();
}
} else {
new ErrorDialog(shell, BaseMessages.getString(PKG, "ScriptDialog.TestFailed.DialogTitle"), BaseMessages.getString(PKG, "ScriptDialog.TestFailed.DialogMessage"), testException);
}
}
} catch (KettleException ke) {
retval = false;
new ErrorDialog(shell, BaseMessages.getString(PKG, "ScriptDialog.TestFailed.DialogTitle"), BaseMessages.getString(PKG, "ScriptDialog.TestFailed.DialogMessage"), ke);
} finally {
if (jscx != null) {
Context.exit();
}
}
return retval;
}
use of org.mozilla.javascript.JavaScriptException in project hackpad by dropbox.
the class StackTraceTest method runWithExpectedStackTrace.
private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace) {
final ContextAction action = new ContextAction() {
public Object run(Context cx) {
final Scriptable scope = cx.initStandardObjects();
try {
cx.evaluateString(scope, _source, "test.js", 0, null);
} catch (final JavaScriptException e) {
assertEquals(_expectedStackTrace, e.getScriptStackTrace());
return null;
}
throw new RuntimeException("Exception expected!");
}
};
Utils.runWithOptimizationLevel(action, -1);
}
use of org.mozilla.javascript.JavaScriptException in project jmeter by apache.
the class BSFJavaScriptEngine method handleError.
/**
* @param t {@link Throwable}
* @throws BSFException
*/
private void handleError(Throwable t) throws BSFException {
Throwable target = t;
if (t instanceof WrappedException) {
target = ((WrappedException) t).getWrappedException();
}
String message = null;
if (target instanceof JavaScriptException) {
message = target.getLocalizedMessage();
// Is it an exception wrapped in a JavaScriptException?
Object value = ((JavaScriptException) target).getValue();
if (value instanceof Throwable) {
// likely a wrapped exception from a LiveConnect call.
// Display its stack trace as a diagnostic
target = (Throwable) value;
}
} else if (target instanceof EvaluatorException || target instanceof SecurityException) {
message = target.getLocalizedMessage();
} else if (target instanceof RuntimeException) {
message = "Internal Error: " + target.toString();
} else if (target instanceof StackOverflowError) {
message = "Stack Overflow";
}
if (message == null) {
message = target.toString();
}
if (target instanceof Error && !(target instanceof StackOverflowError)) {
// a long stacktrace would end up on the user's console
throw (Error) target;
} else {
throw new BSFException(BSFException.REASON_OTHER_ERROR, "JavaScript Error: " + message, target);
}
}
Aggregations