01: /*
02:
03: Derby - Class org.apache.derbyTesting.functionTests.harness.CopySuppFiles
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to You under the Apache License, Version 2.0
09: (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, software
15: distributed under the License is distributed on an "AS IS" BASIS,
16: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: See the License for the specific language governing permissions and
18: limitations under the License.
19:
20: */
21:
22: package org.apache.derbyTesting.functionTests.harness;
23:
24: import java.io.*;
25: import java.util.StringTokenizer;
26:
27: /**
28: For tests which require support files.
29: Copy them to the output directory for the test.
30: */
31: public class CopySuppFiles {
32:
33: public static void main(String[] args) throws Exception {
34: }
35:
36: public static void copyFiles(File outDir, String suppFiles)
37: throws ClassNotFoundException, IOException {
38: // suppFiles is a comma separated list of the files
39: StringTokenizer st = new StringTokenizer(suppFiles, ",");
40: String scriptName = ""; // example: test/math.sql
41: InputStream is = null; // To be used for each support file
42: while (st.hasMoreTokens()) {
43: scriptName = st.nextToken();
44: File suppFile = null;
45: String fileName = "";
46: // Try to locate the file
47: is = RunTest.loadTestResource(scriptName);
48: if (is == null)
49: System.out.println("Could not locate: " + scriptName);
50: else {
51: // Copy the support file so the test can use it
52: int index = scriptName.lastIndexOf('/');
53: fileName = scriptName.substring(index + 1);
54: // suppFile = new File((new File(outDir, fileName)).getCanonicalPath());
55:
56: //these calls to getCanonicalPath catch IOExceptions as a workaround to
57: //a bug in the EPOC jvm.
58: try {
59: suppFile = new File((new File(outDir, fileName))
60: .getCanonicalPath());
61: } catch (IOException e) {
62: File f = new File(outDir, fileName);
63: FileWriter fw = new FileWriter(f);
64: fw.close();
65: suppFile = new File(f.getCanonicalPath());
66: }
67: // need to make a guess so we copy text files to local encoding
68: // on non-ascii systems...
69: if ((fileName.indexOf("sql") > 0)
70: || (fileName.indexOf("txt") > 0)
71: || (fileName.indexOf(".view") > 0)
72: || (fileName.indexOf(".policy") > 0)
73: || (fileName.indexOf(".multi") > 0)
74: || (fileName.indexOf(".properties") > 0)) {
75: BufferedReader inFile = new BufferedReader(
76: new InputStreamReader(is, "UTF-8"));
77: PrintWriter pw = new PrintWriter(
78: new BufferedWriter(
79: new FileWriter(suppFile), 10000),
80: true);
81: int c;
82: while ((c = inFile.read()) != -1)
83: pw.write(c);
84: pw.flush();
85: pw.close();
86: } else {
87: FileOutputStream fos = new FileOutputStream(
88: suppFile);
89: byte[] data = new byte[4096];
90: int len;
91: while ((len = is.read(data)) != -1) {
92: fos.write(data, 0, len);
93: }
94: fos.close();
95: }
96: }
97: }
98: }
99: }
|