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 java.sql.Connection;
34  import java.sql.DriverManager;
35  import java.util.Collection;
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import junit.framework.TestCase;
41  import net.sf.jasperreports.engine.JRDataSource;
42  import net.sf.jasperreports.engine.JasperFillManager;
43  import net.sf.jasperreports.engine.JasperPrint;
44  import net.sf.jasperreports.engine.JasperReport;
45  import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
46  
47  import org.apache.commons.logging.Log;
48  import org.apache.commons.logging.LogFactory;
49  
50  import ar.com.fdvs.dj.core.DynamicJasperHelper;
51  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
52  import ar.com.fdvs.dj.core.layout.LayoutManager;
53  import ar.com.fdvs.dj.domain.DynamicReport;
54  import ar.com.fdvs.dj.util.SortUtils;
55  
56  public abstract class BaseDjReportTest extends TestCase {
57  
58  	public Map getParams() {
59  		return params;
60  	}
61  
62  	protected static final Log log = LogFactory.getLog(BaseDjReportTest.class);
63  
64  	protected JasperPrint jp;
65  	protected JasperReport jr;
66  	protected Map params = new HashMap();
67  	protected DynamicReport dr;
68  
69  	public abstract DynamicReport buildReport() throws Exception;
70  
71  	public void testReport() throws Exception {
72  			dr = buildReport();
73  
74  			/***
75  			 * Get a JRDataSource implementation
76  			 */
77  			JRDataSource ds = getDataSource();
78  
79  
80  			/***
81  			 * Creates the JasperReport object, we pass as a Parameter
82  			 * the DynamicReport, a new ClassicLayoutManager instance (this
83  			 * one does the magic) and the JRDataSource
84  			 */
85  			jr = DynamicJasperHelper.generateJasperReport(dr, getLayoutManager(), params);
86  
87  			/***
88  			 * Creates the JasperPrint object, we pass as a Parameter
89  			 * the JasperReport object, and the JRDataSource
90  			 */
91  			log.debug("Filling the report");
92  			if (ds != null)
93  				jp = JasperFillManager.fillReport(jr, params, ds);
94  			else
95  				jp = JasperFillManager.fillReport(jr, params);
96  
97  			log.debug("Filling done!");
98  			log.debug("Exporting the report (pdf, xls, etc)");
99              exportReport();
100 
101             log.debug("test finished");
102 
103 	}
104 
105 	protected LayoutManager getLayoutManager() {
106 		return new ClassicLayoutManager();
107 	}
108 
109 	protected void exportReport() throws Exception {
110 		ReportExporter.exportReport(jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".pdf");
111 		exportToJRXML();
112 	}
113 	
114 	protected void exportToJRXML() throws Exception {
115 		if (this.jr != null){
116 			DynamicJasperHelper.generateJRXML(this.jr, "UTF-8",System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".jrxml");
117 			
118 		} else {
119 			DynamicJasperHelper.generateJRXML(this.dr, this.getLayoutManager(), this.params, "UTF-8",System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".jrxml");
120 		}
121 	}	
122 
123 	protected void exportToHTML() throws Exception {
124 		ReportExporter.exportReportHtml(this.jp, System.getProperty("user.dir")+ "/target/reports/" + this.getClass().getName() + ".html");
125 	}	
126 
127 	/***
128 	 * @return
129 	 */
130 	protected JRDataSource getDataSource() {
131 		Collection dummyCollection = TestRepositoryProducts.getDummyCollection();
132 		dummyCollection = SortUtils.sortCollection(dummyCollection,dr.getColumns());
133 
134 		JRDataSource ds = new JRBeanCollectionDataSource(dummyCollection);		//Create a JRDataSource, the Collection used
135 																										//here contains dummy hardcoded objects...
136 		return ds;
137 	}
138 
139 	public Collection getDummyCollectionSorted(List columnlist) {
140 		Collection dummyCollection = TestRepositoryProducts.getDummyCollection();
141 		return SortUtils.sortCollection(dummyCollection,columnlist);
142 
143 	}
144 
145 	public DynamicReport getDynamicReport() {
146 		return dr;
147 	}
148 
149 	/***
150 	 * Uses a non blocking HSQL DB. Also uses HSQL default test data
151 	 * @return
152 	 * @throws Exception
153 	 */
154 	public static Connection createSQLConnection() throws Exception {
155 		Connection con = null;
156 		     Class.forName("org.hsqldb.jdbcDriver" );
157 			 con = DriverManager.getConnection("jdbc:hsqldb:file:target/test-classes/hsql/test_dj_db", "sa", "");
158 		return con;
159 	  }
160 }