/*  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.IdentityMap;
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: IdentityMapTest.java 3725 2005-06-09 23:57:03Z nthx $
 *  @author nthx@users.sourceforge.net
 */
public class IdentityMapTest
        extends PatTestCase
{

    public void setUp()
    {
    }


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

        cleanPrevaylersRepo();

        Bank bank = new Bank();
        assertEquals(1, ((IdentityMap)bank).getMapForTestsOnly().size());
        assertNotNull(((IdentityMap)bank).getObject(new Long(1)));
        assertEquals(bank, ((IdentityMap)bank).getObject(new Long(1)));
        assertEquals(bank, Pat.getInstance().getRootAsIdentityMap());

        Pat.unload();
        bank = new Bank();
        assertEquals(1, ((IdentityMap)bank).getMapForTestsOnly().size());
        assertNotNull(((IdentityMap)bank).getObject(new Long(1)));
        assertEquals(bank, ((IdentityMap)bank).getObject(new Long(1)));
        assertEquals(bank, Pat.getInstance().getRootAsIdentityMap());
    }

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

        cleanPrevaylersRepo();

        TestRoot root = new TestRoot();
        TestUser tomasz = root.newUser("Tomasz");

        assertEquals(3, ((IdentityMap)root).getMapForTestsOnly().size());
        assertNotNull(((IdentityMap)root).getObject(new Long(1)));
        assertNotNull(((IdentityMap)root).getObject(new Long(2)));
        assertNotNull(((IdentityMap)root).getObject(new Long(3)));

        assertEquals(root, ((IdentityMap)root).getObject(new Long(2)));
        assertEquals(root.getUsers(), ((IdentityMap)root).getObject(new Long(1)));
        TestUsers usersFromIM = (TestUsers)(((IdentityMap)root).getObject(new Long(1)));
        assertEquals(1, usersFromIM.size());
        assertEquals(root.findUser("Tomasz"), ((IdentityMap)root).getObject(new Long(3)));
        assertEquals(root, Pat.getInstance().getRootAsIdentityMap());

        ((Persisteble)root).takeSnapshot();

        Pat.unload();
        root = new TestRoot();
        assertEquals("Should be 3 BOs", 3, ((IdentityMap)root).getMapForTestsOnly().size());
        assertNotNull(((IdentityMap)root).getObject(new Long(1)));
        assertNotNull(((IdentityMap)root).getObject(new Long(2)));
        assertNotNull(((IdentityMap)root).getObject(new Long(3)));

        assertEquals(root, ((IdentityMap)root).getObject(new Long(2)));
        assertEquals(root.getUsers(), ((IdentityMap)root).getObject(new Long(1)));
        usersFromIM = (TestUsers)(((IdentityMap)root).getObject(new Long(1)));
        assertEquals(1, usersFromIM.size());
        assertEquals(root.findUser("Tomasz"), ((IdentityMap)root).getObject(new Long(3)));
        assertEquals(root, Pat.getInstance().getRootAsIdentityMap());
    }

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

        cleanPrevaylersRepo();

        TestRoot root = new TestRoot();
        TestUser tomasz = root.newUser("Tomasz");

        assertEquals(3, ((IdentityMap)root).getMapForTestsOnly().size());
        assertNotNull(((IdentityMap)root).getObject(new Long(1)));
        assertNotNull(((IdentityMap)root).getObject(new Long(2)));
        assertNotNull(((IdentityMap)root).getObject(new Long(3)));
        assertEquals(root, ((IdentityMap)root).getObject(new Long(2)));
        assertEquals("Differs 1st time",root.getUsers(), ((IdentityMap)root).getObject(new Long(1)));
        TestUsers usersFromIM = (TestUsers)(((IdentityMap)root).getObject(new Long(1)));
        assertEquals(1, usersFromIM.size());
        assertEquals(root.findUser("Tomasz"), ((IdentityMap)root).getObject(new Long(3)));
        assertEquals(root, Pat.getInstance().getRootAsIdentityMap());


        Pat.unload();
        root = new TestRoot();
        assertEquals("Should be 3 BOs", 3, ((IdentityMap)root).getMapForTestsOnly().size());
        assertNotNull(((IdentityMap)root).getObject(new Long(1)));
        assertNotNull(((IdentityMap)root).getObject(new Long(2)));
        assertNotNull(((IdentityMap)root).getObject(new Long(3)));

        assertEquals(root, ((IdentityMap)root).getObject(new Long(2)));
        logTest.debug("SSS: ----------");
        logTest.debug("IM(1): " + ((IdentityMap)root).getObject(new Long(1)));
        logTest.debug("IM(2): " + ((IdentityMap)root).getObject(new Long(2)));
        logTest.debug("IM(3): " + ((IdentityMap)root).getObject(new Long(3)));
        assertEquals("Differs 2nd time",root.getUsers(), ((IdentityMap)root).getObject(new Long(1)));
        usersFromIM = (TestUsers)(((IdentityMap)root).getObject(new Long(1)));
        assertEquals(1, usersFromIM.size());
        assertEquals(root.findUser("Tomasz"), ((IdentityMap)root).getObject(new Long(3)));
        assertEquals(root, Pat.getInstance().getRootAsIdentityMap());
    }


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