KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > CopySuppFiles


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.CopySuppFiles
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, 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 JavaDoc;
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
34     public static void main(String JavaDoc[] args) throws Exception JavaDoc
35     {
36     }
37
38     public static void copyFiles(File outDir, String JavaDoc suppFiles)
39         throws ClassNotFoundException JavaDoc, IOException
40     {
41         // suppFiles is a comma separated list of the files
42
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(suppFiles,",");
43         String JavaDoc scriptName = ""; // example: test/math.sql
44
InputStream is = null; // To be used for each support file
45
while (st.hasMoreTokens())
46         {
47             scriptName = st.nextToken();
48             File suppFile = null;
49             String JavaDoc fileName = "";
50             // Try to locate the file
51
is = RunTest.loadTestResource(scriptName);
52             if ( is == null )
53                 System.out.println("Could not locate: " + scriptName);
54             else
55             {
56                 // Copy the support file so the test can use it
57
int index = scriptName.lastIndexOf('/');
58                 fileName = scriptName.substring(index+1);
59  // suppFile = new File((new File(outDir, fileName)).getCanonicalPath());
60

61         //these calls to getCanonicalPath catch IOExceptions as a workaround to
62
//a bug in the EPOC jvm.
63
try {suppFile = new File((new File(outDir, fileName)).getCanonicalPath());}
64         catch (IOException e) {
65             File f = new File(outDir, fileName);
66             FileWriter fw = new FileWriter(f);
67             fw.close();
68             suppFile = new File(f.getCanonicalPath());
69         }
70                 // need to make a guess so we copy text files to local encoding
71
// on non-ascii systems...
72
if ((fileName.indexOf("sql") > 0) || (fileName.indexOf("txt") > 0) || (fileName.indexOf(".view") > 0) || (fileName.indexOf(".policy") > 0) || (fileName.indexOf(".multi") > 0) || (fileName.indexOf(".properties") > 0))
73                 {
74                     BufferedReader inFile = new BufferedReader(new InputStreamReader(is, "UTF-8"));
75                     PrintWriter pw = new PrintWriter
76                        ( new BufferedWriter(new FileWriter(suppFile), 10000), true );
77                     int c;
78                     while ((c = inFile.read()) != -1)
79                         pw.write(c);
80                     pw.flush();
81                     pw.close();
82                 }
83                 else
84                 {
85                     FileOutputStream fos = new FileOutputStream(suppFile);
86                     byte[] data = new byte[4096];
87                     int len;
88                     while ((len = is.read(data)) != -1)
89                     {
90                         fos.write(data, 0, len);
91                     }
92                     fos.close();
93                 }
94             }
95         }
96     }
97 }
98
Popular Tags