1 20 21 package org.apache.derbyTesting.functionTests.tests.lang; 22 23 import java.io.File ; 24 import java.io.OutputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.PrintStream ; 28 import java.util.Properties ; 29 import java.sql.Connection ; 30 import java.sql.Driver ; 31 import java.sql.DriverManager ; 32 import java.sql.DriverPropertyInfo ; 33 import java.sql.SQLException ; 34 import javax.sql.DataSource ; 35 36 import org.apache.derby.tools.ij; 37 import org.apache.derbyTesting.functionTests.util.TestUtil; 38 39 52 53 54 final class AssertException extends Exception 55 { 56 AssertException (String e) { 57 super(e); 58 }; 59 } 60 61 public class errorStream 62 { 63 private static String derbyHome; 64 65 private static Properties sysProps; 66 private static final String FILE_PROP = "derby.stream.error.file"; 67 private static final String METHOD_PROP = "derby.stream.error.method"; 68 private static final String FIELD_PROP = "derby.stream.error.field"; 69 70 73 private static final String database = "VombatusUrsinusHirsutus"; 74 75 79 private static int runNo = 0; 80 81 85 private static File fileStreamFile; 86 87 88 private static OutputStream methodStream; 89 private static File methodStreamFile; 90 91 95 public static OutputStream fieldStream; 96 private static File fieldStreamFile; 97 98 105 private static OutputStream errStream; 106 private static File errStreamFile; 107 108 112 public static OutputStream getStream() { 113 return methodStream; 114 } 115 116 private static String makeStreamFilename(String type) { 117 return database + "-" + type + "-" + runNo + ".log"; 118 } 119 120 private static String makeDatabaseName() { 121 return database + "-" + runNo; 122 } 123 124 private static void openStreams() throws IOException { 125 126 runNo += 1; 127 128 try { 129 fileStreamFile = new File (derbyHome, makeStreamFilename("file")); 130 131 methodStreamFile = new File (derbyHome, makeStreamFilename("method")); 132 methodStream = new FileOutputStream (methodStreamFile); 133 134 fieldStreamFile = new File (derbyHome, makeStreamFilename("field")); 135 fieldStream = new FileOutputStream (fieldStreamFile); 136 137 errStreamFile = new File (derbyHome, makeStreamFilename("err")); 138 errStream =new FileOutputStream (errStreamFile); 139 System.setErr(new PrintStream (errStream)); 140 } 141 catch (IOException e) { 142 System.out.println("Could not open stream files"); 143 throw e; 144 } 145 } 146 147 148 private static void closeStreams() throws IOException { 149 try { 150 methodStream.close(); 151 fieldStream.close(); 152 errStream.close(); 153 154 System.setErr(System.out); 156 } 157 catch (IOException e) { 158 System.out.println("Could not close stream files"); 159 throw e; 160 } 161 } 162 163 164 private static void assertEmpty(File f) throws AssertException, 165 IOException { 166 if ( ! (f.exists() && (f.length() == 0)) ) { 167 AssertException e = new AssertException("assertEmpty failed: : " + 168 f.getCanonicalPath()); 169 throw e; 170 } 171 } 172 173 174 private static void assertNonEmpty(File f) throws AssertException, 175 IOException { 176 if ( ! f.exists() || (f.length() == 0) ) { 177 AssertException e = new AssertException("assertNonEmpty failed:" + 178 f.getCanonicalPath()); 179 throw e; 180 } 181 } 182 183 184 private static void assertNonExisting(File f) throws AssertException, 185 IOException { 186 if ( f.exists() ) { 187 AssertException e = new AssertException("assertNonExisting failed: " + 188 f.getCanonicalPath()); 189 throw e; 190 } 191 } 192 193 194 private static void resetProps () { 195 sysProps.remove(FILE_PROP); 196 sysProps.remove(METHOD_PROP); 197 sysProps.remove(FIELD_PROP); 198 } 199 200 201 private static void bootDerby () throws SQLException { 202 Properties attrs = new Properties (); 203 attrs.setProperty("databaseName", makeDatabaseName()); 204 attrs.setProperty("createDatabase", "create"); 205 DataSource ds = TestUtil.getDataSource(attrs); 206 try { 207 Connection conn = ds.getConnection(); 208 conn.close(); 209 } 210 catch (SQLException e) { 211 System.out.println("Derby boot failed: " + 212 attrs.getProperty("databaseName") + " : " + 213 e.getSQLState() + ": " + e.getMessage()); 214 throw e; 215 } 216 } 217 218 219 private static void shutdownDerby () throws AssertException, SQLException { 220 Properties attrs = new Properties (); 221 attrs.setProperty("databaseName", ""); 222 attrs.setProperty("shutdownDatabase", "shutdown"); 223 DataSource ds = TestUtil.getDataSource(attrs); 224 try { 225 Connection conn = ds.getConnection(); 226 AssertException e = new AssertException("shutdown failed: " + 227 makeDatabaseName()); 228 throw e; 229 } 230 catch (SQLException e) { 231 System.out.println("shutdown ok: " + e.getSQLState() + ":" + 232 e.getMessage()); 233 } 234 } 235 236 237 private static void checkFile() throws AssertException, IOException , 238 SQLException { 239 openStreams(); 240 241 resetProps(); 242 sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath()); 243 244 bootDerby(); 245 shutdownDerby(); 246 247 closeStreams(); 248 249 assertNonEmpty(fileStreamFile); 250 assertEmpty(methodStreamFile); 251 assertEmpty(fieldStreamFile); 252 assertEmpty(errStreamFile); 253 } 254 255 256 private static void checkWrongFile() throws AssertException, IOException , 257 SQLException { 258 openStreams(); 259 260 sysProps.put(FILE_PROP, 261 new File (derbyHome+"foo", makeStreamFilename("file")).getCanonicalPath()); 263 264 bootDerby(); 265 shutdownDerby(); 266 267 closeStreams(); 268 269 assertNonExisting(fileStreamFile); 270 assertEmpty(methodStreamFile); 271 assertEmpty(fieldStreamFile); 272 assertNonEmpty(errStreamFile); 273 } 274 275 276 private static void checkMethod() throws AssertException, IOException , 277 SQLException { 278 openStreams(); 279 280 resetProps(); 281 sysProps.put(METHOD_PROP, 282 "org.apache.derbyTesting.functionTests.tests.lang."+ 283 "errorStream.getStream"); 284 285 bootDerby(); 286 shutdownDerby(); 287 288 closeStreams(); 289 290 assertNonExisting(fileStreamFile); 291 assertNonEmpty(methodStreamFile); 292 assertEmpty(fieldStreamFile); 293 assertEmpty(errStreamFile); 294 } 295 296 297 private static void checkWrongMethod() throws AssertException, IOException , 298 SQLException { 299 openStreams(); 300 301 resetProps(); 302 sysProps.put(METHOD_PROP, 303 "org.apache.derbyTesting.functionTests.tests.lang."+ 304 "errorStream.nonExistingGetStream"); 305 306 bootDerby(); 307 shutdownDerby(); 308 309 closeStreams(); 310 311 assertNonExisting(fileStreamFile); 312 assertEmpty(methodStreamFile); 313 assertEmpty(fieldStreamFile); 314 assertNonEmpty(errStreamFile); 315 } 316 317 318 private static void checkField() throws AssertException, IOException , 319 SQLException { 320 openStreams(); 321 322 resetProps(); 323 sysProps.put(FIELD_PROP, 324 "org.apache.derbyTesting.functionTests.tests.lang."+ 325 "errorStream.fieldStream"); 326 327 bootDerby(); 328 shutdownDerby(); 329 330 closeStreams(); 331 332 assertNonExisting(fileStreamFile); 333 assertEmpty(methodStreamFile); 334 assertNonEmpty(fieldStreamFile); 335 assertEmpty(errStreamFile); 336 } 337 338 339 private static void checkWrongField() throws AssertException, IOException , 340 SQLException { 341 openStreams(); 342 343 resetProps(); 344 sysProps.put(FIELD_PROP, 345 "org.apache.derbyTesting.functionTests.tests.lang."+ 346 "errorStream.nonExistingFieldStream"); 347 348 bootDerby(); 349 shutdownDerby(); 350 351 closeStreams(); 352 353 assertNonExisting(fileStreamFile); 354 assertEmpty(methodStreamFile); 355 assertEmpty(fieldStreamFile); 356 assertNonEmpty(errStreamFile); 357 } 358 359 360 private static void checkFileOverMethod() throws AssertException, IOException , 361 SQLException { 362 openStreams(); 363 364 resetProps(); 365 sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath()); 366 sysProps.put(METHOD_PROP, 367 "org.apache.derbyTesting.functionTests.tests.lang."+ 368 "errorStream.getStream"); 369 370 bootDerby(); 371 shutdownDerby(); 372 373 closeStreams(); 374 375 assertNonEmpty(fileStreamFile); 376 assertEmpty(methodStreamFile); 377 assertEmpty(fieldStreamFile); 378 assertEmpty(errStreamFile); 379 } 380 381 382 private static void checkFileOverField() throws AssertException, IOException , 383 SQLException { 384 openStreams(); 385 386 resetProps(); 387 sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath()); 388 sysProps.put(FIELD_PROP, 389 "org.apache.derbyTesting.functionTests.tests.lang."+ 390 "errorStream.fieldStream"); 391 392 bootDerby(); 393 shutdownDerby(); 394 395 closeStreams(); 396 397 assertNonEmpty(fileStreamFile); 398 assertEmpty(methodStreamFile); 399 assertEmpty(fieldStreamFile); 400 assertEmpty(errStreamFile); 401 } 402 403 404 private static void checkFileOverMethodAndField() throws AssertException, 405 IOException , 406 SQLException { 407 openStreams(); 408 409 resetProps(); 410 sysProps.put(FILE_PROP, fileStreamFile.getCanonicalPath()); 411 sysProps.put(METHOD_PROP, 412 "org.apache.derbyTesting.functionTests.tests.lang."+ 413 "errorStream.getStream"); 414 sysProps.put(FIELD_PROP, 415 "org.apache.derbyTesting.functionTests.tests.lang."+ 416 "errorStream.fieldStream"); 417 418 bootDerby(); 419 shutdownDerby(); 420 421 closeStreams(); 422 423 assertNonEmpty(fileStreamFile); 424 assertEmpty(methodStreamFile); 425 assertEmpty(fieldStreamFile); 426 assertEmpty(errStreamFile); 427 } 428 429 430 private static void checkMethodOverField() throws AssertException, IOException , 431 SQLException { 432 openStreams(); 433 434 resetProps(); 435 sysProps.put(METHOD_PROP, 436 "org.apache.derbyTesting.functionTests.tests.lang."+ 437 "errorStream.getStream"); 438 sysProps.put(FIELD_PROP, 439 "org.apache.derbyTesting.functionTests.tests.lang."+ 440 "errorStream.fieldStream"); 441 442 bootDerby(); 443 shutdownDerby(); 444 445 closeStreams(); 446 447 assertNonExisting(fileStreamFile); 448 assertNonEmpty(methodStreamFile); 449 assertEmpty(fieldStreamFile); 450 assertEmpty(errStreamFile); 451 } 452 453 454 public static void main(String [] args) 455 { 456 try { 457 ij.getPropertyArg(args); 458 sysProps = System.getProperties(); 459 derbyHome = sysProps.getProperty("derby.system.home"); 460 461 System.out.println("Test errorStream starting"); 462 463 checkFile(); 464 checkWrongFile(); 465 466 checkMethod(); 467 checkWrongMethod(); 468 469 checkField(); 470 checkWrongField(); 471 472 checkFileOverMethod(); 473 checkFileOverField(); 474 checkFileOverMethodAndField(); 475 476 checkMethodOverField(); 477 478 System.out.println("Test errorStream finished successfully"); 479 } 480 catch (Exception e) { 481 System.out.println("Test errorStream failed: " + e.getMessage()); 482 e.printStackTrace(); 483 }; 484 } 485 } 486 | Popular Tags |