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  import java.io.FileNotFoundException;
33  import java.util.Arrays;
34  import java.util.Collection;
35  import java.util.List;
36  
37  import junit.framework.TestCase;
38  import net.sf.jasperreports.engine.JRException;
39  import net.sf.jasperreports.engine.JasperPrint;
40  import ar.com.fdvs.dj.core.DynamicJasperHelper;
41  import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
42  import ar.com.fdvs.dj.domain.DynamicReport;
43  import ar.com.fdvs.dj.domain.builders.ReflectiveReportBuilder;
44  import ar.com.fdvs.dj.util.SortUtils;
45  
46  public class ReflectiveReportTest extends TestCase {
47  
48  //	private DynamicReport buildOrderedReport(final Collection _data, final String[] _properties) {
49  //		return new ReflectiveReportBuilder(_data, _properties).addGroups(3).build();
50  //	}
51  
52  	/**
53  	 * Test N� 1. With only the collection, the ReflectiveReportBuilder make some guesses
54  	 */
55  	public void testReport() {
56          final Collection data = TestRepositoryProducts.getDummyCollection();
57          DynamicReport dynamicReport = new ReflectiveReportBuilder(data).build();
58  		doReport(dynamicReport, data, "");
59      }
60  
61  	/**
62  	 * Test N�2, the same but we tell the builder the order of the columns, we also add 3 groups
63  	 */
64  	public void testOrderedReport() {
65          final Collection data = TestRepositoryProducts.getDummyCollection();
66          final List items = SortUtils.sortCollection(data, Arrays.asList(new String[]{"productLine", "item", "state"}));
67          String[] columOrders = new String[]{"productLine", "item", "state", "id", "branch", "quantity", "amount"};
68  		DynamicReport dynamicReport = new ReflectiveReportBuilder(items, columOrders).addGroups(3).build();
69  		doReport(dynamicReport, items, "ordered");
70      }
71  
72  	public void doReport(final DynamicReport _report, final Collection _data, String name) {
73          try {
74          	final JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(_report, new ClassicLayoutManager(), _data);
75  //        	JasperViewer.viewReport(jasperPrint);
76  			ReportExporter.exportReport(jasperPrint, System.getProperty("user.dir")+ "/target/ReflectiveReportTest "+ name + ".pdf");
77  		} catch (FileNotFoundException e) {
78  			e.printStackTrace();
79  		} catch (JRException e) {
80  			e.printStackTrace();
81  		}
82  	}
83  
84  	public static void main(final String[] _args) {
85          final ReflectiveReportTest reportTest = new ReflectiveReportTest();
86          reportTest.testReport();
87          reportTest.testOrderedReport();
88  	}
89  }