1 16 17 package org.springframework.web.servlet.view.jasperreports; 18 19 import java.sql.SQLException ; 20 import java.util.LinkedList ; 21 import java.util.Locale ; 22 import java.util.Map ; 23 import java.util.Properties ; 24 25 import javax.sql.DataSource ; 26 27 import net.sf.jasperreports.engine.JRDataSource; 28 import net.sf.jasperreports.engine.JRException; 29 import net.sf.jasperreports.engine.JasperReport; 30 import net.sf.jasperreports.engine.data.JRAbstractBeanDataSourceProvider; 31 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; 32 import org.easymock.MockControl; 33 34 import org.springframework.context.ApplicationContextException; 35 import org.springframework.context.support.StaticApplicationContext; 36 import org.springframework.ui.jasperreports.PersonBean; 37 38 42 public abstract class AbstractJasperReportsViewTests extends AbstractJasperReportsTests { 43 44 protected abstract AbstractJasperReportsView getViewImplementation(); 45 46 protected abstract String getDesiredContentType(); 47 48 49 protected AbstractJasperReportsView getView(String url) throws Exception { 50 AbstractJasperReportsView view = getViewImplementation(); 51 view.setUrl(url); 52 StaticApplicationContext ac = new StaticApplicationContext(); 53 ac.addMessage("page", Locale.GERMAN, "MeineSeite"); 54 ac.refresh(); 55 view.setApplicationContext(ac); 56 return view; 57 } 58 59 62 public void testCompiledReport() throws Exception { 63 AbstractJasperReportsView view = getView(COMPILED_REPORT); 64 view.render(getModel(), request, response); 65 assertTrue(response.getContentAsByteArray().length > 0); 66 if (view instanceof AbstractJasperReportsSingleFormatView && 67 ((AbstractJasperReportsSingleFormatView) view).useWriter()) { 68 String output = response.getContentAsString(); 69 assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1); 70 } 71 } 72 73 public void testUncompiledReport() throws Exception { 74 if (!canCompileReport) { 75 return; 76 } 77 78 AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); 79 view.render(getModel(), request, response); 80 assertTrue(response.getContentAsByteArray().length > 0); 81 } 82 83 public void testWithInvalidPath() throws Exception { 84 try { 85 getView("foo.jasper"); 86 fail("Invalid path should throw ApplicationContextException"); 87 } 88 catch (ApplicationContextException ex) { 89 } 91 } 92 93 public void testInvalidExtension() throws Exception { 94 try { 95 getView("foo.bar"); 96 fail("Invalid extension should throw IllegalArgumentException"); 97 } 98 catch (IllegalArgumentException ex) { 99 } 101 } 102 103 public void testContentType() throws Exception { 104 AbstractJasperReportsView view = getView(COMPILED_REPORT); 105 106 view.render(getModel(), request, response); 110 assertEquals("Response content type is incorrect", getDesiredContentType(), response.getContentType()); 111 } 112 113 public void testWithoutDatasource() throws Exception { 114 Map model = getModel(); 115 model.remove("dataSource"); 116 try { 117 AbstractJasperReportsView view = getView(COMPILED_REPORT); 118 view.render(model, request, response); 119 fail("No data source should result in NoDataSourceException"); 120 } 121 catch (IllegalArgumentException ex) { 122 } 124 } 125 126 public void testWithCollection() throws Exception { 127 Map model = getModel(); 128 model.remove("dataSource"); 129 model.put("reportData", getData()); 130 AbstractJasperReportsView view = getView(COMPILED_REPORT); 131 view.render(model, request, response); 132 assertTrue(response.getContentAsByteArray().length > 0); 133 } 134 135 public void testWithMultipleCollections() throws Exception { 136 Map model = getModel(); 137 model.remove("dataSource"); 138 model.put("reportData", getData()); 139 model.put("otherData", new LinkedList ()); 140 try { 141 AbstractJasperReportsView view = getView(COMPILED_REPORT); 142 view.render(model, request, response); 143 fail("No data source should result in NoDataSourceException"); 144 } 145 catch (IllegalArgumentException ex) { 146 } 148 } 149 150 public void testWithJRDataSourceProvider() throws Exception { 151 Map model = getModel(); 152 model.remove("dataSource"); 153 model.put("dataSource", new MockDataSourceProvider(PersonBean.class)); 154 AbstractJasperReportsView view = getView(COMPILED_REPORT); 155 view.render(model, request, response); 156 assertTrue(response.getContentAsByteArray().length > 0); 157 } 158 159 public void testWithSpecificCollection() throws Exception { 160 Map model = getModel(); 161 model.remove("dataSource"); 162 model.put("reportData", getData()); 163 model.put("otherData", new LinkedList ()); 164 AbstractJasperReportsView view = getView(COMPILED_REPORT); 165 view.setReportDataKey("reportData"); 166 view.render(model, request, response); 167 assertTrue(response.getContentAsByteArray().length > 0); 168 } 169 170 public void testWithArray() throws Exception { 171 Map model = getModel(); 172 model.remove("dataSource"); 173 model.put("reportData", getData().toArray()); 174 AbstractJasperReportsView view = getView(COMPILED_REPORT); 175 view.render(model, request, response); 176 assertTrue(response.getContentAsByteArray().length > 0); 177 } 178 179 public void testWithMultipleArrays() throws Exception { 180 Map model = getModel(); 181 model.remove("dataSource"); 182 model.put("reportData", getData().toArray()); 183 model.put("otherData", new String [0]); 184 try { 185 AbstractJasperReportsView view = getView(COMPILED_REPORT); 186 view.render(model, request, response); 187 fail("No data source should result in NoDataSourceException"); 188 } 189 catch (IllegalArgumentException ex) { 190 } 192 } 193 194 public void testWithSpecificArray() throws Exception { 195 Map model = getModel(); 196 model.remove("dataSource"); 197 model.put("reportData", getData().toArray()); 198 model.put("otherData", new String [0]); 199 AbstractJasperReportsView view = getView(COMPILED_REPORT); 200 view.setReportDataKey("reportData"); 201 view.render(model, request, response); 202 assertTrue(response.getContentAsByteArray().length > 0); 203 } 204 205 public void testWithSubReport() throws Exception { 206 if (!canCompileReport) { 207 return; 208 } 209 210 Map model = getModel(); 211 model.put("SubReportData", getProductData()); 212 213 Properties subReports = new Properties (); 214 subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChild.jrxml"); 215 216 AbstractJasperReportsView view = getView(SUB_REPORT_PARENT); 217 view.setReportDataKey("dataSource"); 218 view.setSubReportUrls(subReports); 219 view.setSubReportDataKeys(new String []{"SubReportData"}); 220 view.initApplicationContext(); 221 view.render(model, request, response); 222 223 assertTrue(response.getContentAsByteArray().length > 0); 224 } 225 226 public void testWithNonExistentSubReport() throws Exception { 227 if (!canCompileReport) { 228 return; 229 } 230 231 Map model = getModel(); 232 model.put("SubReportData", getProductData()); 233 234 Properties subReports = new Properties (); 235 subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChildFalse.jrxml"); 236 237 AbstractJasperReportsView view = getView(SUB_REPORT_PARENT); 238 view.setReportDataKey("dataSource"); 239 view.setSubReportUrls(subReports); 240 view.setSubReportDataKeys(new String []{"SubReportData"}); 241 242 try { 243 view.initApplicationContext(); 244 fail("Invalid report URL should throw ApplicationContext Exception"); 245 } 246 catch (ApplicationContextException ex) { 247 } 249 } 250 251 public void testSubReportWithUnspecifiedParentDataSource() throws Exception { 252 if (!canCompileReport) { 253 return; 254 } 255 256 Map model = getModel(); 257 model.put("SubReportData", getProductData()); 258 259 Properties subReports = new Properties (); 260 subReports.put("ProductsSubReport", "org/springframework/ui/jasperreports/subReportChildFalse.jrxml"); 261 262 AbstractJasperReportsView view = getView(SUB_REPORT_PARENT); 263 view.setSubReportUrls(subReports); 264 view.setSubReportDataKeys(new String []{"SubReportData"}); 265 266 try { 267 view.initApplicationContext(); 268 fail("Unspecified reportDataKey should throw exception when subReportDataSources is specified"); 269 } 270 catch (ApplicationContextException ex) { 271 } 273 } 274 275 public void testContentDisposition() throws Exception { 276 AbstractJasperReportsView view = getView(COMPILED_REPORT); 277 view.render(getModel(), request, response); 278 assertEquals("Invalid content type", "inline", response.getHeader("Content-Disposition")); 279 280 } 281 282 public void testOverrideContentDisposition() throws Exception { 283 Properties headers = new Properties (); 284 String cd = "attachment"; 285 headers.setProperty("Content-Disposition", cd); 286 287 AbstractJasperReportsView view = getView(COMPILED_REPORT); 288 view.setHeaders(headers); 289 view.render(getModel(), request, response); 290 assertEquals("Invalid content type", cd, response.getHeader("Content-Disposition")); 291 } 292 293 public void testSetCustomHeaders() throws Exception { 294 Properties headers = new Properties (); 295 296 String key = "foo"; 297 String value = "bar"; 298 299 headers.setProperty(key, value); 300 301 AbstractJasperReportsView view = getView(COMPILED_REPORT); 302 view.setHeaders(headers); 303 view.render(getModel(), request, response); 304 305 assertNotNull("Header not present", response.getHeader(key)); 306 assertEquals("Invalid header value", value, response.getHeader(key)); 307 308 } 309 310 public void testWithSqlDataSource() throws Exception { 311 if (!canCompileReport) { 312 return; 313 } 314 315 AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); 316 view.setJdbcDataSource(getMockSqlDataSource()); 317 318 Map model = getModel(); 319 model.remove("dataSource"); 320 321 try { 322 view.render(model, request, response); 323 fail("DataSource was not used as report DataSource"); 324 } 325 catch (SQLException ex) { 326 } 328 } 329 330 public void testJRDataSourceOverridesDataSource() throws Exception { 331 if (!canCompileReport) { 332 return; 333 } 334 335 AbstractJasperReportsView view = getView(UNCOMPILED_REPORT); 336 view.setJdbcDataSource(getMockSqlDataSource()); 337 338 try { 339 view.render(getModel(), request, response); 340 } 341 catch (SQLException ex) { 342 fail("javax.sql.DataSource was used when JRDataSource should have overriden it"); 343 } 344 } 345 346 private DataSource getMockSqlDataSource() throws SQLException { 347 MockControl ctl = MockControl.createControl(DataSource .class); 348 DataSource ds = (DataSource ) ctl.getMock(); 349 ds.getConnection(); 350 ctl.setThrowable(new SQLException ()); 351 ctl.replay(); 352 return ds; 353 } 354 355 356 private class MockDataSourceProvider extends JRAbstractBeanDataSourceProvider { 357 358 public MockDataSourceProvider(Class clazz) { 359 super(clazz); 360 } 361 362 public JRDataSource create(JasperReport jasperReport) throws JRException { 363 return new JRBeanCollectionDataSource(getData()); 364 } 365 366 public void dispose(JRDataSource jrDataSource) throws JRException { 367 368 } 369 } 370 } 371 | Popular Tags |