/*
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.test.cluster.test;

import junit.framework.Test;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jboss.test.JBossClusteredTestCase;

/**
 * Clustering test case of get/set under scoped class loader.
 *
 * @author Ben Wang
 * @version $Revision: 1.0
 */
public class ScopedTestCase
      extends BaseTest
{
   protected String setUrl;
   protected String getUrl;
   protected String modifyUrl;
   protected String modifyNoSetUrl;
   protected String removeUrl;
   protected String invalidateUrl;
   protected String bindUrl_;
   protected String warName_;
   protected String setUrlBase_;
   protected String getUrlBase_;
   protected String modifyUrlBase_;
   protected String modifyNoSetUrlBase_;
   protected String removeUrlBase_;
   protected String invalidateUrlBase_;
   protected String bindUrlBase_;

   public ScopedTestCase(String name)
   {
      super(name);
      warName_ = "/http-scoped/";
      setUrlBase_ = "setSession.jsp";
      getUrlBase_ = "getAttribute.jsp";
      modifyUrlBase_ = "modifyAttribute.jsp";
      modifyNoSetUrlBase_ = "modifyAttributeNoSet.jsp";
      removeUrlBase_ = "removeAttribute.jsp";
      invalidateUrlBase_ = "invalidateSession.jsp";
      bindUrlBase_ = "bindSession.jsp?Binding=";

      concatenate();
   }

   protected void concatenate()
   {
      setUrl = warName_ +setUrlBase_;
      getUrl = warName_ +getUrlBase_;
      modifyUrl = warName_ +modifyUrlBase_;
      modifyNoSetUrl = warName_ +modifyNoSetUrlBase_;
      removeUrl = warName_ +removeUrlBase_;
      invalidateUrl = warName_ +invalidateUrlBase_;
      bindUrl_ = warName_ + bindUrlBase_;
   }

   public static Test suite() throws Exception
   {
      Test t1 = JBossClusteredTestCase.getDeploySetup(ScopedTestCase.class,
            "http-scoped.war");
      return t1;
   }

   protected void setUp() throws Exception {
      super.setUp();
      invalidate();
   }

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

   /**
    * Main method that deals with the Http Session Replication Test
    *
    * @throws Exception
    */
   public void testNonPrimitiveGet()
         throws Exception
   {
      String attr = "";
      getLog().debug("Enter testNonPrimitiveGet");

      getLog().debug(setUrl + ":::::::" + getUrl);

      // Create an instance of HttpClient.
      HttpClient client = new HttpClient();

      // Set the session attribute first
      makeGet(client, baseURL0_ +setUrl);

      // Create a method instance.
      // Get the Attribute set
      attr = makeGet(client, baseURL0_ +getUrl);

      sleepThread(DEFAULT_SLEEP);

      // Make connection to server 1 and get
      setCookieDomainToThisServer(client, servers_[1]);
      String attr2 = makeGet(client, baseURL1_ +getUrl);

      assertEquals("Get attribute should be but is ", attr, attr2);
      getLog().debug("Exit testNonPrimitiveGet");
   }

   /**
    * Test session modify with non-primitive get/modify.
    *
    * @throws Exception
    */
   public void testNonPrimitiveModify()
         throws Exception
   {
      String attr = "";
      getLog().debug("Enter testNonPrimitiveModify");

      getLog().debug(setUrl + ":::::::" + getUrl);

      // Create an instance of HttpClient.
      HttpClient client = new HttpClient();

      // Set the session attribute first
      makeGet(client, baseURL0_ +setUrl);

      // Get the Attribute set
      String attrOld = makeGet(client, baseURL0_ +getUrl);

      // Modify a method instance.
      makeGet(client, baseURL0_ +modifyUrl);

      // Get the Attribute set
      attr = makeGet(client, baseURL0_ +getUrl);

      sleepThread(DEFAULT_SLEEP);

      // Make connection to server 1 and get
      setCookieDomainToThisServer(client, servers_[1]);
      String attr2 = makeGet(client, baseURL1_ +getUrl);

      // Check the result
      assertNotSame("Old attribute should be different from new one.",
            attrOld, attr);
      assertEquals("Attributes should be the same", attr, attr2);
      getLog().debug("Exit testNonPrimitiveModify");
   }

   /**
    * Test session modify with non-primitive remove.
    *
    * @throws Exception
    */
   public void testNonPrimitiveRemove()
         throws Exception
   {
      String attr = "";
      getLog().debug("Enter testNonPrimitiveRemove");

      getLog().debug(setUrl + ":::::::" + getUrl);

      // Create an instance of HttpClient.
      HttpClient client = new HttpClient();

      // Set the session attribute first
      makeGet(client, baseURL0_ +setUrl);

      // Modify a method instance.
      makeGet(client, baseURL0_ +modifyUrl);

      // Get the Attribute set
      attr = makeGet(client, baseURL0_ +getUrl);

      sleepThread(DEFAULT_SLEEP);

      // Make connection to server 1 and get
      setCookieDomainToThisServer(client, servers_[1]);
      // Get the Attribute set
      makeGet(client, baseURL1_ +getUrl);
      makeGet(client, baseURL1_ +removeUrl);
      // Session is invlaidated. Should have not OK response.
      makeGetFailed(client, baseURL1_ +getUrl);

      sleepThread(200);
      // Make connection to server 1 and get
      setCookieDomainToThisServer(client, servers_[0]);
      // Session is invlaidated. Should have not OK response.
      makeGetFailed(client, baseURL0_ +getUrl);

      getLog().debug("Exit testNonPrimitiveRemove");
   }

   /**
    * Test session modify with non-primitive get/modify from node2 and see if it replicates correctly
    * on node1 or not.
    *
    * @throws Exception
    */
   public void testNonPrimitiveModifyFromAlternativeNode()
         throws Exception
   {
      String attr = "";
      getLog().debug("Enter testNonPrimitiveModifyFromAlternativeNode");

      getLog().debug(setUrl + ":::::::" + getUrl);

      // Create an instance of HttpClient.
      HttpClient client = new HttpClient();

      // Set the session attribute first
      makeGet(client, baseURL0_ +setUrl);

      // Get the Attribute set
      String attrOld = makeGet(client, baseURL0_ +getUrl);
      sleepThread(DEFAULT_SLEEP);
      // Get the Attribute set
      setCookieDomainToThisServer(client, servers_[1]);
      String attrOld1 = makeGet(client, baseURL1_ +getUrl);
      // Check the result
      assertEquals("Attributes should be the same", attrOld, attrOld1);

      // Modify a method instance.
      makeGet(client, baseURL1_ +modifyUrl);
      // Make connection to server 1 and get
      String attr2 = makeGet(client, baseURL1_ +getUrl);

      sleepThread(400);
      // Get the Attribute set
      setCookieDomainToThisServer(client, servers_[0]);
      attr = makeGet(client, baseURL0_ +getUrl);

      assertEquals("Attributes should be the same", attr, attr2);
      getLog().debug("Exit testNonPrimitiveModifyModifyFromAlternativeNode");
   }

   /**
    * Test invalidate session
    *
    * @throws Exception
    */
   public void testInvalidate()
         throws Exception
   {
      String attr = "";
      getLog().debug("Enter testInvalidate");

      getLog().debug(setUrl + ":::::::" + getUrl);

      invalidate();

      getLog().debug("Exit testInvalidate");
   }

    protected void invalidate() throws Exception
    {
      // Create an instance of HttpClient.
      HttpClient client = new HttpClient();

      // Set the session attribute first
      makeGet(client, baseURL0_ +setUrl);

      // Get the Attribute set
      String attrOld = makeGet(client, baseURL0_ +getUrl);

      // Invalidate the Attribute set
      makeGet(client, baseURL0_ +invalidateUrl);

      sleepThread(DEFAULT_SLEEP + 200); // wait a bit longer to propagate

      // Make connection to server 1 and get
      setCookieDomainToThisServer(client, servers_[1]);
      // Session is invlaidated. Should have not OK response.
      makeGetFailed(client, baseURL1_ +getUrl);
   }

    public void testSessionBindingEvent()
       throws Exception
    {
       String attr = "";
       getLog().debug("Enter testSessionBindingEvent");

       // Create an instance of HttpClient.
       HttpClient client = new HttpClient();
       
       // Bind a new HttpSessionListener to the session
       // and check that there is a valueBound() event
       attr = makeGet(client, baseURL0_ + bindUrl_ + "new");
       assertTrue("Got OK when binding a new listener", 
                  (attr != null && attr.indexOf("OK") >= 0 ) );
       
       // Rebind the same HttpSessionListener to the session
       // and check that there is no valueUnbound()
       attr = makeGet(client, baseURL0_ + bindUrl_ + "rebind");
       assertTrue("Got OK when rebinding an existing listener", 
                  (attr != null && attr.indexOf("OK") >= 0 ) );
       
       // Replace the HttpSessionListener with another one
       // and check that there is a valueUnbound()
       attr = makeGet(client, baseURL0_ + bindUrl_ + "replace");
       assertTrue("Got OK when replacing a listener", 
                  (attr != null && attr.indexOf("OK") >= 0 ) );
       
       // Remove the same HttpSessionListener
       // and check that there is a valueUnbound()
       attr = makeGet(client, baseURL0_ + bindUrl_ + "remove");
       assertTrue("Got OK when removing a listener", 
                  (attr != null && attr.indexOf("OK") >= 0 ) );
    }
}
