use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class DefaultBindingContextBuilderTestCase method fromPreviousBindings.
@Test
public void fromPreviousBindings() {
ExpressionModule module = ExpressionModule.builder(namespace).addBinding("id", typedValue).build();
BindingContext previousContext = BindingContext.builder().addBinding(ID, typedValue).addBinding(OTHER_ID, typedValue).addModule(module).build();
BindingContext context = BindingContext.builder(previousContext).build();
assertThat(context.bindings(), hasSize(2));
assertThat(context.identifiers(), hasItems(ID, OTHER_ID));
assertThat(context.lookup(ID).get(), is(sameInstance(typedValue)));
assertThat(context.lookup(OTHER_ID).get(), is(sameInstance(typedValue)));
assertThat(context.modules(), hasSize(1));
assertThat(context.modules(), hasItems(module));
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class DefaultBindingContextBuilderTestCase method addsBinding.
@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)));
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class DefaultBindingContextBuilderTestCase method addsBindings.
@Test
public void addsBindings() {
ExpressionModule module = ExpressionModule.builder(namespace).addBinding(ID, typedValue).build();
BindingContext previousContext = BindingContext.builder().addBinding(ID, typedValue).addBinding(OTHER_ID, typedValue).addModule(module).build();
BindingContext context = builder.addAll(previousContext).build();
assertThat(context.bindings(), hasSize(2));
assertThat(context.identifiers(), hasItems(ID, OTHER_ID));
assertThat(context.lookup(ID).get(), is(sameInstance(typedValue)));
assertThat(context.lookup(OTHER_ID).get(), is(sameInstance(typedValue)));
assertThat(context.modules(), hasSize(1));
assertThat(context.modules(), hasItems(module));
Collection<Binding> moduleBindings = context.modules().iterator().next().bindings();
assertThat(moduleBindings, hasSize(1));
assertThat(moduleBindings.iterator().next().identifier(), is(ID));
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class DefaultExpressionManagerMelDefaultTestCase method simpleCustomVariable.
@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));
}
use of org.mule.runtime.api.el.BindingContext in project mule by mulesoft.
the class DefaultExpressionManagerTestCase method globals.
@Test
@Description("Verifies that global bindings can be added.")
public void globals() {
DataType integerType = fromType(Integer.class);
ExpressionFunction multiply = new ExpressionFunction() {
@Override
public Object call(Object[] objects, BindingContext bindingContext) {
return ((Integer) objects[0]) * ((Integer) objects[1]);
}
@Override
public Optional<DataType> returnType() {
return of(integerType);
}
@Override
public List<FunctionParameter> parameters() {
return asList(new FunctionParameter("x", integerType), new FunctionParameter("y", integerType));
}
};
BindingContext globalContext = builder().addBinding("aNum", new TypedValue<>(4, fromType(Integer.class))).addBinding("times", new TypedValue<>(multiply, fromFunction(multiply))).build();
expressionManager.addGlobalBindings(globalContext);
TypedValue result = expressionManager.evaluate("aNum times 5");
assertThat(result.getValue(), is(20));
expressionManager.addGlobalBindings(builder().addBinding("otherNum", new TypedValue(3, integerType)).build());
result = expressionManager.evaluate("(times(7, 3) + otherNum) / aNum");
assertThat(result.getValue(), is(6));
}
Aggregations