1 21 package com.jaspersoft.jasperserver.util.test; 22 23 27 28 import java.io.BufferedInputStream ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.util.ArrayList ; 34 import java.util.Date ; 35 import java.util.HashMap ; 36 import java.util.Map ; 37 import java.util.Properties ; 38 39 import junit.framework.TestCase; 40 import junit.textui.TestRunner; 41 42 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext; 43 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl; 44 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJob; 45 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobIdHolder; 46 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobMailNotification; 47 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobRepositoryDestination; 48 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSimpleTrigger; 49 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSource; 50 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportJobsPersistenceService; 51 import com.jaspersoft.jasperserver.api.metadata.common.domain.ContentResource; 52 import com.jaspersoft.jasperserver.api.metadata.common.domain.DataType; 53 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource; 54 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder; 55 import com.jaspersoft.jasperserver.api.metadata.common.domain.InputControl; 56 import com.jaspersoft.jasperserver.api.metadata.common.domain.Query; 57 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource; 58 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference; 59 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.ContentResourceImpl; 60 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.DataTypeImpl; 61 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.FolderImpl; 62 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 63 import com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryService; 64 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource; 65 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JndiJdbcReportDataSource; 66 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit; 67 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.client.ReportUnitImpl; 68 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.impl.RepoReportUnit; 69 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection; 70 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianXMLADefinition; 71 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit; 72 import com.jaspersoft.jasperserver.api.metadata.olap.domain.XMLAConnection; 73 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService; 74 import com.jaspersoft.jasperserver.api.metadata.user.service.UserAuthorityService; 75 import com.jaspersoft.jasperserver.util.DataSourceBean; 76 import com.jaspersoft.jasperserver.util.ExportResource; 77 import com.jaspersoft.jasperserver.util.FileResourceBean; 78 import com.jaspersoft.jasperserver.util.ImportResource; 79 import com.jaspersoft.jasperserver.util.ReportUnitBean; 80 81 import org.hibernate.Session; 82 import org.hibernate.SessionFactory; 83 import org.springframework.context.ApplicationContext; 84 import org.springframework.context.support.ClassPathXmlApplicationContext; 85 import org.springframework.orm.hibernate3.HibernateTemplate; 86 import org.springframework.orm.hibernate3.SessionFactoryUtils; 87 import org.springframework.orm.hibernate3.SessionHolder; 88 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 89 import org.springframework.transaction.support.TransactionSynchronizationManager; 90 91 public class ExportCoreObjectsTest extends TestCase { 92 93 static final String PATH_SEP = "/"; 94 static final String LABEL = "_label"; 95 static final String DESC = "_description"; 96 static final String DESC_TEXT = " for export-import testing"; 97 98 static final String FOLDER_NAME = "exportTest03"; 99 static final String IMAGE_NAME = "ExportTestImage03"; 100 101 102 private RepositoryService repo; 103 private OlapConnectionService olapConn; 104 private UserAuthorityService userAuthService; 105 private ReportJobsPersistenceService sched; 106 107 108 private Properties jdbcProps; 109 private ExecutionContext context; 110 111 private long mJobId; 112 113 public ExportCoreObjectsTest(String name) { 114 super(name); 115 } 116 117 public static void main(String [] args) { 118 TestRunner.run(ExportCoreObjectsTest.class); 119 } 120 121 public void setUp() throws Exception { 122 System.out.println("ExportOlapTest: setUp"); 123 124 ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext( 125 new String [] {"hibernateConfig.xml", "viewService.xml", "engine.xml", "customDataSources.xml"}); 126 127 repo = (RepositoryService) appContext.getBean("repoService"); 128 129 olapConn = (OlapConnectionService) appContext.getBean("olapConnectionService"); 130 131 sched = (ReportJobsPersistenceService) appContext.getBean("reportJobsPersistenceService"); 132 133 loadJdbcProps(); 134 } 135 136 public void tearDown() { 137 System.out.println("ExportOlapTest: tearDown"); 138 } 139 140 public void SKIP_testImportOnly() { 141 142 144 ImportResource importer = new ImportResource(repo, 145 null, 146 null, 147 context, 148 ExportResource.CATALOG_DIR_NAME, 149 ExportResource.CATALOG_FILE_NAME, 150 "test_new_dir_03"); 151 152 importer.process(); 153 154 } 155 156 157 163 public void skip_testCoreObject_SetOne() { 164 System.out.println("ExportOlapTest: testCoreObject_SetOne()"); 165 166 createResource(); 167 168 ExportResource exporter = new ExportResource(repo, 169 userAuthService, 170 null, 171 context, 172 PATH_SEP + FOLDER_NAME, 173 false, 174 null, 175 null, 176 false, 177 null, 178 ExportResource.CATALOG_DIR_NAME, 179 ExportResource.CATALOG_FILE_NAME); 180 181 exporter.process(); 182 183 184 deleteResource(); 185 186 187 ImportResource importer = new ImportResource(repo, 188 null, 189 null, 190 context, 191 ExportResource.CATALOG_DIR_NAME, 192 ExportResource.CATALOG_FILE_NAME, 193 ""); 194 195 importer.process(); 196 197 } 199 200 static final String FOLDER_NAME_SINGLE = "exportTestSingle_01"; 201 static final String CONTENT_NAME_PDF_SINGLE = "ContentResourcePdf_01"; 202 203 208 public void skip_testContentResourcePdf_Single() { 209 System.out.println("ExportOlapTest: testCoreObject_SetOne()"); 210 211 createResourceSingle(); 212 213 ExportResource exporter = new ExportResource(repo, 214 userAuthService, 215 null, 216 context, 217 PATH_SEP + FOLDER_NAME_SINGLE + PATH_SEP + CONTENT_NAME_PDF_SINGLE, 218 false, 219 null, 220 null, 221 false, 222 null, 223 ExportResource.CATALOG_DIR_NAME, 224 ExportResource.CATALOG_FILE_NAME); 225 226 exporter.process(); 227 228 229 deleteResourceSingle(); 230 231 232 ImportResource importer = new ImportResource(repo, 233 null, 234 null, 235 context, 236 ExportResource.CATALOG_DIR_NAME, 237 ExportResource.CATALOG_FILE_NAME, 238 ""); 239 240 importer.process(); 241 242 deleteResourceSingle(); 243 } 244 245 private void createResourceSingle() { 246 247 Folder folder = new FolderImpl(); 248 folder.setName(FOLDER_NAME_SINGLE); 249 folder.setLabel(FOLDER_NAME_SINGLE + LABEL); 250 folder.setDescription(FOLDER_NAME_SINGLE + DESC); 251 folder.setParentFolder("/"); 252 repo.saveFolder(null, folder); 253 254 ContentResource cont1 = new ContentResourceImpl(); 255 cont1.setName(CONTENT_NAME_PDF_SINGLE); 256 cont1.setLabel(CONTENT_NAME_PDF_SINGLE); 257 cont1.setParentFolder(folder); 258 InputStream file = getClass().getResourceAsStream("/FontsReport.pdf"); 259 cont1.readData(file); 260 cont1.setFileType(ContentResource.TYPE_PDF); 261 repo.saveResource(null, cont1); 262 } 263 264 private void deleteResourceSingle() { 265 266 repo.deleteResource(null, PATH_SEP + FOLDER_NAME_SINGLE + PATH_SEP + CONTENT_NAME_PDF_SINGLE); 267 repo.deleteFolder(null, PATH_SEP + FOLDER_NAME_SINGLE); 268 } 269 270 static final String FOLDER_NAME_HTML_SINGLE = "exportTestSingle_05"; 271 static final String CONTENT_NAME_HTML_SINGLE = "ContentResourceHtml_05"; 272 273 278 public void testContentResourceHtml_Single() { 279 System.out.println("ExportOlapTest: testCoreObject_SetOne()"); 280 281 createResourceSingleHtml(); 282 283 ExportResource exporter = new ExportResource(repo, 284 userAuthService, 285 null, 286 context, 287 PATH_SEP + FOLDER_NAME_HTML_SINGLE + PATH_SEP + CONTENT_NAME_HTML_SINGLE, 288 false, 289 null, 290 null, 291 false, 292 null, 293 ExportResource.CATALOG_DIR_NAME, 294 ExportResource.CATALOG_FILE_NAME); 295 296 exporter.process(); 297 298 299 deleteResourceSingleHtml(); 300 301 302 ImportResource importer = new ImportResource(repo, 303 null, 304 null, 305 context, 306 ExportResource.CATALOG_DIR_NAME, 307 ExportResource.CATALOG_FILE_NAME, 308 ""); 309 310 importer.process(); 311 312 ContentResource cr1 = (ContentResource) repo.getResource(null, 313 PATH_SEP + FOLDER_NAME_HTML_SINGLE + PATH_SEP + CONTENT_NAME_HTML_SINGLE); 314 315 assertNotNull(cr1); 316 assertEquals(cr1.getName(), CONTENT_NAME_HTML_SINGLE); 317 318 319 deleteResourceSingleHtml(); 320 } 321 322 private void createResourceSingleHtml() { 323 324 Folder folder = new FolderImpl(); 325 folder.setName(FOLDER_NAME_HTML_SINGLE); 326 folder.setLabel(FOLDER_NAME_HTML_SINGLE + LABEL); 327 folder.setDescription(FOLDER_NAME_HTML_SINGLE + DESC); 328 folder.setParentFolder("/"); 329 repo.saveFolder(null, folder); 330 331 ContentResource cont1 = new ContentResourceImpl(); 332 cont1.setName(CONTENT_NAME_HTML_SINGLE); 333 cont1.setLabel(CONTENT_NAME_HTML_SINGLE); 334 cont1.setParentFolder(folder); 335 InputStream file = getClass().getResourceAsStream("/FirstJasper.html"); 336 cont1.readData(file); 337 cont1.setFileType(ContentResource.TYPE_HTML); 338 339 342 ArrayList imagesList = new ArrayList (); 343 344 ContentResource image = new ContentResourceImpl(); 345 image.setName("img_0_0_1"); 346 image.setLabel("img_0_0_1"); 347 file = getClass().getResourceAsStream("/FirstJasper.html_files/img_0_0_1"); 348 image.readData(file); 349 image.setFileType(ContentResource.TYPE_HTML); 350 imagesList.add(image); 351 352 image = new ContentResourceImpl(); 353 image.setName("img_0_7_125"); 354 image.setLabel("img_0_7_125"); 355 file = getClass().getResourceAsStream("/FirstJasper.html_files/img_0_7_125"); 356 image.readData(file); 357 image.setFileType(ContentResource.TYPE_HTML); 358 imagesList.add(image); 359 360 image = new ContentResourceImpl(); 361 image.setName("px"); 362 image.setLabel("px"); 363 file = getClass().getResourceAsStream("/FirstJasper.html_files/px"); 364 image.readData(file); 365 image.setFileType(ContentResource.TYPE_HTML); 366 imagesList.add(image); 367 368 cont1.setResources(imagesList); 369 370 repo.saveResource(null, cont1); 371 } 372 373 private void deleteResourceSingleHtml() { 374 375 repo.deleteResource(null, PATH_SEP + FOLDER_NAME_HTML_SINGLE + PATH_SEP + CONTENT_NAME_HTML_SINGLE); 376 repo.deleteFolder(null, PATH_SEP + FOLDER_NAME_HTML_SINGLE); 377 } 378 379 380 private void createResource() { 381 Folder folder = new FolderImpl(); 382 folder.setName(FOLDER_NAME); 383 folder.setLabel(FOLDER_NAME + LABEL); 384 folder.setDescription(FOLDER_NAME + DESC); 385 folder.setParentFolder("/"); 386 repo.saveFolder(null, folder); 387 388 391 FileResource img1 = (FileResource) repo.newResource(null, FileResource.class); 392 img1.setFileType(FileResource.TYPE_IMAGE); 393 img1.readData(getClass().getResourceAsStream("/images/jasperreports.gif")); 394 img1.setName(IMAGE_NAME); 395 img1.setLabel(IMAGE_NAME + LABEL); 396 img1.setDescription(IMAGE_NAME + DESC); 397 img1.setParentFolder(folder); 398 repo.saveResource(null, img1); 399 400 401 404 ContentResource cont1 = new ContentResourceImpl(); 405 cont1.setName("PdfTestFile"); 406 cont1.setLabel("Pdf test file"); 407 cont1.setParentFolder(folder); 408 InputStream file = getClass().getResourceAsStream("/FontsReport.pdf"); 409 cont1.readData(file); 410 cont1.setFileType(ContentResource.TYPE_PDF); 411 repo.saveResource(null, cont1); 412 413 ContentResource htmlFile = new ContentResourceImpl(); 414 htmlFile.setName("HtmlTestFile"); 415 htmlFile.setLabel("HTML test file with images"); 416 htmlFile.setParentFolder(folder); 417 file = getClass().getResourceAsStream("/FirstJasper.html"); 418 htmlFile.readData(file); 419 htmlFile.setFileType(ContentResource.TYPE_HTML); 420 421 repo.saveResource(null, htmlFile); 422 } 423 424 private void deleteResource() { 425 426 repo.deleteFolder(null, PATH_SEP + FOLDER_NAME); 427 } 428 429 430 434 public void testContentResourceJobSchedule_Single() { 435 System.out.println("ExportOlapTest: testContentResourceJobSchedule_Single()"); 436 437 createSingleJob(); 438 439 String [] reportJobUnitNames = {RU_URI}; 440 441 ExportResource exporter = new ExportResource(repo, 442 userAuthService, 443 sched, 444 context, 445 RU_URI, 446 false, 447 null, 448 null, 449 true, 450 reportJobUnitNames, 451 ExportResource.CATALOG_DIR_NAME, 452 ExportResource.CATALOG_FILE_NAME); 453 454 exporter.process(); 455 456 457 deleteSingleJob(); 458 459 460 ImportResource importer = new ImportResource(repo, 461 null, 462 sched, 463 context, 464 ExportResource.CATALOG_DIR_NAME, 465 ExportResource.CATALOG_FILE_NAME, 466 ""); 467 468 importer.process(); 469 470 deleteSingleJob(); 471 } 472 473 474 static final String RU_FOLDER = "exportTestJobSingle_BBB"; 475 static final String RU_NAME = "TestRUForJob"; 476 static final String RU_REPORT_NAME = "TestRUReport"; 477 static final String RU_IMAGE_NAME = "TestRUImage"; 478 static final String RU_JNDI_NAME = "TestJNDIDS"; 479 static final String RU_FOLDER_PATH = "/" + RU_FOLDER; 480 static final String RU_URI = RU_FOLDER_PATH + PATH_SEP + RU_NAME; 481 static final String RU_JNDI_URI = RU_FOLDER_PATH + PATH_SEP + RU_JNDI_NAME; 482 static final String DEST_FOLDER = "scheduled"; 483 484 private void createSingleJob() { 485 486 489 Folder folder = new FolderImpl(); 490 folder.setName(RU_FOLDER); 491 folder.setLabel(RU_FOLDER + LABEL); 492 folder.setDescription(RU_FOLDER + DESC); 493 folder.setParentFolder("/"); 494 repo.saveFolder(null, folder); 495 496 499 Folder destFolder = new FolderImpl(); 500 destFolder.setName(DEST_FOLDER); 501 destFolder.setLabel(DEST_FOLDER + LABEL); 502 destFolder.setDescription(DEST_FOLDER + DESC); 503 destFolder.setParentFolder(folder); 504 repo.saveFolder(null, destFolder); 505 506 507 510 ReportUnit unit = (ReportUnit) repo.newResource(null, ReportUnit.class); 511 unit.setName(RU_NAME); 512 unit.setLabel(RU_NAME + LABEL); 513 unit.setDescription("Report Unit" + DESC_TEXT); 514 unit.setParentFolder(folder); 515 516 JndiJdbcReportDataSource datasource; 518 if (repo.getResource(context, RU_JNDI_URI)== null) { 519 datasource = (JndiJdbcReportDataSource) repo.newResource(null, JndiJdbcReportDataSource.class); 520 datasource.setName(RU_JNDI_NAME); 521 datasource.setLabel(RU_JNDI_NAME + LABEL); 522 datasource.setDescription("jndi data source" + DESC_TEXT); 523 datasource.setJndiName(jdbcProps.getProperty("test.jndi")); 524 datasource.setParentFolder(folder); 525 repo.saveResource(null, datasource); 526 } else { 527 datasource = (JndiJdbcReportDataSource) repo.getResource(context, RU_JNDI_URI); 528 } 529 unit.setDataSourceReference(RU_JNDI_URI); 530 531 FileResource reportRes = (FileResource) repo.newResource(null, FileResource.class); 533 reportRes.setFileType(FileResource.TYPE_JRXML); 534 reportRes.setName(RU_REPORT_NAME); 535 reportRes.setLabel(RU_REPORT_NAME + LABEL); 536 reportRes.setDescription("Report" + DESC_TEXT); 537 InputStream jrxml = getClass().getResourceAsStream("/reports/jasper/TestReportExportImport01.jrxml"); 538 reportRes.readData(jrxml); 539 unit.setMainReport(reportRes); 540 541 FileResource image1 = (FileResource) repo.newResource(null, FileResource.class); 543 image1.setFileType(FileResource.TYPE_IMAGE); 544 image1.readData(getClass().getResourceAsStream("/images/jasperreports.gif")); 545 image1.setName(RU_IMAGE_NAME); 546 image1.setLabel(RU_IMAGE_NAME + LABEL); 547 image1.setDescription("Image" + DESC_TEXT); 548 unit.addResource(image1); 549 550 repo.saveResource(null, unit); 551 552 555 556 ReportJobSource source = new ReportJobSource(); 557 source.setReportUnitURI(RU_URI); 558 Map params = new HashMap (); 559 params.put("param1", new Integer (5)); 560 params.put("param2", "value2"); 561 source.setParametersMap(params); 562 563 Date startDate = new Date (); 564 ReportJobSimpleTrigger trigger = new ReportJobSimpleTrigger(); 565 trigger.setStartDate(startDate); 566 trigger.setOccurrenceCount(20); 567 trigger.setRecurrenceInterval(10); 568 trigger.setRecurrenceIntervalUnit(ReportJobSimpleTrigger.INTERVAL_DAY); 569 570 ReportJobRepositoryDestination repositoryDestination = new ReportJobRepositoryDestination(); 571 repositoryDestination.setFolderURI(PATH_SEP + RU_FOLDER + PATH_SEP + DEST_FOLDER); 572 573 ReportJobMailNotification mailNotification = new ReportJobMailNotification(); 574 mailNotification.addTo("john@smith.com"); 575 mailNotification.setSubject("Scheduled report"); 576 mailNotification.setMessageText("Executed report"); 577 578 ReportJob job = new ReportJob(); 579 job.setLabel("JobFor_" + RU_NAME + "_Label"); 580 job.setDescription("Description"); 581 job.setSource(source); 582 job.setTrigger(trigger); 583 job.setBaseOutputFilename("foo"); 584 job.addOutputFormat(ReportJob.OUTPUT_FORMAT_PDF); 585 job.addOutputFormat(ReportJob.OUTPUT_FORMAT_RTF); 586 job.setContentRepositoryDestination(repositoryDestination); 587 job.setMailNotification(mailNotification); 588 589 job = sched.saveJob(null, job); 590 mJobId = job.getId(); 591 } 592 593 private void deleteSingleJob() { 594 595 repo.deleteResource(null, RU_URI); 596 repo.deleteFolder(null, PATH_SEP + RU_FOLDER); 597 598 sched.deleteJob(null, new ReportJobIdHolder(mJobId)); 599 } 600 601 protected Properties loadJdbcProps() throws IOException , FileNotFoundException { 602 jdbcProps = new Properties (); 603 String jdbcPropFile = System.getProperty("test.hibernate.jdbc.properties"); 604 BufferedInputStream is = new BufferedInputStream (new FileInputStream (jdbcPropFile)); 605 jdbcProps.load(is); 606 is.close(); 607 return jdbcProps; 608 } 609 610 } 611 | Popular Tags |