KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > util > test > ExportTest


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 package com.jaspersoft.jasperserver.util.test;
22
23 /**
24  * @author tkavanagh
25  * @version $id $
26  */

27
28 import java.io.BufferedInputStream JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import junit.framework.TestCase;
36 import junit.textui.TestRunner;
37
38 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
39 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
40 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportJobsPersistenceService;
41 import com.jaspersoft.jasperserver.api.metadata.common.domain.DataType;
42 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource;
43 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder;
44 import com.jaspersoft.jasperserver.api.metadata.common.domain.InputControl;
45 import com.jaspersoft.jasperserver.api.metadata.common.domain.Query;
46 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
47 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference;
48 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.DataTypeImpl;
49 import com.jaspersoft.jasperserver.api.metadata.common.domain.client.FolderImpl;
50 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
51 import com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.HibernateRepositoryService;
52 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JdbcReportDataSource;
53 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.JndiJdbcReportDataSource;
54 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit;
55 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.client.ReportUnitImpl;
56 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.impl.RepoReportUnit;
57 import com.jaspersoft.jasperserver.util.DataSourceBean;
58 import com.jaspersoft.jasperserver.util.ExportResource;
59 import com.jaspersoft.jasperserver.util.FileResourceBean;
60 import com.jaspersoft.jasperserver.util.ImportResource;
61 import com.jaspersoft.jasperserver.util.ReportUnitBean;
62
63 import org.hibernate.Session;
64 import org.hibernate.SessionFactory;
65 import org.springframework.context.ApplicationContext;
66 import org.springframework.context.support.ClassPathXmlApplicationContext;
67 import org.springframework.orm.hibernate3.HibernateTemplate;
68 import org.springframework.orm.hibernate3.SessionFactoryUtils;
69 import org.springframework.orm.hibernate3.SessionHolder;
70 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
71 import org.springframework.transaction.support.TransactionSynchronizationManager;
72
73 public class ExportTest extends TestCase {
74
75     static final String JavaDoc PATH_SEP = "/";
76     static final String JavaDoc LABEL = "_label";
77     static final String JavaDoc DESC = "_description";
78     static final String JavaDoc DESC_TEXT = " for export-import testing";
79         
80     private RepositoryService repo;
81     
82     private Properties JavaDoc jdbcProps;
83     private ExecutionContext context;
84     
85     /*
86     SessionFactory sf;
87     HibernateTemplate template;
88     */

89     
90     public ExportTest(String JavaDoc name) {
91         super(name);
92     }
93     
94     public static void main(String JavaDoc[] args) {
95         TestRunner.run(ExportTest.class);
96     }
97     
98     public void setUp() throws Exception JavaDoc {
99         System.out.println("ExportTest: setUp");
100         
101         ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
102                 new String JavaDoc[] {"hibernateConfig.xml", "viewService.xml"});
103         
104         repo = (RepositoryService) appContext.getBean("repoService");
105         
106         this.context = new ExecutionContextImpl();
107         
108         loadJdbcProps();
109     }
110     
111     public void tearDown() {
112         System.out.println("ExportTest: tearDown");
113     }
114     
115     
116     public void testFolderExport() {
117         System.out.println("ExportTest: top of testFolderExport()");
118
119         createFolderResources();
120         
121         // junit asserts
122
FileResource fr1 = (FileResource) repo.getResource(null,
123                 PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_IMAGE_NAME1);
124         assertNotNull(fr1);
125         ReportUnit ru1 = (ReportUnit) repo.getResource(null,
126                 PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_RU_NAME);
127         assertNotNull(ru1);
128         
129         // export
130
ExportResource exporter = new ExportResource(repo,
131                 null,
132                 null,
133                 context,
134                 PATH_SEP + FOLDER_FOLDER_NAME1,
135                 false,
136                 null,
137                 null,
138                 false,
139                 null,
140                 ExportResource.CATALOG_DIR_NAME,
141                 ExportResource.CATALOG_FILE_NAME);
142         
143         exporter.process();
144
145         deleteFolderResources();
146         
147         
148         // junit asserts
149
FileResource fr_null = (FileResource) repo.getResource(null,
150                 PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_IMAGE_NAME1);
151         assertNull(fr_null);
152         
153         ReportUnit ru_null = (ReportUnit) repo.getResource(null,
154                 PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_RU_NAME);
155         assertNull(ru_null);
156         
157         
158         ImportResource importer = new ImportResource(repo,
159                 null,
160                 null,
161                 context,
162                 ExportResource.CATALOG_DIR_NAME,
163                 ExportResource.CATALOG_FILE_NAME,
164                 null);
165         
166         importer.process();
167
168         // junit asserts
169
FileResource fr2 = (FileResource) repo.getResource(null,
170                 PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_IMAGE_NAME1);
171         assertNotNull(fr2);
172         
173         ReportUnit ru2 = (ReportUnit) repo.getResource(null,
174                 PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_RU_NAME);
175         assertNotNull(ru2);
176         
177         FileResource fr3 = (FileResource) repo.getResource(null,
178                 PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_FOLDER_NAME3 + PATH_SEP + FOLDER_IMAGE_NAME3);
179         assertNotNull(fr3);
180         
181         
182         deleteFolderResources();
183     }
184     
185     private void createFolderResources() {
186         System.out.println("ExportTest: top of createFolderResources()");
187         
188         // make a folder hierarchy
189
Folder folder = new FolderImpl();
190         folder.setName(FOLDER_FOLDER_NAME1);
191         folder.setLabel(FOLDER_FOLDER_NAME1 + LABEL);
192         folder.setDescription(FOLDER_FOLDER_NAME1 + DESC);
193         folder.setParentFolder("/");
194         repo.saveFolder(null, folder);
195         
196         Folder folder2 = new FolderImpl();
197         folder2.setName(FOLDER_FOLDER_NAME2);
198         folder2.setLabel(FOLDER_FOLDER_NAME2 + LABEL);
199         folder2.setDescription(FOLDER_FOLDER_NAME2 + DESC);
200         folder2.setParentFolder(folder);
201         repo.saveFolder(null, folder2);
202         
203         Folder folder3 = new FolderImpl();
204         folder3.setName(FOLDER_FOLDER_NAME3);
205         folder3.setLabel(FOLDER_FOLDER_NAME3 + LABEL);
206         folder3.setDescription(FOLDER_FOLDER_NAME3 + DESC);
207         folder3.setParentFolder(folder);
208         repo.saveFolder(null, folder3);
209
210         Folder folder4 = new FolderImpl();
211         folder4.setName(FOLDER_FOLDER_NAME4);
212         folder4.setLabel(FOLDER_FOLDER_NAME4 + LABEL);
213         folder4.setDescription(FOLDER_FOLDER_NAME4 + DESC);
214         folder4.setParentFolder(folder3);
215         repo.saveFolder(null, folder4);
216         
217         
218         // make stand alone image in folder1
219
FileResource image1 = (FileResource) repo.newResource(null, FileResource.class);
220         image1.setFileType(FileResource.TYPE_IMAGE);
221         image1.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
222         image1.setName(FOLDER_IMAGE_NAME1);
223         image1.setLabel(FOLDER_IMAGE_NAME1 + LABEL);
224         image1.setDescription(FOLDER_IMAGE_NAME1 + DESC);
225         image1.setParentFolder(folder);
226         repo.saveResource(null, image1);
227         
228         // make stand alone image in folder3
229
FileResource image3 = (FileResource) repo.newResource(null, FileResource.class);
230         image3.setFileType(FileResource.TYPE_IMAGE);
231         image3.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
232         image3.setName(FOLDER_IMAGE_NAME3);
233         image3.setLabel(FOLDER_IMAGE_NAME3 + LABEL);
234         image3.setDescription(FOLDER_IMAGE_NAME3 + DESC);
235         image3.setParentFolder(folder3);
236         repo.saveResource(null, image3);
237         
238         // make stand alone image in folder4
239
FileResource image4 = (FileResource) repo.newResource(null, FileResource.class);
240         image4.setFileType(FileResource.TYPE_IMAGE);
241         image4.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
242         image4.setName(FOLDER_IMAGE_NAME4);
243         image4.setLabel(FOLDER_IMAGE_NAME4 + LABEL);
244         image4.setDescription(FOLDER_IMAGE_NAME4 + DESC);
245         image4.setParentFolder(folder4);
246         repo.saveResource(null, image4);
247         
248         
249         // make ReportUnit, Report jrxml, and image
250
ReportUnit unit = (ReportUnit) repo.newResource(null, ReportUnit.class);
251         unit.setName(FOLDER_RU_NAME);
252         unit.setLabel(FOLDER_RU_NAME + LABEL);
253         unit.setDescription(FOLDER_RU_NAME + DESC);
254         unit.setParentFolder(folder);
255
256         FileResource reportRes = (FileResource) repo.newResource(null, FileResource.class);
257         reportRes.setFileType(FileResource.TYPE_JRXML);
258         reportRes.setName(FOLDER_JRXML_NAME);
259         reportRes.setLabel(FOLDER_JRXML_NAME + LABEL);
260         reportRes.setDescription(FOLDER_JRXML_NAME + DESC);
261         reportRes.setParentFolder(folder);
262         InputStream JavaDoc jrxml = getClass().getResourceAsStream(
263                 "/reports/jasper/TestReportExportImport01.jrxml");
264         reportRes.readData(jrxml);
265         unit.setMainReport(reportRes);
266
267         unit.setDataSourceReference("/datasources/JServerJNDIDS");
268
269         FileResource res1 = (FileResource) repo.newResource(null, FileResource.class);
270         res1.setFileType(FileResource.TYPE_IMAGE);
271         res1.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
272         res1.setName(FOLDER_IMAGE_RU_NAME);
273         res1.setLabel(FOLDER_IMAGE_RU_NAME + LABEL);
274         res1.setDescription(FOLDER_IMAGE_RU_NAME + DESC);
275         unit.addResource(res1);
276         
277         repo.saveResource(null, unit);
278     }
279     
280     private void deleteFolderResources() {
281         System.out.println("ExportTest: top of deleteFolderResources()");
282         
283         repo.deleteResource(null, PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_IMAGE_NAME1);
284         
285         repo.deleteResource(null, PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP
286                 + FOLDER_FOLDER_NAME3 + PATH_SEP + FOLDER_IMAGE_NAME3);
287         
288         repo.deleteResource(null, PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP
289                 + FOLDER_FOLDER_NAME3 + PATH_SEP + FOLDER_FOLDER_NAME4 + PATH_SEP + FOLDER_IMAGE_NAME4);
290         
291         repo.deleteResource(null, PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_RU_NAME);
292         
293         repo.deleteFolder(null, PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP
294                 + FOLDER_FOLDER_NAME3 + PATH_SEP + FOLDER_FOLDER_NAME4);
295         
296         repo.deleteFolder(null, PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_FOLDER_NAME3);
297         repo.deleteFolder(null, PATH_SEP + FOLDER_FOLDER_NAME1 + PATH_SEP + FOLDER_FOLDER_NAME2);
298         repo.deleteFolder(null, PATH_SEP + FOLDER_FOLDER_NAME1);
299     }
300
301     
302     
303     
304     public void skip_testReportUnitExport() {
305         System.out.println("ExportTest: top of testReportUnitExport()");
306         
307         createReportUnitResources();
308         
309         ExportResource exporter = new ExportResource(repo,
310                 null,
311                 null,
312                 context,
313                 RU_URI,
314                 false,
315                 null,
316                 null,
317                 false,
318                 null,
319                 ExportResource.CATALOG_DIR_NAME,
320                 ExportResource.CATALOG_FILE_NAME);
321         
322         exporter.process();
323         
324         deleteReportUnitResources();
325         
326         ImportResource importer = new ImportResource(repo,
327                 null,
328                 null,
329                 context,
330                 ExportResource.CATALOG_DIR_NAME,
331                 ExportResource.CATALOG_FILE_NAME, PATH_SEP);
332         
333         importer.process();
334                 
335         deleteReportUnitResources();
336          
337         System.out.println("ExportTest: end of testReportUnitExport()");
338     }
339     
340     /*
341      * todo: ### pick one of the tests and have it leave a report
342      * existing so that we can run it from the gui or
343      * via httpunit and ensure that it works.
344      * This would be the production cc test.
345      */

346     
347     public void skip_testLinkedResources() {
348         System.out.println("ExportTest: top of testLinkedResources()");
349         
350         createLinkedResources();
351         
352         ExportResource exporter = new ExportResource(repo,
353                 null,
354                 null,
355                 context,
356                 PATH_SEP + LINK_FOLDER_NAME + PATH_SEP + LINK_RU_NAME,
357                 false,
358                 null,
359                 null,
360                 false,
361                 null,
362                 ExportResource.CATALOG_DIR_NAME,
363                 ExportResource.CATALOG_FILE_NAME);
364         
365         exporter.process();
366         
367         ReportUnit ru1 = (ReportUnit) repo.getResource(null,
368                 PATH_SEP + LINK_FOLDER_NAME + PATH_SEP + LINK_RU_NAME);
369         assertNotNull(ru1);
370         assertEquals(ru1.getName(), LINK_RU_NAME);
371         
372         deleteLinkedResources();
373         
374         ReportUnit ru1_null = (ReportUnit) repo.getResource(null,
375                 PATH_SEP + LINK_FOLDER_NAME + PATH_SEP + LINK_RU_NAME);
376         assertNull(ru1_null);
377         
378         
379         ImportResource importer = new ImportResource(repo,
380                 null,
381                 null,
382                 context,
383                 ExportResource.CATALOG_DIR_NAME,
384                 ExportResource.CATALOG_FILE_NAME, PATH_SEP);
385         
386         importer.process();
387
388         ReportUnit ru2 = (ReportUnit) repo.getResource(null,
389                 PATH_SEP + LINK_FOLDER_NAME + PATH_SEP + LINK_RU_NAME);
390         assertNotNull(ru2);
391         assertEquals(ru2.getName(), LINK_RU_NAME);
392         
393         deleteLinkedResources();
394         
395         ReportUnit ru2_null = (ReportUnit) repo.getResource(null,
396                 PATH_SEP + LINK_FOLDER_NAME + PATH_SEP + LINK_RU_NAME);
397         assertNull(ru2_null);
398     }
399     
400     
401     /*
402      * Add the uri to a arbitrary resource and add a prepend path,
403      * then this test will export that resource to the new location.
404      * Good for testing the export-import of a complex ReportUnit
405      * (for instance).
406      *
407      * Note: If the same test is run twice it will fail the second
408      * time because the target resource will already be there.
409      *
410      */

411     public void skip_testRandomPrependResource() {
412         
413         ExportResource exporter = new ExportResource(repo,
414                 null,
415                 null,
416                 context,
417                 "/reports/samples/SalesByMonth",
418                 false,
419                 null,
420                 null,
421                 false,
422                 null,
423                 ExportResource.CATALOG_DIR_NAME,
424                 ExportResource.CATALOG_FILE_NAME);
425         
426         exporter.process();
427         
428         ImportResource importer = new ImportResource(repo,
429                 null,
430                 null,
431                 context,
432                 ExportResource.CATALOG_DIR_NAME,
433                 ExportResource.CATALOG_FILE_NAME,
434                 "newLocation_07");
435         
436         importer.process();
437     }
438     
439     
440     /*
441      * Create some folders and an image and export. Delete originals
442      * and import using a relative path (prepended path).
443      */

444     public void testPrependPathSimple() {
445         System.out.println("ExportTest: --- top of testPrependPathSimple()");
446         try {
447             
448             createPrependResources();
449             
450             ExportResource exporter = new ExportResource(repo,
451                     null,
452                     null,
453                     context,
454                     PREPEND_IMAGE_URI,
455                     false,
456                     null,
457                     null,
458                     false,
459                     null,
460                     ExportResource.CATALOG_DIR_NAME,
461                     ExportResource.CATALOG_FILE_NAME);
462             
463             exporter.process();
464         
465             // assert not null
466
FileResource img1 = (FileResource) repo.getResource(null, PREPEND_IMAGE_URI);
467             assertNotNull(img1);
468             
469             deletePrependResources1();
470             
471             // assert null
472
FileResource img_null = (FileResource) repo.getResource(null, PREPEND_IMAGE_URI);
473             assertNull(img_null);
474             
475             
476             ImportResource importer = new ImportResource(repo,
477                     null,
478                     null,
479                     context,
480                     ExportResource.CATALOG_DIR_NAME,
481                     ExportResource.CATALOG_FILE_NAME,
482                     NEW_PREPEND_PATH);
483             importer.process();
484         
485             
486             // assert not null
487
FileResource img2 = (FileResource) repo.getResource(null, NEW_PREPEND_PATH + PREPEND_IMAGE_URI);
488             assertNotNull(img2);
489             
490             deletePrependResources2(); // delete resources under new prepend named dir
491

492             
493         } catch (Exception JavaDoc e) {
494             System.out.println("caught exception, e " + e.getMessage());
495             e.printStackTrace();
496         }
497         System.out.println("ExportTest: --- end of testPrependPathSimple");
498     }
499     
500     private void createPrependResources() {
501         System.out.println("ExportTest: top of createPrependResources()");
502         
503         Folder imagesFolder = repo.getFolder(null, "/images");
504         
505         if (imagesFolder == null) {
506             imagesFolder = new FolderImpl();
507             imagesFolder.setName("images");
508             imagesFolder.setLabel("Test");
509             imagesFolder.setParentFolder("/");
510             repo.saveFolder(null, imagesFolder);
511         }
512         
513         Folder folder1 = new FolderImpl();
514         folder1.setName(PREPEND_FOLDER1);
515         folder1.setLabel("Test Images 1");
516         folder1.setDescription("Folder" + DESC_TEXT);
517         folder1.setParentFolder(imagesFolder);
518         repo.saveFolder(null, folder1);
519
520         Folder folder2 = new FolderImpl();
521         folder2.setName(PREPEND_FOLDER2);
522         folder2.setLabel("Test Images 2");
523         folder2.setDescription("Folder" + DESC_TEXT);
524         folder2.setParentFolder(folder1);
525         repo.saveFolder(null, folder2);
526         
527         FileResource image = (FileResource) repo.newResource(null, FileResource.class);
528         image.setFileType(FileResource.TYPE_IMAGE);
529         image.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
530         image.setName(PREPEND_IMAGE_NAME);
531         image.setLabel(PREPEND_IMAGE_NAME + LABEL);
532         image.setDescription("Image" + DESC_TEXT);
533         image.setParentFolder(folder2);
534
535         repo.saveResource(null, image);
536     }
537     
538     /*
539      * delete resources used for simple prepend test
540      * (before prepending new folder name).
541      */

542     private void deletePrependResources1() {
543         System.out.println("ExportTest: top of deletePrependResources1()");
544     
545         repo.deleteResource(null, PREPEND_IMAGE_URI);
546         repo.deleteFolder(null, PREPEND_FOLDER2_PATH);
547         repo.deleteFolder(null, PREPEND_FOLDER1_PATH);
548     }
549
550     
551     /*
552      * delete resources used for simple prepend test
553      * (after prepending new folder).
554      */

555     private void deletePrependResources2() {
556         System.out.println("ExportTest: top of deletePrependResources2()");
557     
558         repo.deleteResource(null, NEW_PREPEND_PATH + PREPEND_IMAGE_URI);
559         repo.deleteFolder(null, NEW_PREPEND_PATH + PREPEND_FOLDER2_PATH);
560         repo.deleteFolder(null, NEW_PREPEND_PATH + PREPEND_FOLDER1_PATH);
561         repo.deleteFolder(null, NEW_PREPEND_PATH);
562     }
563
564     
565     public void skip_testSimpleFileResource() {
566         
567         final String JavaDoc MY_IMAGE_NAME = "myTestImage01E12345678";
568         final String JavaDoc MY_EXTRA_NAME = "extra_45678";
569         
570         Folder imagesFolder = repo.getFolder(null, "/myTestImages");
571         
572         if (imagesFolder == null) {
573             imagesFolder = new FolderImpl();
574             imagesFolder.setName("myTestImages");
575             imagesFolder.setLabel("Test");
576             imagesFolder.setDescription("Test");
577             imagesFolder.setParentFolder("/");
578             repo.saveFolder(null, imagesFolder);
579         }
580         
581         FileResource image = (FileResource) repo.newResource(null, FileResource.class);
582         image.setFileType(FileResource.TYPE_IMAGE);
583         image.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
584         image.setName(MY_IMAGE_NAME);
585         image.setLabel(MY_IMAGE_NAME + LABEL);
586         image.setDescription(MY_IMAGE_NAME + DESC_TEXT);
587         image.setParentFolder(imagesFolder);
588
589         repo.saveResource(null, image);
590         
591         ExportResource exporter = new ExportResource(repo,
592                 null,
593                 null,
594                 context,
595                 "/myTestImages/" + MY_IMAGE_NAME,
596                 false,
597                 null,
598                 null,
599                 false,
600                 null,
601                 ExportResource.CATALOG_DIR_NAME,
602                 ExportResource.CATALOG_FILE_NAME);
603         
604         exporter.process();
605
606 // repo.deleteResource(null, "/myTestImages/" + MY_IMAGE_NAME);
607
// repo.deleteFolder(null, "/myTestImages");
608
//
609
//
610
// ImportResource importer = new ImportResource(repo, context,
611
// ExportResource.CATALOG_DIR_NAME, ExportResource.CATALOG_FILE_NAME);
612
// importer.process();
613
//
614
//
615
// FileResource img = (FileResource) repo.getResource(null, "/extra/myTestImages/" + MY_IMAGE_NAME);
616
// assertNotNull(img);
617
//
618
// if (repo.getResource(null, "/extra/myTestImages/" + MY_IMAGE_NAME) != null) {
619
// repo.deleteResource(null, "/extra/myTestImages/" + MY_IMAGE_NAME);
620
// repo.deleteFolder(null, "/extra/myTestImages");
621
// }
622
}
623     
624     
625     public void skip_testInputControl() {
626         System.out.println("ExportTest: top of testInputControl()");
627         
628         createInputControlResources();
629         
630         ExportResource exporter = new ExportResource(repo,
631                 null,
632                 null,
633                 context,
634                 INPUT_CONTROL_URI,
635                 false,
636                 null,
637                 null,
638                 false,
639                 null,
640                 ExportResource.CATALOG_DIR_NAME,
641                 ExportResource.CATALOG_FILE_NAME);
642         
643         exporter.process();
644
645         deleteInputControlResources();
646         
647         // input control shouldn't exist
648
InputControl ic_null = (InputControl) repo.getResource(null, INPUT_CONTROL_URI);
649         assertNull(ic_null);
650         
651         ImportResource importer = new ImportResource(repo,
652                 null,
653                 null,
654                 context,
655                 ExportResource.CATALOG_DIR_NAME,
656                 ExportResource.CATALOG_FILE_NAME, PATH_SEP);
657         
658         importer.process();
659         
660         // input control should be found
661
InputControl ic_chk = (InputControl) repo.getResource(null, INPUT_CONTROL_URI);
662         assertNotNull(ic_chk);
663         assertEquals(INPUT_CONTROL_NAME, ic_chk.getName());
664         
665         // datatype should be found
666
DataType dt_chk = (DataType) exporter.dereference(context, ic_chk.getDataType());
667         assertNotNull(dt_chk);
668         assertEquals(INPUT_CONTROL_DATATYPE_NAME, dt_chk.getName());
669                 
670         deleteInputControlResources();
671     }
672     
673     
674     
675
676     private void createReportUnitResources() {
677         System.out.println("ExportTest: top of createReportUnitResources()");
678         
679         // create new folder
680
Folder folder = new FolderImpl();
681         folder.setName(RU_FOLDER);
682         folder.setLabel(RU_FOLDER + LABEL);
683         folder.setDescription("Folder" + DESC_TEXT);
684         folder.setParentFolder("/reports");
685         repo.saveFolder(null, folder);
686         
687         // create report unit
688
ReportUnit unit = (ReportUnit) repo.newResource(null, ReportUnit.class);
689         unit.setName(RU_NAME);
690         unit.setLabel(RU_NAME + LABEL);
691         unit.setDescription("Report Unit" + DESC_TEXT);
692         unit.setParentFolder(folder);
693         
694         // setup the datasource
695
JndiJdbcReportDataSource datasource;
696         if (repo.getResource(context, RU_JNDI_URI)== null) {
697             datasource = (JndiJdbcReportDataSource) repo.newResource(null, JndiJdbcReportDataSource.class);
698             datasource.setName(RU_JNDI_NAME);
699             datasource.setLabel(RU_JNDI_NAME + LABEL);
700             datasource.setDescription("jndi data source" + DESC_TEXT);
701             datasource.setJndiName(jdbcProps.getProperty("test.jndi"));
702             datasource.setParentFolder(folder);
703             repo.saveResource(null, datasource);
704         } else {
705             datasource = (JndiJdbcReportDataSource) repo.getResource(context, RU_JNDI_URI);
706         }
707         unit.setDataSourceReference(RU_JNDI_URI);
708
709         // setup the mainReport jrxml
710
FileResource reportRes = (FileResource) repo.newResource(null, FileResource.class);
711         reportRes.setFileType(FileResource.TYPE_JRXML);
712         reportRes.setName(RU_REPORT_NAME);
713         reportRes.setLabel(RU_REPORT_NAME + LABEL);
714         reportRes.setDescription("Report" + DESC_TEXT);
715         InputStream JavaDoc jrxml = getClass().getResourceAsStream("/reports/jasper/TestReportExportImport01.jrxml");
716         reportRes.readData(jrxml);
717         unit.setMainReport(reportRes);
718         
719         // setup an image
720
FileResource image1 = (FileResource) repo.newResource(null, FileResource.class);
721         image1.setFileType(FileResource.TYPE_IMAGE);
722         image1.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
723         image1.setName(RU_IMAGE_NAME);
724         image1.setLabel(RU_IMAGE_NAME + LABEL);
725         image1.setDescription("Image" + DESC_TEXT);
726         unit.addResource(image1);
727         
728         repo.saveResource(null, unit);
729     }
730     
731     private void deleteReportUnitResources() {
732         System.out.println("ExportTest: top of deleteReportUnitResources()");
733         
734         repo.deleteResource(null, RU_URI);
735         repo.deleteResource(null, RU_JNDI_URI);
736         repo.deleteFolder(null, "/reports/" + RU_FOLDER);
737     }
738     
739     private void createInputControlResources() {
740         
741         InputControl ic = (InputControl) repo.newResource(null, InputControl.class);
742         
743         ic.setName(INPUT_CONTROL_NAME);
744         ic.setLabel(INPUT_CONTROL_NAME + "_label");
745         ic.setDescription(INPUT_CONTROL_NAME + " Description");
746         ic.setType(InputControl.TYPE_SINGLE_VALUE);
747         ic.setMandatory(false);
748         ic.setReadOnly(true);
749         
750         DataType dt = new DataTypeImpl();
751         dt.setName(INPUT_CONTROL_DATATYPE_NAME);
752         dt.setLabel(INPUT_CONTROL_DATATYPE_NAME + "_label");
753         dt.setDescription(INPUT_CONTROL_DATATYPE_NAME + " Description");
754         dt.setType(DataType.TYPE_NUMBER);
755         dt.setMaxLength(new Integer JavaDoc(10));
756         dt.setMaxValue(new Integer JavaDoc(1000));
757         dt.setStrictMax(true);
758         ic.setDataType(dt);
759         
760         ic.setParentFolder("/datatypes");
761         
762         repo.saveResource(context, ic);
763     }
764     
765     private void deleteInputControlResources() {
766         
767         repo.deleteResource(null, INPUT_CONTROL_URI);
768     }
769     
770     private void createLinkedResources() {
771         System.out.println("ExportTest: top of createLinkedResources()");
772         
773         /*
774          * link target resources (ie resources will point to these)
775          */

776
777         Folder folder1 = new FolderImpl();
778         folder1.setName(LINK_TARGET_FOLDER_NAME);
779         folder1.setLabel(LINK_TARGET_FOLDER_NAME + LABEL);
780         folder1.setDescription(LINK_TARGET_FOLDER_NAME + DESC_TEXT);
781         folder1.setParentFolder("/");
782         repo.saveFolder(null, folder1);
783
784         // target image
785
FileResource image1 = (FileResource) repo.newResource(null, FileResource.class);
786         image1.setFileType(FileResource.TYPE_IMAGE);
787         image1.readData(getClass().getResourceAsStream("/images/jasperreports.gif"));
788         image1.setName(LINK_TARGET_IMAGE_NAME);
789         image1.setLabel(LINK_TARGET_IMAGE_NAME + LABEL);
790         image1.setDescription(LINK_TARGET_IMAGE_NAME + DESC_TEXT);
791         image1.setParentFolder(folder1);
792         repo.saveResource(null, image1);
793
794         // target data source
795
JndiJdbcReportDataSource datasource =
796             (JndiJdbcReportDataSource) repo.newResource(null, JndiJdbcReportDataSource.class);
797         datasource.setName(LINK_TARGET_JNDI_NAME);
798         datasource.setLabel(LINK_TARGET_JNDI_NAME + LABEL);
799         datasource.setDescription(LINK_TARGET_JNDI_NAME + DESC_TEXT);
800         datasource.setJndiName(jdbcProps.getProperty("test.jndi"));
801         datasource.setParentFolder(folder1);
802         repo.saveResource(null, datasource);
803         
804         // target jrxml
805
FileResource reportRes1 = (FileResource) repo.newResource(null, FileResource.class);
806         reportRes1.setFileType(FileResource.TYPE_JRXML);
807         reportRes1.setName(LINK_TARGET_JRXML_NAME);
808         reportRes1.setLabel(LINK_TARGET_JRXML_NAME + LABEL);
809         reportRes1.setDescription(LINK_TARGET_JRXML_NAME + DESC_TEXT);
810         InputStream JavaDoc jrxml = getClass().getResourceAsStream("/reports/jasper/TestReportExportImport01.jrxml");
811         reportRes1.readData(jrxml);
812         reportRes1.setParentFolder(folder1);
813         repo.saveResource(null, reportRes1);
814
815         /*
816          * create linked and other resources
817          */

818         
819         // folder
820
Folder folder2 = new FolderImpl();
821         folder2.setName(LINK_FOLDER_NAME);
822         folder2.setLabel(LINK_FOLDER_NAME + LABEL);
823         folder2.setDescription(LINK_FOLDER_NAME + DESC_TEXT);
824         folder2.setParentFolder("/");
825         repo.saveFolder(null, folder2);
826         
827         // report unit
828
ReportUnit unit = (ReportUnit) repo.newResource(null, ReportUnit.class);
829         unit.setName(LINK_RU_NAME);
830         unit.setLabel(LINK_RU_NAME + LABEL);
831         unit.setDescription(LINK_RU_NAME + DESC_TEXT);
832         unit.setParentFolder(folder2);
833                 
834         // mainReport jrxml (local)
835
FileResource reportRes2 = (FileResource) repo.newResource(null, FileResource.class);
836         reportRes2.setFileType(FileResource.TYPE_JRXML);
837         reportRes2.setName(LINK_JRXML_NAME);
838         reportRes2.setLabel(LINK_JRXML_NAME + LABEL);
839         reportRes2.setDescription(LINK_JRXML_NAME + DESC_TEXT);
840         InputStream JavaDoc jrxml2 = getClass().getResourceAsStream("/reports/jasper/TestReportExportImport01.jrxml");
841         reportRes2.readData(jrxml2);
842         //unit.setMainReport(reportRes2);
843

844         // mainReport jrxml (reference)
845
unit.setMainReportReference(PATH_SEP + LINK_TARGET_FOLDER_NAME + PATH_SEP + LINK_TARGET_JRXML_NAME);
846         
847         // link to existing image
848
FileResource imgLink = (FileResource) repo.newResource(null, FileResource.class);
849         imgLink.setFileType(FileResource.TYPE_IMAGE);
850         imgLink.setName(LINK_IMAGE_NAME1);
851         imgLink.setLabel(LINK_IMAGE_NAME1 + LABEL);
852         imgLink.setDescription(LINK_IMAGE_NAME1 + DESC_TEXT);
853         imgLink.setReferenceURI(PATH_SEP + LINK_TARGET_FOLDER_NAME + PATH_SEP + LINK_TARGET_IMAGE_NAME);
854         unit.addResource(imgLink);
855         
856         // set datasource (local)
857
JndiJdbcReportDataSource datasource2 =
858             (JndiJdbcReportDataSource) repo.newResource(null, JndiJdbcReportDataSource.class);
859         datasource2.setName(LINK_RU_JNDI_NAME);
860         datasource2.setLabel(LINK_RU_JNDI_NAME + LABEL);
861         datasource2.setDescription(LINK_RU_JNDI_NAME + DESC_TEXT);
862         datasource2.setJndiName(jdbcProps.getProperty("test.jndi"));
863         //unit.setDataSource(datasource2);
864

865         // set datasource (reference)
866
unit.setDataSourceReference(PATH_SEP + LINK_TARGET_FOLDER_NAME + PATH_SEP + LINK_TARGET_JNDI_NAME);
867         
868         repo.saveResource(null, unit);
869     }
870         
871     private void deleteLinkedResources() {
872         System.out.println("ExportTest: top of deleteLinkedResources()");
873     
874         repo.deleteResource(null, PATH_SEP + LINK_FOLDER_NAME + PATH_SEP + LINK_RU_NAME);
875         repo.deleteFolder(null, PATH_SEP + LINK_FOLDER_NAME);
876     
877         repo.deleteResource(null, PATH_SEP + LINK_TARGET_FOLDER_NAME + PATH_SEP + LINK_TARGET_IMAGE_NAME);
878         repo.deleteResource(null, PATH_SEP + LINK_TARGET_FOLDER_NAME + PATH_SEP + LINK_TARGET_JNDI_NAME);
879         repo.deleteResource(null, PATH_SEP + LINK_TARGET_FOLDER_NAME + PATH_SEP + LINK_TARGET_JRXML_NAME);
880         repo.deleteFolder(null, PATH_SEP + LINK_TARGET_FOLDER_NAME);
881     }
882
883
884     public void testQueryExportImport() {
885         Folder folder = new FolderImpl();
886         folder.setName("tmp_export");
887         folder.setLabel("tmp_export");
888         repo.saveFolder(context, folder);
889         
890         try {
891             Query query = (Query) repo.newResource(context, Query.class);
892             query.setName("query");
893             query.setLabel("query");
894             query.setParentFolder("/tmp_export");
895             query.setLanguage("sql");
896             query.setSql("select * from dual");
897             
898             JdbcReportDataSource ds = (JdbcReportDataSource) repo.newResource(context, JdbcReportDataSource.class);
899             ds.setName("ds");
900             ds.setLabel("ds");
901             ds.setConnectionUrl("jdbc:mysql://localhost:3306/jasperserver");
902             ds.setDriverClass("com.mysql.jdbc.Driver");
903             ds.setUsername("user");
904             ds.setPassword("passwd");
905             
906             query.setDataSource(ds);
907             
908             repo.saveResource(context, query);
909             
910             boolean queryDeleted = false;
911             try {
912                 ExportResource exporter = new ExportResource(repo,
913                         null,
914                         null,
915                         context,
916                         "/tmp_export/query",
917                         false,
918                         null,
919                         null,
920                         false,
921                         null,
922                         "target",
923                         "query_catalog.xml");
924                 exporter.process();
925                 
926                 repo.deleteResource(context, "/tmp_export/query");
927                 queryDeleted = true;
928                 
929                 Resource queryRes = repo.getResource(context, "/tmp_export/query");
930                 assertNull("Query deleted", queryRes);
931                 
932                 ImportResource importer = new ImportResource(repo,
933                         null,
934                         null,
935                         context,
936                         "target",
937                         "query_catalog.xml",
938                         PATH_SEP);
939                 queryDeleted = false;
940                 importer.process();
941                 
942                 queryRes = repo.getResource(context, "/tmp_export/query");
943                 assertNotNull("Query imported", queryRes);
944                 assertTrue(queryRes instanceof Query);
945                 query = (Query) queryRes;
946                 
947                 assertEquals("sql", query.getLanguage());
948                 ResourceReference dsRef = query.getDataSource();
949                 assertNotNull("Query datasource", dsRef);
950                 assertTrue("Query datasource local", dsRef.isLocal());
951                 assertNotNull(dsRef.getLocalResource());
952                 assertTrue(dsRef.getLocalResource() instanceof JdbcReportDataSource);
953                 ds = (JdbcReportDataSource) dsRef.getLocalResource();
954                 assertEquals("ds", ds.getName());
955                 assertEquals("passwd", ds.getPassword());
956                 
957                 repo.deleteResource(context, "/tmp_export/query");
958                 queryDeleted = true;
959             } finally {
960                 if (!queryDeleted) {
961                     repo.deleteResource(context, "/tmp_export/query");
962                 }
963             }
964         } finally {
965             repo.deleteFolder(null, "/tmp_export");
966         }
967     }
968     
969     protected Properties JavaDoc loadJdbcProps() throws IOException JavaDoc, FileNotFoundException JavaDoc {
970         jdbcProps = new Properties JavaDoc();
971         String JavaDoc jdbcPropFile = System.getProperty("test.hibernate.jdbc.properties");
972         BufferedInputStream JavaDoc is = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(jdbcPropFile));
973         jdbcProps.load(is);
974         is.close();
975         return jdbcProps;
976     }
977     
978     // LINK TESTING
979
static final String JavaDoc LINK_TARGET_FOLDER_NAME = "test_01E";
980     static final String JavaDoc LINK_TARGET_IMAGE_NAME = "TestImage_01E";
981     static final String JavaDoc LINK_TARGET_JNDI_NAME = "TestJNDI_01E";
982     static final String JavaDoc LINK_TARGET_JDBC_NAME = "TestJDBC_01E";
983     static final String JavaDoc LINK_TARGET_JRXML_NAME = "TestJRXML_01E";
984     static final String JavaDoc LINK_FOLDER_NAME = "test_link_01E";
985     static final String JavaDoc LINK_RU_NAME = "TestReportUnitWithLinks_01E";
986     static final String JavaDoc LINK_RU_JNDI_NAME = "TestRUJNDI_01E";
987     static final String JavaDoc LINK_JRXML_NAME = "TestLinkedJRXML_01E";
988     static final String JavaDoc LINK_IMAGE_NAME1 = "TestLinkedImage_01E";
989     
990     // PREPEND PATH IMAGE TESTING
991
static final String JavaDoc PREPEND_FOLDER1 = "imageDir1A_B12";
992     static final String JavaDoc PREPEND_FOLDER2 = "imageDir2A_B12";
993     static final String JavaDoc PREPEND_IMAGE_NAME = "TestImage03A_B12";
994     static final String JavaDoc NEW_PREPEND_PATH = "/extra_01A_B12";
995     static final String JavaDoc PREPEND_FOLDER1_PATH = "/images/" + PREPEND_FOLDER1;
996     static final String JavaDoc PREPEND_FOLDER2_PATH = PREPEND_FOLDER1_PATH + PATH_SEP + PREPEND_FOLDER2;
997     static final String JavaDoc PREPEND_IMAGE_URI = PREPEND_FOLDER2_PATH + PATH_SEP + PREPEND_IMAGE_NAME;
998     
999     // REPORT UNIT
1000
static final String JavaDoc RU_FOLDER = "reportDir1B";
1001    static final String JavaDoc RU_NAME = "TestRUExportImport1B";
1002    static final String JavaDoc RU_REPORT_NAME = "TestRUReportExportImport1B";
1003    static final String JavaDoc RU_IMAGE_NAME = "TestRUImage1B";
1004    static final String JavaDoc RU_JNDI_NAME = "TestJNDIDS1B";
1005    static final String JavaDoc RU_FOLDER_PATH = "/reports/" + RU_FOLDER;
1006    static final String JavaDoc RU_URI = RU_FOLDER_PATH + PATH_SEP + RU_NAME;
1007    static final String JavaDoc RU_JNDI_URI = RU_FOLDER_PATH + PATH_SEP + RU_JNDI_NAME;
1008        
1009    // FOLDERS
1010
static final String JavaDoc FOLDER_FOLDER_NAME1 = "testFolder1C2";
1011    static final String JavaDoc FOLDER_FOLDER_NAME2 = "testFolder2C2";
1012    static final String JavaDoc FOLDER_FOLDER_NAME3 = "testFolder3C2";
1013    static final String JavaDoc FOLDER_FOLDER_NAME4 = "testFolder4C2";
1014    static final String JavaDoc FOLDER_IMAGE_NAME1 = "testImage1C2";
1015    static final String JavaDoc FOLDER_IMAGE_NAME3 = "testImage3C2";
1016    static final String JavaDoc FOLDER_IMAGE_NAME4 = "testImage4C2";
1017    static final String JavaDoc FOLDER_IMAGE_RU_NAME = "testRUImage1C2";
1018    static final String JavaDoc FOLDER_JRXML_NAME = "testJrxml1C2";
1019    static final String JavaDoc FOLDER_RU_NAME = "testReportUnit1C2";
1020
1021    // INPUT CONTROL
1022
static final String JavaDoc INPUT_CONTROL_NAME = "numInputTest1D";
1023    static final String JavaDoc INPUT_CONTROL_DATATYPE_NAME = "numInputDataType1D";
1024    static final String JavaDoc INPUT_CONTROL_URI = "/datatypes/" + INPUT_CONTROL_NAME;
1025    
1026}
1027
Popular Tags