Raible's Wiki
Raible Designs AppFuseHomepage- Korean - Chinese - Italian - Japanese QuickStart Guide User Guide Tutorials Other ApplicationsStruts ResumeSecurity Example Struts Menu
Set your name in
UserPreferences
Referenced by
JSPWiki v2.2.33
Hide Menu |
This is version 10.
It is not the current version, and thus it cannot be edited. Part II: Creating new Managers - A HowTo for creating Business Delegates that talk to the database tier (DAOs) and the web tier (Struts Actions). About this TutorialThis tutorial will show you how to create a Business Delegate class (and a JUnit) to talk to the DAO we created in Part I of this tutorial.In the context of AppFuse, this is called a Manager class. It's main responsibility is converting backend data (POJOs) into front-end data (Struts ActionForms). The main reason I even use Managers, rather than just calling the DAOs directly is testability. It's nice to be able to populate a Form manually (in the test) and call the DAO to persist it, and verify the database gets the proper results. The Business Delegate pattern from Sun says that these objects are usefull for de-coupling your presentation layer from your database layer. Managers should also be where you put any business logic for your application.
Let's get started on creating a new ManagerTest and Manager in AppFuse's architecture. Table of Contents
Create a new ManagerTest to run JUnit tests on the Manager [#1]Since we're continuing from Part I, let's create a JUnit test for the PersonManager. Create PersonManagerTest in the test/web/**/service directory. We'll want to test the same basic methods (get, save, remove) that our DAO has. This may seem redundant (why all the tests!), but these tests are GREAT to have 6 months down the road. This class should extend BaseManagerTestCase, which already exists in the service package. The parent class (BaseManagerTestCase) serves the same functionality as the BaseDaoTestCase - to load a properties file that has the same name as your *Test.class.
The code below is what we need for a basic JUnit test of our Managers. It created and destroys our PersonManager. The "conn" object is initialized (and obtains a connection) in the BaseManagerTestCase class. package org.appfuse.webapp.service; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.appfuse.webapp.form.PersonForm; public class PersonManagerTest extends BaseManagerTestCase { //~ Instance fields ======================================================== private PersonManager mgr = null; private Log log = LogFactory.getLog(PersonManagerTest.class); private PersonForm personForm; //~ Constructors =========================================================== public PersonManagerTest(String name) { super(name); } //~ Methods ================================================================ protected void setUp() throws Exception { super.setUp(); mgr = new PersonManagerImpl(conn); } protected void tearDown() throws Exception { super.tearDown(); mgr = null; } public static void main(String[] args) { junit.textui.TestRunner.run(PersonManagerTest.class); } }Now that we have the JUnit framework down for this class, let's add the meat: the test methods to make sure everything works in our Manager. Here's a snippet from the DAO Tutorial tutorial to help you understand what we're about to do.
Add the following methods to your PersonManagerTest.java file: public void testGetPerson() throws Exception { personForm = (PersonForm) mgr.getPerson("1"); if (log.isDebugEnabled()) { log.debug(personForm); } assertTrue(personForm != null); assertTrue(personForm.getRoles().size() == 1); } public void testSavePerson() throws Exception { personForm = (PersonForm) mgr.getPerson("1"); personForm.setFirstName("Joe"); if (log.isDebugEnabled()) { log.debug("saving person with updated firstName: " + personForm); } personForm = (PersonForm) mgr.savePerson(personForm); assertTrue(personForm.getFirstName().equals("Joe")); } public void testAddAndRemovePerson() throws Exception { personForm = new PersonForm(); // call populate method in super class to populate test data // from a properties file matching this class name personForm = (PersonForm) populate(personForm); personForm = (PersonForm) mgr.savePerson(personForm); assertTrue(personForm.getLastName().equals("Raible")); if (log.isDebugEnabled()) { log.debug("removing person..."); } mgr.removePerson(personForm); try { personForm = (PersonForm) mgr.getPerson("1"); fail("Expected 'Exception' not thrown"); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(e); } assertTrue(e != null); } } Create a new Manager to talk to the DAO [#2]Run the ManagerTest [#3]
|