KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > container > JunitContainer


1 /*
2  * $Id: JunitContainer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.container;
26
27 import java.util.Enumeration JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import junit.framework.TestResult;
31 import junit.framework.TestSuite;
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.ObjectType;
34
35
36 /**
37  *
38  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
39  * @version $Rev: 5462 $
40  * @since 3.1
41  */

42 public class JunitContainer implements Container {
43
44     public static final String JavaDoc module = JunitContainer.class.getName();
45     protected TestResult results = null;
46     protected String JavaDoc configFile = null;
47
48     /**
49      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
50      */

51     public void init(String JavaDoc[] args, String JavaDoc configFile) {
52         this.configFile = configFile;
53     }
54
55     public boolean start() throws ContainerException {
56         ContainerConfig.Container jc = ContainerConfig.getContainer("junit-container", configFile);
57
58         // get the tests to run
59
Iterator JavaDoc ti = jc.properties.values().iterator();
60         if (ti == null) {
61             Debug.log("No tests to load", module);
62             return true;
63         }
64
65         // load the tests into the suite
66
TestSuite suite = new TestSuite();
67         while (ti.hasNext()) {
68             ContainerConfig.Container.Property prop = (ContainerConfig.Container.Property) ti.next();
69             Class JavaDoc clz = null;
70             try {
71                 clz = ObjectType.loadClass(prop.value);
72                 suite.addTestSuite(clz);
73             } catch (Exception JavaDoc e) {
74                 Debug.logError(e, "Unable to load test suite class : " + prop.value, module);
75             }
76         }
77
78         // holder for the results
79
results = new TestResult();
80
81         // run the tests
82
suite.run(results);
83
84         // dispay the results
85
Debug.log("[JUNIT] Pass: " + results.wasSuccessful() + " | # Tests: " + results.runCount() + " | # Failed: " +
86                 results.failureCount() + " # Errors: " + results.errorCount(), module);
87         if (Debug.infoOn()) {
88             Debug.log("[JUNIT] ----------------------------- ERRORS ----------------------------- [JUNIT]", module);
89             Enumeration JavaDoc err = results.errors();
90             if (!err.hasMoreElements()) {
91                 Debug.log("None");
92             } else {
93                 while (err.hasMoreElements()) {
94                     Debug.log("--> " + err.nextElement(), module);
95                 }
96             }
97             Debug.log("[JUNIT] ------------------------------------------------------------------ [JUNIT]", module);
98             Debug.log("[JUNIT] ---------------------------- FAILURES ---------------------------- [JUNIT]", module);
99             Enumeration JavaDoc fail = results.failures();
100             if (!fail.hasMoreElements()) {
101                 Debug.log("None");
102             } else {
103                 while (fail.hasMoreElements()) {
104                     Debug.log("--> " + fail.nextElement(), module);
105                 }
106             }
107             Debug.log("[JUNIT] ------------------------------------------------------------------ [JUNIT]", module);
108         }
109
110         return true;
111     }
112
113     public void stop() throws ContainerException {
114         try {
115             Thread.sleep(5000);
116         } catch (Exception JavaDoc e) {
117             throw new ContainerException(e);
118         }
119     }
120 }
121
Popular Tags