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