001: /*
002: *
003: * Derby - Class org.apache.derbyTesting.functionTests.util.CanonTestCase
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.functionTests.util;
021:
022: import java.io.BufferedReader;
023: import java.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.File;
026: import java.io.FileOutputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.OutputStream;
031: import java.net.URL;
032: import java.security.AccessController;
033: import java.security.PrivilegedActionException;
034:
035: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
036:
037: /**
038: * Run a test that compares itself to a master (canon) file.
039: * This is used to support cannon based tests that ran
040: * under the old Derby test harness without having to convert
041: * them. It is not recommended for new tests. New test should
042: * use the JUnit assert mechanisms.
043: *
044: */
045: abstract class CanonTestCase extends BaseJDBCTestCase {
046:
047: final String outputEncoding = "US-ASCII";
048:
049: private ByteArrayOutputStream rawBytes;
050:
051: CanonTestCase(String name) {
052: super (name);
053: }
054:
055: OutputStream getOutputStream() {
056: return rawBytes = new ByteArrayOutputStream(20 * 1024);
057: }
058:
059: /**
060: * Compare the output to the canon provided.
061: *
062: * @param canon
063: * Name of canon as a resource.
064: */
065: void compareCanon(String canon) throws Throwable {
066: rawBytes.flush();
067: rawBytes.close();
068:
069: byte[] testRawBytes = rawBytes.toByteArray();
070:
071: try {
072: URL canonURL = getTestResource(canon);
073: assertNotNull("No master file " + canon, canonURL);
074:
075: InputStream canonStream = openTestResource(canonURL);
076:
077: BufferedReader cannonReader = new BufferedReader(
078: new InputStreamReader(canonStream, outputEncoding));
079:
080: BufferedReader testOutput = new BufferedReader(
081: new InputStreamReader(new ByteArrayInputStream(
082: testRawBytes), outputEncoding));
083:
084: for (int lineNumber = 1;; lineNumber++) {
085: String testLine = testOutput.readLine();
086:
087: // Skip blank lines.
088: if ("".equals(testLine))
089: continue;
090:
091: String canonLine = cannonReader.readLine();
092:
093: if (canonLine == null && testLine == null)
094: break;
095:
096: if (canonLine == null)
097: fail("More output from test than expected");
098:
099: if (testLine == null)
100: fail("Less output from test than expected, stoped at line"
101: + lineNumber);
102:
103: assertEquals("Output at line " + lineNumber, canonLine,
104: testLine);
105: }
106:
107: cannonReader.close();
108: testOutput.close();
109: } catch (Throwable t) {
110: dumpForFail(testRawBytes);
111: throw t;
112: }
113: }
114:
115: /**
116: * Dump the output that did not compare correctly into the failure folder
117: * with the name this.getName() + ".out".
118: *
119: * @param rawOutput
120: * @throws IOException
121: * @throws PrivilegedActionException
122: */
123: private void dumpForFail(byte[] rawOutput) throws IOException,
124: PrivilegedActionException {
125:
126: File folder = getFailureFolder();
127: final File outFile = new File(folder, getName() + ".out");
128:
129: OutputStream outStream = (OutputStream) AccessController
130: .doPrivileged(new java.security.PrivilegedExceptionAction() {
131:
132: public Object run() throws IOException {
133: return new FileOutputStream(outFile);
134: }
135: });
136:
137: outStream.write(rawOutput);
138: outStream.flush();
139: outStream.close();
140: }
141: }
|