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