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:
023: import javolution.util.FastList;
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.ofbiz.base.component.ComponentConfig;
028: import org.ofbiz.base.config.GenericConfigException;
029: import org.ofbiz.base.config.ResourceHandler;
030: import org.ofbiz.base.util.Debug;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033:
034: /**
035: * Use this class in a JUnit test runner to prepare the TestSuite.
036: */
037: public class JunitSuiteWrapper {
038:
039: public static final String module = JunitSuiteWrapper.class
040: .getName();
041:
042: protected List modelTestSuiteList = FastList.newInstance();
043:
044: public JunitSuiteWrapper(String componentName) {
045: List testSuiteInfoList = ComponentConfig
046: .getAllTestSuiteInfos(componentName);
047: Iterator testSuiteInfoIter = testSuiteInfoList.iterator();
048: while (testSuiteInfoIter.hasNext()) {
049: ComponentConfig.TestSuiteInfo testSuiteInfo = (ComponentConfig.TestSuiteInfo) testSuiteInfoIter
050: .next();
051:
052: ResourceHandler testSuiteResource = testSuiteInfo
053: .createResourceHandler();
054: try {
055: Document testSuiteDocument = testSuiteResource
056: .getDocument();
057: // TODO create TestSuite object based on this that will contain its TestCase objects
058: Element documentElement = testSuiteDocument
059: .getDocumentElement();
060: ModelTestSuite modelTestSuite = new ModelTestSuite(
061: documentElement);
062: this .modelTestSuiteList.add(modelTestSuite);
063: } catch (GenericConfigException e) {
064: String errMsg = "Error reading XML document from ResourceHandler for loader ["
065: + testSuiteResource.getLoaderName()
066: + "] and location ["
067: + testSuiteResource.getLocation() + "]";
068: Debug.logError(e, errMsg, module);
069: throw new IllegalArgumentException(errMsg);
070: }
071: }
072: }
073:
074: public void populateTestSuite(TestSuite suite) {
075: Iterator modelTestSuiteIter = this .modelTestSuiteList
076: .iterator();
077: while (modelTestSuiteIter.hasNext()) {
078: ModelTestSuite modelTestSuite = (ModelTestSuite) modelTestSuiteIter
079: .next();
080: List testList = modelTestSuite.getTestList();
081: Iterator testIter = testList.iterator();
082: while (testIter.hasNext()) {
083: Test tst = (Test) testIter.next();
084: suite.addTest(tst);
085: }
086: }
087: }
088:
089: public List getAllTestList() {
090: List allTestList = FastList.newInstance();
091:
092: Iterator modelTestSuiteIter = this .modelTestSuiteList
093: .iterator();
094: while (modelTestSuiteIter.hasNext()) {
095: ModelTestSuite modelTestSuite = (ModelTestSuite) modelTestSuiteIter
096: .next();
097: List testList = modelTestSuite.getTestList();
098: Iterator testIter = testList.iterator();
099: while (testIter.hasNext()) {
100: Test tst = (Test) testIter.next();
101: allTestList.add(tst);
102: }
103: }
104:
105: return allTestList;
106: }
107: }
|