### Question:
JavaScriptEnv { public boolean evalFilter(String s, Map<String, String> record) throws ScriptException { Gson gson = new Gson(); ScriptEngine engine = this.factory.getEngineByName("JavaScript"); String recordJson = gson.toJson(record); engine.put("record_json", recordJson); try { s = "var record = new Object(JSON.parse(record_json));" + s; return (boolean) engine.eval(s); } catch (NullPointerException e) { throw new NullPointerException("The record filter should return either true or false! -- " + e.toString()); } } JavaScriptEnv(); boolean evalFilter(String s, Map<String, String> record);  }### Answer:
@Test public void testBooleanBehaviourSimple() throws ScriptException { JavaScriptEnv jsEnv = new JavaScriptEnv(); Map<String, String> record = new HashMap<>(); assertTrue(jsEnv.evalFilter("true", record)); assertFalse(jsEnv.evalFilter("false", record)); }
@Test(expected = ScriptException.class) public void testWrongStuffReturned() throws ScriptException { JavaScriptEnv jsEnv = new JavaScriptEnv(); Map<String, String> record = new HashMap<>(); jsEnv.evalFilter("something else", record); }
@Test(expected = NullPointerException.class) public void testAlsoFailIfEmpty() throws ScriptException { JavaScriptEnv jsEnv = new JavaScriptEnv(); Map<String, String> record = new HashMap<>(); jsEnv.evalFilter("", record); }
@Test public void testRealJavaScriptExampleSimple() throws ScriptException { JavaScriptEnv jsEnv = new JavaScriptEnv(); Map<String, String> record = new HashMap<>(); assertTrue(jsEnv.evalFilter("true === true", record)); assertFalse(jsEnv.evalFilter("true === false", record)); }
@Test public void testRealJavaScriptExampleWithRecord() throws ScriptException { JavaScriptEnv jsEnv = new JavaScriptEnv(); Map<String, String> record = new HashMap<>(); record.put("fieldA", "value1"); record.put("fieldB", "value2"); record.put("fieldC", "value1"); assertTrue(jsEnv.evalFilter("record.fieldA == 'value1'", record)); assertTrue(jsEnv.evalFilter("record.fieldA == record['fieldC']", record)); assertFalse(jsEnv.evalFilter("record.fieldA == record.fieldB", record)); }