package org.jboss.cache.aop;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.PropertyConfigurator;
//import org.jboss.cache.data.Address;
//import org.jboss.cache.data.Course;
//import org.jboss.cache.data.RandomString;
//import org.jboss.cache.aop.PojoCache;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.aop.test.Student;
import org.jboss.cache.aop.test.CacheObject;

import javax.naming.Context;
import java.util.Properties;


/**
 * New NewReplicatedAopTest that doesn't use TreeCacheAopTester.
 *
 * @author Ben Wang
 */

public class NewReplicatedAopTest extends TestCase
{
   Log log_=LogFactory.getLog(NewReplicatedAopTest.class);
   PojoCache cache_;
   PojoCache cache1_;

   public NewReplicatedAopTest(String name)
   {
      super(name);
   }

   protected void setUp() throws Exception
   {
      super.setUp();
      Properties prop = new Properties();
      prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
      cache_ = new PojoCache();
      PropertyConfigurator config = new PropertyConfigurator(); // configure tree cache.
      config.configure(cache_, "META-INF/replSync-service.xml");

      cache1_ = new PojoCache();
      config.configure(cache1_, "META-INF/replSync-service.xml");
      cache_.start();
      cache1_.start();
   }

   protected void tearDown() throws Exception
   {
      super.tearDown();
      cache_.stop();
      cache1_.stop();
   }

   /*
   public void testObjectSizePut() throws Exception
   {
      org.jboss.cache.data.Student student = (org.jboss.cache.data.Student)constructObject();
      cache_.putObject("/joe", student);
   }


   static Object constructObject()
   {
      org.jboss.cache.data.Student joe = new org.jboss.cache.data.Student();
      joe.setName("Joe");

      Address add = new Address();
      add.setZip(94086);
      add.setCity("Sunnyvale)");
      add.setStreet("Albertson");
      joe.setAddress(add);

      String str;
      for(int i=0; i < 100; i++)
      {
         Course course = new Course();
         str = RandomString.randomstring(10,20);
         course.setInstructor(str);
         str = RandomString.randomstring(10,20);
         course.setTitle(str);
         str = RandomString.randomstring(10,20);
         course.setRoom(str);
         joe.addCourse(course);
      }

      return joe;
   }
   */
   public void testRemoteRemove() throws Exception
   {
      log_.info("testRemoteRemove() ....");
      Person test = new Person();
      test.setName("Ben");
      test.setAge(10);
      cache_.putObject("/a", test);
      Person result = (Person)cache_.getObject("/a");
      assertEquals(" ", test, result);

      Person remote = (Person)cache1_.getObject("/a");
      assertEquals("Age should be ", 10, remote.getAge());

      // Remote remove
      cache_.removeObject("/a");
      assertNull("Object should be null ", cache_.getObject("/a"));

      assertNull("Object should be null ", cache1_.getObject("/a"));
      // It is 0 since it will be un-initialized.
      assertEquals("Age should be ", 0, remote.getAge());
   }

   /**
    * JBCACHE-200.
    * @throws Exception
    */
   public void testStateTransfer() throws Exception
   {
      log_.info("testStateTransfer() ....");
      Person test = new Person();
      test.setName("Ben");
      test.setAge(10);
      cache_.putObject("/a", test);
      Person result = (Person)cache_.getObject("/a");
      assertEquals(" ", test, result);

      // restart cache1_
      cache1_.stop();
      cache1_.remove("/a");
      cache1_.start();
      // Start from scratch for initial state transfer
      Person remote = (Person)cache1_.getObject("/a");
      assertEquals("Age should be ", 10, remote.getAge());
   }

   public static Test suite() throws Exception
   {
      return new TestSuite(NewReplicatedAopTest.class);
   }


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

}

