KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > CanonTestCase


1 /*
2  *
3  * Derby - Class org.apache.derbyTesting.functionTests.util.CanonTestCase
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.functionTests.util;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.security.AccessController JavaDoc;
33 import java.security.PrivilegedActionException JavaDoc;
34
35 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
36
37 /**
38  * Run a test that compares itself to a master (canon) file.
39  * This is used to support cannon based tests that ran
40  * under the old Derby test harness without having to convert
41  * them. It is not recommended for new tests. New test should
42  * use the JUnit assert mechanisms.
43  *
44  */

45 abstract class CanonTestCase extends BaseJDBCTestCase {
46
47     final String JavaDoc outputEncoding = "US-ASCII";
48
49     private ByteArrayOutputStream JavaDoc rawBytes;
50
51     CanonTestCase(String JavaDoc name) {
52         super(name);
53     }
54
55     OutputStream JavaDoc getOutputStream() {
56         return rawBytes = new ByteArrayOutputStream JavaDoc(20 * 1024);
57     }
58
59     /**
60      * Compare the output to the canon provided.
61      *
62      * @param canon
63      * Name of canon as a resource.
64      */

65     void compareCanon(String JavaDoc canon) throws Throwable JavaDoc {
66         rawBytes.flush();
67         rawBytes.close();
68
69         byte[] testRawBytes = rawBytes.toByteArray();
70
71         try {
72             URL JavaDoc canonURL = getTestResource(canon);
73             assertNotNull("No master file " + canon, canonURL);
74
75             InputStream JavaDoc canonStream = openTestResource(canonURL);
76
77             BufferedReader JavaDoc cannonReader = new BufferedReader JavaDoc(
78                     new InputStreamReader JavaDoc(canonStream, outputEncoding));
79
80             BufferedReader JavaDoc testOutput = new BufferedReader JavaDoc(
81                     new InputStreamReader JavaDoc(
82                             new ByteArrayInputStream JavaDoc(testRawBytes),
83                             outputEncoding));
84
85             for (int lineNumber = 1;; lineNumber++) {
86                 String JavaDoc testLine = testOutput.readLine();
87
88                 // Skip blank lines.
89
if ("".equals(testLine))
90                     continue;
91
92                 String JavaDoc 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 JavaDoc t) {
111             dumpForFail(testRawBytes);
112             throw t;
113         }
114     }
115
116     /**
117      * Dump the output that did not compare correctly into the failure folder
118      * with the name this.getName() + ".out".
119      *
120      * @param rawOutput
121      * @throws IOException
122      * @throws PrivilegedActionException
123      */

124     private void dumpForFail(byte[] rawOutput) throws IOException JavaDoc,
125             PrivilegedActionException JavaDoc {
126
127         File JavaDoc folder = getFailureFolder();
128         final File JavaDoc outFile = new File JavaDoc(folder, getName() + ".out");
129
130         OutputStream JavaDoc outStream = (OutputStream JavaDoc) AccessController
131                 .doPrivileged(new java.security.PrivilegedExceptionAction JavaDoc() {
132
133                     public Object JavaDoc run() throws IOException JavaDoc {
134                         return new FileOutputStream JavaDoc(outFile);
135                     }
136                 });
137
138         outStream.write(rawOutput);
139         outStream.flush();
140         outStream.close();
141     }
142 }
143
Popular Tags