ScriptEngineLambda implements Function, Supplier, Consumer, Predicate, BiConsumer, TriConsumer { public void accept(final Object a) { try { final Bindings bindings = new SimpleBindings(); bindings.put(A, a); this.engine.eval(this.script, bindings); } catch (final ScriptException e) { throw new IllegalArgumentException(e.getMessage()); } } ScriptEngineLambda(final String engineName, final String script); Object apply(final Object a); Object get(); void accept(final Object a); void accept(final Object a, final Object b); void accept(final Object a, final Object b, final Object c); boolean test(final Object a);  }
@Test public void shouldCallAsConsumer() { final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "a.setData('test')"); final Junk junk = new Junk(); lambda.accept(junk); assertEquals("test", junk.getData()); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowOnBadScriptAsConsumer() { new ScriptEngineLambda("nashorn", "1432423)a").accept("1"); }
@Test public void shouldCallAsBiConsumer() { final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "a.setData('testa');b.setData('testb')"); final Junk junkA = new Junk(); final Junk junkB = new Junk(); lambda.accept(junkA, junkB); assertEquals("testa", junkA.getData()); assertEquals("testb", junkB.getData()); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowOnBadScriptAsBiConsumer() { new ScriptEngineLambda("nashorn", "1432423)a").accept("1", "2"); }
@Test public void shouldCallAsTriConsumer() { final ScriptEngineLambda lambda = new ScriptEngineLambda("nashorn", "a.setData('testa');b.setData('testb');c.setData('testc')"); final Junk junkA = new Junk(); final Junk junkB = new Junk(); final Junk junkC = new Junk(); lambda.accept(junkA, junkB, junkC); assertEquals("testa", junkA.getData()); assertEquals("testb", junkB.getData()); assertEquals("testc", junkC.getData()); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowOnBadScriptAsTriConsumer() { new ScriptEngineLambda("nashorn", "1432423)a").accept("1", "2", "3"); }