1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package javax.jdo.util;
19
20 import java.io.PrintStream;
21
22 import junit.framework.TestCase;
23
24 /** */
25 public abstract 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