001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.testtools;
019:
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Locale;
023: import java.util.Map;
024:
025: import junit.framework.AssertionFailedError;
026: import junit.framework.TestResult;
027:
028: import org.ofbiz.base.util.UtilMisc;
029: import org.ofbiz.base.util.UtilValidate;
030: import org.ofbiz.minilang.MiniLangException;
031: import org.ofbiz.minilang.SimpleMethod;
032: import org.ofbiz.service.LocalDispatcher;
033: import org.ofbiz.service.ModelService;
034: import org.w3c.dom.Element;
035:
036: public class SimpleMethodTest extends TestCaseBase {
037:
038: public static final String module = ServiceTest.class.getName();
039:
040: protected String methodLocation;
041: protected String methodName;
042:
043: /**
044: * @param modelTestSuite
045: */
046: public SimpleMethodTest(String caseName,
047: ModelTestSuite modelTestSuite, Element mainElement) {
048: super (caseName, modelTestSuite);
049: this .methodLocation = mainElement.getAttribute("location");
050: this .methodName = mainElement.getAttribute("name");
051: }
052:
053: public int countTestCases() {
054: return 1;
055: }
056:
057: public void run(TestResult result) {
058: result.startTest(this );
059:
060: LocalDispatcher dispatcher = modelTestSuite.getDispatcher();
061:
062: try {
063:
064: Map serviceResult = SimpleMethod.runSimpleService(
065: methodLocation, methodName, dispatcher
066: .getDispatchContext(), UtilMisc.toMap(
067: "test", this , "testResult", result,
068: "locale", Locale.getDefault()));
069:
070: // do something with the errorMessage
071: String errorMessage = (String) serviceResult
072: .get(ModelService.ERROR_MESSAGE);
073: if (UtilValidate.isNotEmpty(errorMessage)) {
074: result.addFailure(this , new AssertionFailedError(
075: errorMessage));
076: }
077:
078: // do something with the errorMessageList
079: List errorMessageList = (List) serviceResult
080: .get(ModelService.ERROR_MESSAGE_LIST);
081: if (UtilValidate.isNotEmpty(errorMessageList)) {
082: Iterator i = errorMessageList.iterator();
083: while (i.hasNext()) {
084: result.addFailure(this , new AssertionFailedError(
085: (String) i.next()));
086: }
087: }
088:
089: // do something with the errorMessageMap
090: Map errorMessageMap = (Map) serviceResult
091: .get(ModelService.ERROR_MESSAGE_MAP);
092: if (!UtilValidate.isEmpty(errorMessageMap)) {
093: Iterator i = errorMessageMap.entrySet().iterator();
094: while (i.hasNext()) {
095: Map.Entry entry = (Map.Entry) i.next();
096: result.addFailure(this , new AssertionFailedError(
097: entry.getKey() + ": " + entry.getValue()));
098: }
099: }
100:
101: } catch (MiniLangException e) {
102: result.addError(this, e);
103: }
104:
105: result.endTest(this);
106: }
107: }
|