KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > TestPublishing


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestPublishing.java,v $
3  * Date : $Date: 2006/03/27 14:52:46 $
4  * Version: $Revision: 1.21 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31  
32 package org.opencms.file;
33
34 import org.opencms.file.types.CmsResourceTypeFolder;
35 import org.opencms.file.types.CmsResourceTypePlain;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsMultiException;
38 import org.opencms.report.CmsShellReport;
39 import org.opencms.test.OpenCmsTestCase;
40 import org.opencms.test.OpenCmsTestLogAppender;
41 import org.opencms.test.OpenCmsTestProperties;
42 import org.opencms.test.OpenCmsTestResourceFilter;
43
44 import junit.extensions.TestSetup;
45 import junit.framework.Test;
46 import junit.framework.TestSuite;
47
48 /**
49  * Unit tests for OpenCms publishing.<p>
50  *
51  * @author Michael Emmerich
52  *
53  * @version $Revision: 1.21 $
54  */

55 public class TestPublishing extends OpenCmsTestCase {
56   
57     /**
58      * Default JUnit constructor.<p>
59      *
60      * @param arg0 JUnit parameters
61      */

62     public TestPublishing(String JavaDoc arg0) {
63         super(arg0);
64     }
65     
66     /**
67      * Test suite for this test class.<p>
68      *
69      * @return the test suite
70      */

71     public static Test suite() {
72         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
73         
74         TestSuite suite = new TestSuite();
75         suite.setName(TestPublishing.class.getName());
76         
77         suite.addTest(new TestPublishing("testPublishNewFiles"));
78         suite.addTest(new TestPublishing("testPublishNewFilesInNewFolder"));
79         suite.addTest(new TestPublishing("testPublishChangedFiles"));
80         suite.addTest(new TestPublishing("testPublishLockedFiles"));
81         suite.addTest(new TestPublishing("testPublishDeletedFiles"));
82         suite.addTest(new TestPublishing("testPublishProjectLastmodified"));
83         suite.addTest(new TestPublishing("testPublishTemporaryProject"));
84         
85         TestSetup wrapper = new TestSetup(suite) {
86             
87             protected void setUp() {
88                 setupOpenCms("simpletest", "/sites/default/");
89             }
90             
91             protected void tearDown() {
92                 removeOpenCms();
93             }
94         };
95         
96         return wrapper;
97     }
98     
99     /**
100      * Test publishing changed files.<p>
101      *
102      * @throws Throwable if something goes wrong
103      */

104     public void testPublishChangedFiles() throws Throwable JavaDoc {
105         
106         CmsObject cms = getCmsObject();
107         echo("Testing publish changed files");
108         
109         String JavaDoc resource1 = "/folder2/image1_new.gif";
110         String JavaDoc resource2 = "/folder2/image1_sibling1.gif";
111         String JavaDoc resource3 = "/folder2/image1_sibling2.gif";
112         String JavaDoc resource4 = "/folder2/image1_sibling3.gif";
113         
114         CmsProject onlineProject = cms.readProject("Online");
115         CmsProject offlineProject = cms.readProject("Offline");
116         
117         CmsProperty prop1;
118         CmsProperty prop2;
119         CmsProperty prop3;
120         CmsProperty prop4;
121
122         
123         // make changes to the resources
124
// do not need to make any changes to resource3 and resource4 as they are
125
// siblings of resource2!
126

127         cms.lockResource(resource1);
128         cms.lockResource(resource2);
129         
130         cms.writePropertyObject(resource1, new CmsProperty("Title", resource1, null));
131         cms.writePropertyObject(resource2, new CmsProperty("Title", resource2, null));
132         cms.writePropertyObject(resource3, new CmsProperty("Title", resource3, null));
133         cms.writePropertyObject(resource4, new CmsProperty("Title", resource4, null));
134            
135         // unlock all resources
136
cms.unlockResource(resource1);
137         cms.unlockResource(resource2);
138                       
139         storeResources(cms, resource1);
140         storeResources(cms, resource2);
141         storeResources(cms, resource3);
142         storeResources(cms, resource4);
143        
144         // publish a modified resource without siblings
145
//
146
cms.publishResource(resource1);
147
148         // the online file must the offline changes
149
cms.getRequestContext().setCurrentProject(onlineProject);
150         prop1 = cms.readPropertyObject(resource1, "Title", false);
151
152         
153         if (!prop1.getValue().equals((resource1))) {
154             fail("Property not published for " + resource1);
155         }
156         assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
157         
158         // check if the file in the offline project is unchancged now
159
cms.getRequestContext().setCurrentProject(offlineProject);
160         assertState(cms, resource1, CmsResource.STATE_UNCHANGED);
161
162         // publish a modified resource with siblings, but keeping the siblings unpublished
163
//
164
cms.publishResource(resource2);
165
166         // the online file must the offline changes
167
cms.getRequestContext().setCurrentProject(onlineProject);
168         prop2 = cms.readPropertyObject(resource2, "Title", false);
169         prop3 = cms.readPropertyObject(resource3, "Title", false);
170         prop4 = cms.readPropertyObject(resource4, "Title", false);
171         
172         if (!prop2.getValue().equals((resource2))) {
173             fail("Property not published for " + resource2);
174         }
175         if (prop3.getValue().equals((resource3))) {
176             fail("Property published for " + resource3);
177         }
178         if (prop4.getValue().equals((resource4))) {
179             fail("Property published for " + resource4);
180         }
181         
182         assertFilter(cms, resource2, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
183         
184         // check if the file in the offline project is unchancged now
185
cms.getRequestContext().setCurrentProject(offlineProject);
186         assertState(cms, resource2, CmsResource.STATE_UNCHANGED);
187         assertState(cms, resource3, CmsResource.STATE_CHANGED);
188         assertState(cms, resource4, CmsResource.STATE_CHANGED);
189
190         // publish a modified resource with siblings, publish the siblings as well
191
//
192
cms.publishResource(resource3, true, new CmsShellReport(cms.getRequestContext().getLocale()));
193
194         // the online file must the offline changes
195
cms.getRequestContext().setCurrentProject(onlineProject);
196         prop3 = cms.readPropertyObject(resource3, "Title", false);
197         prop4 = cms.readPropertyObject(resource4, "Title", false);
198         
199         if (!prop3.getValue().equals((resource3))) {
200             fail("Property not published for " + resource3);
201         }
202         if (!prop4.getValue().equals((resource4))) {
203             fail("Property not published for " + resource4);
204         }
205         
206         assertFilter(cms, resource3, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
207         assertFilter(cms, resource4, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
208                 
209         // check if the file in the offline project is unchancged now
210
cms.getRequestContext().setCurrentProject(offlineProject);
211         assertState(cms, resource3, CmsResource.STATE_UNCHANGED);
212         assertState(cms, resource4, CmsResource.STATE_UNCHANGED);
213     }
214     
215     /**
216      * Test publishing deelted files.<p>
217      *
218      * @throws Throwable if something goes wrong
219      */

220     public void testPublishDeletedFiles() throws Throwable JavaDoc {
221         
222         CmsObject cms = getCmsObject();
223         echo("Testing publish deleted files");
224         
225         String JavaDoc resource1 = "/folder2/image1_new.gif";
226         String JavaDoc resource2 = "/folder2/image1_sibling1.gif";
227         String JavaDoc resource3 = "/folder2/image1_sibling2.gif";
228         String JavaDoc resource4 = "/folder2/image1_sibling3.gif";
229         
230         CmsProject onlineProject = cms.readProject("Online");
231         CmsProject offlineProject = cms.readProject("Offline");
232         
233         // publish a deleted resource without siblings
234
//
235

236         // delete the resources
237
cms.lockResource(resource1);
238         cms.deleteResource(resource1, CmsResource.DELETE_PRESERVE_SIBLINGS);
239         cms.unlockResource(resource1);
240        
241         cms.publishResource(resource1);
242
243         // the online file must be deleted
244
cms.getRequestContext().setCurrentProject(onlineProject);
245         try {
246             cms.readResource(resource1);
247             fail ("Resource " + resource1 + " was not deleted online");
248         } catch (CmsVfsResourceNotFoundException e) {
249             // ok
250
} catch (CmsException e) {
251             fail("Resource " + resource1 + " error:" +e);
252         }
253        
254        cms.getRequestContext().setCurrentProject(offlineProject);
255              
256        // publish a deleted resource with siblings,
257
// delete the siblings also, but publish only the resource itself
258

259        // delete the resources
260
cms.lockResource(resource2);
261        cms.deleteResource(resource2, CmsResource.DELETE_REMOVE_SIBLINGS);
262        cms.unlockResource(resource2);
263        
264        // this test makes only sense when siblings are published
265
cms.publishResource(resource2, false, new CmsShellReport(cms.getRequestContext().getLocale()));
266
267        // the online file must be deleted
268
cms.getRequestContext().setCurrentProject(onlineProject);
269        try {
270            cms.readResource(resource2);
271            fail ("Resource " + resource2 + " was not deleted online");
272        } catch (CmsVfsResourceNotFoundException e) {
273            // ok
274
} catch (CmsException e) {
275            fail("Resource " + resource2 + " error:" +e);
276        }
277        // the other siblings must still be there
278
try {
279           cms.readResource(resource3);
280       } catch (CmsException e) {
281               fail("Resource " + resource3 + " error:" +e);
282       }
283       try {
284           cms.readResource(resource4);
285       } catch (CmsException e) {
286             fail("Resource " + resource4 + " error:" +e);
287       }
288       
289       cms.getRequestContext().setCurrentProject(offlineProject);
290       // in the offline project, the siblings must be still marked as deleted
291
assertState(cms, resource3, CmsResource.STATE_DELETED);
292       assertState(cms, resource4, CmsResource.STATE_DELETED);
293       
294       // publish a deleted resource with siblings, delete the siblings
295
//
296
cms.publishResource(resource3, true, new CmsShellReport(cms.getRequestContext().getLocale()));
297       
298       // the online files must be deleted
299
cms.getRequestContext().setCurrentProject(onlineProject);
300       try {
301           cms.readResource(resource3);
302           fail ("Resource " + resource3 + " was not deleted online");
303       } catch (CmsVfsResourceNotFoundException e) {
304           // ok
305
} catch (CmsException e) {
306           fail("Resource " + resource3 + " error:" +e);
307       }
308       try {
309           cms.readResource(resource4);
310           fail ("Resource " + resource4 + " was not deleted online");
311       } catch (CmsVfsResourceNotFoundException e) {
312           // ok
313
} catch (CmsException e) {
314           fail("Resource " + resource4 + " error:" +e);
315       }
316       
317       cms.getRequestContext().setCurrentProject(offlineProject);
318
319     }
320
321     /**
322      * Test publishing changed files.<p>
323      *
324      * @throws Throwable if something goes wrong
325      */

326     public void testPublishLockedFiles() throws Throwable JavaDoc {
327         
328         CmsObject cms = getCmsObject();
329         echo("Testing publish locked files");
330         
331         String JavaDoc source = "/folder2/subfolder21/image1.gif";
332         String JavaDoc resource1 = "/folder2/image1_new.gif";
333         String JavaDoc resource2 = "/folder2/image1_sibling1.gif";
334         
335         CmsProject onlineProject = cms.readProject("Online");
336         
337         CmsProperty prop0;
338         CmsProperty prop1;
339         CmsProperty prop2;
340   
341         // make changes to the resources
342
// do not need to make any changes to resource3 and resource4 as they are
343
// siblings of resource2!
344

345         cms.lockResource(source);
346         cms.lockResource(resource1);
347         cms.lockResource(resource2);
348         
349         cms.writePropertyObject(source, new CmsProperty("Title", source + " modified", null));
350         cms.writePropertyObject(resource1, new CmsProperty("Title", resource1 + " modified", null));
351         cms.writePropertyObject(resource2, new CmsProperty("Title", resource2 + " modified", null));
352
353         storeResources(cms, source);
354         storeResources(cms, resource1);
355         storeResources(cms, resource2);
356        
357         // publish a modified resource without siblings
358
cms.publishProject();
359
360         // ensure that all changed resources are still changed in the offline project
361
assertState(cms, source, CmsResource.STATE_CHANGED);
362         assertState(cms, resource1, CmsResource.STATE_CHANGED);
363         assertState(cms, resource2, CmsResource.STATE_CHANGED);
364                 
365         // ensure that all changed resources are NOT published
366
cms.getRequestContext().setCurrentProject(onlineProject);
367         prop0 = cms.readPropertyObject(source, "Title", false);
368         prop1 = cms.readPropertyObject(resource1, "Title", false);
369         prop2 = cms.readPropertyObject(resource2, "Title", false);
370         
371         if (prop0.getValue().equals((source + " modified"))) {
372             fail("Property published for " + source);
373         }
374         if (prop1.getValue().equals((resource1 + " modified"))) {
375             fail("Property published for " + resource1);
376         }
377         if (prop2.getValue().equals((resource2 + " modified"))) {
378             fail("Property published for " + resource2);
379         }
380     }
381     
382     
383     
384     
385     /**
386      * Test publishing new files.<p>
387      *
388      * @throws Throwable if something goes wrong
389      */

390     public void testPublishNewFiles() throws Throwable JavaDoc {
391         
392         CmsObject cms = getCmsObject();
393         echo("Testing publish new files");
394         
395         String JavaDoc source = "/folder2/subfolder21/image1.gif";
396         String JavaDoc destination1 = "/folder2/image1_new.gif";
397         String JavaDoc destination2 = "/folder2/image1_sibling1.gif";
398         String JavaDoc destination3 = "/folder2/image1_sibling2.gif";
399         String JavaDoc destination4 = "/folder2/image1_sibling3.gif";
400         
401         CmsProject onlineProject = cms.readProject("Online");
402         CmsProject offlineProject = cms.readProject("Offline");
403
404         // make four copies of a file to be published later
405
cms.copyResource(source, destination1, CmsResource.COPY_AS_NEW);
406         cms.copyResource(source, destination2, CmsResource.COPY_AS_SIBLING);
407         cms.copyResource(source, destination3, CmsResource.COPY_AS_SIBLING);
408         cms.copyResource(source, destination4, CmsResource.COPY_AS_SIBLING);
409         
410         storeResources(cms, destination1);
411         storeResources(cms, destination2);
412         storeResources(cms, destination3);
413         storeResources(cms, destination4);
414         
415         // unlock all new resources
416
// do not neet do unlock destination3 as it is a sibling of destination2
417
cms.unlockResource(destination1);
418         cms.unlockResource(destination2);
419         
420         // publish a new resource
421
//
422
cms.publishResource(destination1);
423
424         // the file must be now available in the online project
425
cms.getRequestContext().setCurrentProject(onlineProject);
426         try {
427             cms.readResource(destination1);
428         } catch (CmsException e) {
429             fail("Resource " + destination1 + " not found in online project:" +e);
430         }
431         assertFilter(cms, destination1, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
432         
433         // check if the file in the offline project is unchancged now
434
cms.getRequestContext().setCurrentProject(offlineProject);
435         assertState(cms, destination1, CmsResource.STATE_UNCHANGED);
436  
437         // publish a sibling without publishing other siblings
438
//
439
cms.publishResource(destination2);
440         
441         // the file must be now available in the online project
442
cms.getRequestContext().setCurrentProject(onlineProject);
443         try {
444             cms.readResource(destination2);
445         } catch (CmsException e) {
446             fail("Resource " + destination2 + " not found in online project:" +e);
447         }
448         // the other siblings must not be available in the online project yet
449
try {
450             cms.readResource(destination3);
451             fail("Resource " + destination3+ " should not available online yet");
452         } catch (CmsVfsResourceNotFoundException e) {
453             // ok
454
} catch (CmsException e) {
455             fail("Resource " + destination3 + " error:" +e);
456         }
457         try {
458             cms.readResource(destination4);
459             fail("Resource " + destination4+ " should not available online yet");
460         } catch (CmsVfsResourceNotFoundException e) {
461             // ok
462
} catch (CmsException e) {
463             fail("Resource " + destination4 + " error:" +e);
464         }
465         
466         assertFilter(cms, destination2, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
467         
468         // check if the file in the offline project is unchancged now
469
cms.getRequestContext().setCurrentProject(offlineProject);
470         assertState(cms, destination2, CmsResource.STATE_UNCHANGED);
471         // the other siblings in the offline project must still be shown as new
472
assertState(cms, destination3, CmsResource.STATE_NEW);
473         assertState(cms, destination4, CmsResource.STATE_NEW);
474         
475         // publish a sibling and all other siblings of it
476
//
477
cms.publishResource(destination3, true, new CmsShellReport(cms.getRequestContext().getLocale()));
478         // the file and its siblings must be now available in the online project
479
cms.getRequestContext().setCurrentProject(onlineProject);
480         try {
481             cms.readResource(destination3);
482         } catch (CmsException e) {
483             fail("Resource " + destination3 + " not found in online project:" +e);
484         }
485         try {
486             cms.readResource(destination4);
487         } catch (CmsException e) {
488             fail("Resource " + destination4 + " not found in online project:" +e);
489         }
490         assertFilter(cms, destination3, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
491         assertFilter(cms, destination4, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
492         
493         // check if the file in the offline project is unchancged now
494
cms.getRequestContext().setCurrentProject(offlineProject);
495         assertState(cms, destination3, CmsResource.STATE_UNCHANGED);
496         assertState(cms, destination4, CmsResource.STATE_UNCHANGED);
497     }
498     
499     /**
500      * Test publishing files within a new unpublished folder.<p>
501      *
502      * @throws Throwable if something goes wrong
503      */

504     public void testPublishNewFilesInNewFolder() throws Throwable JavaDoc {
505         
506         CmsObject cms = getCmsObject();
507         echo("Testing publishing new files in new folder");
508         
509         String JavaDoc source = "/folder1/image1.gif";
510         String JavaDoc newFolder = "/new_folder/";
511         String JavaDoc newFile = newFolder + "new_file";
512         String JavaDoc newSibling = newFolder + "new_sibling";
513         
514         cms.createResource(newFolder, CmsResourceTypeFolder.getStaticTypeId());
515         cms.unlockResource(newFolder);
516         storeResources(cms, newFolder);
517         
518         // change to test project
519
CmsProject project = getTestProject(cms);
520         cms.getRequestContext().setCurrentProject(project);
521         
522         cms.copyResource(source, newFile, CmsResource.COPY_AS_NEW);
523         cms.unlockResource(newFile);
524         
525         cms.copyResource(source, newSibling, CmsResource.COPY_AS_SIBLING);
526         cms.unlockResource(newSibling);
527         
528         // direct publish of the new file will not publish the new file
529
echo ("Publishing the resource directly");
530         
531         storeResources(cms, newFile);
532         
533         boolean error = true;
534         try {
535             // this will generate an error in the log, ensure the test still continues
536
OpenCmsTestLogAppender.setBreakOnError(false);
537             cms.publishResource(newFile);
538         } catch (CmsMultiException e) {
539             CmsVfsException ex = (CmsVfsException)e.getExceptions().get(0);
540             if (ex.getMessageContainer().getKey() == org.opencms.db.Messages.ERR_DIRECT_PUBLISH_PARENT_NEW_2) {
541                 error = false;
542             }
543         }
544         // reset log to stop test on error
545
OpenCmsTestLogAppender.setBreakOnError(true);
546         
547         if (error) {
548             fail("A resource in a new folder could be published without generating an error!");
549         }
550             
551         assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_EQUAL);
552         
553         // direct publish of another sibling will not publish the new sibling
554
echo ("Publishing another sibling");
555         cms.lockResource(source);
556         cms.setDateLastModified(source, System.currentTimeMillis(), false);
557         cms.unlockResource(source);
558         
559         storeResources(cms, newSibling);
560         cms.publishResource(source, true, new CmsShellReport(cms.getRequestContext().getLocale()));
561         assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_EQUAL);
562         
563         // publishing the test project will not publish the new file or the new sibling
564
echo ("Publishing the test project");
565         cms.publishProject();
566         
567         assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_EQUAL);
568         assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_EQUAL);
569         
570         // publishing the offline project will publish the folder but not the resources
571
echo ("Publishing the offline project");
572         
573         cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
574         cms.publishProject();
575         
576         assertFilter(cms, newFolder, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
577         assertState(cms, newFolder, CmsResource.STATE_UNCHANGED);
578         
579         assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_EQUAL);
580         assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_EQUAL);
581         
582         // publishing the test proejct again will now publish the resources
583
echo ("Publishing the test project again");
584         cms.getRequestContext().setCurrentProject(project);
585         cms.publishProject();
586         
587         assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
588         assertState(cms, newFile, CmsResource.STATE_UNCHANGED);
589         
590         assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
591         assertState(cms, newSibling, CmsResource.STATE_UNCHANGED);
592         
593     }
594     
595     /**
596      * Tests publishing resources within a distinct project.<p>
597      *
598      * @throws Throwable if something goes wrong
599      */

600     public void testPublishProjectLastmodified() throws Throwable JavaDoc {
601
602         CmsObject cms = getCmsObject();
603         echo("Testing publishing a distinct project");
604         
605         String JavaDoc path = "/folder1";
606         String JavaDoc res1 = path + "/page1.html";
607         String JavaDoc res2 = path + "/page2.html";
608         
609         long timestamp = System.currentTimeMillis();
610
611         cms.getRequestContext().setCurrentProject(cms.readProject("Online"));
612         storeResources(cms, res1);
613         
614         // change first resource in the offline project
615
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
616         cms.lockResource(res1);
617         cms.setDateLastModified(res1, timestamp, false);
618         
619         cms.unlockProject(cms.getRequestContext().currentProject().getId());
620         
621         // create a new project
622
CmsProject project = getTestProject(cms);
623         cms.getRequestContext().setCurrentProject(project);
624         cms.copyResourceToProject(path);
625         
626         // and change another resource in this project
627
cms.lockResource(res2);
628         cms.setDateLastModified(res2, timestamp, false);
629         cms.unlockProject(cms.getRequestContext().currentProject().getId());
630         storeResources(cms, res2);
631         
632         // when the project is published, only the second resource will be published
633
cms.publishProject();
634         
635         cms.getRequestContext().setCurrentProject(cms.readProject("Online"));
636         this.assertFilter(cms, res1, OpenCmsTestResourceFilter.FILTER_EQUAL);
637         this.assertFilter(cms, res2, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
638         this.assertDateLastModified(cms, res2, timestamp);
639         
640         echo("Testing publishing in different projects");
641         
642         // change back to the test project, create a new resource
643
String JavaDoc res3 = path + "/testPublishProjectLastmodified.txt";
644         cms.getRequestContext().setCurrentProject(project);
645         cms.createResource(res3, CmsResourceTypePlain.getStaticTypeId());
646         
647         // change to offline project, copy resource as sibling
648
// this will also change the project in which the source was lastmodified (now "Offline")
649
String JavaDoc res4 = "/testPublishProjectLastmodified.txt";
650         cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
651         cms.copyResource(res3, res4, CmsResource.COPY_AS_SIBLING);
652         
653         // check the current state of the resources
654
assertState(cms, res3, CmsResource.STATE_NEW);
655         assertState(cms, res4, CmsResource.STATE_NEW);
656         
657         // publish the test project
658
cms.getRequestContext().setCurrentProject(project);
659         cms.unlockProject(project.getId());
660         cms.publishProject();
661         
662         // the resource inside the test project must not be published (not in test project)
663
assertState(cms, res3, CmsResource.STATE_NEW);
664         
665         // the sibling outside the test project must not be published (not in test project)
666
assertState(cms, res4, CmsResource.STATE_NEW);
667         
668         // publish the root folder of the test project within the test project
669
cms.publishResource(path, true, new CmsShellReport(cms.getRequestContext().getLocale()));
670         
671         // the resource inside the test project must not be published (still not in test project)
672
assertState(cms, res3, CmsResource.STATE_NEW);
673         
674         // the sibling outside the test project must not be published (still not in test project)
675
assertState(cms, res4, CmsResource.STATE_NEW);
676     }
677      
678     /**
679      * Test publishing a temporary project.<p>
680      *
681      * @throws Throwable if something goes wrong
682      */

683     public void testPublishTemporaryProject() throws Throwable JavaDoc {
684         CmsObject cms = getCmsObject();
685         echo("Testing publish temporary project");
686         
687         String JavaDoc source = "/folder1/testfile.txt";
688         CmsProject onlineProject = cms.readProject("Online");
689         CmsProject offlineProject = cms.readProject("Offline");
690         
691         //create a new temp project
692
CmsProject tempProject = cms.createProject("deleteme", "Temp project to be deleted after publish", "Users", "Projectmanagers", CmsProject.PROJECT_TYPE_TEMPORARY);
693         cms.copyResourceToProject("/");
694         cms.getRequestContext().setCurrentProject(tempProject);
695         
696         // now create a new resource
697
cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
698         cms.unlockResource(source);
699         storeResources(cms, source);
700         
701         cms.readResource(source);
702         
703         //publish the project
704
cms.publishProject();
705         
706
707         // now try to read the resource
708
try {
709             cms.readResource(source);
710         } catch (CmsException e) {
711             fail("Resource " + source + " not found in project " + cms.getRequestContext().currentProject().toString() + ":" +e);
712         }
713         
714         // now try to read the resource in the online project
715
cms.getRequestContext().setCurrentProject(onlineProject);
716         try {
717             cms.readResource(source);
718         } catch (CmsException e) {
719             fail("Resource " + source + " not found in project " + cms.getRequestContext().currentProject().toString() + ":" +e);
720         }
721         
722         // check if the state of the resource is ok
723
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
724         
725         // check if the file in the offline project is unchancged now
726
cms.getRequestContext().setCurrentProject(offlineProject);
727         assertState(cms, source, CmsResource.STATE_UNCHANGED);
728         
729         // check if the project is deleted
730
try {
731             cms.readProject("deleteme");
732             fail("Temporary project still existing");
733         } catch (CmsException e) {
734             // to nothing, this exception must be thrown
735
}
736     
737         
738     }
739     
740     /**
741      * Returns a project for testing purposes.<p>
742      *
743      * @param cms the cms object
744      * @return a project for testing purposes
745      */

746     CmsProject getTestProject(CmsObject cms) {
747         
748         CmsProject project = null;
749         
750         try {
751             project = cms.readProject("Test");
752         } catch (Exception JavaDoc e) {
753             try {
754                 project = cms.createProject("Test", "", "Users", "Administrators");
755             } catch (Exception JavaDoc ee) {
756                 // noop
757
}
758         }
759         
760         return project;
761     }
762 }
763
Popular Tags