/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;

import junit.framework.TestCase;

public class FileMoverTest extends TestCase
{

    /*
     * Test method for 'org.bodington.util.FileMover.copyFile(File, File)'
     */
    public void testCopyFile() throws IOException
    {
        File source = createSource();
        File destination = createDestination();
        
        FileMover.copyFile(source, destination);
        assertTrue(destination.isFile());
        
        checkContents(destination);
        assertTrue(source.isFile());
        assertTrue(destination.isFile());
        
        // Now both source and destination exist try the copy again
        try
        {
            FileMover.copyFile(source, destination);
            fail("Expected Exception: Can't copy file when destination exists.");
        }
        catch (IOException ioe)
        {}
        
        // Delete the source and try again.
        source.delete();
        try
        {
            FileMover.copyFile(source, destination);
            fail("Expected Exception: Can't copy with no source and destination exists.");
        }
        catch (IOException ioe)
        {}
        
        // Delete the destination and try again.
        try
        {
            FileMover.copyFile(source, destination);
            fail("Expected Exception: Can't copy file when there is no source.");
        }
        catch (IOException ioe)
        {}
        

    }

    /*
     * Test method for 'org.bodington.util.FileMover.moveFile(File, File)'
     */
    public void testMoveFile() throws Exception
    {
        File source = createSource();
        File destination = createDestination();
        
        FileMover.moveFile(source,destination);
        
        checkContents(destination);
        assertFalse(source.isFile());
        assertTrue(destination.isFile());

    }

    private void checkContents(File destination) throws FileNotFoundException, IOException
    {
        LineNumberReader reader = new LineNumberReader(new FileReader(destination));

        assertEquals("Hello",reader.readLine());
        assertNull(reader.readLine());
    }

    private File createDestination() throws IOException
    {
        File destination = File.createTempFile("destination",null);
        destination.delete();
        return destination;
    }

    private File createSource() throws IOException
    {
        File source = File.createTempFile("source", null);
        
        PrintWriter writer = new PrintWriter(new FileWriter(source));
        writer.println("Hello");
        writer.close();
        return source;
    }


}
