1
2
3
4
5
6
7
8
9 package org.kuali.student.common.ui.server.screenreport;
10
11 import java.io.FileNotFoundException;
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.kuali.student.common.assembly.data.Data;
22 import org.kuali.student.common.ui.client.util.ExportElement;
23 import org.kuali.student.common.ui.server.screenreport.jasper.JasperScreenReportProcessorImpl;
24
25 public class TestScreenReport {
26
27 private Data dataMap;
28
29 private List<ExportElement> dataList;
30
31 @Before
32 public void setup() {
33
34 dataMap = new Data();
35
36 dataMap.set("key1", "value1");
37 Data subData = new Data();
38 subData.set("firstName", "Jon");
39 subData.set("ssn", "123456789");
40 dataMap.set("key2", subData);
41
42 Data subData2 = new Data();
43 subData2.set("firstName", "who");
44 subData2.set("ssn", "123456789");
45
46 Data subData2a = new Data();
47 subData2a.set("third", "value");
48 subData2.set("level", subData2a);
49 dataMap.set("key3", subData2);
50
51 Data subData3 = new Data();
52 subData3.set("firstName", "Me");
53 subData3.set("ssn", "123456789");
54 dataMap.set("key4", subData3);
55
56 dataMap.set("key5", "value5");
57
58 String value = null;
59 dataMap.set("key6", value);
60
61 dataMap.set("key7", "This is a very long description. The impact of public policies on the black community and the role of the policy process in affecting the social, economic and political well-being of minorities. Particular attention given to the post-1960 to present era.");
62
63
64 dataMap.set("key8", "simple value");
65 dataMap.set("key9", "simple value");
66 dataMap.set("key10", "simple value");
67 dataMap.set("key11", "simple value");
68 dataMap.set("key12", "simple value");
69 dataMap.set("key13", "simple value");
70 dataMap.set("key14", "simple value");
71 dataMap.set("key15", "simple value");
72 dataMap.set("key16", "simple value");
73 dataMap.set("key17", "simple value");
74 dataMap.set("key18", "simple value");
75 dataMap.set("key19", "simple value");
76 dataMap.set("key20", "simple value");
77
78 dataList = new ArrayList<ExportElement>();
79
80 ExportElement element0 = new ExportElement();
81 element0.setFieldLabel("My label");
82 element0.setFieldValue("Proposal");
83 element0.setFieldValue2("Original Course");
84 dataList.add(element0);
85
86 ExportElement element1 = new ExportElement();
87 element1.setFieldLabel("First label");
88 element1.setFieldValue("First value");
89 dataList.add(element1);
90
91 ExportElement element2 = new ExportElement();
92 element2.setSectionName("Section 1");
93 element2.setFieldLabel("Second label");
94 element2.setFieldValue("Second value");
95 element2.setMandatory(true);
96 dataList.add(element2);
97
98 ExportElement element3 = new ExportElement();
99 element3.setSectionName("Section 1");
100 element3.setFieldLabel("Third label");
101 element3.setFieldValue("Third value");
102 element3.setFieldValue2("Third value O");
103 dataList.add(element3);
104
105 List<ExportElement> subElements = new ArrayList<ExportElement>();
106 ExportElement subElement1 = new ExportElement();
107 subElement1.setSectionName("Subsection");
108 subElement1.setFieldLabel("First sublabel");
109 subElement1.setFieldValue("First subvalue");
110 subElement1.setFieldValue2("First subvalue O");
111 subElements.add(subElement1);
112
113 ExportElement subElement2 = new ExportElement();
114 subElement1.setSectionName("Subsection");
115 subElement2.setFieldLabel("Second sublabel");
116 subElement2.setFieldValue("Second subvalue");
117 subElement2.setFieldValue2("Second subvalue");
118 subElements.add(subElement2);
119
120 List<ExportElement> subsubElements = new ArrayList<ExportElement>();
121 ExportElement subsubElement1 = new ExportElement();
122 subsubElement1.setSectionName("Subsection");
123 subsubElement1.setFieldLabel("First sublabel");
124 subsubElement1.setFieldValue("First subvalue");
125 subsubElement1.setFieldValue2("First subvalue O");
126 subsubElements.add(subsubElement1);
127
128 ExportElement subelement3 = new ExportElement();
129 subelement3.setSectionName("Subsection");
130 subelement3.setFieldLabel("Subsubset label");
131 subelement3.setSubset(subsubElements);
132 subElements.add(subelement3);
133
134 ExportElement element4 = new ExportElement();
135 element4.setSectionName("Section 1");
136 element4.setFieldLabel("Subset label");
137 element4.setSubset(subElements);
138 dataList.add(element4);
139
140 dataList.add(new ExportElement());
141
142 ExportElement element5 = new ExportElement();
143 element5.setSectionName("Section 2");
144 element5.setFieldLabel("5th label");
145 element5.setFieldValue("5th value");
146 element5.setFieldValue2("5th value");
147 dataList.add(element5);
148
149 ExportElement element6 = new ExportElement();
150 element6.setSectionName("Section 2");
151 element6.setFieldLabel("6th label");
152 element6.setFieldValue("6th value");
153 element6.setFieldValue2("6th value");
154 element6.setMandatory(true);
155 dataList.add(element6);
156
157 }
158
159 @Test
160 public void testScreenReport() {
161
162 ScreenReportProcessor processor = new JasperScreenReportProcessorImpl();
163
164 byte[] bytes = processor.createPdf(dataMap, "base.template", "Course Information");
165 Assert.assertNotNull(bytes);
166 printToFile(bytes, "ks-common-ui/target/dataMap.pdf");
167 Assert.assertTrue(bytes.length > 0);
168
169 bytes = processor.createDoc(dataMap, "base.template", "Course Information");
170 Assert.assertNotNull(bytes);
171 printToFile(bytes, "ks-common-ui/target/dataMap.doc");
172 Assert.assertTrue(bytes.length > 0);
173
174 bytes = processor.createXls(dataMap, "base.template", "Course Information");
175 Assert.assertNotNull(bytes);
176 printToFile(bytes, "ks-common-ui/target/dataMap.xls");
177 Assert.assertTrue(bytes.length > 0);
178
179 bytes = processor.createPdf(dataList, "base.template", "Course Information");
180 Assert.assertNotNull(bytes);
181 printToFile(bytes, "ks-common-ui/target/dataList.pdf");
182 Assert.assertTrue(bytes.length > 0);
183
184 bytes = processor.createPdf(dataList, "proposal.template", "Course Information");
185 Assert.assertNotNull(bytes);
186 printToFile(bytes, "ks-common-ui/target/dataList2.pdf");
187 Assert.assertTrue(bytes.length > 0);
188
189 bytes = processor.createDoc(dataList, "base.template", "Course Information");
190 Assert.assertNotNull(bytes);
191 printToFile(bytes, "ks-common-ui/target/dataList.doc");
192 Assert.assertTrue(bytes.length > 0);
193
194 bytes = processor.createXls(dataList, "base.template", "Course Information");
195 Assert.assertNotNull(bytes);
196 printToFile(bytes, "ks-common-ui/target/dataList.xls");
197 Assert.assertTrue(bytes.length > 0);
198
199 }
200
201 @Test
202 public void testAnalysisReport() {
203
204 ScreenReportProcessor processor = new JasperScreenReportProcessorImpl();
205
206 List<ExportElement> list = new ArrayList<ExportElement>();
207 ExportElement element0 = new ExportElement();
208 element0.setFieldValue("Analysis");
209 list.add(element0);
210
211 ExportElement element1 = new ExportElement();
212 element1.setFieldValue("Analysis 1");
213 list.add(element1);
214
215 ExportElement element2 = new ExportElement();
216 list.add(element2);
217
218
219 ExportElement element3 = new ExportElement();
220 element3.setFieldValue("Analysis 2");
221
222 List<ExportElement> sublist = new ArrayList<ExportElement>();
223
224 ExportElement element3a = new ExportElement();
225
226 List<ExportElement> sublista = new ArrayList<ExportElement>();
227
228 ExportElement element3b = new ExportElement();
229
230 List<ExportElement> sublistb = new ArrayList<ExportElement>();
231
232 ExportElement subelement1 = new ExportElement();
233 subelement1.setFieldValue("Sub Analysis 1");
234 sublistb.add(subelement1);
235
236 ExportElement subelement2 = new ExportElement();
237 subelement2.setFieldValue("Sub Analysis 2");
238 sublistb.add(subelement2);
239
240 element3b.setSubset(sublistb);
241 sublista.add(element3b);
242
243 element3a.setSubset(sublista);
244 sublist.add(element3a);
245
246 element3.setSubset(sublist);
247 list.add(element3);
248
249 ExportElement element4 = new ExportElement();
250 element4.setFieldValue("");
251 list.add(element4);
252
253 ExportElement element5 = new ExportElement();
254 element5.setFieldValue("Analysis 3");
255 list.add(element5);
256
257 byte[] bytes = processor.createPdf(list, "analysis.template", "Dependency Analysis");
258 Assert.assertNotNull(bytes);
259 printToFile(bytes, "ks-common-ui/target/analysis.pdf");
260 Assert.assertTrue(bytes.length > 0);
261
262 }
263
264 private void removeEmptyElements(ExportElement parent, List<ExportElement> elements){
265 for (ExportElement element : elements){
266 if (element.isDataEmpty() && element.getSubset() != null && element.getSubset().size() == 1){
267
268 }
269 }
270 }
271
272 private void printToFile(byte[] bytes, String fileName) {
273 OutputStream out;
274 try {
275 out = new FileOutputStream(fileName);
276 out.write(bytes);
277 out.close();
278 } catch (FileNotFoundException e) {
279 e.printStackTrace();
280 } catch (IOException e) {
281 e.printStackTrace();
282 }
283 }
284
285 }