Tuesday, July 1, 2014

Unit Testing the Velocity Templates in spring

Unit Testing the Velocity Templates in spring

It is painful to test the Velocity Template  by deploying to the server while doing frequent changes.
The unit testing  will help as to overcome this and do proper testing without deploying the project to the server.
This post will explain the approach to unit test the Velocity Templates through eclipse.

Spring velocity configurations

Create a Velocity-test.xml configuration file to configure the velocity bean - instead of creating separate configuration file we can also use the project configuration file that has the velocity bean configuration (e.g. application-context.xml)


Change the vm template path accordingly.

Create a test java class (VelocityTest.java)

import java.io.*;
import java.util.ArrayList;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.thomson.ecom.core.velocity.VelocityContextBuilder;
public class VelocityTest
{
public static void main(String[] args) throws Exception
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:velocity-test.xml");
ArrayList<Employee> employeeList=new ArrayList<Employee>();

Employee emp1=new Employee();
emp1.setContactNo("111111");
emp1.setDept("CSC");
emp1.setEmail("[email protected]");
emp1.setEmpName("Albin");
emp1.setEmpNo("1");
emp1.setLocation("Bangalore");
employeeList.add(emp1);

Employee emp2=new Employee();
emp2.setContactNo("22222");
emp2.setDept("IT");
emp2.setEmail("[email protected]");
emp2.setEmpName("Albin1");
emp2.setEmpNo("2");
emp2.setLocation("Bangalore");
employeeList.add(emp2);

Employee emp3=new Employee();
emp3.setContactNo("33333");
emp3.setDept("IT");
emp3.setEmail("[email protected]");
emp3.setEmpName("Albin2");
emp3.setEmpNo("3");
emp3.setLocation("Bangalore");
employeeList.add(emp3);

VelocityEngine engine = ctx.getBean("velocityEngine", VelocityEngine.class);
Context velocityCtx = VelocityContextBuilder.getInstance().buildContext();
velocityCtx.put("empList", employeeList);

Writer w = new FileWriter(new File("D:\\Albin\\velocity.html"));
engine.mergeTemplate("EmployeeEmailTemplate.vm", velocityCtx, w);
w.close();

System.exit(0);
}
}

Change the velocity.html file path and the vm template name accordingly.
Pass the required inputs to the velocityCtx map.
Execute the VelocityTest.java; this will generate the velocity.html file in the specified location.



Velocity Email template:

I have used the sample vm file EmployeeEmailTemplate.vm as below
#macro(writeEmployeeDetails $emp)
<tr>
    <td style="text-align:center;border:1px solid black;">$emp.empName</td>
    <td style="text-align:center;border:1px solid black;">$emp.empNo</td>
    <td style="text-align:center;border:1px solid black;">$emp.email</td>
    <td style="text-align:center;border:1px solid black;">$emp.contactNo</td>
    <td style="text-align:center;border:1px solid black;">$emp.location</td>
       <td style="text-align:center;border:1px solid black;">$emp.dept</td>                 
                
</tr>
#end

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>Employee Details</title>
</head>
<body>
       <div>
        <table style="width:50%;" cellspacing="0" cellpadding="0">
        <tr>
        <th style="text-align:center;border:1px solid black;"><b>Name</b></th>
        <th style="text-align:center;border:1px solid black;"><b>EmpNo</b></th>
        <th style="text-align:center;border:1px solid black;"><b>Email</b></th>
        <th style="text-align:center;border:1px solid black;"><b>ContactNo</b></th>
        <th style="text-align:center;border:1px solid black;"><b>Location</b></th>
        <th style="text-align:center;border:1px solid black;"><b>Dept</b></th>        
        </tr>        
        #foreach($emp in $empList)        
        #writeEmployeeDetails($emp)                
               #end            
        </table>
       </div>      
</body>
</html>

No comments:

Post a Comment