/*****************************************
 *                                       *
 *  JBoss Portal: The OpenSource Portal  *
 *                                       *
 *   Distributable under LGPL license.   *
 *   See terms of license at gnu.org.    *
 *                                       *
 *****************************************/
package org.jboss.cache;

import junit.framework.TestCase;
import org.jboss.cache.lock.IsolationLevel;
import org.jgroups.View;

import javax.transaction.TransactionManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
 * @version $Id: TreeCacheListenerTest.java,v 1.5 2006/04/06 16:24:19 msurtani Exp $
 */
public class TreeCacheListenerTest extends TestCase
{

   public TreeCacheListenerTest(String s)
   {
      super(s);
   }

   private TreeCache cache;

   protected void setUp() throws Exception
   {
      super.setUp();
      cache = new TreeCache();
      cache.setCacheMode(TreeCache.LOCAL);
      cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
      cache.setTransactionManagerLookup(new DummyTransactionManagerLookup());
      cache.create();
      cache.start();
   }

   protected void tearDown() throws Exception
   {
      super.tearDown();
      cache.stop();
      cache.destroy();
   }

   public void testRemovePropertyWithCommit() throws Exception
   {
      cache.put("/", "name", "value");

      EventLog el = new EventLog();
      cache.addTreeCacheListener(el);

      TransactionManager tm = cache.getTransactionManager();
      tm.begin();
      cache.remove("/", "name");
      tm.commit();

      assertEquals(Arrays.asList(new Object[]{"modify about to modify local /","modified /", "modify Modified local /"}), el.events);
      el.events.clear();
   }

   public void testNodeCreatedAndModifiedEvent() throws Exception
   {
       EventLog el = new EventLog();
       cache.addTreeCacheListener(el);
       cache.put("/hello/world", "x", "y");
       System.out.println(el.events);

       List expectedChanges = new ArrayList();
       expectedChanges.add("created /hello");
       expectedChanges.add("created /hello/world");
       expectedChanges.add("modify about to modify local /hello/world");
       expectedChanges.add("modified /hello/world");
       expectedChanges.add("modify Modified local /hello/world");

       assertEquals(expectedChanges.size(), el.events.size());
       assertEquals(expectedChanges, el.events);
       el.events.clear();
   }

   public void testRemoveNodeWithRollback() throws Exception
   {
      cache.put("/child", new HashMap());

      EventLog el = new EventLog();
      cache.addTreeCacheListener(el);

      TransactionManager tm = cache.getTransactionManager();
      tm.begin();
      cache.remove("/child");
      tm.rollback();

      assertEquals(Arrays.asList(new Object[]{"remove about to remove local /child","removed /child","remove Removed local /child","created /child"}), el.events);
      el.events.clear();
   }

   public static class EventLog extends AbstractTreeCacheListener
   {
      public final List events = new ArrayList();
      public void nodeCreated(Fqn fqn)
      {
         events.add("created " + fqn);
      }
      public void nodeRemove(Fqn fqn, boolean pre, boolean isLocal)
      {
         events.add("remove "+
                 (pre? "about to remove " : "Removed ") +
                 (isLocal? "local " : "remote ") + fqn);
      }      
      public void nodeRemoved(Fqn fqn)
      {
         events.add("removed " + fqn);
      }
      public void nodeLoaded(Fqn fqn)
      {
         throw new UnsupportedOperationException();
      }
      public void nodeEvicted(Fqn fqn)
      {
         throw new UnsupportedOperationException();
      }
      public void nodeModify(Fqn fqn, boolean pre, boolean isLocal)
      {
         events.add("modify "+
                 (pre? "about to modify " : "Modified ") +
                 (isLocal? "local " : "remote ") + fqn);
      }
      public void nodeModified(Fqn fqn)
      {
         events.add("modified " + fqn);
      }
      public void nodeVisited(Fqn fqn)
      {
         throw new UnsupportedOperationException();
      }
      public void cacheStarted(TreeCache treeCache)
      {
         throw new UnsupportedOperationException();
      }
      public void cacheStopped(TreeCache treeCache)
      {
         throw new UnsupportedOperationException();
      }
      public void viewChange(View view)
      {
         throw new UnsupportedOperationException();
      }
   }


}
