1 20 package org.apache.derbyTesting.junit; 21 22 import junit.framework.TestCase; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.Reader ; 28 import java.io.PrintStream ; 29 import java.net.URL ; 30 import java.sql.SQLException ; 31 import java.security.AccessController ; 32 33 import java.security.PrivilegedActionException ; 34 35 36 39 public abstract class BaseTestCase 40 extends TestCase { 41 42 49 private BaseTestCase() {} 50 51 56 public BaseTestCase(String name) { 57 super(name); 58 } 59 60 72 public final void runBare() throws Throwable { 73 if (getTestConfiguration().defaultSecurityManagerSetup()) 74 assertSecurityManager(); 75 76 super.runBare(); 77 } 78 79 82 public final TestConfiguration getTestConfiguration() 83 { 84 return TestConfiguration.getCurrent(); 85 } 86 87 93 public final File getFailureFolder() { 94 return getTestConfiguration().getFailureFolder(this); 95 } 96 97 101 public static void alarm(final String text) { 102 out.println("ALARM: " + text); 103 } 104 105 109 public static void println(final String text) { 110 if (TestConfiguration.getCurrent().isVerbose()) { 111 out.println("DEBUG: " + text); 112 } 113 } 114 115 119 public static void printStackTrace(Throwable t) 120 { 121 while ( t!= null) { 122 t.printStackTrace(out); 123 124 if (t instanceof SQLException ) { 125 t = ((SQLException ) t).getNextException(); 126 } else { 127 break; 128 } 129 } 130 } 131 132 private final static PrintStream out = System.out; 133 134 140 protected static void setSystemProperty(final String name, 141 final String value) 142 { 143 144 AccessController.doPrivileged 145 (new java.security.PrivilegedAction (){ 146 147 public Object run(){ 148 System.setProperty( name, value); 149 return null; 150 151 } 152 153 } 154 ); 155 156 } 157 162 protected static void removeSystemProperty(final String name) 163 { 164 165 AccessController.doPrivileged 166 (new java.security.PrivilegedAction (){ 167 168 public Object run(){ 169 System.getProperties().remove(name); 170 return null; 171 172 } 173 174 } 175 ); 176 177 } 178 183 protected static String getSystemProperty(final String name) 184 { 185 186 return (String )AccessController.doPrivileged 187 (new java.security.PrivilegedAction (){ 188 189 public Object run(){ 190 return System.getProperty(name); 191 192 } 193 194 } 195 ); 196 } 197 198 204 protected static URL getTestResource(final String name) 205 { 206 207 return (URL )AccessController.doPrivileged 208 (new java.security.PrivilegedAction (){ 209 210 public Object run(){ 211 return BaseTestCase.class.getClassLoader(). 212 getResource(name); 213 214 } 215 216 } 217 ); 218 } 219 220 226 protected static InputStream openTestResource(final URL url) 227 throws PrivilegedActionException  228 { 229 return (InputStream )AccessController.doPrivileged 230 (new java.security.PrivilegedExceptionAction (){ 231 232 public Object run() throws IOException { 233 return url.openStream(); 234 235 } 236 237 } 238 ); 239 } 240 241 245 public static void assertSecurityManager() 246 { 247 assertNotNull("No SecurityManager installed", 248 System.getSecurityManager()); 249 } 250 251 260 public static void assertEquals(InputStream is1, InputStream is2) 261 throws IOException { 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 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 292 public static void assertEquals(Reader r1, Reader r2) 293 throws IOException { 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 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 }
| Popular Tags
|