001: /*
002: *
003: * Derby - Class BaseTestCase
004: *
005: * Licensed to the Apache Software Foundation (ASF) under one or more
006: * contributor license agreements. See the NOTICE file distributed with
007: * this work for additional information regarding copyright ownership.
008: * The ASF licenses this file to You under the Apache License, Version 2.0
009: * (the "License"); you may not use this file except in compliance with
010: * the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020: package org.apache.derbyTesting.junit;
021:
022: import junit.framework.TestCase;
023:
024: import java.io.File;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.Reader;
028: import java.io.PrintStream;
029: import java.net.URL;
030: import java.sql.SQLException;
031: import java.security.AccessController;
032:
033: import java.security.PrivilegedActionException;
034:
035: /**
036: * Base class for JUnit tests.
037: */
038: public abstract class BaseTestCase extends TestCase {
039:
040: /**
041: * No argument constructor made private to enforce naming of test cases.
042: * According to JUnit documentation, this constructor is provided for
043: * serialization, which we don't currently use.
044: *
045: * @see #BaseTestCase(String)
046: */
047: private BaseTestCase() {
048: }
049:
050: /**
051: * Create a test case with the given name.
052: *
053: * @param name name of the test case.
054: */
055: public BaseTestCase(String name) {
056: super (name);
057: }
058:
059: /**
060: * Run the test and force installation of a security
061: * manager with the default test policy file.
062: * Individual tests can run without a security
063: * manager or with a different policy file using
064: * the decorators obtained from SecurityManagerSetup.
065: * <BR>
066: * Method is final to ensure security manager is
067: * enabled by default. Tests should not need to
068: * override runTest, instead use test methods
069: * setUp, tearDown methods and decorators.
070: */
071: public final void runBare() throws Throwable {
072: if (getTestConfiguration().defaultSecurityManagerSetup())
073: assertSecurityManager();
074:
075: super .runBare();
076: }
077:
078: /**
079: * Return the current configuration for the test.
080: */
081: public final TestConfiguration getTestConfiguration() {
082: return TestConfiguration.getCurrent();
083: }
084:
085: /**
086: * Get the folder where a test leaves any information
087: * about its failure.
088: * @return Folder to use.
089: * @see TestConfiguration#getFailureFolder(TestCase)
090: */
091: public final File getFailureFolder() {
092: return getTestConfiguration().getFailureFolder(this );
093: }
094:
095: /**
096: * Print alarm string
097: * @param text String to print
098: */
099: public static void alarm(final String text) {
100: out.println("ALARM: " + text);
101: }
102:
103: /**
104: * Print debug string.
105: * @param text String to print
106: */
107: public static void println(final String text) {
108: if (TestConfiguration.getCurrent().isVerbose()) {
109: out.println("DEBUG: " + text);
110: }
111: }
112:
113: /**
114: * Print debug string.
115: * @param t Throwable object to print stack trace from
116: */
117: public static void printStackTrace(Throwable t) {
118: while (t != null) {
119: t.printStackTrace(out);
120:
121: if (t instanceof SQLException) {
122: t = ((SQLException) t).getNextException();
123: } else {
124: break;
125: }
126: }
127: }
128:
129: private final static PrintStream out = System.out;
130:
131: /**
132: * Set system property
133: *
134: * @param name name of the property
135: * @param value value of the property
136: */
137: protected static void setSystemProperty(final String name,
138: final String value) {
139:
140: AccessController
141: .doPrivileged(new java.security.PrivilegedAction() {
142:
143: public Object run() {
144: System.setProperty(name, value);
145: return null;
146:
147: }
148:
149: });
150:
151: }
152:
153: /**
154: * Remove system property
155: *
156: * @param name name of the property
157: */
158: protected static void removeSystemProperty(final String name) {
159:
160: AccessController
161: .doPrivileged(new java.security.PrivilegedAction() {
162:
163: public Object run() {
164: System.getProperties().remove(name);
165: return null;
166:
167: }
168:
169: });
170:
171: }
172:
173: /**
174: * Get system property.
175: *
176: * @param name name of the property
177: */
178: protected static String getSystemProperty(final String name) {
179:
180: return (String) AccessController
181: .doPrivileged(new java.security.PrivilegedAction() {
182:
183: public Object run() {
184: return System.getProperty(name);
185:
186: }
187:
188: });
189: }
190:
191: /**
192: * Obtain the URL for a test resource, e.g. a policy
193: * file or a SQL script.
194: * @param name Resource name, typically - org.apache.derbyTesing.something
195: * @return URL to the resource, null if it does not exist.
196: */
197: protected static URL getTestResource(final String name) {
198:
199: return (URL) AccessController
200: .doPrivileged(new java.security.PrivilegedAction() {
201:
202: public Object run() {
203: return BaseTestCase.class.getClassLoader()
204: .getResource(name);
205:
206: }
207:
208: });
209: }
210:
211: /**
212: * Open the URL for a a test resource, e.g. a policy
213: * file or a SQL script.
214: * @param url URL obtained from getTestResource
215: * @return An open stream
216: */
217: protected static InputStream openTestResource(final URL url)
218: throws PrivilegedActionException {
219: return (InputStream) AccessController
220: .doPrivileged(new java.security.PrivilegedExceptionAction() {
221:
222: public Object run() throws IOException {
223: return url.openStream();
224:
225: }
226:
227: });
228: }
229:
230: /**
231: * Assert a security manager is installed.
232: *
233: */
234: public static void assertSecurityManager() {
235: assertNotNull("No SecurityManager installed", System
236: .getSecurityManager());
237: }
238:
239: /**
240: * Compare the contents of two streams.
241: * The streams are closed after they are exhausted.
242: *
243: * @param is1 the first stream
244: * @param is2 the second stream
245: * @throws IOException if reading from the streams fail
246: * @throws AssertionFailedError if the stream contents are not equal
247: */
248: public static void assertEquals(InputStream is1, InputStream is2)
249: throws IOException {
250: if (is1 == null || is2 == null) {
251: assertNull("InputStream is2 is null, is1 is not", is1);
252: assertNull("InputStream is1 is null, is2 is not", is2);
253: return;
254: }
255: long index = 0;
256: int b1 = is1.read();
257: int b2 = is2.read();
258: do {
259: // Avoid string concatenation for every byte in the stream.
260: if (b1 != b2) {
261: assertEquals("Streams differ at index " + index, b1, b2);
262: }
263: index++;
264: b1 = is1.read();
265: b2 = is2.read();
266: } while (b1 != -1 || b2 != -1);
267: is1.close();
268: is2.close();
269: }
270:
271: /**
272: * Compare the contents of two readers.
273: * The readers are closed after they are exhausted.
274: *
275: * @param r1 the first reader
276: * @param r2 the second reader
277: * @throws IOException if reading from the streams fail
278: * @throws AssertionFailedError if the reader contents are not equal
279: */
280: public static void assertEquals(Reader r1, Reader r2)
281: throws IOException {
282: long index = 0;
283: if (r1 == null || r2 == null) {
284: assertNull("Reader r2 is null, r1 is not", r1);
285: assertNull("Reader r1 is null, r2 is not", r2);
286: return;
287: }
288: int c1 = r1.read();
289: int c2 = r2.read();
290: do {
291: // Avoid string concatenation for every char in the stream.
292: if (c1 != c2) {
293: assertEquals("Streams differ at index " + index, c1, c2);
294: }
295: index++;
296: c1 = r1.read();
297: c2 = r2.read();
298: } while (c1 != -1 || c2 != -1);
299: r1.close();
300: r2.close();
301: }
302: } // End class BaseTestCase
|