package org.jboss.cache.aop.util;

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.PojoCache;
import org.jboss.cache.aop.util.ObjectUtil;
import org.jboss.cache.aop.test.NodeManager;
import org.jboss.cache.aop.test.TestNode;


/**
 * Test case for ObjectUtil
 *
 * @author Ben Wang
 */

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

   public ObjectUtilAopTest(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 testIsReachable() throws Exception
   {
      log_.info("testIsReachable() ....");
      NodeManager pm_ = new NodeManager();

      pm_.setRootNode("root");
      pm_.addNode("root", "kanto");
      pm_.addNode("root.kanto", "tokyo");
      pm_.addNode("root.kanto", "yakahoma");
      pm_.addNode("root.kanto.tokyo", "handanshita");

      TestNode kanto = pm_.findNode("root.kanto");
      TestNode yakahoma = pm_.findNode("root.kanto.yakahoma");
      TestNode hadanshita = pm_.findNode("root.kanto.tokyo.handanshita");

      pm_ = new NodeManager();
      pm_.setRootNode("rt");
      pm_.addNode("rt", "test");
      TestNode test = pm_.findNode("rt.test");

//      cache_.putObject("/pm", pm_);

      assertTrue("Hadanshita should be reachable from Kanto ", ObjectUtil.isReachable(cache_, kanto, hadanshita));
      assertTrue("Hadanshita should also be reachable from Yakahoma! ", ObjectUtil.isReachable(cache_, yakahoma, hadanshita));
      assertFalse("Kanto should not be reachable from test! ", ObjectUtil.isReachable(cache_, kanto, test));
   }

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


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

}

