KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestPublishIssues.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.db.CmsPublishList;
35 import org.opencms.file.types.CmsResourceTypeFolder;
36 import org.opencms.file.types.CmsResourceTypePlain;
37 import org.opencms.lock.CmsLock;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.OpenCms;
40 import org.opencms.report.CmsShellReport;
41 import org.opencms.test.OpenCmsTestCase;
42 import org.opencms.test.OpenCmsTestProperties;
43 import org.opencms.util.CmsUUID;
44
45 import java.util.Collections JavaDoc;
46 import java.util.List JavaDoc;
47
48 import junit.extensions.TestSetup;
49 import junit.framework.Test;
50 import junit.framework.TestSuite;
51
52 /**
53  * Unit tests for special publish issues.<p>
54  *
55  * @author Alexander Kandzior
56  *
57  * @version $Revision: 1.21 $
58  */

59 /**
60  * Comment for <code>TestPermissions</code>.<p>
61  */

62 public class TestPublishIssues extends OpenCmsTestCase {
63   
64     /**
65      * Default JUnit constructor.<p>
66      *
67      * @param arg0 JUnit parameters
68      */

69     public TestPublishIssues(String JavaDoc arg0) {
70         super(arg0);
71     }
72     
73     /**
74      * Test suite for this test class.<p>
75      *
76      * @return the test suite
77      */

78     public static Test suite() {
79         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
80         
81         TestSuite suite = new TestSuite();
82         suite.setName(TestPublishIssues.class.getName());
83                 
84         suite.addTest(new TestPublishIssues("testPublishScenarioA"));
85         suite.addTest(new TestPublishIssues("testPublishScenarioB"));
86         suite.addTest(new TestPublishIssues("testPublishScenarioC"));
87         suite.addTest(new TestPublishIssues("testMultipleProjectCreation"));
88         suite.addTest(new TestPublishIssues("testDirectPublishWithSiblings"));
89         suite.addTest(new TestPublishIssues("testPublishScenarioD"));
90         suite.addTest(new TestPublishIssues("testPublishScenarioE"));
91         
92         TestSetup wrapper = new TestSetup(suite) {
93             
94             protected void setUp() {
95                 setupOpenCms("simpletest", "/sites/default/");
96             }
97             
98             protected void tearDown() {
99                 removeOpenCms();
100             }
101         };
102         
103         return wrapper;
104     }
105  
106     /**
107      * Tests publish scenario "A".<p>
108      *
109      * This scenario is described as follows:
110      * We have users "test1" and "test2".
111      * We have two projects, "project1" and "project2".
112      * Project "project1" consists of the folder "/".
113      * Project "project2" consists of the folder "/folder1/subfolder11/".
114      * User "test2" edits the file "/folder1/subfolder11/index.html".
115      * After this, user "test1" locks the folder "/folder1" in "project1", and unlocks it again.
116      * User "test2" logs in and now publishes "project2".<p>
117      *
118      * Wanted result: the changed resource "/folder1/subfolder11/index.html" is published
119      * with "project2".<p>
120      *
121      * The test illustrates a change in the logic from OpenCms 5 to OpenCms 6:
122      * In OpenCms 5, locking a file caused it to switch to the current users project.
123      * In OpenCms 6, this is no longer true, at last not if you just lock a parent folder.
124      * So in OpenCms 5, this test would fail, since the resource would be in "project1", not "project2".<p>
125      *
126      * @throws Throwable if something goes wrong
127      */

128     public void testPublishScenarioA() throws Throwable JavaDoc {
129         
130         CmsObject cms = getCmsObject();
131         echo("Testing publish scenario A");
132         String JavaDoc projectRes1 = "/folder1/subfolder11/";
133         String JavaDoc resource1 = projectRes1 + "index.html";
134         String JavaDoc resource2 = "/folder1/";
135         long timestamp = System.currentTimeMillis();
136                 
137         // we use the default "Offline" project as "project1"
138
CmsProject project1 = cms.readProject("Offline");
139         
140         // create "project2" as Admin user
141
cms.createProject(
142             "project2",
143             "Test project 2 for scenario A",
144             OpenCms.getDefaultUsers().getGroupUsers(),
145             OpenCms.getDefaultUsers().getGroupUsers());
146
147         CmsProject project2 = cms.readProject("project2");
148         cms.getRequestContext().setCurrentProject(project2);
149         cms.copyResourceToProject(projectRes1);
150         
151         // check if the project was created as planned
152
List resources = cms.readProjectResources(project2);
153         assertEquals(1, resources.size());
154         assertEquals("/sites/default" + projectRes1, (String JavaDoc)resources.get(0));
155         
156         // login as user "test2"
157
cms.loginUser("test2", "test2");
158         cms.getRequestContext().setCurrentProject(project2);
159                 
160         // perform some edit on the file
161
cms.lockResource(resource1);
162         cms.setDateLastModified(resource1, System.currentTimeMillis(), false);
163         
164         // assert some basic status info
165
assertDateLastModifiedAfter(cms, resource1, timestamp);
166         assertProject(cms, resource1, project2);
167         assertLock(cms, resource1, CmsLock.TYPE_EXCLUSIVE);
168         assertState(cms, resource1, CmsResource.STATE_CHANGED);
169         
170         // now login as user "test1" (default will be in the "Offline" project)
171
cms.loginUser("test1", "test1");
172         cms.getRequestContext().setCurrentProject(project1);
173         
174         // lock the folder
175
cms.lockResource(resource2);
176         
177         // assert some basic status info
178
assertProject(cms, resource1, project2);
179         assertLock(cms, resource1, CmsLock.TYPE_INHERITED);
180         
181         // now unlock the folder again
182
cms.unlockResource(resource2);
183         
184         // back to the user "test2"
185
cms.loginUser("test2", "test2");
186         cms.getRequestContext().setCurrentProject(project2);
187
188         // get the publish list
189
CmsPublishList publishList = cms.getPublishList();
190         assertEquals(1, publishList.getFileList().size());
191         
192         // project should have no locked resources
193
int resourceProjectCount = cms.countLockedResources(project2.getId());
194         assertEquals(0, resourceProjectCount);
195         
196         // unlock the project
197
cms.publishProject();
198         
199         // ensure the file was published - state must be "unchanged"
200
assertState(cms, resource1, CmsResource.STATE_UNCHANGED);
201     }
202     
203     /**
204      * Tests publish scenario "B".<p>
205      *
206      * This scenario is described as follows:
207      * We have users "test1" and "test2" and projects "projectA" and "projectB".
208      * Both projects contain all resources.
209      * We have two folders "/foldera/" and "/folderb/".
210      * There is a resource "test.txt" that has a sibling in both folders.
211      * User "test1" locks folder "/foldera/" and user "test2" locks folder "/folderb".
212      * Now both users try to edit the sibling of "test.txt" in their folder.
213      *
214      * TODO: How are concurrent file modifications avoided on the sibling?.<p>
215      *
216      * @throws Throwable if something goes wrong
217      */

218     public void testPublishScenarioB() throws Throwable JavaDoc {
219         
220         CmsObject cms = getCmsObject();
221         echo("Testing publish scenario B");
222         
223         // set up the test case first
224
String JavaDoc folderA = "/foldera/";
225         String JavaDoc folderB = "/folderb/";
226         String JavaDoc resourceA = folderA + "test.txt";
227         String JavaDoc resourceB = folderB + "test.txt";
228         
229         // create the resource and the sibling
230
cms.createResource(folderA, CmsResourceTypeFolder.getStaticTypeId());
231         cms.createResource(folderB, CmsResourceTypeFolder.getStaticTypeId());
232         cms.createResource(resourceA, CmsResourceTypePlain.getStaticTypeId());
233         cms.createSibling(resourceA, resourceB, Collections.EMPTY_LIST);
234         CmsFile cmsFile = cms.readFile(resourceA);
235         cmsFile.setContents("Hello, this is a test!".getBytes());
236         cms.writeFile(cmsFile);
237         
238         // now publish the project
239
cms.unlockProject(cms.getRequestContext().currentProject().getId());
240         cms.publishProject();
241         
242         // check if the setup was created as planned
243
cmsFile = cms.readFile(resourceA);
244         assertEquals(2, cmsFile.getSiblingCount());
245         assertState(cms, resourceA, CmsResource.STATE_UNCHANGED);
246         cmsFile = cms.readFile(resourceB);
247         assertEquals(2, cmsFile.getSiblingCount());
248         assertState(cms, resourceB, CmsResource.STATE_UNCHANGED);
249         
250         // we use the default "Offline" project as "projectA"
251
// CmsProject projectA = cms.readProject("Offline");
252

253         // create "projectB" as Admin user
254
cms.createProject(
255             "projectB",
256             "Test project 2 for scenario B",
257             OpenCms.getDefaultUsers().getGroupUsers(),
258             OpenCms.getDefaultUsers().getGroupUsers());
259
260         CmsProject projectB = cms.readProject("projectB");
261         cms.getRequestContext().setCurrentProject(projectB);
262         cms.copyResourceToProject("/");
263         
264         // check if the project was created as planned
265
List resources = cms.readProjectResources(projectB);
266         assertEquals(1, resources.size());
267         assertEquals("/sites/default/", (String JavaDoc)resources.get(0));
268         
269         // TODO: The wanted behaviour in this case must be defined!
270
}
271     
272     /**
273      * Tests multiple creation of a project with the same name.<p>
274      *
275      * @throws Throwable if something goes wrong
276      */

277     public void testMultipleProjectCreation() throws Throwable JavaDoc {
278         
279         CmsObject cms = getCmsObject();
280         echo("Testing multiple creation of a project with the same name");
281         
282         String JavaDoc projectName = "projectX";
283         boolean gotException;
284         
285         // try to read a non-existant project
286
gotException = false;
287         try {
288             cms.readProject(projectName);
289         } catch (CmsException e) {
290             gotException = true;
291         }
292         if (! gotException) {
293             fail("Required exception was not thrown!");
294         }
295         
296         // create the project
297
cms.createProject(
298             projectName,
299             "Test project",
300             OpenCms.getDefaultUsers().getGroupUsers(),
301             OpenCms.getDefaultUsers().getGroupUsers());
302
303         CmsProject project = cms.readProject(projectName);
304         cms.getRequestContext().setCurrentProject(project);
305         cms.copyResourceToProject("/folder1/");
306         
307         // check if the project was created as planned
308
List resources = cms.readProjectResources(project);
309         assertEquals(1, resources.size());
310         assertEquals("/sites/default/folder1/", (String JavaDoc)resources.get(0));
311         
312         // copy the root folder of the sito to the project - this must remove the "/folder1/" folder
313
cms.copyResourceToProject("/");
314         resources = cms.readProjectResources(project);
315         assertEquals(1, resources.size());
316         assertEquals("/sites/default/", (String JavaDoc)resources.get(0));
317         
318         // now create the project again - this must throw an exception
319
gotException = false;
320         CmsProject newProject = cms.createProject(
321             projectName,
322             "Test project 2nd time",
323             OpenCms.getDefaultUsers().getGroupUsers(),
324             OpenCms.getDefaultUsers().getGroupUsers());
325         
326         // TODO: above create statement fails "sometimes" - check table contraints (?)
327

328         // check if the projects have different ids
329
int id1 = project.getId();
330         int id2 = newProject.getId();
331         if (id1 == id2) {
332             fail("Two different projects created with same name have the same id!");
333         }
334         
335         // read the projects again and asserts same name but differnet description
336
project = cms.readProject(id1);
337         newProject = cms.readProject(id2);
338         assertEquals(project.getName(), newProject.getName());
339         if (project.getDescription().equals(newProject.getDescription())) {
340             fail("Projects should have differnet descriptions!");
341         }
342     }
343     
344     /**
345      * Tests publish scenario "C".<p>
346      *
347      * This scenario is described as follows:
348      * Direct publishing of folders containing subfolders skips all changed subfolders e.g. direct publish of /folder1/
349      * publishes /folder1/ and /folder1/index.html/, but not /folder1/subfolder11/.<p>
350      *
351      * @throws Throwable if something goes wrong
352      */

353     public void testPublishScenarioC() throws Throwable JavaDoc {
354         
355         CmsObject cms = getCmsObject();
356         echo("Testing publish scenario C");
357         
358         long touchTime = System.currentTimeMillis();
359         
360         cms.lockResource("/folder1/");
361         cms.setDateLastModified("/folder1/", touchTime, false);
362         cms.setDateLastModified("/folder1/index.html", touchTime, false);
363         cms.setDateLastModified("/folder1/subfolder11/", touchTime, false);
364         cms.setDateLastModified("/folder1/subfolder11/index.html", touchTime, false);
365         
366         cms.unlockResource("/folder1/");
367         cms.publishResource("/folder1/");
368         
369         assertState(cms, "/folder1/", CmsResource.STATE_UNCHANGED);
370         assertState(cms, "/folder1/index.html", CmsResource.STATE_UNCHANGED);
371         assertState(cms, "/folder1/subfolder11/", CmsResource.STATE_UNCHANGED);
372         assertState(cms, "/folder1/subfolder11/index.html", CmsResource.STATE_UNCHANGED);
373         
374         cms.createResource("/folder_a/", CmsResourceTypeFolder.getStaticTypeId());
375         cms.createResource("/folder_a/file_a.txt", CmsResourceTypePlain.getStaticTypeId());
376         cms.createResource("/folder_a/folder_b/", CmsResourceTypeFolder.getStaticTypeId());
377         cms.createResource("/folder_a/folder_b/file_b.txt", CmsResourceTypePlain.getStaticTypeId());
378         
379         cms.unlockResource("/folder_a/");
380         cms.publishResource("/folder_a/");
381         
382         assertState(cms, "/folder_a/", CmsResource.STATE_UNCHANGED);
383         assertState(cms, "/folder_a/file_a.txt", CmsResource.STATE_UNCHANGED);
384         assertState(cms, "/folder_a/folder_b/", CmsResource.STATE_UNCHANGED);
385         assertState(cms, "/folder_a/folder_b/file_b.txt", CmsResource.STATE_UNCHANGED);
386         
387     }
388     
389     /**
390      * Tests publish scenario "D".<p>
391      *
392      * This scenario is described as follows:
393      * Direct publishing of folders containing subfolders skips all (sibling)
394      * resources in subfolders.
395      * e.g. direct publish of /folder2/folder1/
396      * publishes /folder2/folder1/ and /folder2/folder1/index.html/,
397      * but not /folder2/folder1/subfolder11/index.html.<p>
398      *
399      * @throws Throwable if something goes wrong
400      */

401     public void testPublishScenarioD() throws Throwable JavaDoc {
402         
403         CmsObject cms = getCmsObject();
404         echo("Testing publish scenario D");
405         
406         // change to the offline project
407
CmsProject project = cms.readProject("Offline");
408         cms.getRequestContext().setCurrentProject(project);
409
410         
411         cms.lockResource("/folder1/");
412         // copy the whole folder creating siblings of all resources
413
cms.copyResource("/folder1/", "/folder2/folder1", CmsResource.COPY_AS_SIBLING);
414         cms.unlockResource("/folder1/");
415         
416         // direct publish the new folder
417
cms.unlockResource("/folder2/folder1/");
418         cms.publishResource("/folder2/folder1/");
419         
420         assertState(cms, "/folder2/folder1/", CmsResource.STATE_UNCHANGED);
421         assertState(cms, "/folder2/folder1/index.html", CmsResource.STATE_UNCHANGED);
422         assertState(cms, "/folder2/folder1/subfolder11/", CmsResource.STATE_UNCHANGED);
423         assertState(cms, "/folder2/folder1/subfolder11/index.html", CmsResource.STATE_UNCHANGED);
424         assertState(cms, "/folder2/folder1/subfolder11/subsubfolder111/", CmsResource.STATE_UNCHANGED);
425         assertState(cms, "/folder2/folder1/subfolder11/subsubfolder111/jsp.jsp", CmsResource.STATE_UNCHANGED);
426         assertState(cms, "/folder2/folder1/subfolder12/", CmsResource.STATE_UNCHANGED);
427         assertState(cms, "/folder2/folder1/subfolder12/index.html", CmsResource.STATE_UNCHANGED);
428         assertState(cms, "/folder2/folder1/subfolder12/subsubfolder121/", CmsResource.STATE_UNCHANGED);
429         assertState(cms, "/folder2/folder1/subfolder12/subsubfolder121/index.html", CmsResource.STATE_UNCHANGED);
430         
431     }
432     
433     /**
434      * Tests publish scenario "E".<p>
435      *
436      * This scenario is described as follows:
437      * Deletion of folders containing shared locked siblings after copying
438      * a folder creating siblings into a new folder and publication. <p>
439      *
440      * @throws Throwable if something goes wrong
441      */

442     public void testPublishScenarioE() throws Throwable JavaDoc {
443         
444         CmsObject cms = getCmsObject();
445         echo("Testing publish scenario E");
446
447         // change to the offline project
448
CmsProject project = cms.readProject("Offline");
449         cms.getRequestContext().setCurrentProject(project);
450       
451         // create folder
452
cms.createResource("/test", CmsResourceTypeFolder.getStaticTypeId());
453         
454         // create siblings
455
cms.copyResource("/folder1/subfolder12/subsubfolder121", "/test/subtest", CmsResource.COPY_AS_SIBLING);
456         cms.unlockResource("/test");
457         
458         // publish
459
cms.publishResource("/test");
460
461         // lock sibling
462
cms.lockResource("/folder1/subfolder12/subsubfolder121/image1.gif");
463         
464         // login as user test2
465
cms.addUserToGroup("test2", "Projectmanagers");
466         cms.loginUser("test2", "test2");
467         cms.getRequestContext().setCurrentProject(project);
468         
469         // check lock
470
assertEquals(cms.getLock("/test/subtest/image1.gif").getType(), CmsLock.TYPE_SHARED_EXCLUSIVE);
471         
472         // delete the folder
473
cms.lockResource("/test/subtest");
474         cms.deleteResource("/test/subtest", CmsResource.DELETE_PRESERVE_SIBLINGS);
475         cms.unlockResource("/test/subtest");
476
477         // publish
478
cms.publishResource("/test");
479         
480     }
481     
482     /**
483      * Tests publish scenario "publish all Siblings".<p>
484      *
485      * This scenario is described as follows:
486      * If a direct publish is made, and the option "publish all siblings" is selected,
487      * a file that contains siblings will be added multiple times to the publish list,
488      * causing a primary key collision in the publish history table.<p>
489      *
490      * @throws Throwable if something goes wrong
491      */

492     public void testDirectPublishWithSiblings() throws Throwable JavaDoc {
493         
494         CmsObject cms = getCmsObject();
495         echo("Testing publish scenario using 'publish all Siblings'");
496         
497         cms.lockResource("/folder1/");
498         cms.setDateLastModified("/folder1/", System.currentTimeMillis(), true);
499         cms.unlockResource("/folder1/");
500         
501         // publish the project (this did cause an exception because of primary key violation!)
502
CmsUUID publishId = cms.publishResource("/folder1/", true, new CmsShellReport(cms.getRequestContext().getLocale()));
503         
504         // read the published resources from the history
505
List publishedResources = cms.readPublishedResources(publishId);
506         
507         // make sure the publish history contains the required amount of resources
508
assertEquals(35, publishedResources.size());
509     }
510 }
511
Popular Tags