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.common.distribute;
024
025import java.io.File;
026import java.io.OutputStream;
027import java.util.HashSet;
028import java.util.Map;
029import java.util.Set;
030import java.util.WeakHashMap;
031
032import dk.netarkivet.common.exceptions.IOFailure;
033
034/**
035 * Version of RemoteFile that reads/writes a file to local storage.
036 * <p>
037 * 
038 * <pre>
039 * Created by IntelliJ IDEA.
040 * User: csr
041 * Date: Mar 2, 2005
042 * Time: 3:09:26 PM
043 * </pre>
044 */
045@SuppressWarnings({"serial"})
046public class TestRemoteFile extends HTTPRemoteFile implements RemoteFile {
047    public boolean failsOnCopy;
048
049    public static Map<RemoteFile, String> remainingRemoteFiles = new WeakHashMap<RemoteFile, String>();
050
051    public TestRemoteFile(File localFile, boolean useChecksum, boolean fileDeletable, boolean multipleDownloads)
052            throws IOFailure {
053        super(localFile, useChecksum, fileDeletable, multipleDownloads);
054        remainingRemoteFiles.put(this, localFile.getName());
055    }
056
057    public static RemoteFile getInstance(File remoteFile, Boolean useChecksums, Boolean fileDeletable,
058            Boolean multipleDownloads) throws IOFailure {
059        return new TestRemoteFile(remoteFile, useChecksums, fileDeletable, multipleDownloads);
060    }
061
062    public void copyTo(File destFile) {
063        if (failsOnCopy) {
064            throw new IOFailure("Expected IO error in copying " + "- you told me so!");
065        }
066        super.copyTo(destFile);
067    }
068
069    public void appendTo(OutputStream out) {
070        if (failsOnCopy) {
071            throw new IOFailure("Expected IO error in copying " + "- you told me so!");
072        }
073        super.appendTo(out);
074    }
075
076    public void cleanup() {
077        remainingRemoteFiles.remove(this);
078        super.cleanup();
079    }
080
081    public boolean isDeleted() {
082        return !remainingRemoteFiles.containsKey(this);
083    }
084
085    public String toString() {
086        return "TestRemoteFile: '" + file.getPath() + "'";
087    }
088
089    /** Remove any remote files that may have been left over. */
090    public static void removeRemainingFiles() {
091        // Must copy keyset to avoid concurrent modificaion of set
092        for (RemoteFile rf : new HashSet<RemoteFile>(remainingRemoteFiles.keySet())) {
093            rf.cleanup();
094        }
095        remainingRemoteFiles.clear();
096    }
097
098    /**
099     * Give the set of remaining remote files.
100     *
101     * @return the Set of remaining files
102     */
103    public static Set<RemoteFile> remainingFiles() {
104        return remainingRemoteFiles.keySet();
105    }
106
107    public File getFile() {
108        return file;
109    }
110
111    protected boolean isLocal() {
112        return true;
113    }
114}