package org.laughingpanda.ejb_migration_example; import jdave.Specification; import jdave.Block; import jdave.junit4.JDaveRunner; import org.jmock.Expectations; import org.junit.runner.RunWith; import org.laughingpanda.ejb_migration_example.ejb.PersonEntity; import javax.ejb.RemoveException; import java.rmi.RemoteException; import java.util.Arrays; import java.util.List; import java.io.PrintStream; import java.io.ByteArrayOutputStream; @RunWith(JDaveRunner.class) public class PersonFinderAppSpec extends Specification { private Person frank = new PersonStub(); private PersonStub bill = new PersonStub(); private PersonStub joe = new PersonStub(); public class AfterCreation { private ServiceLocator locator; private PersonFinder finder; private ByteArrayOutputStream outputStream; public PersonFinderApp create() { PersonFinderApp app = new PersonFinderApp(); locator = mock(ServiceLocator.class); finder = mock(PersonFinder.class); app.setServiceLocator(locator); outputStream = new ByteArrayOutputStream(2000); PersonFinderApp.setOut(new PrintStream(outputStream)); PersonFinderApp.setInstance(app); return app; } public void invokesFinderFromServiceLocator() throws RemoteException { final SearchParameters searchParameters = new SearchParameters(""); checking(new Expectations() {{ one(locator).getPersonFinder(); will(returnValue(finder)); one(finder).findPersons(searchParameters); will(returnValue(Arrays.asList(frank, bill, joe))); }}); List results = context.findPersons(searchParameters); specify(results, containsExactly(frank, bill, joe)); } public void usesFirstCommandLineParameterAsSearchTerm() throws RemoteException { final SearchParameters searchParameters = new SearchParameters("foo"); checking(new Expectations() {{ one(locator).getPersonFinder(); will(returnValue(finder)); one(finder).findPersons(searchParameters); will(returnValue(Arrays.asList(frank, bill, joe))); }}); PersonFinderApp.main("foo"); String output = getOutput(); specify(output.contains(frank.toString())); specify(output.contains(bill.toString())); specify(output.contains(joe.toString())); } public void complainsIfNoSearchTermIsSupplied() { specify(new Block(){ public void run() throws Throwable { PersonFinderApp.main(); } }, should.raise(IllegalArgumentException.class, "Please supply a search term as a command-line parameter")); } private String getOutput() { return outputStream.toString(); } } public class InContainer { private PersonEntity person; public PersonFinderApp create() throws RemoteException { PersonDao dao = new ServiceLocatorImpl().getPersonDao(); person = dao.create("appPerson-" + System.currentTimeMillis(), "Bill", "Apperson"); return new PersonFinderApp(); } public void destroy() throws RemoveException, RemoteException { person.remove(); } public void findsPersonsFromDatabase() { List results = context.findPersons(new SearchParameters("Bill")); specify(results, contains(person)); } } private class PersonStub implements Person { private Long id; private String userName; private String firstName; private String lastName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } }