/*
 * 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.PropertyConfigurator;
import org.jboss.cache.aop.test.Person;

import java.util.Map;

/**
 * Test for use of old TreeCacheAop api.
 *
 * @author Ben Wang
 */

public class TreeCacheAopCompatibilityAopTest extends TestCase
{
   Log log_= LogFactory.getLog(TreeCacheAopCompatibilityAopTest.class);
   TreeCacheAopMBean cache_;

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

   protected void setUp() throws Exception
   {
      super.setUp();
      log_.info("setUp() ....");
      String configFile = "META-INF/local-service.xml";
      cache_ = (TreeCacheAopMBean)new TreeCacheAop();
      PropertyConfigurator config = new PropertyConfigurator();
      config.configure(cache_, configFile); // read in generic replSync xml
      cache_.start();
   }

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

//   public void testDummy() {}

   public void testIfc() throws Exception
   {
      TreeCacheAopIfc ifc = (TreeCacheAopIfc)cache_;   
   }

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

      try {
         cache_.putObject(InternalDelegate.JBOSS_INTERNAL, test);
         fail("putObject under JBoss_Internal should fail");
      } catch (IllegalArgumentException iex)
      {
         // ok
      }

      try {
         cache_.removeObject(InternalDelegate.JBOSS_INTERNAL);
         fail("putObject under JBoss_Internal should fail");
      } catch (IllegalArgumentException iex)
      {
         // ok
      }
   }

   public void testPutRemove() throws Exception
   {
      log_.info("testPutRemove() ....");
      Person test = new Person();
      test.setName("Ben");
      test.setAge(10);
      cache_.putObject("/a", test);
      Person result = (Person)cache_.getObject("/a");
      assertEquals(" ", test, result);
      result.setAge(20);
      cache_.removeObject("/a");
      assertNull("Object should be null ", cache_.getObject("/a"));
      assertEquals("Age should be updated as ", 20, test.getAge());
   }

   public void testPutRemoveNodeExistence() throws Exception
   {
      log_.info("testPutRemove() ....");
      Person test = new Person();
      test.setName("Ben");
      test.setAge(10);
      cache_.putObject("/a", test);
      Person result = (Person)cache_.getObject("/a");
      assertEquals(" ", test, result);
      result.setAge(20);
      cache_.removeObject("/a");
      assertNull("Object should be null ", cache_.getObject("/a"));
      assertEquals("Age should be updated as ", 20, test.getAge());

      assertNull("DataNode should not exisit ", cache_.get("/a"));
   }

   public void testFindObjects() throws Exception
   {
      log_.info("testFindObjects() ....");
      Map map = cache_.findObjects("/");
      assertEquals("Objects size should be ", 0, map.size());
      Person ben = new Person();
      ben.setName("Ben");
      ben.setAge(10);
      cache_.putObject("/a/b/c", ben);
      cache_.putObject("/e", ben); // multiple keys, same pojo
      Person joe = new Person();
      joe.setName("Joe");
      joe.setAge(10);
      cache_.putObject("/f/joe", joe);
      map = cache_.findObjects("/");
      assertEquals("Objects size should be ", 3, map.size());

      map = cache_.findObjects("/a");
      assertEquals("Objects size should be ", 1, map.size());
      cache_.removeObject("/e");
      map = cache_.findObjects("/");
      assertEquals("Objects size should be ", 2, map.size());
   }

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


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

}
