1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  package javax.jdo.util;
18  
19  import java.io.PrintStream;
20  
21  import junit.framework.AssertionFailedError;
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestResult;
25  import junit.textui.ResultPrinter;
26  
27  /***
28   * Default result printer implementation for running tests in batch mode.
29   * 
30   * @author Michael Bouschen
31   */
32  public class BatchResultPrinter
33      extends ResultPrinter
34  {
35      /*** */
36      public BatchResultPrinter(PrintStream writer) {
37          super(writer);
38      }
39          
40      /*** Called in case of a test error. */
41      public void addError(Test test, Throwable t) {
42          getWriter().print("   ERROR");
43      }
44          
45      /*** Called in case of a test failure. */ 
46      public void addFailure(Test test, AssertionFailedError t) {
47          getWriter().print("   FAILURE");
48      }
49          
50      /*** Called when a test case is finished. */
51      public void endTest(Test test) {
52          getWriter().println();
53      }
54          
55      /*** Called when a test case is started. */
56      public void startTest(Test test) {
57          String testName;
58          if (test instanceof TestCase) {
59              testName = getClassBaseName(test) + "." + ((TestCase)test).getName();
60          }
61          else {
62              testName = test.toString();
63          }
64          getWriter().print("RUN " + testName);
65      }
66          
67      /*** */
68      protected void printHeader(long runTime) {
69          getWriter().println("Time: "+elapsedTimeAsString(runTime));
70      }
71          
72      /*** */
73      protected void printFooter(TestResult result) {
74          if (result.wasSuccessful()) {
75              getWriter().print("OK");
76              getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")");
77                  
78          } else {
79              getWriter().println("FAILURES!!!");
80              getWriter().println("Tests run: "+result.runCount()+ 
81                                  ",  Failures: "+result.failureCount()+
82                                  ",  Errors: "+result.errorCount());
83          }
84      }
85          
86      // helper method
87          
88      /*** 
89       * @return Name of the class of the given object without package prefix
90       */
91      private String getClassBaseName(Object obj) {
92          if (obj == null) return null;
93          String className = obj.getClass().getName();
94          int index = className.lastIndexOf('.');
95          if (index != -1) {
96              className = className.substring(index + 1);
97          }
98          return className;
99      }
100         
101 }
102 
103