KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > jasperreports > service > impl > ReportSchedulingTest


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21
22 package com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl;
23
24 import java.util.Date JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import junit.framework.TestCase;
32 import junit.textui.TestRunner;
33
34 import org.springframework.context.support.ClassPathXmlApplicationContext;
35
36 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
37 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
38 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJob;
39 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobIdHolder;
40 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobMailNotification;
41 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobRepositoryDestination;
42 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSimpleTrigger;
43 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSource;
44 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSummary;
45 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportJobsPersistenceService;
46
47
48 /**
49  * @author Lucian Chirita (lucianc@users.sourceforge.net)
50  * @version $Id: ReportSchedulingTest.java 3784 2006-06-21 17:52:11Z lucian $
51  */

52 public class ReportSchedulingTest extends TestCase
53 {
54     private ReportJobsPersistenceService reportJobsPersistenceService;
55     private ExecutionContext executionContext;
56     
57     public ReportSchedulingTest(String JavaDoc name){
58         super(name);
59     }
60
61
62     protected void setUp() throws Exception JavaDoc {
63         ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
64                 new String JavaDoc[]{"hibernateConfig.xml", "viewService.xml", "engine.xml", "customDataSources.xml"});
65
66         reportJobsPersistenceService = (ReportJobsPersistenceService) appContext.getBean("reportJobsPersistenceService");
67         executionContext = new ExecutionContextImpl();
68     }
69
70     protected void tearDown() throws Exception JavaDoc {
71     }
72
73
74     public static void main(String JavaDoc[] args) {
75         TestRunner.run(ReportSchedulingTest.class);
76     }
77
78     public void testPersistence() {
79         ReportJobSource source = new ReportJobSource();
80         source.setReportUnitURI("/test/reportURI");
81         Map JavaDoc params = new HashMap JavaDoc();
82         params.put("param1", new Integer JavaDoc(5));
83         params.put("param2", "value2");
84         source.setParametersMap(params);
85         
86         Date JavaDoc startDate = new Date JavaDoc();
87         ReportJobSimpleTrigger trigger = new ReportJobSimpleTrigger();
88         trigger.setStartDate(startDate);
89         trigger.setOccurrenceCount(20);
90         trigger.setRecurrenceInterval(10);
91         trigger.setRecurrenceIntervalUnit(ReportJobSimpleTrigger.INTERVAL_DAY);
92         
93         ReportJobRepositoryDestination repositoryDestination = new ReportJobRepositoryDestination();
94         repositoryDestination.setFolderURI("/test/scheduled");
95
96         ReportJobMailNotification mailNotification = new ReportJobMailNotification();
97         mailNotification.addTo("john@smith.com");
98         mailNotification.setSubject("Scheduled report");
99         mailNotification.setMessageText("Executed report");
100         
101         ReportJob job = new ReportJob();
102         job.setLabel("foo");
103         job.setDescription("bar");
104         job.setSource(source);
105         job.setTrigger(trigger);
106         job.setBaseOutputFilename("foo");
107         job.addOutputFormat(ReportJob.OUTPUT_FORMAT_PDF);
108         job.addOutputFormat(ReportJob.OUTPUT_FORMAT_RTF);
109         job.setContentRepositoryDestination(repositoryDestination);
110         job.setMailNotification(mailNotification);
111         
112         job = reportJobsPersistenceService.saveJob(executionContext, job);
113         assertNotNull(job);
114         long jobId = job.getId();
115         boolean deleted = true;
116         try {
117             job = reportJobsPersistenceService.loadJob(executionContext, new ReportJobIdHolder(jobId));
118             assertNotNull(job);
119             assertEquals("foo", job.getLabel());
120             Set JavaDoc outputFormats = job.getOutputFormats();
121             assertNotNull(outputFormats);
122             assertEquals(2, outputFormats.size());
123             assertTrue(outputFormats.contains(new Byte JavaDoc(ReportJob.OUTPUT_FORMAT_PDF)));
124             assertTrue(outputFormats.contains(new Byte JavaDoc(ReportJob.OUTPUT_FORMAT_RTF)));
125             
126             source = job.getSource();
127             assertNotNull(source);
128             assertEquals("/test/reportURI", source.getReportUnitURI());
129             params = source.getParametersMap();
130             assertNotNull(params);
131             assertEquals(2, params.size());
132             assertTrue(params.containsKey("param1"));
133             assertEquals(new Integer JavaDoc(5), params.get("param1"));
134             assertTrue(params.containsKey("param2"));
135             assertEquals("value2", params.get("param2"));
136             
137             assertNotNull(job.getTrigger());
138             assertTrue(job.getTrigger() instanceof ReportJobSimpleTrigger);
139             trigger = (ReportJobSimpleTrigger) job.getTrigger();
140             assertEquals(20, trigger.getOccurrenceCount());
141             assertNotNull(trigger.getRecurrenceIntervalUnit());
142             assertEquals(ReportJobSimpleTrigger.INTERVAL_DAY, trigger.getRecurrenceIntervalUnit().byteValue());
143             
144             repositoryDestination = job.getContentRepositoryDestination();
145             assertNotNull(repositoryDestination);
146             assertEquals("/test/scheduled", repositoryDestination.getFolderURI());
147             
148             mailNotification = job.getMailNotification();
149             assertNotNull(mailNotification);
150             assertEquals("Scheduled report", mailNotification.getSubject());
151             List JavaDoc toAddresses = mailNotification.getToAddresses();
152             assertNotNull(toAddresses);
153             assertEquals(1, toAddresses.size());
154             assertEquals("john@smith.com", toAddresses.get(0));
155             
156             long origJobId = job.getId();
157             int origJobVersion = job.getVersion();
158             long origTriggerId = trigger.getId();
159             int origTriggerVersion = trigger.getVersion();
160             long origMailId = mailNotification.getId();
161             int origMailVersion = mailNotification.getVersion();
162             job.setDescription("updated");
163             mailNotification.setSubject("updated subject");
164             mailNotification.addTo("joan@smith.com");
165             mailNotification.addCc("mary@smith.com");
166             reportJobsPersistenceService.updateJob(executionContext, job);
167             job = reportJobsPersistenceService.loadJob(executionContext, new ReportJobIdHolder(jobId));
168             assertNotNull(job);
169             assertEquals("foo", job.getLabel());
170             assertEquals("updated", job.getDescription());
171             assertEquals(origJobId, job.getId());
172             assertEquals(origJobVersion + 1, job.getVersion());
173             assertNotNull(job.getTrigger());
174             assertTrue(job.getTrigger() instanceof ReportJobSimpleTrigger);
175             trigger = (ReportJobSimpleTrigger) job.getTrigger();
176             assertEquals(origTriggerId, trigger.getId());
177             assertEquals(origTriggerVersion, trigger.getVersion());
178             mailNotification = job.getMailNotification();
179             assertNotNull(mailNotification);
180             assertEquals(origMailId, mailNotification.getId());
181             assertEquals(origMailVersion + 1, mailNotification.getVersion());
182             assertEquals("updated subject", mailNotification.getSubject());
183             toAddresses = mailNotification.getToAddresses();
184             assertEquals(2, toAddresses.size());
185             assertEquals("john@smith.com", toAddresses.get(0));
186             assertEquals("joan@smith.com", toAddresses.get(1));
187             List JavaDoc ccAddresses = mailNotification.getCcAddresses();
188             assertNotNull(ccAddresses);
189             assertEquals(1, ccAddresses.size());
190             assertEquals("mary@smith.com", ccAddresses.get(0));
191
192             List JavaDoc jobs = reportJobsPersistenceService.listJobs(executionContext, "/test/reportURI");
193             assertNotNull(jobs);
194             assertTrue(1 <= jobs.size());
195             boolean found = false;
196             for (Iterator JavaDoc it = jobs.iterator(); it.hasNext();) {
197                 Object JavaDoc element = it.next();
198                 assertTrue(element instanceof ReportJobSummary);
199                 ReportJobSummary summary = (ReportJobSummary) element;
200                 if (summary.getId() == jobId) {
201                     found = true;
202                     assertEquals("foo", summary.getLabel());
203                     break;
204                 }
205             }
206             assertTrue(found);
207             
208             reportJobsPersistenceService.deleteJob(executionContext, new ReportJobIdHolder(jobId));
209             deleted = true;
210             job = reportJobsPersistenceService.loadJob(executionContext, new ReportJobIdHolder(jobId));
211             assertNull(job);
212         } finally {
213             if (!deleted) {
214                 reportJobsPersistenceService.deleteJob(executionContext, new ReportJobIdHolder(jobId));
215             }
216         }
217     }
218 }
219
Popular Tags