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