/*  PAT: Persistent Applications Toolkit (patsystem.sf.net)
 *  Copyright (C) 2004, 2005  Tomasz Nazar, nthx at irc dot pl
 *
 *  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 of the License, or (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Full version of the license is /docs/LICENSE.txt
 */
package org.nthx.pat.tests;

import org.nthx.pat.Pat;
import org.nthx.pat.Persisteble;

/** Test for testing if writing Prevayler's files: command logs, snapshots works
 *  correctly with PAT.
 *  
 *  @version $Id: FileReadingWritingTest.java 3725 2005-06-09 23:57:03Z nthx $
 *  @author nthx@users.sourceforge.net
 */
public class FileReadingWritingTest
        extends PatTestCase
{

    public void setUp()
    {
    }


    // ----------------------------------------
    // ---- Tests -----------------------------
    // ----------------------------------------
    public void testCreatingRootOnly()
    {
        Pat.unload();

        cleanPrevaylersRepo();
        assertEquals("Should be 0 files - clean repo", 0, getFilesCountInRepo()); 

        TestRoot root = new TestRoot();
        assertEquals("Should be 1 file - clean snapshot", 1, getFilesCountInRepo());

        assertEquals("Should be 0 users", 0, root.getUsers().size());
    }

    
    public void testCreatingRootAndSnapshot()
    {
        Pat.unload();

        cleanPrevaylersRepo();
        assertEquals("Should be 0 files - clean repo!", 0, getFilesCountInRepo());
        
        TestRoot root = new TestRoot();
        assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
        
        ((Persisteble)root).takeSnapshot();

        assertEquals("Should be still 1 file", 1, getFilesCountInRepo());
        String firstSnapshotName = "000000000000000000000.snapshot";
        assertEquals("Name should be: " + firstSnapshotName, 
                firstSnapshotName, getLastFileFromRepo());
        
        
        //TEST SECOND SESSION
        Pat.unload();

        TestRoot rootReloaded = new TestRoot();
        if (rootReloaded == root)
            throw new AssertionError("Roots' addresses should differ");
        assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
        assertEquals("Should be 0 users", 0, rootReloaded.getUsers().size());
    }

    
    public void testCreatingRootAndSnapshotAndChangesToRoot()
    {
        Pat.unload();

        cleanPrevaylersRepo();
        assertEquals("Should be 0 files - clean repo!", 0, getFilesCountInRepo());

        
        TestRoot root = new TestRoot();
        assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
        
        ((Persisteble)root).takeSnapshot();

        assertEquals("Should be still 1 file", 1, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000000.snapshot", 
                "000000000000000000000.snapshot", getLastFileFromRepo());

        root.newUser("user 1");
        assertEquals("Should be 1 user", 1, root.getUsers().size());
        assertEquals("Should be 2 files: snap and log", 2, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000001.commandLog", 
                "000000000000000000001.commandLog", getLastFileFromRepo());


        //TEST SECOND SESSION
        Pat.unload();

        TestRoot rootReloaded = new TestRoot();
        if (rootReloaded == root)
            throw new AssertionError("Roots' addresses should differ");
        assertEquals("Should be 2 files", 2, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000001.commandLog", 
                "000000000000000000001.commandLog", getLastFileFromRepo());
        assertEquals("Should be 1 user", 1, rootReloaded.getUsers().size());
    }
    
    public void testDiskChangesMadeSeveralTimes()
    {
        Pat.unload();

        cleanPrevaylersRepo();
        assertEquals("Should be 0 files - clean repo!", 0, getFilesCountInRepo());
        
        TestRoot root = new TestRoot();
        assertEquals("Should be 1 file - fresh snapshot", 1, getFilesCountInRepo());
        
        ((Persisteble)root).takeSnapshot();

        assertEquals("Should be still 1 file", 1, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000000.snapshot", 
                "000000000000000000000.snapshot", getLastFileFromRepo());

        root.newUser("user 1");
        assertEquals("Should be 1 user", 1, root.getUsers().size());
        assertEquals("Should be 2 files: snap and log", 2, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000001.commandLog", 
                "000000000000000000001.commandLog", getLastFileFromRepo());


        //TEST SESSION no. 2
        Pat.unload();

        root = new TestRoot();
        assertEquals("Should be 2 files", 2, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000001.commandLog", 
                "000000000000000000001.commandLog", getLastFileFromRepo());
        assertEquals("Should be 1 user", 1, root.getUsers().size());

        root.newUser("user - 2");
        assertEquals("Should be 2 user", 2, root.getUsers().size());
        assertEquals("Should be 3 files: snap and 2xlog", 3, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000002.commandLog", 
                "000000000000000000002.commandLog", getLastFileFromRepo());
        
        
        
        //TEST SESSION no. 3 -- reading data from command logs
        Pat.unload();

        root = new TestRoot();
        assertEquals("Should be 3 files", 3, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000002.commandLog", 
                "000000000000000000002.commandLog", getLastFileFromRepo());
        root.newUser("user 3");
        assertEquals("Should be 3 users", 3, root.getUsers().size());
        assertEquals("Should be 4 files", 4, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000003.commandLog", 
                "000000000000000000003.commandLog", getLastFileFromRepo());

        ((Persisteble)root).takeSnapshot();

        assertEquals("Should be 5 files: snap,3xlog,smap", 
                5, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000003.snapshot", 
                "000000000000000000003.snapshot", getLastFileFromRepo());
        


        //TEST SESSION no. 4 -- reading data from snapshot
        Pat.unload();

        root = new TestRoot();
        assertEquals("Should be 3 users", 3, root.getUsers().size());
        assertEquals("Should be 5 files: snap,3xlog,snap", 
                5, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000003.snapshot", 
                "000000000000000000003.snapshot", getLastFileFromRepo());

        ((Persisteble)root).takeSnapshot();

        assertEquals("Should be 5 files: snap,3xlog,snap", 
                5, getFilesCountInRepo());
        assertEquals("Name should be: 000000000000000000003.snapshot", 
                "000000000000000000003.snapshot", getLastFileFromRepo());
        //above true is '5' instead of '6', because Prevayler makes new snapshot
        //with the same name as the last one if there are no command logs
        //between two snapshots. But if the root's objects are changed, then of
        //course, size of those two snapshot files will differ.
        //xxx: test size of snapshots before and after snapshot
        
        
        //TEST SESSION no. 5 -- reading last snapshot
        Pat.unload();

        root = new TestRoot();
        assertEquals("Should be 3 users", 3, root.getUsers().size());
    }



    // ----------------------------------------
    // ---- JUnit's stuff ---------------------
    // ----------------------------------------
    public FileReadingWritingTest(String arg)
    {
        super(arg);
    }
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(FileReadingWritingTest.class);
    }
    

}