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.aop.test.Person;

import java.util.Map;


/**
 * New LocalAopTest that doesn't use TreeCacheAopTester. There should have no need for it not.
 *
 * @author Ben Wang
 */

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

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

   protected void setUp() throws Exception
   {
      super.setUp();
      log_.info("setUp() ....");
      String configFile = "META-INF/local-service.xml";
      cache_ = new PojoCache();
      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 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(NewLocalAopTest.class);
   }


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

}

