At line 27 changed 1 line. |
;:%%(color: blue)''I usually copy (open → save as) an existing test (i.e. UserManagerTest.java) and find/replace [[Uu]ser with [[Pp]erson, or whatever the name of my object is.''%% |
;:%%(color: blue)''I usually copy (open → save as) an existing test (i.e. UserManagerTest.java) and find/replace [[Uu]ser with [[Pp]erson, or whatever the name of my object is.''%% |
At line 29 changed 231 lines. |
The code below is what we need for a basic JUnit test of our Managers. It created and destroys our PersonManager. The "ctx" object is initialized in the [BaseManagerTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManagerTestCase.java.html] class. |
|
[{Java2HtmlPlugin |
|
package org.appfuse.service; |
|
import org.apache.commons.logging.Log; |
import org.apache.commons.logging.LogFactory; |
|
import org.appfuse.webapp.form.PersonForm; |
|
|
public class PersonManagerTest extends BaseManagerTestCase { |
//~ Static fields/initializers ============================================= |
|
private PersonForm personForm; |
|
//~ Instance fields ======================================================== |
|
private PersonManager mgr = null; |
private Log log = LogFactory.getLog(PersonManagerTest.class); |
|
|
//~ Methods ================================================================ |
|
protected void setUp() throws Exception { |
mgr = (PersonManager) ctx.getBean("personManager"); |
} |
|
protected void tearDown() throws Exception { |
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|CreateDAO] tutorial to help you understand what we're about to do. |
|
;:''...we create methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our <junit> task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous.'' |
|
Add the following methods to your PersonManagerTest.java file: |
|
[{Java2HtmlPlugin |
|
public void testGetPerson() throws Exception { |
personForm = (PersonForm) mgr.getPerson("1"); |
|
assertTrue("person.firstName not null", |
personForm.getFirstName() != null); |
} |
|
public void testSavePerson() throws Exception { |
personForm = (PersonForm) mgr.getPerson("1"); |
String name = personForm.getFirstName(); |
personForm.setFirstName("test"); |
|
personForm = (PersonForm) mgr.savePerson(personForm); |
assertTrue("name updated", personForm.getFirstName().equals("test")); |
|
personForm.setFirstName(name); |
mgr.savePerson(personForm); |
} |
|
public void testAddAndRemovePerson() throws Exception { |
personForm = new PersonForm(); |
personForm = (PersonForm) populate(personForm); |
|
personForm = (PersonForm) mgr.savePerson(personForm); |
assertTrue(personForm.getFirstName().equals("Abbie")); |
assertTrue(personForm.getId() != null); |
|
if (log.isDebugEnabled()) { |
log.debug("removing personForm, personId: " + |
personForm.getId()); |
} |
|
mgr.removePerson(personForm); |
|
try { |
personForm = (PersonForm) mgr.getPerson(personForm.getId()); |
fail("Expected 'DAOException' not thrown"); |
} catch (DAOException e) { |
if (log.isDebugEnabled()) { |
log.debug(e); |
} |
|
assertTrue(e != null); |
} |
} |
}] |
|
This class won't compile at this point because we have not created our PersonManager interface. |
|
;:%%(color: blue)''I think it's funny how I've followed so many patterns to allow __extendibility__ in AppFuse. In reality, on most projects I've been on - I learn so much in a year that I don't want to extend the architecture - I want to rewrite it. Hopefully by keeping AppFuse up to date with my perceived best practices, this won't happen as much. Each year will just be an upgrade to the latest AppFuse, rather than a re-write. ;-)'' |
|
!!Create a new Manager to talk to the DAO [#2] |
|
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.'' |
|
;:%%(color: blue)''As usual, I usually duplicate (open → save as) an existing file (i.e. UserManager.java).''%% |
|
[{Java2HtmlPlugin |
|
package org.appfuse.service; |
|
public interface PersonManager { |
|
public Object getPerson(String id) throws Exception; |
|
public Object savePerson(Object person) throws Exception; |
|
public void removePerson(Object Person) throws Exception; |
} |
}] |
|
Now let's create a PersonManagerImpl class that implements the methods in PersonManager and uses [BeanUtils.copyProperties|http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#copyProperties(java.lang.Object,%20java.lang.Object)] |
to convert POJOs → ActionForms and ActionForms → POJOs. To do this, create a new class in src/service/**/service and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager. |
|
__IMPORTANT:__ If you're using AppFuse 1.4, make sure that the defaultLong variable in BaseManager.java is equal to null. This [was changed|https://appfuse.dev.java.net/source/browse/appfuse/src/service/org/appfuse/service/BaseManager.java?r1=1.1&r2=1.2] after the 1.4 release and is necessary for inserts to work. |
|
;:''The [BaseManager|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManager.java.html] is for registering different Converters (i.e. [DateConverter|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/util/DateConverter.java.html]) so that BeanUtils.copyProperties knows how to convert Strings → Objects. It also provides a {{convert(Object)}} method that converts POJOs -> Forms and vise-versa. If you have Lists on your POJOs (i.e. for parent-child relationships), you will need to manually convert those if you want to work with indexed properties (see UserManagerImpl.java and the conversion of Roles for an example).'' |
|
[{Java2HtmlPlugin |
|
package org.appfuse.service; |
|
import org.apache.commons.logging.Log; |
import org.apache.commons.logging.LogFactory; |
|
import org.appfuse.model.Person; |
import org.appfuse.persistence.PersonDao; |
|
|
public class PersonManagerImpl extends BaseManager implements PersonManager { |
private Log log = LogFactory.getLog(PersonManagerImpl.class); |
private PersonDao dao; |
|
public void setPersonDao(PersonDao dao) { |
this.dao = dao; |
} |
|
public Object getPerson(String id) throws Exception { |
Person person = dao.getPerson(Long.valueOf(id)); |
|
return convert(person); |
} |
|
public Object savePerson(Object obj) throws Exception { |
Person person = (Person) convert(obj); |
dao.savePerson(person); |
|
return convert(person); |
} |
|
public void removePerson(Object obj) throws Exception { |
Person person = (Person) convert(obj); |
dao.removePerson(person); |
} |
} |
}] |
|
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"... |
|
__Doh!__ I guess not: |
{{{ |
PersonManagerImpl.java:11: cannot resolve symbol |
[javac] symbol : class PersonForm |
[javac] location: package form |
[javac] import org.appfuse.webapp.form.PersonForm; |
}}} |
We need to add XDoclet tags to the Person.java Object to create our Struts ActionForm. In the JavaDoc for the Person.java file, add the following @struts.form tags (use User.java if you need an example): |
|
[{Java2HtmlPlugin |
|
* @struts.form include-all="true" extends="BaseForm" |
}] |
|
;:''We extend org.appfuse.webapp.form.BaseForm because it has a toString() method that allows us to call log.debug(formName) to print out a reader-friendly view of the Form object.'' |
|
Now, running "ant compile-web" should work just fine. |
|
Finally, we need to create the PersonManagerTest.properties file in test/service/**/service so that {{personForm = (PersonForm) populate(personForm);}} will work in our test. |
|
;:''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. |
|
!!Configure Spring for this Manager and Transactions [#3] |
|
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. |
|
[{Java2HtmlPlugin |
|
<!-- Person Manager: Struts implementation --> |
<bean id="personManagerTarget" class="org.appfuse.service.PersonManagerImpl" singleton="false"> |
<property name="personDao"><ref local="personDAO"/></property> |
</bean> |
|
<!-- 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 local="transactionManager"/></property> |
<property name="target"><ref local="personManagerTarget"/></property> |
<property name="transactionAttributes"> |
<props> |
<prop key="save*">PROPAGATION_REQUIRED</prop> |
<prop key="remove*">PROPAGATION_REQUIRED</prop> |
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop> |
</props> |
</property> |
</bean> |
}] |
|
!!Run the ManagerTest [#4] |
|
Save all your edited files and try running "ant test-service -Dtestcase=PersonManager" one more time. |
|
__Yeah Baby, Yeah:__ |
%%(color:green)BUILD SUCCESSFUL\\ |
Total time: 9 seconds%% |
|
---- |
|
''Next Up:'' __Part III:__ [Creating Actions and JSPs|CreateActions] - A HowTo for creating Actions and JSPs in the AppFuse architecture. |
The code below is what we need for a basic JUnit test of our Managers. It created and destroys our PersonManager. The |