KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > junit > BaseTestCase


1 /*
2  *
3  * Derby - Class BaseTestCase
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.junit;
21
22 import junit.framework.TestCase;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.PrintStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.security.AccessController JavaDoc;
32
33 import java.security.PrivilegedActionException JavaDoc;
34
35
36 /**
37  * Base class for JUnit tests.
38  */

39 public abstract class BaseTestCase
40     extends TestCase {
41     
42     /**
43      * No argument constructor made private to enforce naming of test cases.
44      * According to JUnit documentation, this constructor is provided for
45      * serialization, which we don't currently use.
46      *
47      * @see #BaseTestCase(String)
48      */

49     private BaseTestCase() {}
50
51     /**
52      * Create a test case with the given name.
53      *
54      * @param name name of the test case.
55      */

56     public BaseTestCase(String JavaDoc name) {
57         super(name);
58     }
59     
60     /**
61      * Run the test and force installation of a security
62      * manager with the default test policy file.
63      * Individual tests can run without a security
64      * manager or with a different policy file using
65      * the decorators obtained from SecurityManagerSetup.
66      * <BR>
67      * Method is final to ensure security manager is
68      * enabled by default. Tests should not need to
69      * override runTest, instead use test methods
70      * setUp, tearDown methods and decorators.
71      */

72     public final void runBare() throws Throwable JavaDoc {
73         if (getTestConfiguration().defaultSecurityManagerSetup())
74             assertSecurityManager();
75          
76         super.runBare();
77     }
78
79     /**
80      * Return the current configuration for the test.
81      */

82     public final TestConfiguration getTestConfiguration()
83     {
84         return TestConfiguration.getCurrent();
85     }
86     
87     /**
88      * Get the folder where a test leaves any information
89      * about its failure.
90      * @return Folder to use.
91      * @see TestConfiguration#getFailureFolder(TestCase)
92      */

93     public final File JavaDoc getFailureFolder() {
94         return getTestConfiguration().getFailureFolder(this);
95     }
96     
97     /**
98      * Print alarm string
99      * @param text String to print
100      */

101     public static void alarm(final String JavaDoc text) {
102         out.println("ALARM: " + text);
103     }
104
105     /**
106      * Print debug string.
107      * @param text String to print
108      */

109     public static void println(final String JavaDoc text) {
110         if (TestConfiguration.getCurrent().isVerbose()) {
111             out.println("DEBUG: " + text);
112         }
113     }
114
115     /**
116      * Print debug string.
117      * @param t Throwable object to print stack trace from
118      */

119     public static void printStackTrace(Throwable JavaDoc t)
120     {
121         while ( t!= null) {
122             t.printStackTrace(out);
123             
124             if (t instanceof SQLException JavaDoc) {
125                 t = ((SQLException JavaDoc) t).getNextException();
126             } else {
127                 break;
128             }
129         }
130     }
131
132     private final static PrintStream JavaDoc out = System.out;
133     
134     /**
135      * Set system property
136      *
137      * @param name name of the property
138      * @param value value of the property
139      */

140     protected static void setSystemProperty(final String JavaDoc name,
141                         final String JavaDoc value)
142     {
143     
144     AccessController.doPrivileged
145         (new java.security.PrivilegedAction JavaDoc(){
146             
147             public Object JavaDoc run(){
148             System.setProperty( name, value);
149             return null;
150             
151             }
152             
153         }
154          );
155     
156     }
157     /**
158      * Remove system property
159      *
160      * @param name name of the property
161      */

162     protected static void removeSystemProperty(final String JavaDoc name)
163     {
164     
165     AccessController.doPrivileged
166         (new java.security.PrivilegedAction JavaDoc(){
167             
168             public Object JavaDoc run(){
169             System.getProperties().remove(name);
170             return null;
171             
172             }
173             
174         }
175          );
176     
177     }
178     /**
179      * Get system property.
180      *
181      * @param name name of the property
182      */

183     protected static String JavaDoc getSystemProperty(final String JavaDoc name)
184     {
185
186     return (String JavaDoc )AccessController.doPrivileged
187         (new java.security.PrivilegedAction JavaDoc(){
188
189             public Object JavaDoc run(){
190             return System.getProperty(name);
191
192             }
193
194         }
195          );
196     }
197     
198     /**
199      * Obtain the URL for a test resource, e.g. a policy
200      * file or a SQL script.
201      * @param name Resource name, typically - org.apache.derbyTesing.something
202      * @return URL to the resource, null if it does not exist.
203      */

204     protected static URL JavaDoc getTestResource(final String JavaDoc name)
205     {
206
207     return (URL JavaDoc)AccessController.doPrivileged
208         (new java.security.PrivilegedAction JavaDoc(){
209
210             public Object JavaDoc run(){
211             return BaseTestCase.class.getClassLoader().
212                 getResource(name);
213
214             }
215
216         }
217          );
218     }
219   
220     /**
221      * Open the URL for a a test resource, e.g. a policy
222      * file or a SQL script.
223      * @param url URL obtained from getTestResource
224      * @return An open stream
225     */

226     protected static InputStream JavaDoc openTestResource(final URL JavaDoc url)
227         throws PrivilegedActionException JavaDoc
228     {
229         return (InputStream JavaDoc)AccessController.doPrivileged
230         (new java.security.PrivilegedExceptionAction JavaDoc(){
231
232             public Object JavaDoc run() throws IOException JavaDoc{
233             return url.openStream();
234
235             }
236
237         }
238          );
239     }
240     
241     /**
242      * Assert a security manager is installed.
243      *
244      */

245     public static void assertSecurityManager()
246     {
247         assertNotNull("No SecurityManager installed",
248                 System.getSecurityManager());
249     }
250
251     /**
252      * Compare the contents of two streams.
253      * The streams are closed after they are exhausted.
254      *
255      * @param is1 the first stream
256      * @param is2 the second stream
257      * @throws IOException if reading from the streams fail
258      * @throws AssertionFailedError if the stream contents are not equal
259      */

260     public static void assertEquals(InputStream JavaDoc is1, InputStream JavaDoc is2)
261             throws IOException JavaDoc {
262         if (is1 == null || is2 == null) {
263             assertNull("InputStream is2 is null, is1 is not", is1);
264             assertNull("InputStream is1 is null, is2 is not", is2);
265             return;
266         }
267         long index = 0;
268         int b1 = is1.read();
269         int b2 = is2.read();
270         do {
271             // Avoid string concatenation for every byte in the stream.
272
if (b1 != b2) {
273                 assertEquals("Streams differ at index " + index, b1, b2);
274             }
275             index++;
276             b1 = is1.read();
277             b2 = is2.read();
278         } while (b1 != -1 || b2 != -1);
279         is1.close();
280         is2.close();
281     }
282
283     /**
284      * Compare the contents of two readers.
285      * The readers are closed after they are exhausted.
286      *
287      * @param r1 the first reader
288      * @param r2 the second reader
289      * @throws IOException if reading from the streams fail
290      * @throws AssertionFailedError if the reader contents are not equal
291      */

292     public static void assertEquals(Reader JavaDoc r1, Reader JavaDoc r2)
293             throws IOException JavaDoc {
294         long index = 0;
295         if (r1 == null || r2 == null) {
296             assertNull("Reader r2 is null, r1 is not", r1);
297             assertNull("Reader r1 is null, r2 is not", r2);
298             return;
299         }
300         int c1 = r1.read();
301         int c2 = r2.read();
302         do {
303             // Avoid string concatenation for every char in the stream.
304
if (c1 != c2) {
305                 assertEquals("Streams differ at index " + index, c1, c2);
306             }
307             index++;
308             c1 = r1.read();
309             c2 = r2.read();
310         } while (c1 != -1 || c2 != -1);
311         r1.close();
312         r2.close();
313     }
314 } // End class BaseTestCase
315
Free Books   Free Magazines  
Popular Tags