/* Copyright (c) 2007-2013 Timothy Wall, All Rights Reserved
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.  
 */
package com.sun.jna.platform;

import com.sun.jna.Platform;
import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

public class FileUtilsTest extends TestCase {
    
    public void testMoveToTrash() throws Exception {
        FileUtils utils = FileUtils.getInstance();
        if (!utils.hasTrash()) 
            return;

        File tmpdir = new File(System.getProperty("java.io.tmpdir"));
        File file = File.createTempFile(getName(), ".tmp", tmpdir);
        try {
            assertTrue("File should exist", file.exists());
            try {
                utils.moveToTrash(new File[] { file });
            }
            catch(IOException e) {
                fail(e.toString());
            }
            assertFalse("File still exists after move to trash: " + file, file.exists());
        }
        finally {
            if (file.exists()) {
                file.delete();
            }
        }
    }
    
    public void testMoveSymlinkToTrash() throws Exception {
        if (Platform.isWindows()) {
            return;
        }
        FileUtils utils = FileUtils.getInstance();
        if (!utils.hasTrash()) 
            return;

        File tmpdir = new File(System.getProperty("java.io.tmpdir"));
        File file = File.createTempFile(getName(), ".tmp", tmpdir);
        File symlink = new File(tmpdir, file.getName() + ".link");
        Runtime.getRuntime().exec(new String[] { "ln", "-s", file.getAbsolutePath(), symlink.getAbsolutePath() });
        try {
            assertTrue("File should exist", symlink.exists());
            try {
                utils.moveToTrash(new File[] { symlink });
            }
            catch(IOException e) {
                fail(e.toString());
            }
            assertFalse("Symlink still exists after move to trash: " + symlink, symlink.exists());
            assertTrue("Original file should still exist after move to trash: " + file, file.exists());
        }
        finally {
            if (symlink.exists()) {
                symlink.delete();
            }
            if (file.exists()) {
                file.delete();
            }
        }
    }
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(FileUtilsTest.class);
    }
}
