1   /*
2    * ExcelReportTest.java
3    * Created on 22 Август 2006 г., 17:02
4    */
5   
6   package org.weda.report;
7   
8   import java.io.FileOutputStream;
9   import java.io.InputStream;
10  import java.sql.Date;
11  import org.apache.commons.io.IOUtils;
12  import org.apache.poi.hssf.usermodel.HSSFCell;
13  import org.apache.poi.hssf.usermodel.HSSFRow;
14  import org.apache.poi.hssf.usermodel.HSSFSheet;
15  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
16  import org.weda.ObjectStoreTestCase;
17  import org.weda.domain.Company;
18  import org.weda.report.excel.*;
19  import org.weda.store.ObjectSourceRegistry;
20  
21  /**
22   *
23   * @author Mikhail Titov
24   */
25  public class ExcelReportTest extends ObjectStoreTestCase {
26      
27      public ExcelReportTest(String name) throws Exception {
28          super(name);
29      }
30      
31      public void _test_poi() throws Exception {
32          HSSFWorkbook wb = new HSSFWorkbook();
33          HSSFSheet sheet = wb.createSheet("new sheet");
34  
35          // Create a row and put some cells in it. Rows are 0 based.
36          HSSFRow row = sheet.createRow((short)0);
37          // Create a cell and put a value in it.
38          HSSFCell cell = row.createCell((short)0);
39          cell.setCellValue(1);
40  
41          // Or do it on one line.
42          row.createCell((short)1).setCellValue(1.2);
43          row.createCell((short)2).setCellValue("This is a string");
44          row.createCell((short)3).setCellValue(true);
45  
46          // Write the output to a file
47          FileOutputStream fileOut = new FileOutputStream("target/temp/workbook.xls");
48          //ByteArrayInputStream is = new ByteArrayInputStream(wb.getBytes());
49          wb.write(fileOut);
50          fileOut.close();        
51      }
52      
53      public void test_report1() throws Exception {
54          Company comp = new Company();
55          comp.setName("ОАО Рога и Копыта");
56          comp.setFoundationDate(new Date(System.currentTimeMillis()));
57          comp.setComment("Комментарий к ОАО Рога и Копыта Комментарий к ОАО Рога и Копыта Комментарий к ОАО Рога и Копыта");
58          store.save(comp);
59          
60          comp = new Company();
61          comp.setName("ОАО Копыта и Рога и Хвост");
62          comp.setFoundationDate(new Date(System.currentTimeMillis()));
63          comp.setComment("Комментарий к ОАО Копыта и Рога и Хвост");
64          store.save(comp);
65          //
66          ObjectSourceRegistry obs = 
67                  (ObjectSourceRegistry)registry.getService(
68                      ObjectSourceRegistry.class);
69          obs.getObjectSource("company-report").refresh();
70          ReportRegistry reportRegistry = 
71                  (ReportRegistry)registry.getService(ReportRegistry.class);
72          Report rep = reportRegistry.getReport("test", "report1");
73          InputStream is = rep.generate();
74          assertNotNull(is);
75          FileOutputStream fos = 
76                  new FileOutputStream("target/temp/test-report1.xls");
77          IOUtils.copy(is, fos);
78          fos.close();
79          //
80          rep = reportRegistry.getReport("test", "report2");
81          is = rep.generate();
82          assertNotNull(is);
83          fos = new FileOutputStream("target/temp/test-report2.xls");
84          IOUtils.copy(is, fos);
85          fos.close();
86          //
87          rep = reportRegistry.getReport("test", "report3");
88          is = rep.generate();
89          assertNotNull(is);
90          fos = new FileOutputStream("target/temp/test-report3.xls");
91          IOUtils.copy(is, fos);
92          fos.close();        
93          //
94          rep = reportRegistry.getReport("test", "report4");
95          is = rep.generate();
96          assertNotNull(is);
97          fos = new FileOutputStream("target/temp/test-report4.xls");
98          IOUtils.copy(is, fos);
99          fos.close();
100         //
101         rep = reportRegistry.getReport("test", "report5");
102         is = rep.generate();
103         assertNotNull(is);
104         fos = new FileOutputStream("target/temp/test-report5.xls");
105         IOUtils.copy(is, fos);
106         fos.close();
107         
108     }
109     
110 }