package org.jboss.cache.replicated;

import junit.framework.TestCase;
import org.jboss.cache.TreeCache;
import org.jboss.cache.Fqn;
import org.jboss.cache.misc.TestingUtil;


public class AnycastTest extends TestCase
{
   int numCaches = 5;
   TreeCache[] caches;
   Fqn fqn = Fqn.fromString("/test");

   protected void setUp() throws Exception
   {
      caches = new TreeCache[numCaches];
      for (int i=0; i<caches.length; i++) caches[i] = createCache();
      
      TestingUtil.blockUntilViewsReceived(caches, 10000);
   }

   protected void tearDown()
   {
      if (caches != null)
      {
         for (int i=0; i<caches.length; i++) caches[i].stopService();
      }
      caches = null;
   }

   private TreeCache createCache() throws Exception
   {
      TreeCache c = new TreeCache();
      c.setCacheMode("REPL_SYNC");
      c.startService();
      return c;
   }

   public void testMCastReplication() throws Exception
   {
      for (int i=0; i<caches.length; i++) assertNull(caches[i].get(fqn));

      caches[0].put(fqn, "k", "v");

      for (int i=0; i<caches.length; i++)  assertEquals("v", caches[i].get(fqn, "k"));
   }

   public void testAnycastReplication() throws Exception
   {
      for (int i=0; i<caches.length; i++) assertNull(caches[i].get(fqn));

      caches[0].setForceAnycast(true);
      caches[0].put(fqn, "k", "v");

      for (int i=0; i<caches.length; i++)  assertEquals("v", caches[i].get(fqn, "k"));
   }

}
