xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/Testspace.java (revision 9b5f68a37fc987e9a0fc536c064f96c7c3b13aca)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.test.common;
25 
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.URL;
30 
31 /**
32  * A testspace is a directory on your hard drive where stores the files generated during testing, e.g. test result, logs, temp files.
33  *
34  */
35 public class Testspace {
36 
37     private static final File testspace = initTestspace();
38 
39     private static final File testdata = new File(System.getProperty("testdata", "data"));
40 
initTestspace()41     private static File initTestspace() {
42         String v = System.getProperty("testspace");
43         File file = null;
44         if (v == null) {
45             file = new File("../testspace");
46             System.setProperty("testspace", file.getAbsolutePath());
47         } else {
48             file = new File(v);
49         }
50 
51         return file;
52     }
53 
getPath()54     public static String getPath() {
55         return testspace.getAbsolutePath();
56     }
57 
getPath(String filePath)58     public static String getPath(String filePath) {
59         return getFile(filePath).getAbsolutePath();
60     }
61 
getUrl(String filePath)62     public static String getUrl(String filePath) {
63         return FileUtil.getUrl(getFile(filePath));
64     }
65 
getFile()66     public static File getFile() {
67         return testspace;
68     }
69 
getFile(String filePath)70     public static File getFile(String filePath) {
71         File file = new File(filePath);
72         if (file.isAbsolute())
73             return file;
74 
75         return new File(testspace, filePath);
76     }
77 
prepareData(String dataFilePath)78     public static String prepareData(String dataFilePath) {
79         File dataFile = prepareDataFile(dataFilePath);
80         return dataFile.getAbsolutePath();
81     }
82 
prepareData(String dataFilePath, String to)83     public static String prepareData(String dataFilePath, String to) {
84         File dataFile = prepareDataFile(dataFilePath, to);
85         return dataFile.getAbsolutePath();
86     }
87 
prepareDataFile(String dataFilePath)88     public static File prepareDataFile(String dataFilePath) {
89         getFile("temp").mkdir();
90         return prepareDataFile(dataFilePath, "temp");
91     }
92 
prepareDataFile(String dataFilePath, String to)93     public static File prepareDataFile(String dataFilePath, String to) {
94         File workingFile = getFile(to);
95         if (FileUtil.isUrl(dataFilePath)) {
96             if ((workingFile = FileUtil.download(dataFilePath, workingFile)) == null) {
97                 throw new RuntimeException("Can not prepare data: " + dataFilePath);
98             }
99             return workingFile;
100         }
101 
102         File dataFile = new File(dataFilePath);
103         if (workingFile.isDirectory())
104             workingFile = new File(workingFile, dataFile.getName());
105 
106         if (!dataFile.isAbsolute())
107             dataFile = new File(testdata, dataFilePath);
108         if (!dataFile.exists()) {
109             InputStream in = Testspace.class.getClassLoader().getResourceAsStream(dataFilePath);
110             if (in == null)
111                 throw new RuntimeException("Can not prepare data: " + dataFilePath);
112             if (!FileUtil.writeToFile(in, workingFile))
113                 throw new RuntimeException("Can not prepare data: " + dataFilePath);
114         } else {
115             if (!FileUtil.copyFile(dataFile, workingFile))
116                 throw new RuntimeException("Can not prepare data: " + dataFilePath);
117         }
118 
119         return workingFile;
120     }
121 
getDataFile(String dataFilePath)122     public static File getDataFile(String dataFilePath) {
123         File dataFile = new File(dataFilePath);
124         if (!dataFile.isAbsolute())
125             dataFile = new File(testdata, dataFilePath);
126         if (!dataFile.exists()) {
127             URL url = Testspace.class.getClassLoader().getResource(dataFilePath);
128             if (url == null)
129                 return null;
130             return new File(url.getFile());
131         } else {
132             return dataFile;
133         }
134     }
135 
getDataPath(String dataFilePath)136     public static String getDataPath(String dataFilePath) {
137         File file = getDataFile(dataFilePath);
138         if (file == null)
139             return null;
140         try {
141             return file.getCanonicalPath();
142         } catch (IOException e) {
143             return null;
144         }
145     }
146 
deleteFile(String path)147     public static boolean deleteFile(String path) {
148         return FileUtil.deleteFile(getPath(path));
149     }
150 }
151