xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/Testspace.java (revision 62968b57a613f1556da0195a5bf90e10108510a7)
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 		getFile("temp").mkdir();
89 		return prepareDataFile(dataFilePath, "temp");
90 	}
91 
92 	public static File prepareDataFile(String dataFilePath, String to) {
93 		File workingFile = getFile(to);
94 		if (FileUtil.isUrl(dataFilePath)) {
95 			if ((workingFile = FileUtil.download(dataFilePath, workingFile)) == null) {
96 				throw new RuntimeException("Can not prepare data: " + dataFilePath);
97 			}
98 			return workingFile;
99 		}
100 
101 		File dataFile = new File(dataFilePath);
102 		if (workingFile.isDirectory())
103 			workingFile = new File(workingFile, dataFile.getName());
104 
105 		if (!dataFile.isAbsolute())
106 			dataFile = new File(testdata, dataFilePath);
107 		if (!dataFile.exists()) {
108 			InputStream in = Testspace.class.getClassLoader().getResourceAsStream(dataFilePath);
109 			if (in == null)
110 				throw new RuntimeException("Can not prepare data: " + dataFilePath);
111 			if (!FileUtil.writeToFile(in, workingFile))
112 				throw new RuntimeException("Can not prepare data: " + dataFilePath);
113 		} else {
114 			if (!FileUtil.copyFile(dataFile, workingFile))
115 				throw new RuntimeException("Can not prepare data: " + dataFilePath);
116 		}
117 
118 		return workingFile;
119 	}
120 
121 	public static File getDataFile(String dataFilePath) {
122 		File dataFile = new File(dataFilePath);
123 		if (!dataFile.isAbsolute())
124 			dataFile = new File(testdata, dataFilePath);
125 		if (!dataFile.exists()) {
126 			URL url = Testspace.class.getClassLoader().getResource(dataFilePath);
127 			if (url == null)
128 				return null;
129 			return new File(url.getFile());
130 		} else {
131 			return dataFile;
132 		}
133 	}
134 
135 
136 	public static boolean deleteFile(String path) {
137 		return FileUtil.deleteFile(getPath(path));
138 	}
139 }
140