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 29 /** 30 * A testspace is a directory on your hard drive where stores the files generated during testing, e.g. test result, logs, temp files. 31 * 32 */ 33 public class Testspace { 34 35 private static final File testspace = initTestspace(); 36 37 private static final File testdata = new File(System.getProperty("testdata", "data")); 38 39 private static File initTestspace() { 40 String v = System.getProperty("testspace"); 41 File file = null; 42 if (v == null) { 43 file = new File("../testspace"); 44 System.setProperty("testspace", file.getAbsolutePath()); 45 } else { 46 file = new File(v); 47 } 48 49 return file; 50 } 51 52 public static String getPath() { 53 return testspace.getAbsolutePath(); 54 } 55 56 public static String getPath(String filePath) { 57 return getFile(filePath).getAbsolutePath(); 58 } 59 60 public static String getUrl(String filePath) { 61 return FileUtil.getUrl(getFile(filePath)); 62 } 63 64 public static File getFile() { 65 return testspace; 66 } 67 68 public static File getFile(String filePath) { 69 File file = new File(filePath); 70 if (file.isAbsolute()) 71 return file; 72 73 return new File(testspace, filePath); 74 } 75 76 public static String prepareData(String dataFilePath) { 77 return prepareData(dataFilePath, "temp/" + dataFilePath); 78 } 79 80 public static String prepareData(String dataFilePath, String to) { 81 File dataFile = new File(dataFilePath); 82 File workingFile = getFile(to); 83 84 if (!dataFile.isAbsolute()) 85 dataFile = new File(testdata, dataFilePath); 86 if (!dataFile.exists()) { 87 InputStream in = Testspace.class.getClassLoader().getResourceAsStream(dataFilePath); 88 if (in == null) 89 throw new RuntimeException("Can not prepare data: " + dataFilePath); 90 if (!FileUtil.writeToFile(in, workingFile)) 91 throw new RuntimeException("Can not prepare data: " + dataFilePath); 92 } else { 93 if (!FileUtil.copyFile(dataFile, workingFile)) 94 throw new RuntimeException("Can not prepare data: " + dataFilePath); 95 } 96 97 return workingFile.getAbsolutePath(); 98 } 99 } 100