package org.dotw.usermanager.useraddress;

import javax.ejb.*;
import java.util.Collection;
import java.util.Iterator;
import javax.rmi.PortableRemoteObject;
import javax.naming.NamingException;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.dotw.usermanager.UserAddressData;
import org.dotw.usermanager.useraccount.UserAddressHome;
import org.dotw.usermanager.useraccount.UserAddress;
import org.dotw.usermanager.useraccount.UserAddressPK;

/**
 * Unit Test for the UserAddress EJB
 *
 * @author jwh
 * @version $Revision$
 */
public class UserAddressTest extends junit.framework.TestCase
{
  /**
   * Default Constructor
   * @param name the name of the test case
   */
  public UserAddressTest( String name )
  {
    super( name );
  }

  /**
   * Tests the lookup
   */
  public void testLookup() throws Exception
  { 
    UserAddressHome home = (UserAddressHome)lookupHome();
    assertNotNull(home);
  }

  /**
   * Tests the creation of a user address
   */
  public void testCreateAddress() throws Exception
  { 
    UserAddressData data = createJamesAddressData();
    UserAddressHome home = (UserAddressHome)lookupHome();
    home.create(data);
  }

  /**
   * Tests the location of a user address by PK
   */
  public void testFindByPKAddress() throws Exception
  { 
    UserAddressData data = createJamesAddressData();
    UserAddressHome home = (UserAddressHome)lookupHome();
    UserAddress address = home.findByPrimaryKey( new UserAddressPK( new Integer(1) ) );
    assertNotNull(address);
    assertEquals(data.getStreet1(), address.getStreet1());
  }

  /**
   * Tests the deletion of a user address
   */
  public void testDeleteAddress() throws Exception
  { 
    UserAddressData data = createJamesAddressData();
    UserAddressHome home = (UserAddressHome)lookupHome();
    UserAddress address = home.findByPrimaryKey( new UserAddressPK( new Integer(1) ) );
    address.remove();
  }

  /**
   * Tests the find all query
   */
  public void testFindAll() throws Exception
  { 
    UserAddressData jamesData = createJamesAddressData();
    UserAddressData aronData = createAronAddressData();
    UserAddressData mikeData = createMikeAddressData();
    UserAddressHome home = (UserAddressHome)lookupHome();
    home.create(jamesData);
    home.create(aronData);
    home.create(mikeData);

    Collection collection = home.findAll();
    assertNotNull(collection);
    assertEquals(3,collection.size());
    Iterator iterator = collection.iterator();
    while(iterator.hasNext())
    {
      UserAddress address = (UserAddress)iterator.next();
      System.out.println("Found and removing address: "+address.getStreet1());
      address.remove();
    }
  }

  //
  // -- Unit test helpers
  //
  private UserAddressData createJamesAddressData()
  {
    return new UserAddressData(new Integer(1),"123 James St","","Austin","TX","78728");
  }

  private UserAddressData createAronAddressData()
  {
    return new UserAddressData(new Integer(2),"456 Aron St","","Austin","TX","78759");
  }

  private UserAddressData createMikeAddressData()
  {
    return new UserAddressData(new Integer(3),"789 Mike St","","Austin","TX","78660");
  }

  //
  // - Junit helpers
  //

  /**
   * Performs necessary setup work for the test case
   */
  protected void setUp() throws Exception
  {
    super.setUp();
  }

  /**
   * Performs necessary tear down work for the test case
   */
  protected void tearDown() throws Exception
  {
    super.tearDown();
  }

  private EJBHome lookupHome()
      throws NamingException 
  {
    String jndiName = UserAddressHome.JNDI_NAME;
    Class homeClass = UserAddressHome.class;
    Context ctx = new InitialContext();
    Object home = ctx.lookup(jndiName);
    return (EJBHome)  PortableRemoteObject.narrow(home, homeClass);
  }
}



