[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit/javascript JavaScript.java,1.21,1.22 Java
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-09-13 18:35:02
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/javascript In directory usw-pr-cvs1:/tmp/cvs-serv23375/src/com/meterware/httpunit/javascript Modified Files: JavaScript.java JavaScriptEngineFactory.java Log Message: Added control of script error messages Index: JavaScript.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/javascript/JavaScript.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- JavaScript.java 9 Sep 2002 20:04:26 -0000 1.21 +++ JavaScript.java 13 Sep 2002 18:34:57 -0000 1.22 @@ -24,6 +24,7 @@ import com.meterware.httpunit.WebResponse; import com.meterware.httpunit.WebLink; import com.meterware.httpunit.WebImage; +import com.meterware.httpunit.ScriptException; import com.meterware.httpunit.scripting.ScriptingEngine; import com.meterware.httpunit.scripting.ScriptableDelegate; @@ -33,6 +34,7 @@ import java.lang.reflect.InvocationTargetException; import java.util.Arrays; +import java.util.ArrayList; import java.io.IOException; import org.mozilla.javascript.*; @@ -47,6 +49,30 @@ private final static Object[] NO_ARGS = new Object[0]; + private static boolean _throwExceptionsOnError = true; + + private static ArrayList _errorMessages = new ArrayList(); + + + static boolean isThrowExceptionsOnError() { + return _throwExceptionsOnError; + } + + + static void setThrowExceptionsOnError( boolean throwExceptionsOnError ) { + _throwExceptionsOnError = throwExceptionsOnError; + } + + + static void clearErrorMessages() { + _errorMessages.clear(); + } + + + static String[] getErrorMessages() { + return (String[]) _errorMessages.toArray( new String[ _errorMessages.size() ] ); + } + /** * Initiates JavaScript execution for the specified web response. @@ -88,10 +114,8 @@ script = script.trim(); if (script.startsWith( "<!--" )) script = script.substring( 4 ); Context.getCurrentContext().evaluateString( this, script, "httpunit", 0, null ); - } catch (JavaScriptException e) { - throw new RuntimeException( "Script '" + script + "' failed: " + e ); - } catch (EcmaError e) { - throw new RuntimeException( "Syntax Error at line " + e.getLineNumber() + ": " + e.getLineSource() ); + } catch (Exception e) { + handleScriptException( e, "Script '" + script + "'" ); } } @@ -104,8 +128,8 @@ Object result = f.call( context, this, this, NO_ARGS ); return (result instanceof Boolean) ? ((Boolean) result).booleanValue() : true; } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException( "Script '" + eventScript + "' failed: " + e ); + handleScriptException( e, "Event '" + eventScript + "'" ); + return false; } } @@ -117,8 +141,20 @@ Object result = Context.getCurrentContext().evaluateString( this, urlString, "httpunit", 0, null ); return (result == null || result instanceof Undefined) ? null : result.toString(); } catch (Exception e) { - e.printStackTrace(); - throw new RuntimeException( "Script '" + urlString + "' failed: " + e ); + handleScriptException( e, "URL '" + urlString + "'" ); + return null; + } + } + + + private void handleScriptException( Exception e, String badScript ) { + final String errorMessage = badScript + " failed: " + e; + if (!(e instanceof EcmaError)) { + throw new RuntimeException( errorMessage ); + } else if (isThrowExceptionsOnError()) { + throw new ScriptException( errorMessage ); + } else { + _errorMessages.add( errorMessage ); } } Index: JavaScriptEngineFactory.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/javascript/JavaScriptEngineFactory.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- JavaScriptEngineFactory.java 30 Aug 2002 15:20:09 -0000 1.5 +++ JavaScriptEngineFactory.java 13 Sep 2002 18:34:57 -0000 1.6 @@ -43,9 +43,31 @@ public void associate( WebResponse response ) { try { JavaScript.run( response ); + } catch (RuntimeException e) { + throw e; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( e.toString() ); } + } + + + public void setThrowExceptionsOnError( boolean throwExceptions ) { + JavaScript.setThrowExceptionsOnError( throwExceptions ); + } + + + public boolean isThrowExceptionsOnError() { + return JavaScript.isThrowExceptionsOnError(); + } + + + public String[] getErrorMessages() { + return JavaScript.getErrorMessages(); + } + + + public void clearErrorMessages() { + JavaScript.clearErrorMessages(); } } |