1 16 17 package org.springframework.ui.jasperreports; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.StringWriter ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Locale ; 26 import java.util.Map ; 27 import java.util.ResourceBundle ; 28 29 import junit.framework.TestCase; 30 import net.sf.jasperreports.engine.JRDataSource; 31 import net.sf.jasperreports.engine.JRParameter; 32 import net.sf.jasperreports.engine.JasperFillManager; 33 import net.sf.jasperreports.engine.JasperPrint; 34 import net.sf.jasperreports.engine.JasperReport; 35 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; 36 import net.sf.jasperreports.engine.export.JRHtmlExporter; 37 import net.sf.jasperreports.engine.export.JRPdfExporter; 38 import net.sf.jasperreports.engine.util.JRLoader; 39 import org.apache.poi.hssf.usermodel.HSSFCell; 40 import org.apache.poi.hssf.usermodel.HSSFRow; 41 import org.apache.poi.hssf.usermodel.HSSFSheet; 42 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 43 44 import org.springframework.core.io.ClassPathResource; 45 46 51 public class JasperReportsUtilsTests extends TestCase { 52 53 public void testRenderAsCsvWithDataSource() throws Exception { 54 StringWriter writer = new StringWriter (); 55 JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getDataSource(), writer); 56 String output = writer.getBuffer().toString(); 57 assertCsvOutputCorrect(output); 58 } 59 60 public void testRenderAsCsvWithCollection() throws Exception { 61 StringWriter writer = new StringWriter (); 62 JasperReportsUtils.renderAsCsv(getReport(), getParameters(), getData(), writer); 63 String output = writer.getBuffer().toString(); 64 assertCsvOutputCorrect(output); 65 } 66 67 public void testRenderAsHtmlWithDataSource() throws Exception { 68 StringWriter writer = new StringWriter (); 69 JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getDataSource(), writer); 70 String output = writer.getBuffer().toString(); 71 assertHtmlOutputCorrect(output); 72 } 73 74 public void testRenderAsHtmlWithCollection() throws Exception { 75 StringWriter writer = new StringWriter (); 76 JasperReportsUtils.renderAsHtml(getReport(), getParameters(), getData(), writer); 77 String output = writer.getBuffer().toString(); 78 assertHtmlOutputCorrect(output); 79 } 80 81 public void testRenderAsPdfWithDataSource() throws Exception { 82 ByteArrayOutputStream os = new ByteArrayOutputStream (); 83 JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getDataSource(), os); 84 byte[] output = os.toByteArray(); 85 assertPdfOutputCorrect(output); 86 } 87 88 public void testRenderAsPdfWithCollection() throws Exception { 89 ByteArrayOutputStream os = new ByteArrayOutputStream (); 90 JasperReportsUtils.renderAsPdf(getReport(), getParameters(), getData(), os); 91 byte[] output = os.toByteArray(); 92 assertPdfOutputCorrect(output); 93 } 94 95 public void testRenderAsXlsWithDataSource() throws Exception { 96 ByteArrayOutputStream os = new ByteArrayOutputStream (); 97 JasperReportsUtils.renderAsXls(getReport(), getParameters(), getDataSource(), os); 98 byte[] output = os.toByteArray(); 99 assertXlsOutputCorrect(output); 100 } 101 102 public void testRenderAsXlsWithCollection() throws Exception { 103 ByteArrayOutputStream os = new ByteArrayOutputStream (); 104 JasperReportsUtils.renderAsXls(getReport(), getParameters(), getData(), os); 105 byte[] output = os.toByteArray(); 106 assertXlsOutputCorrect(output); 107 } 108 109 public void testRenderWithWriter() throws Exception { 110 StringWriter writer = new StringWriter (); 111 JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource()); 112 JasperReportsUtils.render(new JRHtmlExporter(), print, writer); 113 String output = writer.getBuffer().toString(); 114 assertHtmlOutputCorrect(output); 115 } 116 117 public void testRenderWithOutputStream() throws Exception { 118 ByteArrayOutputStream os = new ByteArrayOutputStream (); 119 JasperPrint print = JasperFillManager.fillReport(getReport(), getParameters(), getDataSource()); 120 JasperReportsUtils.render(new JRPdfExporter(), print, os); 121 byte[] output = os.toByteArray(); 122 assertPdfOutputCorrect(output); 123 } 124 125 private void assertCsvOutputCorrect(String output) { 126 assertTrue("Output length should be greater than 0", (output.length() > 0)); 127 assertTrue("Output should start with Dear Lord!", output.startsWith("Dear Lord!")); 128 assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1); 129 } 130 131 private void assertHtmlOutputCorrect(String output) { 132 assertTrue("Output length should be greater than 0", (output.length() > 0)); 133 assertTrue("Output should contain <html>", output.indexOf("<html>") > -1); 134 assertTrue("Output should contain 'MeineSeite'", output.indexOf("MeineSeite") > -1); 135 } 136 137 private void assertPdfOutputCorrect(byte[] output) throws Exception { 138 assertTrue("Output length should be greater than 0", (output.length > 0)); 139 140 String translated = new String (output, "US-ASCII"); 141 assertTrue("Output should start with %PDF", translated.startsWith("%PDF")); 142 } 143 144 private void assertXlsOutputCorrect(byte[] output) throws Exception { 145 HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream (output)); 146 HSSFSheet sheet = workbook.getSheetAt(0); 147 assertNotNull("Sheet should not be null", sheet); 148 HSSFRow row = sheet.getRow(3); 149 HSSFCell cell = row.getCell((short)1); 150 assertNotNull("Cell should not be null", cell); 151 assertEquals("Cell content should be Dear Lord!", "Dear Lord!", cell.getStringCellValue()); 152 } 153 154 private JasperReport getReport() throws Exception { 155 ClassPathResource resource = new ClassPathResource("DataSourceReport.jasper", getClass()); 156 return (JasperReport) JRLoader.loadObject(resource.getInputStream()); 157 } 158 159 private Map getParameters() { 160 Map model = new HashMap (); 161 model.put("ReportTitle", "Dear Lord!"); 162 model.put(JRParameter.REPORT_LOCALE, Locale.GERMAN); 163 model.put(JRParameter.REPORT_RESOURCE_BUNDLE, 164 ResourceBundle.getBundle("org/springframework/ui/jasperreports/messages", Locale.GERMAN)); 165 return model; 166 } 167 168 private JRDataSource getDataSource() { 169 return new JRBeanCollectionDataSource(getData()); 170 } 171 172 private List getData() { 173 List list = new ArrayList (); 174 for (int x = 0; x < 10; x++) { 175 PersonBean bean = new PersonBean(); 176 bean.setId(x); 177 bean.setName("Rob Harrop"); 178 bean.setStreet("foo"); 179 list.add(bean); 180 } 181 return list; 182 } 183 184 } 185 | Popular Tags |