@Test public void addsBinding() { BindingContext context = builder.addBinding(ID, typedValue).build(); assertThat(context.bindings(), hasSize(1)); assertThat(context.identifiers(), hasItem("id")); assertThat(context.lookup("id").get(), is(sameInstance(typedValue))); }
@Test public void bindingValueNotReferencedByDWParserCache() throws MuleException { Object bindingValue = new Object(); PhantomReference<CoreEvent> bindingValueRef = new PhantomReference(bindingValue, new ReferenceQueue<>()); expressionLanguage.evaluate("#[value]", null, BindingContext.builder() .addBinding("value", new TypedValue<>(bindingValue, OBJECT)) .build()); bindingValue = null; new PollingProber(GC_POLLING_TIMEOUT, DEFAULT_POLLING_INTERVAL).check(new JUnitLambdaProbe(() -> { System.gc(); assertThat(bindingValueRef.isEnqueued(), is(true)); return true; }, "A hard reference is being mantained to the bindingValue.")); }
@Test @Description("Verifies that custom variables are considered.") public void simpleCustomVariable() { Object object = new Object(); BindingContext context = builder().addBinding(MY_VAR, new TypedValue(object, OBJECT)).build(); assertThat(expressionManager.evaluate("#[myVar]", context).getValue(), equalTo(object)); }
private BindingContext globalBinding() { return BindingContext.builder().addBinding("g", new TypedValue<>("gValue", STRING)).build(); }
@Benchmark public Object withParent() { return BindingContext.builder(globalCtx) .addBinding("onChild", new TypedValue<>("hello!", DataType.STRING)) .build(); }
@Test public void session() throws MuleException { Object object = new Object(); BindingContext context = builder().addBinding(MY_VAR, new TypedValue(object, OBJECT)).build(); ExpressionManagerSession session = expressionManager.openSession(context); assertThat(session.evaluate("#[myVar]").getValue(), equalTo(object)); } }
@Test public void miscLazyLookupInFirstDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("misc", mapValue()) .build(); assertLookup(BindingContext.builder(localBinding).addAll(globalBinding()).build(), "misc"); }
@Test public void varLookupInFirstDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("vars", new LazyValue<>(() -> mapValue())) .build(); assertLookup(BindingContext.builder(localBinding).addAll(globalBinding()).build(), "vars"); }
@Test public void attributesLookupInFirstDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("attributes", mapValue()) .build(); assertLookup(BindingContext.builder(localBinding).addAll(globalBinding()).build(), "attributes"); }
@Test @Description("Verifies that custom variables are considered.") public void simpleCustomVariable() { Object object = new Object(); BindingContext context = builder().addBinding(MY_VAR, new TypedValue(object, OBJECT)).build(); assertThat(expressionManager.evaluate("#[myVar]", context).getValue(), equalTo(object)); }
@Test public void varLookupInSecondDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("vars", new LazyValue<>(() -> mapValue())) .build(); assertLookup(BindingContext.builder(globalBinding()).addAll(localBinding).build(), "vars"); }
@Test public void payloadLookupInSecondDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("payload", new LazyValue<>(() -> mapValue())) .build(); assertLookup(BindingContext.builder(globalBinding()).addAll(localBinding).build(), "payload"); }
private Object evaluateHeaders(InputStream headers) { String hs = IOUtils.toString(headers); BindingContext context = BindingContext.builder().addBinding("payload", new TypedValue<>(hs, DataType.XML_STRING)).build(); return expressionExecutor.evaluate("%dw 2.0 \n" + "output application/java \n" + "---\n" + "payload.headers mapObject (value, key) -> {\n" + " '$key' : write((key): value, \"application/xml\")\n" + "}", context).getValue(); }
@Test public void miscLookupInFirstDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("misc", mapValue()) .build(); assertLookup(BindingContext.builder(localBinding).addAll(globalBinding()).build(), "misc"); }
@Test public void payloadLookupInFirstDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("payload", mapValue()) .build(); assertLookup(BindingContext.builder(localBinding).addAll(globalBinding()).build(), "payload"); }
@Test public void mvelWithNullBinding() throws MuleException { String expression = "#[mel: 2+2 ==4]"; TypedValue result = expressionManager .evaluate(expression, BOOLEAN, builder().addBinding("payload", new TypedValue(null, OBJECT)).build(), testEvent()); assertThat(result.getValue(), is(true)); assertThat(result.getDataType(), is(BOOLEAN)); }
@Override public BindingContext getBindingContext() { TestExpressionFunction function = new TestExpressionFunction(); return builder() .addBinding("number", new TypedValue(1, NUMBER)) .addBinding("repeat", new TypedValue(function, fromFunction(function))) .build(); } }
@Test public void miscLookupInSecondDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("misc", new LazyValue<>(() -> mapValue())) .build(); assertLookup(BindingContext.builder(globalBinding()).addAll(localBinding).build(), "misc"); }
@Test public void miscLazyLookupInSecondDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("misc", new LazyValue<>(() -> mapValue())) .build(); assertLookup(BindingContext.builder(globalBinding()).addAll(localBinding).build(), "misc"); }
@Test public void attributesLookupInSecondDelegate() { BindingContext localBinding = BindingContext.builder() .addBinding("attributes", new LazyValue<>(() -> mapValue())) .build(); assertLookup(BindingContext.builder(globalBinding()).addAll(localBinding).build(), "attributes"); }