/*
 *
 * JBoss, the OpenSource J2EE webOS
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package org.jboss.ejb3.test.longlived.unit;

//import java.rmi.*;


import org.jboss.ejb3.test.longlived.Customer;
import org.jboss.ejb3.test.longlived.ShoppingCart;
import org.jboss.ejb3.test.longlived.StatelessRemote;
import org.jboss.test.JBossTestCase;
import junit.framework.Test;

/**
 * Sample client for the jboss container.
 *
 * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
 * @version $Id: EntityUnitTestCase.java,v 1.2.2.4 2005/09/14 22:19:11 bill Exp $
 */

public class EntityUnitTestCase
extends JBossTestCase
{
   org.apache.log4j.Category log = getLog();

   static boolean deployed = false;
   static int test = 0;

   public EntityUnitTestCase(String name)
   {

      super(name);

   }

   public void testLongLivedSession() throws Exception
   {
      ShoppingCart test = (ShoppingCart) this.getInitialContext().lookup(ShoppingCart.class.getName());
      StatelessRemote remote = (StatelessRemote) this.getInitialContext().lookup(StatelessRemote.class.getName());
      Customer c;

      long id = test.createCustomer();
      c = remote.find(id);
      assertEquals("William", c.getName());
      c = test.find(id);
      assertEquals("William", c.getName());
      test.update();
      c = remote.find(id);
      assertEquals("Bill", c.getName());
      c = test.find(id);
      assertEquals("Bill", c.getName());
      test.update2();
      c = remote.find(id);
      assertEquals("Billy", c.getName());
      c = test.find(id);
      assertEquals("Billy", c.getName());
      test.update3();
      c = remote.find(id);
      assertEquals("Bill Jr.", c.getName());
      c = test.find(id);
      assertEquals("Bill Jr.", c.getName());
      test.checkout();
   }

   public void testWithFlushMode() throws Exception
   {
      ShoppingCart test = (ShoppingCart) this.getInitialContext().lookup(ShoppingCart.class.getName());
      StatelessRemote remote = (StatelessRemote) this.getInitialContext().lookup(StatelessRemote.class.getName());
      Customer c;
      long id;


      id = test.createCustomer();
      c = remote.find(id);
      assertEquals("William", c.getName());
      c = test.find(id);
      assertEquals("William", c.getName());
      test.never();
      c = remote.find(id);
      assertEquals("William", c.getName());

      test.checkout();
      c = remote.find(id);
      assertEquals("Bob", c.getName());

      // make sure FlushMode

      test = (ShoppingCart) this.getInitialContext().lookup(ShoppingCart.class.getName());


      id = test.createCustomer();
      c = remote.find(id);
      assertEquals("William", c.getName());
      c = test.find(id);
      assertEquals("William", c.getName());
      test.update();
      c = remote.find(id);
      assertEquals("Bill", c.getName());
      c = test.find(id);
      assertEquals("Bill", c.getName());
      test.update2();
      c = remote.find(id);
      assertEquals("Billy", c.getName());
      c = test.find(id);
      assertEquals("Billy", c.getName());
      test.update3();
      c = remote.find(id);
      assertEquals("Bill Jr.", c.getName());
      c = test.find(id);
      assertEquals("Bill Jr.", c.getName());
      test.checkout();
   }

   public void testHibernateLongLivedSession() throws Exception
   {
      ShoppingCart test = (ShoppingCart) this.getInitialContext().lookup("HibernateShoppingCart");
      StatelessRemote remote = (StatelessRemote) this.getInitialContext().lookup(StatelessRemote.class.getName());
      Customer c;

      long id = test.createCustomer();
      c = remote.find(id);
      assertEquals("William", c.getName());
      c = test.find(id);
      assertEquals("William", c.getName());
      test.update();
      c = remote.find(id);
      assertEquals("Bill", c.getName());
      c = test.find(id);
      assertEquals("Bill", c.getName());
      test.update2();
      c = remote.find(id);
      assertEquals("Billy", c.getName());
      c = test.find(id);
      assertEquals("Billy", c.getName());
      test.update3();
      c = remote.find(id);
      assertEquals("Bill Jr.", c.getName());
      c = test.find(id);
      assertEquals("Bill Jr.", c.getName());
      test.checkout();
   }

   public void testHibernateWithFlushMode() throws Exception
   {
      ShoppingCart test = (ShoppingCart) this.getInitialContext().lookup("HibernateShoppingCart");
      StatelessRemote remote = (StatelessRemote) this.getInitialContext().lookup(StatelessRemote.class.getName());
      Customer c;
      long id;


      id = test.createCustomer();
      c = remote.find(id);
      assertEquals("William", c.getName());
      c = test.find(id);
      assertEquals("William", c.getName());
      test.never();
      c = remote.find(id);
      assertEquals("William", c.getName());

      test.checkout();
      c = remote.find(id);
      assertEquals("Bob", c.getName());

      // make sure FlushMode

      test = (ShoppingCart) this.getInitialContext().lookup("HibernateShoppingCart");


      id = test.createCustomer();
      c = remote.find(id);
      assertEquals("William", c.getName());
      c = test.find(id);
      assertEquals("William", c.getName());
      test.update();
      c = remote.find(id);
      assertEquals("Bill", c.getName());
      c = test.find(id);
      assertEquals("Bill", c.getName());
      test.update2();
      c = remote.find(id);
      assertEquals("Billy", c.getName());
      c = test.find(id);
      assertEquals("Billy", c.getName());
      test.update3();
      c = remote.find(id);
      assertEquals("Bill Jr.", c.getName());
      c = test.find(id);
      assertEquals("Bill Jr.", c.getName());
      test.checkout();
   }

   public static Test suite() throws Exception
   {
      return getDeploySetup(EntityUnitTestCase.class, "longlived-test.ejb3");
   }

}
