View Javadoc

1   /*
2    * DynamicJasper: A library for creating reports dynamically by specifying
3    * columns, groups, styles, etc. at runtime. It also saves a lot of development
4    * time in many cases! (http://sourceforge.net/projects/dynamicjasper)
5    *
6    * Copyright (C) 2008  FDV Solutions (http://www.fdvsolutions.com)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   *
11   * License as published by the Free Software Foundation; either
12   *
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19   *
20   * Lesser General Public License for more details.
21   *
22   * You should have received a copy of the GNU Lesser General Public
23   * License along with this library; if not, write to the Free Software
24   *
25   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26   *
27   *
28   */
29  
30  package ar.com.fdvs.dj.test;
31  
32  
33  import ar.com.fdvs.dj.core.DynamicJasperHelper;
34  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
35  import ar.com.fdvs.dj.core.layout.LayoutManager;
36  import ar.com.fdvs.dj.domain.DynamicReport;
37  import ar.com.fdvs.dj.util.SortUtils;
38  import junit.framework.TestCase;
39  import net.sf.jasperreports.engine.JRDataSource;
40  import net.sf.jasperreports.engine.JasperFillManager;
41  import net.sf.jasperreports.engine.JasperPrint;
42  import net.sf.jasperreports.engine.JasperReport;
43  import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import java.sql.Connection;
48  import java.sql.DriverManager;
49  import java.util.*;
50  
51  public abstract class BaseDjReportTest extends TestCase {
52  
53  	public Map getParams() {
54  		return params;
55  	}
56  
57  	protected static final Log log = LogFactory.getLog(BaseDjReportTest.class);
58  
59  	protected JasperPrint jp;
60  	protected JasperReport jr;
61  	protected Map params = new HashMap();
62  	protected DynamicReport dr;
63  
64  	public abstract DynamicReport buildReport() throws Exception;
65  
66  	public void testReport() throws Exception {
67  			dr = buildReport();
68  
69  			/**
70  			 * Get a JRDataSource implementation
71  			 */
72  			JRDataSource ds = getDataSource();
73  
74  
75  			/**
76  			 * Creates the JasperReport object, we pass as a Parameter
77  			 * the DynamicReport, a new ClassicLayoutManager instance (this
78  			 * one does the magic) and the JRDataSource
79  			 */
80  			jr = DynamicJasperHelper.generateJasperReport(dr, getLayoutManager(), params);
81  
82  			/**
83  			 * Creates the JasperPrint object, we pass as a Parameter
84  			 * the JasperReport object, and the JRDataSource
85  			 */
86  			log.debug("Filling the report");
87  			if (ds != null)
88  				jp = JasperFillManager.fillReport(jr, params, ds);
89  			else
90  				jp = JasperFillManager.fillReport(jr, params);
91  
92  			log.debug("Filling done!");
93  			log.debug("Exporting the report (pdf, xls, etc)");
94              exportReport();
95  
96              log.debug("test finished");
97  
98  	}
99  
100 	protected LayoutManager getLayoutManager() {
101 		return new ClassicLayoutManager();
102 	}
103 
104 	protected void exportReport() throws Exception {
105 		ReportExporter.exportReport(jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".pdf");
106 		exportToJRXML();
107 	}
108 	
109 	protected void exportToJRXML() throws Exception {
110 		if (this.jr != null){
111 			DynamicJasperHelper.generateJRXML(this.jr, "UTF-8",System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".jrxml");
112 			
113 		} else {
114 			DynamicJasperHelper.generateJRXML(this.dr, this.getLayoutManager(), this.params, "UTF-8",System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".jrxml");
115 		}
116 	}	
117 
118 	protected void exportToHTML() throws Exception {
119 		ReportExporter.exportReportHtml(this.jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".html");
120 	}	
121 
122 	/**
123 	 * @return JRDataSource
124 	 */
125 	protected JRDataSource getDataSource() {
126 		Collection dummyCollection = TestRepositoryProducts.getDummyCollection();
127 		dummyCollection = SortUtils.sortCollection(dummyCollection,dr.getColumns());
128 
129 		JRDataSource ds = new JRBeanCollectionDataSource(dummyCollection);		//Create a JRDataSource, the Collection used
130 																				//here contains dummy hardcoded objects...
131 		return ds;
132 	}
133 
134 	public Collection getDummyCollectionSorted(List columnlist) {
135 		Collection dummyCollection = TestRepositoryProducts.getDummyCollection();
136 		return SortUtils.sortCollection(dummyCollection,columnlist);
137 
138 	}
139 
140 	public DynamicReport getDynamicReport() {
141 		return dr;
142 	}
143 
144 	/**
145 	 * Uses a non blocking HSQL DB. Also uses HSQL default test data
146 	 * @return a Connection
147 	 * @throws Exception
148 	 */
149 	public static Connection createSQLConnection() throws Exception {
150 		Connection con = null;
151 		     Class.forName("org.hsqldb.jdbcDriver" );
152 			 con = DriverManager.getConnection("jdbc:hsqldb:file:target/test-classes/hsql/test_dj_db", "sa", "");
153 
154         return con;
155 	}
156 
157     public int getYear(){
158         return Calendar.getInstance().get(Calendar.YEAR);
159     }
160 }