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 bytes = processor.createText(dataList, "base.template", "Course Information");
200 Assert.assertNotNull(bytes);
201 printToFile(bytes, "ks-common-ui/target/dataList.txt");
202 Assert.assertTrue(bytes.length > 0);
203
204 bytes = processor.createRtf(dataList, "base.template", "Course Information");
205 Assert.assertNotNull(bytes);
206 printToFile(bytes, "ks-common-ui/target/dataList.rtf");
207 Assert.assertTrue(bytes.length > 0);
208
209 }
210
211 @Test
212 public void testAnalysisReport() {
213
214 ScreenReportProcessor processor = new JasperScreenReportProcessorImpl();
215
216 List<ExportElement> list = new ArrayList<ExportElement>();
217 ExportElement element0 = new ExportElement();
218 element0.setFieldValue("Analysis");
219 list.add(element0);
220
221 ExportElement element1 = new ExportElement();
222 element1.setFieldValue("Analysis 1");
223 list.add(element1);
224
225 ExportElement element2 = new ExportElement();
226 list.add(element2);
227
228
229 ExportElement element3 = new ExportElement();
230 element3.setFieldValue("Analysis 2");
231
232 List<ExportElement> sublist = new ArrayList<ExportElement>();
233
234 ExportElement element3a = new ExportElement();
235
236 List<ExportElement> sublista = new ArrayList<ExportElement>();
237
238 ExportElement element3b = new ExportElement();
239
240 List<ExportElement> sublistb = new ArrayList<ExportElement>();
241
242 ExportElement subelement1 = new ExportElement();
243 subelement1.setFieldValue("Sub Analysis 1");
244 sublistb.add(subelement1);
245
246 ExportElement subelement2 = new ExportElement();
247 subelement2.setFieldValue("Sub Analysis 2");
248 sublistb.add(subelement2);
249
250 element3b.setSubset(sublistb);
251 sublista.add(element3b);
252
253 element3a.setSubset(sublista);
254 sublist.add(element3a);
255
256 element3.setSubset(sublist);
257 list.add(element3);
258
259 ExportElement element4 = new ExportElement();
260 element4.setFieldValue("");
261 list.add(element4);
262
263 ExportElement element5 = new ExportElement();
264 element5.setFieldValue("Analysis 3");
265 list.add(element5);
266
267 byte[] bytes = processor.createPdf(list, "analysis.template", "Dependency Analysis");
268 Assert.assertNotNull(bytes);
269 printToFile(bytes, "ks-common-ui/target/analysis.pdf");
270 Assert.assertTrue(bytes.length > 0);
271
272 }
273
274 private void removeEmptyElements(ExportElement parent, List<ExportElement> elements){
275 for (ExportElement element : elements){
276 if (element.isDataEmpty() && element.getSubset() != null && element.getSubset().size() == 1){
277
278 }
279 }
280 }
281
282 private void printToFile(byte[] bytes, String fileName) {
283 OutputStream out;
284 try {
285 out = new FileOutputStream(fileName);
286 out.write(bytes);
287 out.close();
288 } catch (FileNotFoundException e) {
289 e.printStackTrace();
290 } catch (IOException e) {
291 e.printStackTrace();
292 }
293 }
294
295 }