View Javadoc

1   /********************************************************************************
2    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
3    * Copyright (c) 2004, Joerg and Kai Gellien
4    * All rights reserved.
5    *
6    * The Software is provided under the terms of the Common Public License 1.0
7    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
8    * Redistribution and use in source and binary forms, with or without
9    * modification, are permitted provided that the following conditions
10   * are met:
11   *
12   *     + Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     + Redistributions in binary form must reproduce the above
16   *       copyright notice, this list of conditions and the following
17   *       disclaimer in the documentation and/or other materials provided
18   *       with the distribution.
19   *
20   *     + Neither the name of the authors or DDTUnit, nor the
21   *       names of its contributors may be used to endorse or promote
22   *       products derived from this software without specific prior
23   *       written permission.
24   *
25   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36   ********************************************************************************/
37  package junitx.ddtunit;
38  
39  import junit.framework.AssertionFailedError;
40  import junit.framework.Test;
41  import junit.framework.TestCase;
42  import junit.textui.ResultPrinter;
43  
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  /**
48   * This class implements a simple monitor for testruns. <br/>It is implemented
49   * as singleton so we can use it internally to protocol test progress and
50   * results. <br/>Lateron there will be an implementaion of a TestRunner that
51   * conformes to the interface DDTTestListener
52   * 
53   * @author jg
54   */
55  public class DDTRunMonitor extends ResultPrinter implements DDTTestListener {
56      private static DDTRunMonitor singletonRef;
57  
58      private Logger log = LoggerFactory.getLogger(DDTRunMonitor.class);
59  
60      private int testRuns = 0;
61  
62      private int testErrors = 0;
63  
64      private int testFailures = 0;
65  
66      /**
67       * Create object of type DDTRunMonitor
68       */
69      private DDTRunMonitor() {
70          super(System.out);
71      }
72  
73      /**
74       * Get singleton instance of this class
75       * 
76       * @return singleton instance
77       */
78      static public DDTRunMonitor getInstance() {
79          if (singletonRef == null) {
80              singletonRef = new DDTRunMonitor();
81          }
82  
83          return singletonRef;
84      }
85  
86      /**
87       * Add error of executed test
88       * 
89       * @see junitx.ddtunit.DDTTestListener#addMethodTestError(junit.framework.Test,
90       *      java.lang.String, java.lang.Throwable)
91       */
92      public void addMethodTestError(Test test, String testName,
93              Throwable throwable) {
94          String method = "";
95  
96          if (TestCase.class.isInstance(test)) {
97              method = ((TestCase) test).getName();
98          }
99  
100         log.info("[" + test.getClass() + "] Error method " + method + ", test "
101                 + testName);
102         log.info("Cause: ", throwable);
103         this.testErrors++;
104     }
105 
106     /**
107      * Add failure of executed test
108      * 
109      * @see junitx.ddtunit.DDTTestListener#addMethodTestFailure(junit.framework.Test,
110      *      java.lang.String, junit.framework.AssertionFailedError)
111      */
112     public void addMethodTestFailure(Test test, String testName,
113             AssertionFailedError assertFailed) {
114         String method = "";
115 
116         if (TestCase.class.isInstance(test)) {
117             method = ((TestCase) test).getName();
118         }
119 
120         log.info("[" + test.getClass() + "] Failure method " + method
121                 + ", test " + testName);
122         log.info("Cause: ", assertFailed);
123         this.testFailures++;
124     }
125 
126     /**
127      * Notify about end of method test execution
128      * 
129      * @see junitx.ddtunit.DDTTestListener#endMethodTest(junitx.ddtunit.DDTTestCase,
130      *      java.lang.String)
131      */
132     public void endMethodTest(IDDTTestCase test, String testName) {
133         int testCount = test.countMethodTests();
134         String method = test.getName();
135         log.info("[" + test.getClass() + "] (" + this.testRuns + "/"
136                 + testCount + ") method \"" + method + "\", test \"" + testName
137                 + "\"");
138     }
139 
140     /**
141      * Notify about start of method test execution
142      * 
143      * @see junitx.ddtunit.DDTTestListener#startMethodTest(junitx.ddtunit.DDTTestCase,
144      *      java.lang.String)
145      */
146     public void startMethodTest(IDDTTestCase test, String testName) {
147         String method = test.getName();
148         int testCount = test.countMethodTests();
149         this.testRuns++;
150         log.debug("[" + test.getClass() + "] Start method " + method + " ("
151                 + this.testRuns + " of " + testCount + ") test " + testName);
152     }
153 
154     /**
155      * Just let the standard runner do this work - do nothing
156      * 
157      * @see junit.framework.TestListener#addError(junit.framework.Test,
158      *      java.lang.Throwable)
159      */
160     public void addError(Test test, Throwable t) {
161         String method = "";
162 
163         if (TestCase.class.isInstance(test)) {
164             method = ((TestCase) test).getName();
165         }
166         log.info("[" + test.getClass() + "] method " + method + " error:", t);
167     }
168 
169     /**
170      * Just let the standard runner do this work - do nothing
171      * 
172      * @see junit.framework.TestListener#addFailure(junit.framework.Test,
173      *      junit.framework.AssertionFailedError)
174      */
175     public void addFailure(Test test, AssertionFailedError t) {
176         String method = "";
177 
178         if (TestCase.class.isInstance(test)) {
179             method = ((TestCase) test).getName();
180         }
181 
182         log.info("[" + test.getClass() + "] Add Faiure method " + method, t);
183     }
184 
185     /**
186      * Just let the standard runner do this work - do nothing
187      * 
188      * @see junit.framework.TestListener#endTest(junit.framework.Test)
189      */
190     public void endTest(Test test) {
191         String method = "";
192         if (TestCase.class.isInstance(test)) {
193             method = ((TestCase) test).getName();
194         }
195         if (IDDTTestCase.class.isInstance(test)) {
196             int testCount = ((IDDTTestCase) test).countMethodTests();
197             log.info("[" + test.getClass() + "] method \"" + method + "\": "
198                     + testRuns + " of " + testCount + " test(s), " + testErrors
199                     + " error(s), " + testFailures + " failure(s)");
200         } else {
201             log
202                 .info("[" + test.getClass() + "] method \"" + method + "\": "
203                         + testErrors + " error(s), " + testFailures
204                         + " failure(s)");
205         }
206     }
207 
208     /**
209      * Just let the standard runner do this work - do nothing
210      * 
211      * @see junit.framework.TestListener#startTest(junit.framework.Test)
212      */
213     public void startTest(Test test) {
214         String method = "";
215         this.testRuns = 0;
216         this.testErrors = 0;
217         this.testFailures = 0;
218 
219         if (TestCase.class.isInstance(test)) {
220             method = ((TestCase) test).getName();
221         }
222         log.debug("[" + test.getClass() + "] Start method \"" + method + "\"");
223     }
224 }