/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.cache.loader;

import org.jboss.cache.Fqn;
import org.jboss.cache.buddyreplication.BuddyManager;

/**
 * Base class for unit tests of ExtendedCacheLoader implementations.
 * 
 * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
 * @version $Revision$
 */
public abstract class ExtendedCacheLoaderTestsBase extends CacheLoaderTestsBase
{

   static final Fqn SUBTREE_FQN = new Fqn(FQN, "subtree");

   static final Fqn BUDDY_BASE = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, "test");
   
   static final Fqn BUDDY_PLUS_FQN = new Fqn(BUDDY_BASE, FQN);
   
   static final Fqn BUDDY_PLUS_SUBTREE_FQN = new Fqn(BUDDY_BASE, SUBTREE_FQN);
   
   ExtendedCacheLoader      extendedLoader=null;
   
   /**
    * Create a new ExtendedCacheLoaderTestsBase.
    * 
    */
   public ExtendedCacheLoaderTestsBase()
   {
      super();
   }
   
   protected void setUp() throws Exception {
      super.setUp();
      extendedLoader= (ExtendedCacheLoader) loader;
   }

   /**
    * Test load/store state.
    */
   public void testPartialLoadAndStore()
      throws Exception {

      /* Empty state. */
      extendedLoader.remove(Fqn.fromString("/"));
      // assertEquals(0, loader.loadEntireState().length);
//      loader.storeEntireState(new byte[0]);
//      assertEquals(0, loader.loadEntireState().length);
//      loader.storeEntireState(null);
//      assertEquals(0, loader.loadEntireState().length);
//      assertEquals(null, loader.get(FQN));

      /* Use a complex object to ensure that the class catalog is used. */
      Complex c1 = new Complex();
      Complex c2 = new Complex(c1);
      Complex c3 = new Complex();
      Complex c4 = new Complex(c3);

      /* Add objects. */
      extendedLoader.put(FQN, new Integer(1), c1);
      extendedLoader.put(FQN, new Integer(2), c2);
      extendedLoader.put(SUBTREE_FQN, new Integer(1), c3);
      extendedLoader.put(SUBTREE_FQN, new Integer(2), c4);
      addDelay();
      assertEquals(c1, extendedLoader.get(FQN).get(new Integer(1)));
      assertEquals(c2, extendedLoader.get(FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(FQN).size());
      assertEquals(c3, extendedLoader.get(SUBTREE_FQN).get(new Integer(1)));
      assertEquals(c4, extendedLoader.get(SUBTREE_FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(SUBTREE_FQN).size());

      /* Save state. */
      byte[] state = extendedLoader.loadState(SUBTREE_FQN);
      assertTrue(state.length > 0);
      extendedLoader.remove(SUBTREE_FQN);


      /* Restore state. */
      extendedLoader.storeState(state, SUBTREE_FQN);
      addDelay();
      assertEquals(c1, extendedLoader.get(FQN).get(new Integer(1)));
      assertEquals(c2, extendedLoader.get(FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(FQN).size());
      assertEquals(c3, extendedLoader.get(SUBTREE_FQN).get(new Integer(1)));
      assertEquals(c4, extendedLoader.get(SUBTREE_FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(SUBTREE_FQN).size());
   }
   
   public void testBuddyBackupStore() throws Exception
   {
      /* Empty state. */
      extendedLoader.remove(Fqn.ROOT);

      /* Use a complex object to ensure that the class catalog is used. */
      Complex c1 = new Complex();
      Complex c2 = new Complex(c1);
      Complex c3 = new Complex();
      Complex c4 = new Complex(c3);

      /* Add objects. */
      extendedLoader.put(FQN, new Integer(1), c1);
      extendedLoader.put(FQN, new Integer(2), c2);
      extendedLoader.put(SUBTREE_FQN, new Integer(1), c3);
      extendedLoader.put(SUBTREE_FQN, new Integer(2), c4);
      addDelay();
      assertEquals(c1, extendedLoader.get(FQN).get(new Integer(1)));
      assertEquals(c2, extendedLoader.get(FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(FQN).size());
      assertEquals(c3, extendedLoader.get(SUBTREE_FQN).get(new Integer(1)));
      assertEquals(c4, extendedLoader.get(SUBTREE_FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(SUBTREE_FQN).size());

      /* Save state. */
      byte[] state = extendedLoader.loadState(FQN);
      assertTrue(state.length > 0);

      /* Restore state. */
      extendedLoader.storeState(state, BUDDY_BASE);
      addDelay();
      assertEquals(c1, extendedLoader.get(BUDDY_PLUS_FQN).get(new Integer(1)));
      assertEquals(c2, extendedLoader.get(BUDDY_PLUS_FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(BUDDY_PLUS_FQN).size());
      assertEquals(c3, extendedLoader.get(BUDDY_PLUS_SUBTREE_FQN).get(new Integer(1)));
      assertEquals(c4, extendedLoader.get(BUDDY_PLUS_SUBTREE_FQN).get(new Integer(2)));
      assertEquals(2, extendedLoader.get(BUDDY_PLUS_SUBTREE_FQN).size());
      
   }

}
