/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.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 invalidateUrl;
   protected String bindUrl;

   public ScopedTestCase(String name)
   {
      super(name);
      setUrl = "/http-scoped/setSession.jsp";
      getUrl = "/http-scoped/getAttribute.jsp";
      modifyUrl = "/http-scoped/modifyAttribute.jsp";
      invalidateUrl = "/http-scoped/invalidateSession.jsp";
      bindUrl = "/http-scoped/bindSession.jsp?Binding=";
   }

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

   /**
    * 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(200);

      // 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(200);

      // 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 invalidate session
    *
    * @throws Exception
    */
   public void testInvalidate()
         throws Exception
   {
      String attr = "";
      getLog().debug("Enter testInvalidate");

      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);

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

      sleepThread(200);

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

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

   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 ) );
   }

}
