/*
 * Copyright (c) 2002, Iowa State University
 * 
 * This file is part of the StickSync project and is licensed
 * under the BSD license.
 * 
 * $Id: TestLocation.java,v 1.3 2002/10/29 05:45:16 cclifton Exp $
 */
package sticksync;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;

/**
 * This is a test for the Location class.
 * @author Curtis Clifton
 * @author Gary T. Leavens
 * @version $Revision: 1.3 $
 */
public class TestLocationNoJUnit {
	
    /**
     * Main method for testing Location.
     */
	public static void main(String[] args) {
		TestLocationNoJUnit t = new TestLocationNoJUnit();
		try {
		t.setUp();
		t.testConstructors();
		t.testAbsorbChangesFrom();
		t.testCopyTo();
		t.testReadable();
		t.testWriteable();
		t.tearDown();
		} catch (IOException e) {
			System.err.println(e);
		} catch (Exception e) {
			System.err.println(e);
		}
	}

    private File[] testFiles;
    private Location[] testLocs;

    /* -------------------------------------------------------------------------
     * TESTS
     * -------------------------------------------------------------------------*/

    public void testConstructors() {
        for (int i=0; i < testFiles.length; i++) {
            Location l1 = new Location(testFiles[i].getPath());
            System.out.print("l1.getPath() = ");
            System.out.println(l1.getPath());
            System.out.print("testLocs[" + i + "].getPath() = ");
            System.out.println(testLocs[i].getPath());
        }
    }
    
    public void testReadable() {
        for (int i=0; i < testLocs.length; i++) {
            System.out.print("testLocs[" + i + "].readable() = ");
            System.out.println(testLocs[i].readable());
        }
    }

    public void testWriteable() {
        for (int i=0; i < testLocs.length; i++) {
            System.out.print("testLocs[" + i + "].writeable() = ");
            System.out.println(testLocs[i].writeable());
            testFiles[i].setReadOnly();
            System.out.print("after setReadOnly: testLocs[" + i + "].readable() = ");
            System.out.println(!testLocs[i].writeable());
        }
    }

    public void testCopyTo() throws IOException {
        String orig0 = testLocs[0].contentsAsString();
        String orig1 = testLocs[1].contentsAsString();
        //@ assume !orig0.equals(orig1);
        Date d = testLocs[0].copyTo(testLocs[1]);
        System.out.println("Date returned by copyTo = " + d);
        System.out.print("contents equal after copyTo = ");
        System.out.println(orig0.equals(testLocs[1].contentsAsString()));
        System.out.print("testLocs[0].modTime() = ");
        System.out.println(testLocs[0].modTime());
        System.out.print("testLocs[0].modTime() = ");
        System.out.println(testLocs[1].modTime());
    }

    public void testAbsorbChangesFrom() throws IOException {
        // !E2! This test assumes merge will fail
        boolean result = testLocs[0].absorbChangesFrom(testLocs[1]);
        System.out.print("absorbChangesFrom() returns = ");
        System.out.println(result);
        System.out.print("checking contents of location and nearbylocation ...");
        System.out.println(testLocs[1].contentsAsString().equals(
                     testLocs[0].getNearbyLocation().contentsAsString()));
    }

    /* -------------------------------------------------------------------------
     * TEST FIXTURE SETUP/TEARDOWN
     * -------------------------------------------------------------------------*/

    protected void setUp() throws Exception {
        testFiles = configureTestFiles(2);
        testLocs = new Location[testFiles.length];
        for (int i=0; i<testLocs.length; i++) {
            testLocs[i] = new Location(testFiles[i]);
        }
    }

    protected void tearDown() throws Exception {
        removeTestFiles();
    }

    /* -------------------------------------------------------------------------
     * STATIC HELPER METHODS     * -------------------------------------------------------------------------*/

    /**
     * Creates the given number of test files, named fileI.txt, where 0 &lt;= I
     * and I &lt; count, in the directory returned by getWorkingDirectory().
     * @see #getWorkingDirectory()
     * @return An array of newly instantiated Location objects representing
     *          the newly created files.
     */
    static File[] configureTestFiles(int count) throws IOException {
        File work = getWorkingDirectory();
        work.mkdir();
        File[] result = new File[count];
        for (int i=0; i < result.length; i++) {
            File f = new File(work,"file"+i+".txt");
            Writer wr = new BufferedWriter(new FileWriter(f));
            wr.write("Contents of file"+i);
            wr.close();
            result[i] = f;
        }
        return result;
    }

    /**
     * Removes the directory returned by getWorkingDirectory(), along with any files
     * contained therein.
     * @see #getWorkingDirectory()     * @throws IOException     */
    static void removeTestFiles() throws IOException {
        File work = getWorkingDirectory();
        File[] files = work.listFiles();
        for (int i=0; i < files.length; i++) {
            files[i].delete();
        }
        if (!work.delete()) {
            throw new IOException("Unable to delete " + work);
        }
    }

    private static File getWorkingDirectory() {
        File temp = new File(System.getProperty("java.io.tmpdir"));
        return new File(temp,"TestStickSync");
    }
}
