/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.aop.loader;

import org.w3c.dom.Element;
import org.jboss.cache.xml.XmlHelper;
import org.jboss.cache.aop.test.Address;
import org.jboss.cache.aop.test.Person;
import junit.framework.Test;
import junit.framework.TestSuite;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

/**
 * Test case for testing Collection manipulation where Pojos are loaded/stored in a CacheLoader.
 *
 * @author ?
 * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 */
public class FileCacheLoaderAopCollectionsTest extends CacheLoaderTestsBase
{

   protected Element getCacheLoaderConfig(String loc) throws Exception
   {
      String xml = "            <config>\n" +
                  "                \n" +
                  "                <passivation>false</passivation>\n" +
                  "                <preload></preload>\n" +
                  "\n" +
                  "                <cacheloader>\n" +
                  "                    <class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                  "                    <properties>\n" +
                  "                        location=" + loc + "\n" +
                  "                    </properties>\n" +
                  "                    <async>false</async>\n" +
                  "                    <fetchPersistentState>false</fetchPersistentState>\n" +
                  "                    <ignoreModifications>false</ignoreModifications>\n" +
                  "                </cacheloader>\n" +
                  "                \n" +
                  "            </config>";
      return XmlHelper.stringToElement(xml);
   }

   protected void configureCache() throws Exception {
      String tmp_location = System.getProperty("java.io.tmpdir", "c:\\tmp");
      cache.setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "", false, true, false));
//       cache.setCacheLoaderConfiguration(getCacheLoaderConfig(tmp_location));
   }

   public static Test suite() {
      return new TestSuite(FileCacheLoaderAopCollectionsTest.class);
   }

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

   // leave in only the collectiosn test - override the rest.

   public void testEvictionWithCacheLoader()
   {
   }

   public void testPojoEvictionWithCacheLoader()
   {
   }

   public void testPojoRemoveWithCacheLoader()
   {
   }

   public void testPojoListLoadAndRemoveAfterRestart() throws Exception
   {
      String fqn = "/list";

      Person brian = createPerson("Brian");
      Person manik = createPerson("Manik");

      List list = new ArrayList();
      list.add(brian);
      list.add(manik);

      cache.putObject(fqn, list);

      List proxyList = (List) cache.getObject(fqn);

      brian = (Person) proxyList.get(0);
      assertEquals("Brian", brian.getName());
      manik = (Person) proxyList.get(1);
      assertEquals("Manik", manik.getName());

      stopCache();

      startCache();

      proxyList = (List) cache.getObject(fqn);

      brian = (Person) proxyList.get(0);
      assertEquals("Brian", brian.getName());
      manik = (Person) proxyList.get(1);
      assertEquals("Manik", manik.getName());

      brian = (Person) proxyList.remove(0);
      assertEquals("Brian", brian.getName());

      manik = (Person) proxyList.get(0);
      assertEquals("Manik", manik.getName());
   }

   public void testPojoMapLoadAndRemoveAfterRestart() throws Exception
   {
      String fqn = "/map";

      Person brian = createPerson("Brian");
      Person manik = createPerson("Manik");

      Map map = new HashMap();
      map.put("key-brian", brian);
      map.put("key-manik", manik);

      cache.putObject(fqn, map);

      Map proxyMap = (Map) cache.getObject(fqn);

      brian = (Person) proxyMap.get("key-brian");
      assertEquals("Brian", brian.getName());
      manik = (Person) proxyMap.get("key-manik");
      assertEquals("Manik", manik.getName());

      stopCache();

      startCache();

      proxyMap = (Map) cache.getObject(fqn);

      brian = (Person) proxyMap.get("key-brian");
      assertEquals("Brian", brian.getName());
      manik = (Person) proxyMap.get("key-manik");
      assertEquals("Manik", manik.getName());

      brian = (Person) proxyMap.remove("key-brian");
      assertEquals("Brian", brian.getName());

      manik = (Person)proxyMap.get("key-manik");
      assertEquals("Manik", manik.getName());
   }

//   /**
//    * Test for removal of Collection element, where the Collection has been loaded with Pojos created in the test. I
//    * used this to compare it to the newly added tests where the Collection is loaded from the CacheLoader.
//    */
//   public void testPojoCollectionRemove() throws Exception
//   {
//      String fqn = "/list";
//
//      Person brian = createPerson("Brian");
//      Person manik = createPerson("Manik");
//
//      List<Person> list = new ArrayList<Person>();
//      list.add(brian);
//      list.add(manik);
//
//      cache.putObject(fqn, list);
//
//      List<Person> proxyList = (List<Person>) cache.getObject(fqn);
//
//      brian = proxyList.get(0);
//      assertEquals("Brian", brian.getName());
//      manik = proxyList.get(1);
//      assertEquals("Manik", manik.getName());
//
//      proxyList.remove(0);
//
//      manik = proxyList.get(0);
//      assertEquals("Manik", manik.getName());
//   }

   protected Person createPerson(String name)
   {
      Person p = new Person(); // instantiate a Person object named joe
      p.setName(name);
      return p;
   }
}
