001/* 002 * #%L 003 * Netarchivesuite - common - test 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023package dk.netarkivet.testutils.preconfigured; 024 025import java.io.File; 026import java.io.IOException; 027 028import dk.netarkivet.common.exceptions.ArgumentNotValid; 029import dk.netarkivet.common.exceptions.IOFailure; 030import dk.netarkivet.common.utils.FileUtils; 031import dk.netarkivet.testutils.TestFileUtils; 032 033public class MoveTestFiles implements TestConfigurationIF { 034 private File originalsDir; 035 private File workingDir; 036 037 public MoveTestFiles(File originalsDir, File workingDir) { 038 this.originalsDir = originalsDir; 039 this.workingDir = workingDir; 040 } 041 042 public void setUp() { 043 FileUtils.removeRecursively(workingDir); 044 workingDir.mkdirs(); 045 TestFileUtils.copyDirectoryNonCVS(originalsDir, workingDir); 046 } 047 048 public void tearDown() { 049 setReadWrite(workingDir); 050 FileUtils.removeRecursively(workingDir); 051 } 052 053 /** 054 * Recursively set all files readable and writable. Used to ensure working dir is deletable. 055 * 056 * @param file File or directory to start from. 057 */ 058 private void setReadWrite(File file) { 059 file.setReadable(true); 060 file.setWritable(true); 061 if (file.isDirectory()) { 062 for (File file1 : file.listFiles()) { 063 setReadWrite(file1); 064 } 065 } 066 } 067 068 public File working(File f) { 069 if (!f.getAbsolutePath().startsWith(originalsDir.getAbsolutePath())) { 070 throw new ArgumentNotValid(f + " is not in " + originalsDir); 071 } 072 return new File(workingDir, f.getAbsolutePath().substring(originalsDir.getAbsolutePath().length())); 073 } 074 075 public File newTmpFile() { 076 try { 077 return File.createTempFile(this.getClass().getSimpleName(), "Tmp", workingDir); 078 } catch (IOException e) { 079 throw new IOFailure("Failed to create a temp file in " + workingDir, e); 080 } 081 } 082 083 public File newTmpDir() { 084 try { 085 File tmpFile = File.createTempFile(this.getClass().getSimpleName(), "Tmp", workingDir); 086 tmpFile.delete(); // Maybe not necessary 087 tmpFile.mkdir(); 088 return tmpFile; 089 } catch (IOException e) { 090 throw new IOFailure("Failed to create a temp dir in " + workingDir, e); 091 } 092 } 093}