### Question:
JSRScriptEngineAdapter implements ScriptEngine { @Override public Object evaluate(final DynamicScopeContext context, final ScriptSource script) { try { copyVariables(context); return adaptee.eval(script.getSourceCode()); } catch (javax.script.ScriptException e) { throw new ScriptException(e); } } JSRScriptEngineAdapter(final javax.script.ScriptEngine adaptee); @Override Object evaluate(final DynamicScopeContext context,
            final ScriptSource script);  }### Answer:
@Test public void isDelegateInvoked() throws Exception { final Object adapteeResult = new Object(); mockContext.returns(new HashSet<KeyValuePair<Variable>>().iterator()) .iterator(); adaptee.returns(adapteeResult).eval(same(SCRIPT)); final Object result = adapter.evaluate(mockContext.getMock(), scriptSource); mockContext.assertInvoked().iterator(); adaptee.assertInvoked().eval(same(SCRIPT)); assertSame("Unexpected script result", adapteeResult, result); }
@Test public void areContextVariablesCopied() { final Variable value1 = new NodeVariable("var1value"); final Variable value2 = new NodeVariable("var2value"); final Set<KeyValuePair<Variable>> variables = new HashSet<KeyValuePair<Variable>>(); variables.add(new KeyValuePair<Variable>("var1name", value1)); variables.add(new KeyValuePair<Variable>("var2name", value2)); mockContext.returns(variables.iterator()).iterator(); adapter.evaluate(mockContext.getMock(), scriptSource); mockContext.assertInvoked().iterator(); adaptee.assertInvoked().put(eq("var1name"), same(value1)); adaptee.assertInvoked().put(eq("var2name"), same(value2)); }
@Test public void evaluateInCaseOfException() throws Exception { mockContext.returns(new HashSet<KeyValuePair<Variable>>().iterator()) .iterator(); adaptee.raises(new ScriptException("test")).eval((String) null); try { adapter.evaluate(mockContext.getMock(), scriptSource); fail("ScriptException expected"); } catch (org.webharvest.exception.ScriptException e) { } adaptee.assertInvoked().eval(same(SCRIPT)); }