package org.jboss.cache.loader;

import junit.framework.Test;
import junit.framework.TestSuite;
import org.jboss.cache.loader.tcp.TcpCacheServer;

/**
 * Tests the TcpDelegatingCacheLoader
 * @author Bela Ban
 * @version $Id: TcpCacheLoaderTest.java,v 1.4 2006/01/27 15:47:17 msurtani Exp $
 */
public class TcpCacheLoaderTest extends CacheLoaderTestsBase {
   static TcpCacheServer cache_server=null;

   static {
      Thread runner=new Thread() {
         public void run() {
            try {
               System.out.println("Starting TcpCacheServer");
               cache_server=new TcpCacheServer();
               cache_server.setBindAddress("127.0.0.1");
               cache_server.setPort(12121);
               cache_server.setConfig("META-INF/local-service.xml"); // must be in classpath (./etc/META-INF)
               cache_server.createService();
               cache_server.startService();
            }
            catch(Exception ex) {
               ex.printStackTrace();
            }
         }
      };

      Runtime.getRuntime().addShutdownHook(new Thread() {
         public void run() {
            if(cache_server != null) {
               System.out.println("Stopping TcpCacheServer");
               cache_server.stopService();
            }
         }
      });

      runner.start();
   }



   protected void configureCache() throws Exception {
      cache.setCacheLoaderConfiguration(getSingleCacheLoaderConfig("",
                                                                   "org.jboss.cache.loader.tcp.TcpDelegatingCacheLoader",
                                                                   "host=127.0.0.1\nport=12121", false, true, false));
   }






   public static Test suite() {
      return new TestSuite(TcpCacheLoaderTest.class);
   }


   public static void main(String[] args) {
      junit.textui.TestRunner.run(suite());
   }

}