/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt 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.statetransfer;

import org.jboss.cache.CacheException;
import org.jboss.cache.Fqn;
import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.TreeCacheMBean;
import org.jboss.cache.Version;
import org.jboss.cache.lock.TimeoutException;

/**
 * A FailedStateTransferTest.
 * 
 * @author Brian Stansberry
 * @version $Revision$
 */
public class FailedStateTransferTest extends StateTransferTestBase
{
   
   public void testFailedStateTransfer() throws Exception
   {
      TreeCacheMBean tree =new SecretiveStateCache();
      PropertyConfigurator config=new PropertyConfigurator();
      config.configure(tree, "META-INF/replAsync-service.xml");
      tree.setClusterName("VersionedTestBase");
      tree.setReplicationVersion(getReplicationVersion());
      // Use a long timeout to facilitate setting debugger breakpoints
      tree.setInitialStateRetrievalTimeout(60000);
      
      // Put the cache in the map before starting, so if it fails in
      // start it can still be destroyed later
      caches.put("secretive", tree);
      
      tree.createService();
      tree.startService();
      
      TreeCacheMBean recipient = createCache("recipient", false, false, false, false, false);
      try
      {
         recipient.startService();
         fail("startService() should throw an exception");
      }
      catch (CacheException good)
      {
         // this is what we want
      }
   }

   protected String getReplicationVersion()
   {
      return Version.version;
   }

   /**
    * Overrides the superclass to always throw an exception in _getState
    */
   private static class SecretiveStateCache extends TreeCache
   {
      SecretiveStateCache() throws Exception
      {
         super();
      }

      public byte[] _getState(Fqn fqn, long timeout, boolean force, boolean suppressErrors) throws Throwable
      {
         throw new TimeoutException("Planned Timeout");
      }      
      
   }
}
