package com.oreilly.jent.people.ejb;

/**
 * In general, you may use the code in this book in your programs and 
 * documentation. You do not need to contact us for permission unless 
 * you're reproducing a significant portion of the code. For example, 
 * writing a program that uses several chunks of code from this book does 
 * not require permission. Selling or distributing a CD-ROM of examples 
 * from O'Reilly books does require permission. Answering a question by 
 * citing this book and quoting example code does not require permission. 
 * Incorporating a significant amount of example code from this book into 
 * your product's documentation does require permission.
 * 
 * We appreciate, but do not require, attribution. An attribution usually 
 * includes the title, author, publisher, and ISBN. For example: 
 * 
 *   "Java Enterprise in a Nutshell, Third Edition, 
 *    by Jim Farley and William Crawford 
 *    with Prakash Malani, John G. Norman, and Justin Gehtland. 
 *    Copyright 2006 O'Reilly Media, Inc., 0-596-10142-2."
 *  
 *  If you feel your use of code examples falls outside fair use or the 
 *  permission given above, feel free to contact us at 
 *  permissions@oreilly.com.
 */

import java.util.logging.Logger;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import junit.framework.Test;

import org.apache.cactus.FilterTestCase;

import com.oreilly.jent.people.InvalidSearchException;
import com.oreilly.jent.people.PersistenceException;
import com.oreilly.jent.people.SearchArg;

/**
 * Class: TestPeopleFinderEJB
 * 
 * Test cases for the PeopleFinder EJB component.
 * 
 * @author <a href="mailto:jim@jimfarley.org">Jim Farley</a>
 *
 */
public class TestPeopleFinderEJB extends FilterTestCase {
    private PeopleFinderLocal mFinderBean = null;
    private static Logger sLog = 
        Logger.getLogger(TestPeopleFinderEJB.class.getName());

    public TestPeopleFinderEJB() {
        super();
    }

    public TestPeopleFinderEJB(String name) {
        super(name);
    }

    public TestPeopleFinderEJB(String name, Test child) {
        super(name, child);
    }
    
    /**
     * Create an instance of the EJB component under test, to be used by 
     * any of the test methods.  Since the PeopleFinder EJB is a stateless
     * session bean, and therefore keeps no client state, this is a sensible
     * thing to have in the test fixture.
     */
    protected void setUp() {
        // Initialize an EJB reference
        String beanName = "java:comp/env/ejb/junit-PeopleFinder";
        try {
            InitialContext ctx = new InitialContext();
            PeopleFinderLocalHome home = 
                (PeopleFinderLocalHome)PortableRemoteObject.narrow(
                    ctx.lookup(beanName), PeopleFinderLocalHome.class);
            this.mFinderBean = home.create();
        }
        catch (NamingException ne) {
            fail("Unable to lookup EJB component using name '" + 
                 beanName + "': " + ne.getMessage());
        }
        catch (CreateException ce) {
            fail("Unable to create PeopleFinder bean: " + ce.getMessage());
        }
    }
    
    /** Clean up our EJB instance */
    protected void tearDown() {
        try {
            mFinderBean.remove();
            mFinderBean = null;
        }
        catch (RemoveException re) {
            fail("Removal of EJB in fixture failed: " + re.getMessage());
        }
        
    }
    
    // Test methods
    
    // Verify that the EJB correctly rejects a query with no arguments
    public void testNoSearchArguments() {
        // Try to pass in an empty parameter set
        try {
            mFinderBean.findPeople(new SearchArg[0]);
            fail("PeopleFinder allowed an empty search");
        }
        catch (InvalidSearchException ise) {
            sLog.info("PeopleFinder correctly rejected an empty search");
        }
        catch (PersistenceException pe) {
            fail("Unexpected persistence exception: " + pe.getMessage());
        }
    }
    
    // Verify that the EJB correctly rejects invalid search arguments
    public void testBadSearchArgument() {
        SearchArg arg = new SearchArg();
        // Try to pass in an invalid search argument
        arg.setName("invalidArg");
        arg.setValue("foobar");
        try {
            mFinderBean.findPeople(new SearchArg[] {arg});
            fail("PeopleFinder allowed an invalid search argument");
        }
        catch (InvalidSearchException ise) {
            sLog.info("PeopleFinder correctly rejected an invalid parameter");
        }
        catch (PersistenceException pe) {
            fail("Unexpected persistence exception: " + pe.getMessage());
        }
    }

}
