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.Enumeration;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import javolution.util.FastList;
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.base.util.ObjectType;
030: import org.ofbiz.base.util.UtilValidate;
031: import org.ofbiz.base.util.UtilXml;
032: import org.ofbiz.entity.GenericDelegator;
033: import org.ofbiz.service.GenericDispatcher;
034: import org.ofbiz.service.LocalDispatcher;
035: import org.w3c.dom.Element;
036:
037: /**
038: * Use this class in a JUnit test runner to bootstrap the Test Suite runner.
039: */
040: public class ModelTestSuite {
041:
042: public static final String module = ModelTestSuite.class.getName();
043:
044: protected String suiteName;
045: protected String delegatorName;
046: protected String dispatcherName;
047:
048: protected GenericDelegator delegator;
049: protected LocalDispatcher dispatcher;
050:
051: protected List testList = FastList.newInstance();
052:
053: public ModelTestSuite(Element mainElement) {
054: this .suiteName = mainElement.getAttribute("suite-name");
055:
056: this .delegatorName = mainElement.getAttribute("delegator-name");
057: if (UtilValidate.isEmpty(this .delegatorName))
058: this .delegatorName = "test";
059:
060: this .dispatcherName = mainElement
061: .getAttribute("dispatcher-name");
062: if (UtilValidate.isEmpty(this .dispatcherName))
063: this .dispatcherName = "test-dispatcher";
064:
065: this .delegator = GenericDelegator
066: .getGenericDelegator(this .delegatorName);
067: this .dispatcher = GenericDispatcher.getLocalDispatcher(
068: this .dispatcherName, delegator);
069:
070: List testCaseElementList = UtilXml.childElementList(
071: mainElement, "test-case");
072: Iterator testCaseElementIter = testCaseElementList.iterator();
073: while (testCaseElementIter.hasNext()) {
074: Element testCaseElement = (Element) testCaseElementIter
075: .next();
076: String caseName = testCaseElement.getAttribute("case-name");
077:
078: Element childElement = UtilXml
079: .firstChildElement(testCaseElement);
080: String nodeName = childElement.getNodeName();
081: if ("junit-test-suite".equals(nodeName)) {
082: String className = childElement
083: .getAttribute("class-name");
084:
085: try {
086: Class clz = ObjectType.loadClass(className);
087: TestSuite suite = new TestSuite();
088: suite.addTestSuite(clz);
089: Enumeration testEnum = suite.tests();
090: int testsAdded = 0;
091: int casesAdded = 0;
092: while (testEnum.hasMoreElements()) {
093: Test tst = (Test) testEnum.nextElement();
094: this .testList.add(tst);
095: casesAdded += tst.countTestCases();
096: testsAdded++;
097: }
098: Debug.logInfo("Added " + testsAdded + " tests ["
099: + casesAdded + " cases] from the class: "
100: + className, module);
101: } catch (Exception e) {
102: String errMsg = "Unable to load test suite class : "
103: + className;
104: Debug.logError(e, errMsg, module);
105: }
106: } else if ("service-test".equals(nodeName)) {
107: this .testList.add(new ServiceTest(caseName, this ,
108: childElement));
109: } else if ("simple-method-test".equals(nodeName)) {
110: this .testList.add(new SimpleMethodTest(caseName, this ,
111: childElement));
112: } else if ("entity-xml-assert".equals(nodeName)) {
113: this .testList.add(new EntityXmlAssertTest(caseName,
114: this , childElement));
115: } else if ("jython-test".equals(nodeName)) {
116: this .testList.add(new JythonTest(caseName, this ,
117: childElement));
118: }
119: }
120: }
121:
122: String getSuiteName() {
123: return this .suiteName;
124: }
125:
126: GenericDelegator getDelegator() {
127: return this .delegator;
128: }
129:
130: LocalDispatcher getDispatcher() {
131: return this .dispatcher;
132: }
133:
134: List getTestList() {
135: return testList;
136: }
137: }
|