/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.cache.aop;

import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.aop.PojoCache;
import org.jboss.cache.aop.test.Person;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.PropertyConfigurator;

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

/**
 * Test for putObject with existing pojo for bulkd remove for speed.
 *
 * @author Ben Wang
 */

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

   public ReplicatedPutWithBulkRemoveAopTest(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 testPutPutLocal() throws Exception
   {
      log_.info("testPutPut() ....");
      Person test = new Person();
      test.setName("Ben");
      test.setAge(10);
      Address addr = new Address();
      addr.setZip(95123);
      addr.setCity("Sunnyvale");
      test.setAddress(addr);
      cache_.putObject("/a", test);
      Person result = (Person)cache_.getObject("/a");
      assertEquals(" ", test, result);

      Person joe = new Person();
      joe.setName("joe");
      joe.setAge(20);
      cache_.putObject("/a", joe);
      Person joe1 = (Person)cache_.getObject("/a");
      assertEquals("Age should be ", 20, joe1.getAge());

      assertEquals("Age should be ", 10, result.getAge());
      assertEquals("Zip should be ", 95123, result.getAddress().getZip());

      // Try to re-use the pojo
      cache_.putObject("/a", test);
      Person result1 = (Person)cache_.getObject("/a");
      assertEquals("Zip should be ", 95123, result1.getAddress().getZip());

   }

   public void testPutPut() throws Exception
   {
      log_.info("testPutPut() ....");
      Person test = new Person();
      test.setName("Ben");
      test.setAge(10);
      Address addr = new Address();
      addr.setZip(95123);
      addr.setCity("Sunnyvale");
      test.setAddress(addr);
      cache_.putObject("/a", test);
      Person result = (Person)cache_.getObject("/a");
      assertEquals(" ", test, result);

      Person result1 = (Person)cache1_.getObject("/a");
      assertEquals("Age should be ", 10, result1.getAge());
      assertEquals("Zip should be ", 95123, result1.getAddress().getZip());

      Person joe = new Person();
      joe.setName("joe");
      joe.setAge(20);
      cache_.putObject("/a", joe);
      Person joe1 = (Person)cache_.getObject("/a");
      assertEquals("Age should be ", 20, joe1.getAge());

      assertEquals("Age should be ", 10, result.getAge());
      assertEquals("Zip should be ", 95123, result.getAddress().getZip());
   }

   Address getAddress(String city)
   {
      Address addr = new Address();
      addr.setCity(city);
      addr.setZip(95123);
      addr.setStreet("Sunnyvale");
      return addr;
   }

   public void testPutCollection() throws Exception
   {
      log_.info("testPutCollection() ....");
      Person p1 = new Person();
      p1.setName("Ben");
      p1.setAge(10);
      p1.setAddress(getAddress("Sunnyvale"));

      List lang = new ArrayList();
      lang.add(getAddress("Taipei"));
      lang.add(getAddress("Tainan"));
      p1.setLanguages(lang);

      cache_.putObject("/a", p1);
      Person result = (Person)cache_.getObject("/a");
      assertEquals(" ", p1, result);

      Person result1 = (Person)cache1_.getObject("/a");
      assertEquals("Age should be ", 10, result1.getAge());
      assertEquals("Zip should be ", 95123, result1.getAddress().getZip());

      Person p2 = new Person();
      p2.setName("joe");
      p2.setAge(20);
      cache_.putObject("/a", p2);
      Person joe1 = (Person)cache_.getObject("/a");
      assertEquals("Age should be ", 20, joe1.getAge());

      // put p1 again
      cache_.putObject("/a", p1);

      // remove p1
      Person p3 = (Person)cache_.removeObject("/a");
      assertEquals("Zip should be ", 95123, p3.getAddress().getZip());

      assertEquals("Age should be ", 10, result.getAge());
      assertEquals("Zip should be ", 95123, result.getAddress().getZip());
   }

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


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

}
