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

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

//import java.rmi.*;


import org.jboss.ejb3.test.factory.Entity1;
import org.jboss.ejb3.test.factory.Entity2;
import org.jboss.ejb3.test.factory.Session1;
import org.jboss.ejb3.test.factory.Session2;
import org.jboss.ejb3.test.factory.Stateful1;
import org.jboss.ejb3.test.factory.Util;
import org.jboss.ejb3.test.factory.MyService;
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: FactoryUnitTestCase.java,v 1.2.2.4 2005/09/14 22:19:07 bill Exp $
 */

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

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

   public FactoryUnitTestCase(String name)
   {

      super(name);

   }

   public void testMe() throws Exception
   {
      Session1 session1 = (Session1) this.getInitialContext().lookup(Session1.class.getName());
      Session2 session2 = (Session2) this.getInitialContext().lookup(Session2.class.getName());
      MyService service = (MyService) this.getInitialContext().lookup(MyService.class.getName());

      int oneF = session1.create1FromFactory();
      int oneM = session1.create1FromManager();
      int twoF = session1.create2FromFactory();
      int twoM = session1.create2FromManager();
      session1.doUtil(new Util());

      session2.find1FromFactory(oneF);
      assertNotNull(session2.find1FromManager(oneM));
      session2.find2FromFactory(twoF);
      assertNotNull(session2.find2FromManager(twoM));
      assertNotNull(service.find2FromManager(twoM));
      assertNotNull(session2.findUtil1FromManager(1));
      assertNotNull(session2.findUtil2FromManager(2));

   }

   public void testExtended() throws Exception
   {
      Stateful1 stateful1 = (Stateful1) this.getInitialContext().lookup(Stateful1.class.getName());
      Session2 session2 = (Session2) this.getInitialContext().lookup(Session2.class.getName());

      int oneId = stateful1.create1();
      int twoId = stateful1.create2();

      stateful1.update1();
      stateful1.update2();

      {
         Entity1 one = session2.find1FromManager(oneId);
         assertEquals(one.getString(), "changed");

         Entity2 two = session2.find2FromManager(twoId);
         assertEquals(two.getString(), "changed");
      }

      stateful1.never();

      {
         Entity1 one = session2.find1FromManager(oneId);
         assertEquals(one.getString(), "changed");

         Entity2 two = session2.find2FromManager(twoId);
         assertEquals(two.getString(), "changed");
      }

      stateful1.checkout();

      {
         Entity1 one = session2.find1FromManager(oneId);
         assertEquals(one.getString(), "never");

         Entity2 two = session2.find2FromManager(twoId);
         assertEquals(two.getString(), "never");
      }
   }

   public static Test suite() throws Exception
   {
      return getDeploySetup(FactoryUnitTestCase.class, "factory-test.ear");
   }

}
