/** * Creates a new variable in this test context with the respective value. In case variable already * exists variable is overwritten. * * @param variableName the name of the new variable * @param value the new variable value * @throws CitrusRuntimeException * @return */ public void setVariable(final String variableName, Object value) { if (!StringUtils.hasText(variableName) || VariableUtils.cutOffVariablesPrefix(variableName).length() == 0) { throw new CitrusRuntimeException( "Can not create variable '" + variableName + "', please define proper variable name"); } if (value == null) { throw new VariableNullValueException( "Trying to set variable: " + VariableUtils.cutOffVariablesPrefix(variableName) + ", but variable value is null"); } if (log.isDebugEnabled()) { log.debug( "Setting variable: " + VariableUtils.cutOffVariablesPrefix(variableName) + " with value: '" + value + "'"); } variables.put(VariableUtils.cutOffVariablesPrefix(variableName), value); }
/** * Method replacing variable declarations and functions in a string, optionally the variable * values get surrounded with single quotes. * * @param str the string to parse for variable place holders. * @param enableQuoting flag marking surrounding quotes should be added or not. * @return resulting string without any variable place holders. */ public String replaceDynamicContentInString(final String str, boolean enableQuoting) { String result; result = VariableUtils.replaceVariablesInString(str, this, enableQuoting); result = FunctionUtils.replaceFunctionsInString(result, this, enableQuoting); return result; }
/** * Checks weather the given expression is a variable or function and resolves the value * accordingly * * @param expression the expression to resolve * @return the resolved expression value */ public String resolveDynamicValue(String expression) { if (VariableUtils.isVariableName(expression)) { return getVariable(expression); } else if (functionRegistry.isFunction(expression)) { return FunctionUtils.resolveFunction(expression, this); } return expression; }
/** * Gets the value for the given variable as object representation. Use this method if you seek for * test objects stored in the context. * * @param variableExpression expression to search for. * @throws CitrusRuntimeException * @return value of the variable as object */ public Object getVariableObject(final String variableExpression) { String variableName = VariableUtils.cutOffVariablesPrefix(variableExpression); if (!variables.containsKey(variableName)) { throw new CitrusRuntimeException("Unknown variable '" + variableName + "'"); } return variables.get(variableName); }
@Override public void doExecute(TestContext context) { log.info("Executing template '" + getName() + "' with " + actions.size() + " embedded actions"); TestContext innerContext; // decide wheather to use global test context or not if (globalContext) { innerContext = context; } else { innerContext = new TestContext(); innerContext.setFunctionRegistry(context.getFunctionRegistry()); GlobalVariables globalVariables = new GlobalVariables(); globalVariables.getVariables().putAll(context.getGlobalVariables()); innerContext.setGlobalVariables(globalVariables); innerContext.getVariables().putAll(context.getVariables()); innerContext.setMessageValidatorRegistry(context.getMessageValidatorRegistry()); innerContext.setValidationMatcherRegistry(context.getValidationMatcherRegistry()); innerContext.setTestListeners(context.getTestListeners()); innerContext.setMessageConstructionInterceptors(context.getMessageConstructionInterceptors()); } for (Entry<String, String> entry : parameter.entrySet()) { String param = entry.getKey(); String paramValue = entry.getValue(); paramValue = VariableUtils.replaceVariablesInString(paramValue, innerContext, false); if (context.getFunctionRegistry().isFunction(paramValue)) { paramValue = FunctionUtils.resolveFunction(paramValue, context); } log.info("Setting parameter for template " + param + "=" + paramValue); innerContext.setVariable(param, paramValue); } for (TestAction action : actions) { action.execute(innerContext); } log.info("Template was executed successfully"); }