package org.jboss.cache.marshall;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Test on RegionManager class.
 * @author Ben Wang, Feb 11, 2004
 */
public class RegionManagerTest extends TestCase
{
   private final String DEFAULT_REGION = "/_default_/";

   public RegionManagerTest(String s)
   {
      super(s);
   }

   public void setUp() throws Exception
   {
      super.setUp();
   }

   public void tearDown() throws Exception
   {
      super.tearDown();
   }

   public void testCreateRegion() {
      String fqn1 = "/a/b/c";
      String fqn2 = "/a/b";
      String fqn3 = "/aop";

      RegionManager regionManager = new RegionManager();
      try {
         regionManager.createRegion(DEFAULT_REGION, null);
         regionManager.createRegion(fqn1, null);
         regionManager.createRegion(fqn2, null);
         regionManager.createRegion(fqn3, null);
      } catch (RegionNameConflictException e) {
         fail("Failed to create region");
         e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      }

      Region[] regions = regionManager.getRegions();
      assertEquals("Region size ", 4, regions.length);
   }

   public void testCreateRegion2() {
      String fqn1 = "/a/b/c";
      String fqn2 = "/a/b/";
      String fqn3 = "/_default_";

      RegionManager regionManager = new RegionManager();
      try {
         regionManager.createRegion(fqn1, null);
         regionManager.createRegion(fqn2, null);
         regionManager.createRegion(fqn3, null);
      } catch (RegionNameConflictException e) {
         fail("Failed to create region");
         e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      }

      Region[] regions = regionManager.getRegions();
      assertEquals("Region size ", 3, regions.length);
      assertEquals("Region 0 ", "/a/b/", regions[1].getFqn());
      assertEquals("Region 1 ", "/a/b/c/", regions[2].getFqn());
      Region region = regionManager.getRegion("/a/b/c/d");
      assertNotNull("Region ", region);
      assertEquals("Region ", "/a/b/c/", region.getFqn());
      region = regionManager.getRegion("/a/b");
      assertNotNull("Region ", region);
      assertEquals("Region ", "/a/b/", region.getFqn());
      region = regionManager.getRegion("/a");
      // Should be default.
      assertNull("Region ", region);
   }

   public void testNoDefaultRegion() {
      String fqn1 = "/a/b/c";
      String fqn2 = "/a/b/";

      RegionManager regionManager = new RegionManager();
      try {
         regionManager.createRegion(fqn1, null);
         regionManager.createRegion(fqn2, null);
      } catch (RegionNameConflictException e) {
         fail("Failed to create region");
         e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
      }

      Region region = null;
      try {
         region = regionManager.getRegion("/a");
      } catch (Exception e)
      {
         fail("If we don't configure the default region, it still should be ok!");
      }

      assertNull("DEfault region is ", region);
   }

   public void testRegionConflict() {
      String fqn1 = "/a/b";
      String fqn2 = "/a/b/c";
      String fqn3 = "/aop";
      String fqn4 = "/_default_";

      RegionManager regionManager = new RegionManager();
      try {
         regionManager.createRegion(DEFAULT_REGION, null);
         regionManager.createRegion(fqn1, null);
         regionManager.createRegion(fqn2, null);
         regionManager.createRegion(fqn3, null);
         regionManager.createRegion(fqn4, null);
      } catch (RegionNameConflictException e) {
         Region[] regions = regionManager.getRegions();
         assertEquals("Region size ", 2, regions.length);
         return;
      }
      fail("testRegionConflict failed");
   }

   public void testRegionConflict2() {
      String fqn1 = "/a/b/";
      String fqn2 = "/a/b/c/";
      String fqn3 = "/a/bc";

      RegionManager regionManager = new RegionManager();
      try {
         regionManager.createRegion(fqn1, null);
         regionManager.createRegion(fqn2, null);
         regionManager.createRegion(fqn3, null);
      } catch (RegionNameConflictException e) {
         Region[] regions = regionManager.getRegions();
         assertEquals("Region size ", 1, regions.length);
         return;
      }
      fail("testRegionConflict failed");
   }

   public void testGetRegion() {
      String fqn1 = "/a/bc";
      String fqn2 = "/a/b";
      String fqn3 = "/_default_";

      RegionManager regionManager = new RegionManager();
      try {
         regionManager.createRegion(fqn1, null);
         regionManager.createRegion(fqn3, null);
      } catch (RegionNameConflictException e) {
         fail("testRegionConflict3 failed " +e);
      }

      Region region = regionManager.getRegion(fqn2);
      assertNull("Region " , region);
   }

   public void testRemoveRegion() {
      String fqn1 = "/a/bc";
      String fqn2 = "/a/b";
      String fqn3 = "/_default_";

      RegionManager regionManager = new RegionManager();
      try {
         regionManager.createRegion(fqn1, null);
         regionManager.createRegion(fqn3, null);
         regionManager.removeRegion(fqn1);
         regionManager.createRegion(fqn1, null);
      } catch (RegionNameConflictException e) {
         fail("testRemoveRegion failed " +e);
      }

      Region region = regionManager.getRegion(fqn2);
      assertNull("Region " , region);
   }

   void log(String msg)
   {
      System.out.println("-- " + msg);
   }

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

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

}
