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 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