1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
71
72 JRDataSource ds = getDataSource();
73
74
75
76
77
78
79
80 jr = DynamicJasperHelper.generateJasperReport(dr, getLayoutManager(), params);
81
82
83
84
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
124
125 protected JRDataSource getDataSource() {
126 Collection dummyCollection = TestRepositoryProducts.getDummyCollection();
127 dummyCollection = SortUtils.sortCollection(dummyCollection,dr.getColumns());
128
129 JRDataSource ds = new JRBeanCollectionDataSource(dummyCollection);
130
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
146
147
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 }