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