package org.mockejb.test.entity;
import java.util.Collection;
import javax.naming.*;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.mockejb.*;
import org.mockejb.interceptor.*;
import org.mockejb.jndi.*;
public class BMPEntityBeanTest extends TestCase {
private MockContainer mockContainer;
private Context context;
private AspectSystem aspectSystem;
private PersonHome personHome;
public BMPEntityBeanTest(String testName) {
super(testName);
}
public void setUp() throws Exception {
aspectSystem = AspectSystemFactory.getAspectSystem();
MockContextFactory.setAsInitial();
context = new InitialContext();
mockContainer = new MockContainer( context );
EntityBeanDescriptor personDescriptor =
new EntityBeanDescriptor( Person.JNDI_NAME,
PersonHome.class, Person.class, PersonBMPBean.class );
mockContainer.deploy( personDescriptor );
personHome = (PersonHome)context.lookup(Person.JNDI_NAME);
}
public void tearDown() {
MockContextFactory.revertSetAsInitial();
}
public void testFinderUsingAspect() throws Exception {
aspectSystem.add( new PersonFinderAspect() );
Person person = personHome.findByName("John", "Doe" );
assertNotNull(person);
assertEquals("Doe", person.getLastName());
}
public void testFinderUsingEntityDatabase() throws Exception {
MockControl personBeanControl = MockControl.createControl( Person.class);
Person createdPerson = (Person) personBeanControl.getMock();
createdPerson.getId();
personBeanControl.setReturnValue(1L);
createdPerson.getFirstName();
personBeanControl.setReturnValue("John");
createdPerson.getLastName();
personBeanControl.setReturnValue("Doe");
personBeanControl.replay();
mockContainer.getEntityDatabase().add( PersonHome.class, new Long(1), createdPerson );
PersonHome personHome = (PersonHome)context.lookup(Person.JNDI_NAME);
Person foundPerson = personHome.findByName("John", "Doe" );
assertEquals( createdPerson, foundPerson );
Collection people = personHome.findByFirstName("John");
assertTrue( people.size()>0);
foundPerson = (Person)people.iterator().next();
assertEquals( createdPerson, foundPerson );
}
public void testFinderAndEjbLoad() throws Exception {
PersonHome personHome = (PersonHome)context.lookup(Person.JNDI_NAME);
Person foundPerson = personHome.findByName("John", "Doe" );
assertEquals( 1L, foundPerson.getId() );
Collection people = personHome.findByFirstName("John");
assertTrue( people.size()>0);
foundPerson = (Person)people.iterator().next();
assertEquals( 1L, foundPerson.getId());
}
class PersonFinderAspect implements Aspect {
public Pointcut getPointcut(){
return new MethodPatternPointcut( "PersonBMPBean\\.ejbFindByName" );
}
public void intercept( InvocationContext invocationContext ) throws Exception {
Object [] paramVals = invocationContext.getParamVals();
invocationContext.setReturnObject( create( (String)paramVals[0], (String)paramVals[1] ) );
}
private Person create( String firstName, String lastName ) throws Exception {
Context context = new InitialContext();
GenericHome home = (GenericHome)context.lookup(Person.JNDI_NAME);
Person person = (Person) home.genericCreate();
person.setFirstName(firstName);
person.setLastName(lastName);
return person;
}
}
}