Search in sources :

Example 1 with SimpleMethod

use of org.apache.ofbiz.minilang.SimpleMethod in project ofbiz-framework by apache.

the class CallSimpleMethod method exec.

@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;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) HashMap(java.util.HashMap) SimpleMethod(org.apache.ofbiz.minilang.SimpleMethod) MethodContext(org.apache.ofbiz.minilang.method.MethodContext)

Example 2 with SimpleMethod

use of org.apache.ofbiz.minilang.SimpleMethod in project ofbiz-framework by apache.

the class CallSimpleMethod method gatherArtifactInfo.

@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);
    }
}
Also used : SimpleMethod(org.apache.ofbiz.minilang.SimpleMethod) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 3 with SimpleMethod

use of org.apache.ofbiz.minilang.SimpleMethod in project ofbiz-framework by apache.

the class MiniLangTests method testAssignmentOperators.

public void testAssignmentOperators() throws Exception {
    // <check-errors> and <add-error> tests
    SimpleMethod methodToTest = createSimpleMethod("<simple-method name=\"testCheckErrors\"><check-errors/></simple-method>");
    MethodContext context = createServiceMethodContext();
    String result = methodToTest.exec(context);
    assertEquals("<check-errors> success result", methodToTest.getDefaultSuccessCode(), result);
    List<String> messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
    assertNull("<check-errors> null error message list", messages);
    methodToTest = createSimpleMethod("<simple-method name=\"testCheckErrors\"><add-error><fail-message message=\"This should fail\"/></add-error><check-errors/></simple-method>");
    context = createServiceMethodContext();
    result = methodToTest.exec(context);
    assertEquals("<check-errors> error result", methodToTest.getDefaultErrorCode(), result);
    messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
    assertNotNull("<check-errors> error message list", messages);
    assertTrue("<check-errors> error message text", messages.contains("This should fail"));
    // <assert>, <not>,  and <if-empty> tests
    methodToTest = createSimpleMethod("<simple-method name=\"testAssert\"><assert><not><if-empty field=\"locale\"/></not></assert><check-errors/></simple-method>");
    context = createServiceMethodContext();
    result = methodToTest.exec(context);
    assertEquals("<assert> success result", methodToTest.getDefaultSuccessCode(), result);
    messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
    assertNull("<assert> null error message list", messages);
    methodToTest = createSimpleMethod("<simple-method name=\"testAssert\"><assert><if-empty field=\"locale\"/></assert><check-errors/></simple-method>");
    context = createServiceMethodContext();
    result = methodToTest.exec(context);
    assertEquals("<assert> error result", methodToTest.getDefaultErrorCode(), result);
    messages = context.getEnv(methodToTest.getServiceErrorMessageListName());
    assertNotNull("<assert> error message list", messages);
    String errorMessage = messages.get(0);
    assertTrue("<assert> error message text", errorMessage.startsWith("Assertion failed:"));
}
Also used : SimpleMethod(org.apache.ofbiz.minilang.SimpleMethod) MethodContext(org.apache.ofbiz.minilang.method.MethodContext)

Example 4 with SimpleMethod

use of org.apache.ofbiz.minilang.SimpleMethod in project ofbiz-framework by apache.

the class MiniLangTests method testFieldToResultOperation.

public void testFieldToResultOperation() throws Exception {
    String simpleMethodXml = "<simple-method name=\"testFieldToResult\">" + "  <set field=\"resultValue\" value=\"someResultValue\"/>" + "  <set field=\"result1\" value=\"dynamicResultName\"/>" + "  <field-to-result field=\"resultValue\" result-name=\"constantResultName\"/>" + "  <field-to-result field=\"resultValue\" result-name=\"${result1}\"/>" + "</simple-method>";
    SimpleMethod methodToTest = createSimpleMethod(simpleMethodXml);
    MethodContext context = createServiceMethodContext();
    String result = methodToTest.exec(context);
    assertEquals("testFieldToResult success result", methodToTest.getDefaultSuccessCode(), result);
    assertEquals("Plain expression result name set", "someResultValue", context.getResult("constantResultName"));
    assertEquals("Nested expression result name set", "someResultValue", context.getResult("dynamicResultName"));
}
Also used : SimpleMethod(org.apache.ofbiz.minilang.SimpleMethod) MethodContext(org.apache.ofbiz.minilang.method.MethodContext)

Example 5 with SimpleMethod

use of org.apache.ofbiz.minilang.SimpleMethod in project ofbiz-framework by apache.

the class ModelTestSuite method parseTestElement.

private void parseTestElement(String caseName, Element testElement) {
    String nodeName = testElement.getNodeName();
    if ("junit-test-suite".equals(nodeName)) {
        String className = testElement.getAttribute("class-name");
        try {
            @SuppressWarnings("unchecked") Class<? extends TestCase> clz = (Class<? extends TestCase>) ObjectType.loadClass(className);
            TestSuite suite = new TestSuite();
            suite.addTestSuite(clz);
            Enumeration<?> testEnum = suite.tests();
            int testsAdded = 0;
            int casesAdded = 0;
            while (testEnum.hasMoreElements()) {
                Test tst = (Test) testEnum.nextElement();
                this.testList.add(tst);
                casesAdded += tst.countTestCases();
                testsAdded++;
            }
            Debug.logInfo("Added " + testsAdded + " tests [" + casesAdded + " cases] from the class: " + className, module);
        } catch (Exception e) {
            String errMsg = "Unable to load test suite class : " + className;
            Debug.logError(e, errMsg, module);
        }
    } else if ("groovy-test-suite".equals(nodeName)) {
        try {
            Class testClass = GroovyUtil.getScriptClassFromLocation(testElement.getAttribute("location"));
            TestCase groovyTestCase = (GroovyScriptTestCase) testClass.newInstance();
            this.testList.add(new TestSuite(testClass, testElement.getAttribute("name")));
        } catch (GeneralException | InstantiationException | IllegalAccessException e) {
            Debug.logError(e, module);
        }
    } else if ("service-test".equals(nodeName)) {
        this.testList.add(new ServiceTest(caseName, testElement));
    } else if ("simple-method-test".equals(nodeName)) {
        if (UtilValidate.isNotEmpty(testElement.getAttribute("name"))) {
            this.testList.add(new SimpleMethodTest(caseName, testElement));
        } else {
            String methodLocation = testElement.getAttribute("location");
            List<SimpleMethod> simpleMethods;
            try {
                simpleMethods = SimpleMethod.getSimpleMethodsList(methodLocation, null);
                for (SimpleMethod simpleMethod : simpleMethods) {
                    String methodName = simpleMethod.getMethodName();
                    if (methodName.startsWith("test")) {
                        this.testList.add(new SimpleMethodTest(caseName + "." + methodName, methodLocation, methodName));
                    }
                }
            } catch (MiniLangException e) {
                Debug.logError(e, module);
            }
        }
    } else if ("webdriver-test".equals(nodeName)) {
        try {
            String className = "org.apache.ofbiz.testtools.WebDriverTest";
            Class<?> cl;
            cl = Class.forName(className);
            Constructor<?> con = cl.getConstructor(String.class, Element.class);
            this.testList.add((Test) con.newInstance(caseName, testElement));
        } catch (Exception e) {
            Debug.logError(e, module);
        }
    } else if ("entity-xml".equals(nodeName)) {
        this.testList.add(new EntityXmlAssertTest(caseName, testElement));
    } else if ("entity-xml-assert".equals(nodeName)) {
        // this is the old, deprecated name for the element, changed because it now does assert or load
        this.testList.add(new EntityXmlAssertTest(caseName, testElement));
    }
}
Also used : SimpleMethod(org.apache.ofbiz.minilang.SimpleMethod) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) GeneralException(org.apache.ofbiz.base.util.GeneralException) TestSuite(junit.framework.TestSuite) EntityTestCase(org.apache.ofbiz.entity.testtools.EntityTestCase) TestCase(junit.framework.TestCase) OFBizTestCase(org.apache.ofbiz.service.testtools.OFBizTestCase) Test(junit.framework.Test) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

SimpleMethod (org.apache.ofbiz.minilang.SimpleMethod)7 MiniLangException (org.apache.ofbiz.minilang.MiniLangException)4 MethodContext (org.apache.ofbiz.minilang.method.MethodContext)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 ArtifactInfoContext (org.apache.ofbiz.minilang.artifact.ArtifactInfoContext)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Test (junit.framework.Test)1 TestCase (junit.framework.TestCase)1 TestSuite (junit.framework.TestSuite)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 EntityTestCase (org.apache.ofbiz.entity.testtools.EntityTestCase)1 MiniLangRuntimeException (org.apache.ofbiz.minilang.MiniLangRuntimeException)1 GroupModel (org.apache.ofbiz.service.group.GroupModel)1 GroupServiceModel (org.apache.ofbiz.service.group.GroupServiceModel)1