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:
022: import junit.framework.TestResult;
023: import junit.framework.TestSuite;
024:
025: import org.ofbiz.base.container.Container;
026: import org.ofbiz.base.container.ContainerException;
027: import org.ofbiz.base.util.Debug;
028:
029: /**
030: * A Container implementation to run the tests configured through this testtools stuff.
031: */
032: public class TestRunContainer implements Container {
033:
034: public static final String module = TestRunContainer.class
035: .getName();
036: protected TestResult results = null;
037: protected String configFile = null;
038: protected String component = null;
039:
040: /**
041: * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
042: */
043: public void init(String[] args, String configFile) {
044: this .configFile = configFile;
045: if (args != null) {
046: for (int i = 0; i < args.length; i++) {
047: String argument = args[i];
048: // arguments can prefix w/ a '-'. Just strip them off
049: if (argument.startsWith("-")) {
050: int subIdx = 1;
051: if (argument.startsWith("--")) {
052: subIdx = 2;
053: }
054: argument = argument.substring(subIdx);
055: }
056:
057: // parse the arguments
058: if (argument.indexOf("=") != -1) {
059: String argumentName = argument.substring(0,
060: argument.indexOf("="));
061: String argumentVal = argument.substring(argument
062: .indexOf("=") + 1);
063:
064: if ("component".equalsIgnoreCase(argumentName)) {
065: this .component = argumentVal;
066: }
067: }
068: }
069: }
070: }
071:
072: public boolean start() throws ContainerException {
073: //ContainerConfig.Container jc = ContainerConfig.getContainer("junit-container", configFile);
074:
075: // get the tests to run
076: JunitSuiteWrapper jsWrapper = new JunitSuiteWrapper(component);
077:
078: // load the tests into the suite
079: TestSuite suite = new TestSuite();
080: jsWrapper.populateTestSuite(suite);
081:
082: // holder for the results
083: results = new TestResult();
084:
085: // run the tests
086: suite.run(results);
087:
088: // dispay the results
089: Debug.log("[JUNIT] Pass: " + results.wasSuccessful()
090: + " | # Tests: " + results.runCount() + " | # Failed: "
091: + results.failureCount() + " # Errors: "
092: + results.errorCount(), module);
093: if (Debug.importantOn()) {
094: Debug
095: .log(
096: "[JUNIT] ----------------------------- ERRORS ----------------------------- [JUNIT]",
097: module);
098: Enumeration err = results.errors();
099: if (!err.hasMoreElements()) {
100: Debug.log("None");
101: } else {
102: while (err.hasMoreElements()) {
103: Debug.log("--> " + err.nextElement(), module);
104: }
105: }
106: Debug
107: .log(
108: "[JUNIT] ------------------------------------------------------------------ [JUNIT]",
109: module);
110: Debug
111: .log(
112: "[JUNIT] ---------------------------- FAILURES ---------------------------- [JUNIT]",
113: module);
114: Enumeration fail = results.failures();
115: if (!fail.hasMoreElements()) {
116: Debug.log("None");
117: } else {
118: while (fail.hasMoreElements()) {
119: Debug.log("--> " + fail.nextElement(), module);
120: }
121: }
122: Debug
123: .log(
124: "[JUNIT] ------------------------------------------------------------------ [JUNIT]",
125: module);
126: }
127:
128: return true;
129: }
130:
131: public void stop() throws ContainerException {
132: try {
133: Thread.sleep(2000);
134: } catch (Exception e) {
135: throw new ContainerException(e);
136: }
137: }
138: }
|