@Override public void gatherArtifactInfo(ArtifactInfoContext aic) { SimpleMethod simpleMethodToCall; try { simpleMethodToCall = SimpleMethod.getSimpleMethod(this.xmlURL, this.methodName); if (simpleMethodToCall != null) { if (!aic.hasVisited(simpleMethodToCall)) { aic.addSimpleMethod(simpleMethodToCall); simpleMethodToCall.gatherArtifactInfo(aic); } } } catch (MiniLangException e) { Debug.logWarning( "Could not find <simple-method name=\"" + this.methodName + "\"> in XML document " + this.xmlResource + ": " + e.toString(), module); } }
public CallSimpleMethod(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); if (MiniLangValidate.validationOn()) { MiniLangValidate.attributeNames( simpleMethod, element, "method-name", "xml-resource", "scope"); MiniLangValidate.requiredAttributes(simpleMethod, element, "method-name"); MiniLangValidate.constantAttributes( simpleMethod, element, "method-name", "xml-resource", "scope"); MiniLangValidate.childElements(simpleMethod, element, "result-to-field"); } this.methodName = element.getAttribute("method-name"); String xmlResourceAttribute = element.getAttribute("xml-resource"); if (xmlResourceAttribute.isEmpty()) { xmlResourceAttribute = simpleMethod.getFromLocation(); } this.xmlResource = xmlResourceAttribute; URL xmlURL = null; try { xmlURL = FlexibleLocation.resolveLocation(this.xmlResource); } catch (MalformedURLException e) { MiniLangValidate.handleError( "Could not find SimpleMethod XML document in resource: " + this.xmlResource + "; error was: " + e.toString(), simpleMethod, element); } this.xmlURL = xmlURL; this.scope = element.getAttribute("scope"); List<? extends Element> resultToFieldElements = UtilXml.childElementList(element, "result-to-field"); if (UtilValidate.isNotEmpty(resultToFieldElements)) { if (!"function".equals(this.scope)) { MiniLangValidate.handleError( "Inline scope cannot include <result-to-field> elements.", simpleMethod, element); } List<ResultToField> resultToFieldList = new ArrayList<ResultToField>(resultToFieldElements.size()); for (Element resultToFieldElement : resultToFieldElements) { resultToFieldList.add(new ResultToField(resultToFieldElement, simpleMethod)); } this.resultToFieldList = resultToFieldList; } else { this.resultToFieldList = null; } }
@Override public boolean exec(MethodContext methodContext) throws MiniLangException { if (UtilValidate.isEmpty(this.methodName)) { throw new MiniLangRuntimeException("method-name attribute is empty", this); } SimpleMethod simpleMethodToCall = SimpleMethod.getSimpleMethod(this.xmlURL, this.methodName); if (simpleMethodToCall == null) { throw new MiniLangRuntimeException( "Could not find <simple-method name=\"" + this.methodName + "\"> in XML document " + this.xmlResource, this); } MethodContext localContext = methodContext; if ("function".equals(this.scope)) { Map<String, Object> localEnv = new HashMap<String, Object>(); localEnv.putAll(methodContext.getEnvMap()); localEnv.remove(this.simpleMethod.getEventResponseCodeName()); localEnv.remove(this.simpleMethod.getServiceResponseMessageName()); localContext = new MethodContext(localEnv, methodContext.getLoader(), methodContext.getMethodType()); } String returnVal = simpleMethodToCall.exec(localContext); if (Debug.verboseOn()) Debug.logVerbose( "Called simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], returnVal is [" + returnVal + "]", module); if (simpleMethodToCall.getDefaultErrorCode().equals(returnVal)) { if (methodContext.getMethodType() == MethodContext.EVENT) { methodContext.putEnv( simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); } else if (methodContext.getMethodType() == MethodContext.SERVICE) { methodContext.putEnv( simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); } return false; } if (methodContext.getMethodType() == MethodContext.EVENT) { // FIXME: This doesn't make sense. We are comparing the called method's response code with // this method's // response code. Since response codes are configurable per method, this code will fail. String responseCode = (String) localContext.getEnv(this.simpleMethod.getEventResponseCodeName()); if (this.simpleMethod.getDefaultErrorCode().equals(responseCode)) { Debug.logWarning( "Got error [" + responseCode + "] calling inline simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], message is " + methodContext.getEnv(this.simpleMethod.getEventErrorMessageName()), module); return false; } } else if (methodContext.getMethodType() == MethodContext.SERVICE) { // FIXME: This doesn't make sense. We are comparing the called method's response message with // this method's // response message. Since response messages are configurable per method, this code will fail. String responseMessage = (String) localContext.getEnv(this.simpleMethod.getServiceResponseMessageName()); if (this.simpleMethod.getDefaultErrorCode().equals(responseMessage)) { Debug.logWarning( "Got error [" + responseMessage + "] calling inline simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], message is " + methodContext.getEnv(this.simpleMethod.getServiceErrorMessageName()) + ", and the error message list is: " + methodContext.getEnv(this.simpleMethod.getServiceErrorMessageListName()), module); return false; } } if ("function".equals(this.scope) && this.resultToFieldList != null) { Map<String, Object> results = localContext.getResults(); if (results != null) { for (ResultToField resultToField : this.resultToFieldList) { resultToField.exec(methodContext.getEnvMap(), results); } } } return true; }
@Override public void run(TestResult result) { result.startTest(this); try { // define request Security security = SecurityFactory.getInstance(delegator); MockServletContext servletContext = new MockServletContext(); request.setAttribute("security", security); request.setAttribute("servletContext", servletContext); request.setAttribute("delegator", delegator); request.setAttribute("dispatcher", dispatcher); Map<String, Object> serviceResult = SimpleMethod.runSimpleService( methodLocation, methodName, dispatcher.getDispatchContext(), UtilMisc.toMap( "test", this, "testResult", result, "locale", Locale.getDefault(), "request", request, "response", response)); // do something with the errorMessage String errorMessage = (String) serviceResult.get(ModelService.ERROR_MESSAGE); if (UtilValidate.isNotEmpty(errorMessage)) { result.addFailure(this, new AssertionFailedError(errorMessage)); } // do something with the errorMessageList List<Object> errorMessageList = UtilGenerics.cast(serviceResult.get(ModelService.ERROR_MESSAGE_LIST)); if (UtilValidate.isNotEmpty(errorMessageList)) { for (Object message : errorMessageList) { result.addFailure(this, new AssertionFailedError(message.toString())); } } // do something with the errorMessageMap Map<String, Object> errorMessageMap = UtilGenerics.cast(serviceResult.get(ModelService.ERROR_MESSAGE_MAP)); if (!UtilValidate.isEmpty(errorMessageMap)) { for (Map.Entry<String, Object> entry : errorMessageMap.entrySet()) { result.addFailure( this, new AssertionFailedError(entry.getKey() + ": " + entry.getValue())); } } } catch (MiniLangException e) { result.addError(this, e); } catch (SecurityConfigurationException e) { result.addError(this, e); } result.endTest(this); }