private boolean isThisTheLastVariable(CallMethod callMethod, int i) { return i < callMethod.getCallFieldValues().length - 1; }
private Map<String, Object> initVars(CallMethod callMethod) { Map<String, Object> vars = new HashMap<String, Object>(); vars.put( "__fact__", populatedData.get(callMethod.getVariable())); return vars; } }
@Test public void testCallMethodOnStandardArgumentOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("addToTotalAmount"); CallFieldValue field = new CallFieldValue(); field.value = "5"; mCall.addFieldValue(field); methodExecutor.executeMethod(mCall); assertTrue(listChesse.getTotalAmount() == 1005); }
private String build(CallMethod callMethod, Map<String, Object> vars) { StringBuilder methodCallAsSting = new StringBuilder(); methodCallAsSting.append("__fact__." + callMethod.getMethodName()); methodCallAsSting.append("("); for (int i = 0; i < callMethod.getCallFieldValues().length; i++) { CallFieldValue field = callMethod.getCallFieldValues()[i]; if (field.hasValue()) { String variableId = String.format("__val%d__", i); vars.put( variableId, getFieldValue(field)); methodCallAsSting.append(variableId); if (isThisTheLastVariable(callMethod, i)) { methodCallAsSting.append(","); } } } methodCallAsSting.append(")"); return methodCallAsSting.toString(); }
@Test public void testCallMethodNoArgumentOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("setTotalAmountToZero"); methodExecutor.executeMethod(mCall); assertTrue(listChesse.getTotalAmount() == 0); }
@Test public void testCallMethodOnClassArgumentAndOnArgumentStandardOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); Cheesery.Maturity m = new Cheesery.Maturity("veryYoung"); populatedData.put("m", m); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("setAgeToMaturity"); CallFieldValue field = new CallFieldValue(); field.value = "=m"; mCall.addFieldValue(field); CallFieldValue field2 = new CallFieldValue(); field2.value = "veryold"; mCall.addFieldValue(field2); methodExecutor.executeMethod(mCall); assertTrue(m.getAge().equals("veryold")); } }
private String build(CallMethod callMethod, Map<String, Object> vars) { StringBuilder methodCallAsSting = new StringBuilder(); methodCallAsSting.append("__fact__." + callMethod.getMethodName()); methodCallAsSting.append("("); for (int i = 0; i < callMethod.getCallFieldValues().length; i++) { CallFieldValue field = callMethod.getCallFieldValues()[i]; if (field.hasValue()) { String variableId = String.format("__val%d__", i); vars.put( variableId, getFieldValue(field)); methodCallAsSting.append(variableId); if (isThisTheLastVariable(callMethod, i)) { methodCallAsSting.append(","); } } } methodCallAsSting.append(")"); return methodCallAsSting.toString(); }
@Test public void testCallMethodOnClassArgumentOnFact() throws Exception { HashMap<String, Object> populatedData = new HashMap<String, Object>(); MethodExecutor methodExecutor = new MethodExecutor(populatedData); Cheesery listChesse = new Cheesery(); listChesse.setTotalAmount(1000); populatedData.put("cheese", listChesse); Cheesery.Maturity m = new Cheesery.Maturity(); populatedData.put("m", m); CallMethod mCall = new CallMethod(); mCall.setVariable("cheese"); mCall.setMethodName("setGoodMaturity"); CallFieldValue field = new CallFieldValue(); field.value = "=m"; mCall.addFieldValue(field); methodExecutor.executeMethod(mCall); assertTrue(listChesse.getMaturity().equals(m)); assertTrue(listChesse.getMaturity() == m); }
private Map<String, Object> initVars(CallMethod callMethod) { Map<String, Object> vars = new HashMap<String, Object>(); vars.put( "__fact__", populatedData.get(callMethod.getVariable())); return vars; } }
private boolean isThisTheLastVariable(CallMethod callMethod, int i) { return i < callMethod.getCallFieldValues().length - 1; }
public Object executeMethod(CallMethod callMethod) { Map<String, Object> vars = initVars(callMethod); eval( build(callMethod, vars), vars); return populatedData.get(callMethod.getVariable()); }
public Object executeMethod(CallMethod callMethod) { Map<String, Object> vars = initVars(callMethod); eval( build(callMethod, vars), vars); return populatedData.get(callMethod.getVariable()); }