1 21 22 package com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl; 23 24 import java.util.Date ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 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 52 public class ReportSchedulingTest extends TestCase 53 { 54 private ReportJobsPersistenceService reportJobsPersistenceService; 55 private ExecutionContext executionContext; 56 57 public ReportSchedulingTest(String name){ 58 super(name); 59 } 60 61 62 protected void setUp() throws Exception { 63 ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( 64 new String []{"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 { 71 } 72 73 74 public static void main(String [] args) { 75 TestRunner.run(ReportSchedulingTest.class); 76 } 77 78 public void testPersistence() { 79 ReportJobSource source = new ReportJobSource(); 80 source.setReportUnitURI("/test/reportURI"); 81 Map params = new HashMap (); 82 params.put("param1", new Integer (5)); 83 params.put("param2", "value2"); 84 source.setParametersMap(params); 85 86 Date startDate = new Date (); 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 outputFormats = job.getOutputFormats(); 121 assertNotNull(outputFormats); 122 assertEquals(2, outputFormats.size()); 123 assertTrue(outputFormats.contains(new Byte (ReportJob.OUTPUT_FORMAT_PDF))); 124 assertTrue(outputFormats.contains(new Byte (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 (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 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 ccAddresses = mailNotification.getCcAddresses(); 188 assertNotNull(ccAddresses); 189 assertEquals(1, ccAddresses.size()); 190 assertEquals("mary@smith.com", ccAddresses.get(0)); 191 192 List jobs = reportJobsPersistenceService.listJobs(executionContext, "/test/reportURI"); 193 assertNotNull(jobs); 194 assertTrue(1 <= jobs.size()); 195 boolean found = false; 196 for (Iterator it = jobs.iterator(); it.hasNext();) { 197 Object 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 |