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.TestCase;
22  
23  /*** */
24  public class AbstractTest extends TestCase {
25  
26      /*** */
27      protected static PrintStream out = System.out;
28      
29      /*** If true, print extra messages. */
30      protected boolean verbose;
31  
32      /***
33       * Construct and initialize from properties.
34       */
35      protected AbstractTest() {
36          super(null);
37          verbose = Boolean.getBoolean("verbose");
38      }
39      
40      /***
41       * Determine if a class is loadable in the current environment.
42       */
43      protected static boolean isClassLoadable(String className) {
44          try {
45              Class.forName(className);
46              return true;
47          } catch (ClassNotFoundException ex) {
48              return false;
49          }
50      }
51      
52      /***
53       */
54      protected void println(String s) {
55          if (verbose) 
56              out.println(s);
57      }
58      
59      /*** New line.
60       */
61      public static final String NL = System.getProperty("line.separator");
62      
63      /*** A buffer of of error messages.
64       */
65      protected static StringBuffer messages;
66      
67      /*** Appends to error messages.
68       */
69      protected static synchronized void appendMessage(String message) {
70          if (message != null) {
71              if (messages == null) {
72                  messages = new StringBuffer();
73              }
74              messages.append(message);
75              messages.append(NL);
76          }
77      }
78      
79      /***
80       * Returns collected error messages, or <code>null</code> if there
81       * are none, and clears the buffer.
82       */
83      protected static synchronized String retrieveMessages() {
84          if (messages == null) {
85              return null;
86          }
87          final String msg = messages.toString();
88          messages = null;
89          return msg;
90      }
91      
92      /*** 
93       * Fail the test if there are any error messages.
94       */
95      protected void failOnError() {
96          String errors = retrieveMessages();
97          if (errors != null) {
98              fail (errors);
99          }
100     }
101 }
102