1 20 package org.apache.derbyTesting.functionTests.util; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.OutputStream ; 31 import java.net.URL ; 32 import java.security.AccessController ; 33 import java.security.PrivilegedActionException ; 34 35 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 36 37 45 abstract class CanonTestCase extends BaseJDBCTestCase { 46 47 final String outputEncoding = "US-ASCII"; 48 49 private ByteArrayOutputStream rawBytes; 50 51 CanonTestCase(String name) { 52 super(name); 53 } 54 55 OutputStream getOutputStream() { 56 return rawBytes = new ByteArrayOutputStream (20 * 1024); 57 } 58 59 65 void compareCanon(String canon) throws Throwable { 66 rawBytes.flush(); 67 rawBytes.close(); 68 69 byte[] testRawBytes = rawBytes.toByteArray(); 70 71 try { 72 URL canonURL = getTestResource(canon); 73 assertNotNull("No master file " + canon, canonURL); 74 75 InputStream canonStream = openTestResource(canonURL); 76 77 BufferedReader cannonReader = new BufferedReader ( 78 new InputStreamReader (canonStream, outputEncoding)); 79 80 BufferedReader testOutput = new BufferedReader ( 81 new InputStreamReader ( 82 new ByteArrayInputStream (testRawBytes), 83 outputEncoding)); 84 85 for (int lineNumber = 1;; lineNumber++) { 86 String testLine = testOutput.readLine(); 87 88 if ("".equals(testLine)) 90 continue; 91 92 String canonLine = cannonReader.readLine(); 93 94 if (canonLine == null && testLine == null) 95 break; 96 97 if (canonLine == null) 98 fail("More output from test than expected"); 99 100 if (testLine == null) 101 fail("Less output from test than expected, stoped at line" 102 + lineNumber); 103 104 assertEquals("Output at line " + lineNumber, canonLine, 105 testLine); 106 } 107 108 cannonReader.close(); 109 testOutput.close(); 110 } catch (Throwable t) { 111 dumpForFail(testRawBytes); 112 throw t; 113 } 114 } 115 116 124 private void dumpForFail(byte[] rawOutput) throws IOException , 125 PrivilegedActionException { 126 127 File folder = getFailureFolder(); 128 final File outFile = new File (folder, getName() + ".out"); 129 130 OutputStream outStream = (OutputStream ) AccessController 131 .doPrivileged(new java.security.PrivilegedExceptionAction () { 132 133 public Object run() throws IOException { 134 return new FileOutputStream (outFile); 135 } 136 }); 137 138 outStream.write(rawOutput); 139 outStream.flush(); 140 outStream.close(); 141 } 142 } 143 | Popular Tags |