At line 1 changed 1 line. |
__Part II:__ [Creating new Managers|CreateManager] - A HowTo for creating [Business Delegates|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] that talk to the database tier (DAOs) and the web tier (Struts Actions). |
__Part II:__ [Creating new Managers|CreateManager] - A HowTo for creating Business Facades that talk to the database tier (DAOs) and handle transaction management. |
At line 6 changed 1 line. |
This tutorial will show you how to create a Business Delegate class (and a JUnit Test) to talk to the [DAO we created in Part I|CreateDAO]. |
This tutorial will show you how to create a Business Facade class (and a JUnit Test) to talk to the [DAO we created in Part I|CreateDAO]. |
At line 8 changed 2 lines. |
In the context of [AppFuse], this is called a Manager class. It's main responsibility to act as a bridge between the persistence (DAO) layer and the |
web layer. The [Business Delegate|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] pattern from Sun says that these objects are useful for de-coupling your presentation layer from your database layer (i.e. for Swing apps). Managers should also be where you put any business logic for your application. |
In the context of [AppFuse], this is called a Manager class. Its main responsibility is to act as a bridge between the persistence (DAO) layer and the web layer. It's also useful for de-coupling your presentation layer from your database layer (i.e. for Swing apps). Managers should also be where you put any business logic for your application. |
At line 26 changed 1 line. |
This class should extend [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html], 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, as well as to initialize Spring's ApplicationContext. |
This class should extend [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html], which already exists in the ''service'' package. The parent class (BaseManagerTestCase) serves similar functionality as the BaseDaoTestCase. |
At line 30 changed 1 line. |
The code below is what we need for a basic JUnit test of our Managers. The code below simply creates and destroys the PersonManager. The "ctx" object is initialized in the [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html] class. |
The code below is what you need for a basic JUnit test of your Manager. Unlike the DaoTest, this test uses [jMock|http://jmock.org] to isolate the Manager from its dependencies and make it a ''true'' "unit" test. This can be very helpful because it allows you to test your business logic w/o worrying about other dependencies. The code below simply sets up the Manger and its dependencies (as Mocks) for testing. |
At line 36 changed 2 lines. |
import org.apache.commons.logging.Log; |
import org.apache.commons.logging.LogFactory; |
import java.util.List; |
import java.util.ArrayList; |
At line 38 added 1 line. |
import org.appfuse.dao.PersonDao; |
At line 40 added 1 line. |
import org.appfuse.service.impl.PersonManagerImpl; |
At line 42 added 2 lines. |
import org.jmock.Mock; |
import org.springframework.orm.ObjectRetrievalFailureException; |
At line 46 added 4 lines. |
private final String personId = "1"; |
private PersonManager personManager = new PersonManagerImpl(); |
private Mock personDao = null; |
private Person person = null; |
At line 44 changed 6 lines. |
private Person person; |
private PersonManager mgr = null; |
private Log log = LogFactory.getLog(PersonManagerTest.class); |
|
protected void setUp() { |
mgr = (PersonManager) ctx.getBean("personManager"); |
protected void setUp() throws Exception { |
super.setUp(); |
personDao = new Mock(PersonDao.class); |
personManager.setPersonDao((PersonDao) personDao.proxy()); |
At line 52 changed 2 lines. |
protected void tearDown() { |
mgr = null; |
protected void tearDown() throws Exception { |
super.tearDown(); |
personManager = null; |
At line 55 removed 4 lines. |
|
public static void main(String[] args) { |
junit.textui.TestRunner.run(PersonManagerTest.class); |
} |
At line 62 changed 1 line. |
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|CreateDAO] tutorial to help you understand what we're about to do. |
Now that you have the skeleton done for this class, you need to add the meat: the test methods to make sure everything works. Here's a snippet from the [DAO Tutorial|CreateDAO] tutorial to help you understand what we're about to do. |
At line 71 changed 4 lines. |
person = (Person) mgr.getPerson("1"); |
|
assertTrue("person.firstName not null", |
person.getFirstName() != null); |
// set expected behavior on dao |
personDao.expects(once()).method("getPerson") |
.will(returnValue(new Person())); |
person = personManager.getPerson(personId); |
assertTrue(person != null); |
personDao.verify(); |
At line 78 changed 3 lines. |
person = (Person) mgr.getPerson("1"); |
String name = person.getFirstName(); |
person.setFirstName("test"); |
// set expected behavior on dao |
personDao.expects(once()).method("savePerson") |
.with(same(person)).isVoid(); |
At line 82 changed 2 lines. |
person = (Person) mgr.savePerson(person); |
assertTrue("name updated", person.getFirstName().equals("test")); |
personManager.savePerson(person); |
personDao.verify(); |
} |
At line 85 removed 4 lines. |
person.setFirstName(name); |
mgr.savePerson(person); |
} |
|
At line 91 removed 1 line. |
person = (Person) populate(person); |
At line 93 changed 3 lines. |
person = (Person) mgr.savePerson(person); |
assertTrue(person.getFirstName().equals("Bill")); |
assertTrue(person.getId() != null); |
// set required fields |
person.setFirstName("firstName"); |
person.setLastName("lastName"); |
At line 97 changed 4 lines. |
if (log.isDebugEnabled()) { |
log.debug("removing person, personId: " + |
person.getId()); |
} |
// set expected behavior on dao |
personDao.expects(once()).method("savePerson") |
.with(same(person)).isVoid(); |
personManager.savePerson(person); |
personDao.verify(); |
At line 102 changed 1 line. |
mgr.removePerson(person.getId().toString()); |
// reset expectations |
personDao.reset(); |
At line 104 changed 1 line. |
assertNull(mgr.getPerson(person.getId().toString())); |
personDao.expects(once()).method("removePerson").with(eq(new Long(personId))); |
personManager.removePerson(personId); |
personDao.verify(); |
|
// reset expectations |
personDao.reset(); |
// remove |
Exception ex = new ObjectRetrievalFailureException(Person.class, person.getId()); |
personDao.expects(once()).method("removePerson").isVoid(); |
personDao.expects(once()).method("getPerson").will(throwException(ex)); |
personManager.removePerson(personId); |
try { |
personManager.getPerson(personId); |
fail("Person with identifier '" + personId + "' found in database"); |
} catch (ObjectRetrievalFailureException e) { |
assertNotNull(e.getMessage()); |
} |
personDao.verify(); |
At line 114 changed 1 line. |
First off, create a PersonManager.java interface in the src/service/**/service directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.'' |
First off, create a PersonManager.java interface in the src/service/**/service directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.'' The ''setPersonDao()'' method is not used in most cases - its just exists so the PersonManagerTest can set the DAO on the interface. |
At line 122 changed 1 line. |
public interface PersonManager { |
import org.appfuse.model.Person; |
import org.appfuse.dao.PersonDao; |
At line 124 changed 2 lines. |
public List getPeople(Person person); |
|
public interface PersonManager { |
public void setPersonDao(PersonDao dao); |
At line 127 changed 3 lines. |
|
public Person savePerson(Object person); |
|
public void savePerson(Person person); |
At line 134 changed 1 line. |
Now let's create a PersonManagerImpl class that implements the methods in PersonManager. To do this, create a new class in src/service/**/service and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager. |
Now let's create a PersonManagerImpl class that implements the methods in PersonManager. To do this, create a new class in src/service/**/service/impl and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager. |
At line 138 changed 1 line. |
package org.appfuse.service; |
package org.appfuse.service.impl; |
At line 140 removed 3 lines. |
import org.apache.commons.logging.Log; |
import org.apache.commons.logging.LogFactory; |
|
At line 144 changed 1 line. |
import org.appfuse.persistence.PersonDao; |
import org.appfuse.dao.PersonDao; |
import org.appfuse.service.PersonManager; |
At line 146 removed 6 lines. |
import java.util.List; |
|
/** |
* @author mraible |
* @version $Revision: $ $Date: May 25, 2004 11:46:54 PM $ |
*/ |
At line 153 removed 1 line. |
private static Log log = LogFactory.getLog(PersonManagerImpl.class); |
At line 160 removed 4 lines. |
public List getPeople(Person person) { |
return dao.getPeople(person); |
} |
|
At line 168 changed 1 line. |
public Person savePerson(Object person) { |
public void savePerson(Person person) { |
At line 170 removed 1 line. |
return (Person) person; |
At line 179 changed 2 lines. |
One thing to note is the {{setPersonDao}} method. This is used by Spring to bind the PersonDao to this Manager. This is configured in the applicationContext-service.xml file. We'll get to configuring that in Step 3[3]. |
You should be able to compile everything now using "ant compile-service"... |
One thing to note is the {{setPersonDao()}} method. This is used by Spring to bind the PersonDao to this Manager. This is configured in the applicationContext-service.xml file. We'll get to configuring that in Step 3[3]. You should be able to compile everything now using "ant compile-service". |
At line 182 changed 1 line. |
Finally, we need to create the PersonManagerTest.properties file in test/service/**/service so that {{personForm = (PersonForm) populate(personForm);}} will work in our test. |
Now you need to edit Spring's config file for our services layer so it will know about this new Manager. |
At line 184 removed 7 lines. |
;:''Duplicating (and renaming) the PersonDaoTest.properties is the easy route. Alternatively, you could set the properties manually.'' |
|
{{{firstName=Bill |
lastName=Joy}}} |
|
Now we need to edit Spring's config file for our services layer so it will know about this new Manager. |
|
At line 193 changed 1 line. |
To notify Spring of this our PersonManager interface and it's implementation, open the src/service/**/service/applicationContext-service.xml file. In here, you will see an existing configuration for the UserManager. You should be able to copy that and change a few things to get the XML fragment below. Add the following to the bottom of this file. |
To notify Spring of this our PersonManager interface and its implementation, open the src/service/**/service/applicationContext-service.xml file. Add the following to the bottom of this file. |
At line 197 changed 3 lines. |
<!-- Person Manager --> |
<bean id="personManagerTarget" class="org.appfuse.service.PersonManagerImpl" singleton="false"> |
<property name="personDao"><ref bean="personDAO"/></property> |
<bean id="personManager" class="org.appfuse.service.impl.PersonManagerImpl"> |
<property name="personDao" ref="personDao"/> |
At line 201 removed 8 lines. |
|
<!-- Transaction declarations for business services. To apply a generic transaction proxy to |
all managers, you might look into using the BeanNameAutoProxyCreator --> |
<bean id="personManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> |
<property name="transactionManager"><ref bean="transactionManager"/></property> |
<property name="target"><ref local="personManagerTarget"/></property> |
<property name="transactionAttributeSource"><ref local="defaultTxAttributes"/></property> |
</bean> |
At line 211 changed 1 line. |
''Note: [I|DaveKeller] had SAX throw an error because defaultTxAttributes was not defined so I replaced the transactionAttributeSource with this instead, which allowed my unit test to run successfully.'' |
This bean must have a name that ends in "Manager". This is because there is AOP advice applied at the top of this file for all *Manager beans. |
{{{<aop:advisor id="managerTx" advice-ref="txAdvice" pointcut="execution(* *..service.*Manager.*(..))" order="2"/>}}} For more information on transactions with Spring, see [Spring's documentation|http://www.springframework.org/docs/reference/transaction.html]. |
At line 213 removed 11 lines. |
[{Java2HtmlPlugin |
|
<property name="transactionAttributes"> |
<props> |
<prop key="*">PROPAGATION_REQUIRED</prop> |
</props> |
</property> |
|
}] |
|
|
At line 226 changed 1 line. |
Save all your edited files and try running "ant test-service -Dtestcase=PersonManager" one more time. |
Save all your edited files and try running __ant test-service -Dtestcase=PersonManager__. |
At line 228 changed 1 line. |
__Yeah Baby, Yeah:__ |
''Yeah Baby, Yeah:'' |
At line 211 added 2 lines. |
The files that were modified and added to this point are [available for download|https://appfuse.dev.java.net/files/documents/1397/7484/appfuse-tutorial-managers-1.6.zip]. |
|