xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/Testspace.java (revision f325a5bfe95c01f05d7a63b8d98c3bae4115aa37)
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.InputStream;
28 import java.net.URL;
29 
30 /**
31  * A testspace is a directory on your hard drive where stores the files generated during testing, e.g. test result, logs, temp files.
32  *
33  */
34 public class Testspace {
35 
36     private static final File testspace = initTestspace();
37 
38     private static final File testdata = new File(System.getProperty("testdata", "data"));
39 
40     private static File initTestspace() {
41         String v = System.getProperty("testspace");
42         File file = null;
43         if (v == null) {
44             file = new File("../testspace");
45             System.setProperty("testspace", file.getAbsolutePath());
46         } else {
47             file = new File(v);
48         }
49 
50         return file;
51     }
52 
53     public static String getPath() {
54         return testspace.getAbsolutePath();
55     }
56 
57     public static String getPath(String filePath) {
58         return getFile(filePath).getAbsolutePath();
59     }
60 
61     public static String getUrl(String filePath) {
62         return FileUtil.getUrl(getFile(filePath));
63     }
64 
65     public static File getFile() {
66         return testspace;
67     }
68 
69     public static File getFile(String filePath) {
70         File file = new File(filePath);
71         if (file.isAbsolute())
72             return file;
73 
74         return new File(testspace, filePath);
75     }
76 
77     public static String prepareData(String dataFilePath) {
78         File dataFile = prepareDataFile(dataFilePath);
79         return dataFile.getAbsolutePath();
80     }
81 
82     public static String prepareData(String dataFilePath, String to) {
83         File dataFile = prepareDataFile(dataFilePath,to);
84         return dataFile.getAbsolutePath();
85     }
86 
87     public static File prepareDataFile(String dataFilePath) {
88         String name = new File(dataFilePath).getName();
89         return prepareDataFile(dataFilePath, "temp/" + name);
90     }
91 
92     public static File prepareDataFile(String dataFilePath, String to) {
93         File workingFile = getFile(to);
94 
95         if (FileUtil.isUrl(dataFilePath)) {
96             if (FileUtil.download(dataFilePath, workingFile) == null) {
97                 throw new RuntimeException("Can not prepare data: " + dataFilePath);
98             }
99             return workingFile;
100         }
101 
102 
103         File dataFile = new File(dataFilePath);
104 
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 
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 
136 
137     public static boolean deleteFile(String path) {
138         return FileUtil.deleteFile(getPath(path));
139     }
140 }
141