Time to complete this step: 10 minutes
In this step, you will provide additional Spring service beans for the ILoginDao, IProductDao and the ITicketDao developed in the previous tutorial.
You will perform the following tasks in this step:
TechnicianProductTicket
Copy the following class files from the extracted resources/delegates folder to the com.oracle.ticketsystem.delegates package.
Open and review each interface.
Similarly, copy the following Spring service bean class files from the extracted resources/delegates/impl folder to the com.oracle.ticketsystem.delegates.impl package.
Open and review each Spring service bean class. The delegate implementation does certian data validations and wraps the call to the respective JPA DAOs. Note that the TicketSpringDelegate class has been annotated with the @Transactional annotation. The @Transactional annotation causes all the methods in the class to get an automatic transaction, so a transaction will be provided whenever a caller invokes any method.
...
@Transactional(propagation=Propagation.REQUIRED)
public class TicketSpringDelegate implements ITicketDelegate {
...
...
@Override
public Ticket add(long productId, String customerName,
String customerEmail, String title, String description)
throws Exception {
try {
return getTicketDao().add(productId, customerName, customerEmail, title, description);
} catch(RuntimeException e) {
throw new Exception("add Ticket operation failed: " + e.getMessage());
}
}
@Override
public Ticket get(long ticketId) throws Exception {
try {
return getTicketDao().get(ticketId);
} catch(RuntimeException e) {
throw new Exception("get(by ticketI) operation failed: " + e.getMessage());
}
}
...
...
Open the TroubleTicketSystemServer > WebContent > WEB-INF > applicationContext.xml (Spring Configuration) file. Add a bean definition for each delegate that was copied in the previous step (after the definition of the DepartmentDelegate bean and before the closing </beans> element)..
...
...
<bean class="com.oracle.ticketsystem.delegates.impl.DepartmentSpringDelegate" id="DepartmentDelegate">
<property name="dao">
<bean class="com.oracle.ticketsystem.dao.impl.DepartmentJPADao"/>
</property>
</bean>
<bean class="com.oracle.ticketsystem.delegates.impl.LoginSpringDelegate" id="LoginDelegate">
<property name="loginDao">
<bean class="com.oracle.ticketsystem.dao.impl.LoginJPADao"/>
</property>
</bean>
<bean class="com.oracle.ticketsystem.delegates.impl.ProductSpringDelegate" id="ProductDelegate">
<property name="productDao">
<bean class="com.oracle.ticketsystem.dao.impl.ProductJPADao"/>
</property>
</bean>
<bean class="com.oracle.ticketsystem.delegates.impl.TicketSpringDelegate" id="TicketDelegate">
<property name="ticketDao">
<bean class="com.oracle.ticketsystem.dao.impl.TicketJPADao"/>
</property>
</bean>
</beans>
Save the applicationContext.xml file.
The figure below shows the JPA DAO classes and its hierarchy.
The LoginDao, the ProductDao and the TicketDao classes extends the BaseJPADao class. The BaseJPADao class provides a reference of EntityManager using the JPADaoFactory. The JPADaoFactory class creates and returns a singleton reference to an EntityManager for the TroubleTicketSystemServer persistence unit.
Since in the previous step, we have added the Spring framework support to the TroubleTicketSystemServer project, instead of manually intializing it, we can use the Spring framework facility to initialize and inject the EntityManager for the TroubleTicketSystemServer persistence unit into the BaseJPADao class, and thus it can be used by the sub-classes.
Open the BaseJPADao class from the com.oracle.ticketsystem.dao.impl package.
public EntityManager getEntityManager() {
return JPADaoFactory.createEntityManager();
}
@PersistenceContext
private EntityManager em;
/**
* Returns JPA EntityManager reference.
* @return
*/
public EntityManager getEntityManager() {
return em;
}
Open the LoginJPADao class from the com.oracle.ticketsystem.dao.impl package.
...
@Repository
public class LoginJPADao extends BaseJPADao implements ILoginDao {
...
...
Similarly, annotate the ProductJPADao and TicketJPADao classes with the @Repository annotation.
Since, we have annotated the TicketJPADao class with the @Transactional annation, which causes all the methods to get an automatic transaction, it is not required to create a new transaction and commit it after the work is done. Hence, remove the code for creating and committing a transaction from the add(..) method of the TicketJPADao class.
Similarly, remove the transaction creation and commit code from the update(..) and remove(..) methods of the TicketJPADao class.
Save the TicketJPADao class.
Time to complete this step: 10 minutes
Spring 2.5 provides support for integration testing through the classes in the org.springframework.test package (available in the spring-test.jar). These classes allow you to dependency inject your test cases off of your existing Spring configuration, either by using your production Spring configuration file or one you've defined especially for the test case.
In this step, using the Spring Test framework for JUnit, you will execute different methods of Spring Service Beans and DAOs that were developed in the previous steps.
You will perform the following tasks in this step:
In this step, you will import the JUnit TestCases for executing various operations of Spring service beans / business delegates.
Create a new com.oracle.ticketsystem.delegates.tests package in TroubleTicketSystemServer project.
Copy all the test classes from the extracted resources/tests folder to the com.oracle.ticketsystem.delegates.tests package.
All test cases are based on JUnit 4 framework and annotated with Spring annotations. Here is an explanation of the annotations being used.
Open and review the test classes.
Since, the provided JUnit 4 test cases requires to auto wire the JdbcTemplate class, we need to add a service bean entry to the Spring configuration (applicationContext.xml) file. The JdbcTemplate bean shall be injected with the DataSource and is used for asserting the state of entities after various CRUD operations have been performed.
Open the TroubleTicketSystemServer > WebContent > WEB-INF > applicationContext.xml (Spring Configuration) file. Add following bean definition for DataSource and JdbcTemplate classes (after the definition of TicketSpringDelegate bean and before the closing </beans> element). If you have any trouble with schema validation errors, remove the comments <!-- -->.
...
...
<!-- === only for the JUnit Tests === -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
<property name="username" value="troubleticketuser"/>
<property name="password" value="password"/>
</bean>
<bean id=jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- ====== -->
</beans>
Edit the property values so they are consistent with your local database settings.
Save the applicationContext.xml file.
The JUnit tests requires the applicationContext.xml file in its classpath. Right-click the TroubleTicketSystemServer project and select Properties from the popup menu.
In the Properties dialog, select Java Build Path in the left pane and select the Source tab in the right-pane.
Click the Add Folder... button and select the TroubleTicketSystemServer > WebContent > WEB-INF folder. Click OK.
That adds the TroubleTicketSystemServer/WebContent/WEB-INF folder to the build path. Click OK in the Properties dialog.
Right-click the LoginDelegateTest class and Run As > JUnit Test .
That launches the unit test LoginDelegateTest and shows the result in JUnit UI. It executes the tests for valid and invalid login parameters.
Similarly, execute the DepartmentDelegateTest JUnit test. Verify the test results for fetching a specific or all departments and persist/remove department operations.
Similarly, execute the ProductDelegateTest JUnit test. Verify the test results for fetching all products or a specific product.
Similarly, execute the TicketDelegateTest JUnit test. Verify all the CRUD operations performed on Ticket entity..