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 1.
It is not the current version, and thus it cannot be edited. Part II: Criando novos Managers (gerentes) - Um HowTo para criação de Business Delegates que conversa com as camadas banco de dados (DAOs) e web (Struts Actions or Spring Controllers).
Sobre este TutorialEste tutorial mostrará como criar uma classe que delega regras de negócio (e um teste JUnit) para conversar com o DAO que criamos na Parte I.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 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.
Let's get started by 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]In Part I, we created a Person object and PersonDao - so let's continue developing this entity. First, let's create a JUnit test for the PersonManager. Create PersonManagerTest in the test/service/**/service directory. We'll want to test the same basic methods (get, save, remove) that our DAO has.
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, as well as to initialize Spring's ApplicationContext.
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 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:
This class won't compile at this point because we have not created our PersonManager interface.
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.
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.
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"... Finally, we need to create the PersonManagerTest.properties file in test/service/**/service so that person = (Person) populate(person); will work in our test.
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.
Note: I 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.
Run the ManagerTest [#4]Save all your edited files and try running "ant test-service -Dtestcase=PersonManager" one more time. Yeah Baby, Yeah:
BUILD SUCCESSFUL Next Up: Part III: Creating Actions and JSPs - A HowTo for creating Actions and JSPs in the AppFuse architecture.
|