/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package org.jboss.ejb3.test.bank.unit;

import javax.ejb.EJBAccessException;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import org.jboss.ejb3.test.bank.Bank;
import org.jboss.ejb3.test.bank.ProxyFactoryInterface;
import org.jboss.ejb3.test.bank.Teller;
import org.jboss.ejb3.test.bank.TellerInterceptor;
import org.jboss.ejb3.test.bank.TestStatus;
import org.jboss.logging.Logger;
import org.jboss.security.SecurityAssociation;
import org.jboss.security.SimplePrincipal;
import org.jboss.test.JBossTestCase;
import junit.framework.Test;

/**
 * Test for EJB3 deployment of EJB2.0 Bank EJBs
 * 
 * @version <tt>$Revision: 1.13.2.4 $</tt>
 * @author <a href="mailto:bdecoste@jboss.com">William DeCoste</a>
 */
public class BankDeploymentDescriptorTestCase
    extends JBossTestCase {
 //   extends TestCase {

   private static final Logger log = Logger
         .getLogger(BankDeploymentDescriptorTestCase.class);

   public BankDeploymentDescriptorTestCase(String name)
   {
      super(name);
   }
   
   public void testEnvEntry() throws Exception
   {
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      String id = bank.getEnvEntryId();
      assertEquals(id, "5678");
   }

   public void testStatelessTeller() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail"));
      SecurityAssociation.setCredential("password".toCharArray());
 
      String greeting;
      Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
      assertNotNull(teller);
      
      greeting = teller.greetWithRequiredTransaction("Hello");
      assertNotNull(greeting);
      assertEquals("Hello", greeting);
      greeting = teller.greetWithNotSupportedTransaction("Hello");
      assertNotNull(greeting);
      assertEquals("Hello", greeting);
      greeting = teller.greetUnchecked("Hello");
      assertNotNull(greeting);
      assertEquals("Hello", greeting);
      
      try {
         greeting = teller.greetChecked("Hello");
         assertTrue(false);
      } catch (Exception e){
         assertTrue(e instanceof EJBAccessException);
      }
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      try{
         greeting = teller.greetChecked("Hello");
         assertNotNull(greeting);
         assertEquals("Hello", greeting);
      } catch (Exception e){
         e.printStackTrace();
      }
   }

   public void testInjectionAnnotations() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
      assertNotNull(teller);
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      String greeting = teller.greetChecked("Hello");
      assertNotNull(greeting);
      assertEquals("Hello", greeting);
      assertTrue(teller.isConstructed());

   }

   public void testFieldInject() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
 
      Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
      assertNotNull(teller);
      
      String greeting = teller.greetWithServiceTimer("Hello");
      assertEquals("Hello", greeting);
   }
   
   public void testRunAs() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail"));
      SecurityAssociation.setCredential("password".toCharArray());
 
      String customerId = "CustomerId";
      String greeting;
      Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
      assertNotNull(teller);
      
      String tmpId = teller.retrieveCustomerId();
      assertEquals("defaultId", tmpId);
   }

   public void testStatefulBank() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      String customerId = "CustomerId";
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
      SecurityAssociation.setCredential("password".toCharArray());
      try {
         bank.storeCustomerId(customerId);
         assertTrue(false);
      } catch (Exception e){
         assertTrue(e instanceof EJBAccessException);
      }
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      bank.storeCustomerId(customerId);
      String tmpId = bank.retrieveCustomerId();
      assertEquals(customerId, tmpId);
   }
   
   public void testStatefulState() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      String customerId = "CustomerId";
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      bank.storeCustomerId(customerId);
      String tmpId = bank.retrieveCustomerId();
      assertEquals(customerId, tmpId);
      
      bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      tmpId = bank.retrieveCustomerId();
      assertEquals("defaultId", tmpId);
   }
 
   public void testStatefulBank21() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME + "21");
      assertNotNull(bank);
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      String activated = bank.isActivated();
      assertEquals(activated, "_CREATED");
   }
 
   public void testCallbackListenersAndInteceptors() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      TestStatus status = (TestStatus) getInitialContext().lookup(TestStatus.class.getName());
      status.clear();
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      String id = bank.interceptCustomerId("CustomerId");
      assertEquals("CustomerId_SecondInterceptor_FirstInterceptor", id);
      assertTrue(status.postConstruct());
   }

   public void testResource() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      bank.testResource();
   }
   
   public void testRemove() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      bank.remove();
      
      try {
         bank.testResource();
         assertTrue(false);
      } catch (EJBException e)
      {
         assertTrue(e instanceof EJBException);
      }
   }
   
   public void testInit() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
      assertNotNull(bank);
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      String initialized = bank.isInitialized();
      assertEquals("YESYES", initialized);
   }
   
   public void testTeller() throws Exception
   {
      InitialContext jndiContext = new InitialContext();
      Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
      assertNotNull(teller);
      
      SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
      SecurityAssociation.setCredential("password".toCharArray());
      
      try {
         teller.excludedMethod();
         assertTrue(false);
      } catch (Exception e){
         assertTrue(e instanceof EJBAccessException);
      }
   }
   
   public void testRemoteBindingProxyFactory() throws Exception
   {
      ProxyFactoryInterface teller = (ProxyFactoryInterface)getInitialContext().lookup(Teller.JNDI_NAME);
      assertNotNull(teller);
   }
   
   public void testRemoteBindingInterceptorStack() throws Exception
   {
      Teller teller = (Teller)getInitialContext().lookup(Teller.JNDI_NAME);
      assertNotNull(teller);
      assertTrue(TellerInterceptor.accessed);
   }

   protected void setUp() throws Exception
   {
   }

   public static Test suite() throws Exception
   {
   //   return new TestSuite(BankDeploymentDescriptorTestCase.class);
      return getDeploySetup(BankDeploymentDescriptorTestCase.class, "bank.ejb3");
   }

}
 