/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This 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.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.cache.marshall;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.buddyreplication.BuddyManager;

import junit.framework.TestCase;

/**
 * Test for JBCACHE-1225 -- whether enqueuing of method calls for buddy
 * backup regions work.
 * 
 * @author <a href="brian.stansberry@jboss.com">Brian Stansberry</a>
 * @version $Revision: 1.1 $
 */
public class BuddyBackupEnqueingTestCase extends TestCase
{
   private static final Log log = LogFactory.getLog(BuddyBackupEnqueingTestCase.class);
   
   private TreeCache tree;

   protected void setUp() throws Exception
   {
      super.setUp();
      tree=new TreeCache();
      PropertyConfigurator config=new PropertyConfigurator();
      config.configure(tree, "META-INF/replAsync-service.xml"); // read in generic replAsync xml
      tree.setClusterName("Test");
      // Use marshaller
      tree.setUseRegionBasedMarshalling(true);
      tree.setInactiveOnStartup(true);
      tree.createService();
      tree.startService();
   }

   protected void tearDown() throws Exception
   {
      super.tearDown();
      
      tree.stopService();
   }
   
   /**
    * Confirms that we get an ISE if we try to enqueue a method call for
    * a buddy-backup FQN where no equivalent main tree region exists, and
    * that we don't get the ISE any more once the main tree region is created.
    * 
    * This test design is based on the fact that _enqueueMethodCall is public,
    * so this is a bit too much of a white-box test. But, oh well. ;)
    * 
    * @throws Throwable
    */
   public void testBuddyBackupEnqueuing() throws Throwable
   {
      Fqn raw = Fqn.fromString("/region/node");
      Fqn group = Fqn.fromString("group");
      Fqn fqn = new Fqn(group, raw);
      fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, fqn);
      JBCMethodCall call = MethodCallFactory.create(MethodDeclarations.existsMethod, new Object[]{ fqn } );
      
      try
      {
         tree._enqueueMethodCall(fqn.toString(), call);
         fail("Able to enqueue method call for non-existent region");
      }
      catch (IllegalStateException good) {}
      
      tree.activateRegion(raw.toString());
      
      try
      {
         tree._enqueueMethodCall(fqn.toString(), call);         
      }
      catch (IllegalStateException bad) 
      {
         String msg = "Caught ISE enqueuing method call for " + fqn;
         log.error(msg, bad);
         fail(msg);
      }
   }

}
