KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestSiblings.java,v $
3  * Date : $Date: 2006/04/27 15:32:45 $
4  * Version: $Revision: 1.18 $
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.CmsResourceTypeBinary;
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.OpenCms;
39 import org.opencms.report.CmsShellReport;
40 import org.opencms.test.OpenCmsTestCase;
41 import org.opencms.test.OpenCmsTestProperties;
42 import org.opencms.test.OpenCmsTestResourceFilter;
43 import org.opencms.util.CmsResourceTranslator;
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 test for operations on siblings.<p>
54  *
55  * @author Thomas Weckert
56  * @version $Revision: 1.18 $
57  */

58 public class TestSiblings extends OpenCmsTestCase {
59
60     /**
61      * Default JUnit constructor.<p>
62      *
63      * @param arg0 JUnit parameters
64      */

65     public TestSiblings(String JavaDoc arg0) {
66
67         super(arg0);
68     }
69     
70     /**
71      * Test suite for this test class.<p>
72      *
73      * @return the test suite
74      */

75     public static Test suite() {
76         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
77         
78         TestSuite suite = new TestSuite();
79         suite.setName(TestSiblings.class.getName());
80         
81         suite.addTest(new TestSiblings("testSiblingsCopy"));
82         suite.addTest(new TestSiblings("testSiblingsCreate"));
83         suite.addTest(new TestSiblings("testSiblingIssueAfterImport"));
84         suite.addTest(new TestSiblings("testDeleteAllSiblings"));
85         suite.addTest(new TestSiblings("testSiblingStateIssue"));
86         
87         TestSetup wrapper = new TestSetup(suite) {
88             
89             protected void setUp() {
90                 setupOpenCms("simpletest", "/sites/default/");
91             }
92             
93             protected void tearDown() {
94                 removeOpenCms();
95             }
96         };
97         
98         return wrapper;
99     }
100
101     /**
102      * Creates a copy of a resource as a new sibling.<p>
103      *
104      * @param tc the OpenCms test case
105      * @param cms the current user's Cms object
106      * @param source path/resource name of the existing resource
107      * @param target path/resource name of the new sibling
108      * @throws Throwable if something goes wrong
109      */

110     public static void copyResourceAsSibling(OpenCmsTestCase tc, CmsObject cms, String JavaDoc source, String JavaDoc target)
111     throws Throwable JavaDoc {
112
113         // save the source in the store
114
tc.storeResources(cms, source);
115         
116         // copy source to target as a sibling, the new sibling should not be locked
117
cms.copyResource(source, target, CmsResource.COPY_AS_SIBLING);
118
119         // validate the source sibling
120

121         // validate if all unmodified fields on the source resource are still equal
122
tc.assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING);
123         // validate if the last-modified-in-project field is the current project
124
tc.assertProject(cms, source, cms.getRequestContext().currentProject());
125         // validate if the sibling count field has been incremented
126
tc.assertSiblingCountIncremented(cms, source, 1);
127         // validate if the sibling does not have a red flag
128
tc.assertModifiedInCurrentProject(cms, source, false);
129         // validate if the lock is an exclusive shared lock for the current user
130
tc.assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
131
132         // validate the target sibling
133

134         // validate the fields that both in the existing and the new sibling have to be equal
135
tc.assertFilter(cms, source, target, OpenCmsTestResourceFilter.FILTER_EXISTING_AND_NEW_SIBLING);
136         // validate if the state of the new sibling is "new" (blue)
137
tc.assertState(cms, target, CmsResource.STATE_NEW);
138         // validate if the new sibling has a red flag
139
tc.assertModifiedInCurrentProject(cms, target, true);
140         // validate if the lock is an exclusive lock for the current user
141
tc.assertLock(cms, target, CmsLock.TYPE_EXCLUSIVE);
142     }
143
144     /**
145      * Creates a new sibling of a resource.<p>
146      *
147      * @param tc the OpenCms test case
148      * @param cms the current user's Cms object
149      * @param source path/resource name of the existing resource
150      * @param target path/resource name of the new sibling
151      * @throws Throwable if something goes wrong
152      */

153     public static void createSibling(OpenCmsTestCase tc, CmsObject cms, String JavaDoc source, String JavaDoc target) throws Throwable JavaDoc {
154
155         // save the source in the store
156
tc.storeResources(cms, source);
157         
158         // create a new sibling from the source
159
List JavaDoc properties = cms.readPropertyObjects(source, false);
160         cms.createSibling(source, target, properties);
161
162         // validate the source sibling
163

164         // validate if all unmodified fields of the source are still equal
165
tc.assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING);
166         // validate if the last-modified-in-project field is the current project
167
tc.assertProject(cms, source, cms.getRequestContext().currentProject());
168         // validate if the sibling count field has been incremented
169
tc.assertSiblingCountIncremented(cms, source, 1);
170         // validate if the sibling does not have a red flag
171
tc.assertModifiedInCurrentProject(cms, source, false);
172         // validate if the lock is an exclusive shared lock for the current user
173
tc.assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
174
175         // validate the target sibling
176

177         // validate the fields that both in the existing and the new sibling have to be equal
178
tc.assertFilter(cms, source, target, OpenCmsTestResourceFilter.FILTER_EXISTING_AND_NEW_SIBLING);
179         // validate if the state of the new sibling is "new" (blue)
180
tc.assertState(cms, target, CmsResource.STATE_NEW);
181         // validate if the new sibling has a red flag
182
tc.assertModifiedInCurrentProject(cms, target, true);
183         // validate if the lock is an exclusive lock for the current user
184
tc.assertLock(cms, target, CmsLock.TYPE_EXCLUSIVE);
185     }
186     
187     /**
188      * Tests the "copy as new sibling" function.<p>
189      *
190      * @throws Throwable if something goes wrong
191      */

192     public void testSiblingsCopy() throws Throwable JavaDoc {
193
194         CmsObject cms = getCmsObject();
195         String JavaDoc source = "/index.html";
196         String JavaDoc target = "/index_sibling.html";
197         echo("Copying " + source + " as a new sibling to " + target);
198         copyResourceAsSibling(this, cms, source, target);
199     }
200     
201     /**
202      * Tests the "copy as new sibling" function.<p>
203      *
204      * @throws Throwable if something goes wrong
205      */

206     public void testSiblingsCreate() throws Throwable JavaDoc {
207
208         CmsObject cms = getCmsObject();
209         String JavaDoc source = "/folder1/image1.gif";
210         String JavaDoc target = "/folder1/image1_sibling.gif";
211         echo("Creating a new sibling " + target + " from " + source);
212         createSibling(this, cms, source, target);
213     }
214     
215     /**
216      * Error scenario where an import is deleted that contains siblings inside the same folders.<p>
217      *
218      * @throws Exception if something goes wrong
219      */

220     public void testSiblingIssueAfterImport() throws Exception JavaDoc {
221         
222         echo("Testing sibling issue after import");
223         
224         CmsResourceTranslator oldFolderTranslator = OpenCms.getResourceManager().getFolderTranslator();
225         
226         CmsResourceTranslator folderTranslator = new CmsResourceTranslator(
227             new String JavaDoc[] {
228                 "s#^/sites/default/content/bodys(.*)#/system/bodies$1#",
229                 "s#^/sites/default/pics/system(.*)#/system/workplace/resources$1#",
230                 "s#^/sites/default/pics(.*)#/system/galleries/pics$1#",
231                 "s#^/sites/default/download(.*)#/system/galleries/download$1#",
232                 "s#^/sites/default/externallinks(.*)#/system/galleries/externallinks$1#",
233                 "s#^/sites/default/htmlgalleries(.*)#/system/galleries/htmlgalleries$1#",
234                 "s#^/sites/default/content(.*)#/system$1#"
235                 },
236             false);
237         
238         // set modified folder translator
239
OpenCms.getResourceManager().setTranslators(
240             folderTranslator,
241             OpenCms.getResourceManager().getFileTranslator());
242
243         try {
244             
245             CmsObject cms = getCmsObject();
246             
247             cms.getRequestContext().setSiteRoot("/");
248     
249             // need to create the "galleries" folder manually
250
cms.createResource("/system/galleries", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
251             cms.unlockResource("/system/galleries");
252             
253             cms.getRequestContext().setSiteRoot("/sites/default");
254             
255             // import the files
256
String JavaDoc importFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testimport01.zip");
257             OpenCms.getImportExportManager().importData(cms, importFile, "/", new CmsShellReport(cms.getRequestContext().getLocale()));
258             
259             // clean up for the next test
260
cms.getRequestContext().setSiteRoot("/");
261             cms.lockResource("/sites/default");
262             cms.lockResource("/system");
263             
264             // using the option "DELETE_REMOVE_SIBLINGS" caused an error here!
265
cms.deleteResource("/sites/default/importtest", CmsResource.DELETE_REMOVE_SIBLINGS);
266             cms.deleteResource("/system/bodies", CmsResource.DELETE_REMOVE_SIBLINGS);
267             cms.deleteResource("/system/galleries/pics", CmsResource.DELETE_REMOVE_SIBLINGS);
268             
269             cms.unlockResource("/sites/default");
270             cms.unlockResource("/system");
271             cms.publishProject();
272             
273         } finally {
274             
275             // reset the translation rules
276
OpenCms.getResourceManager().setTranslators(
277                 oldFolderTranslator,
278                 OpenCms.getResourceManager().getFileTranslator());
279             
280         }
281     }
282     
283     /**
284      * Does an "undo changes" from the online project on a resource with more than 1 sibling.<p>
285      */

286     /*
287     public static void undoChangesWithSiblings(...) throws Throwable {
288         // this test should do the following:
289         // - create a sibling of a resource
290         // - e.g. touch the black/unchanged sibling so that it gets red/changed
291         // - make an "undo changes" -> the last-modified-in-project ID in the resource record
292         // of the resource must be the ID of the current project, and not 0
293         // - this is to ensure that the new/changed/deleted other sibling still have a valid
294         // state which consits of the last-modified-in-project ID plus the resource state
295         // - otherwise this may result in grey flags
296
297         Another issue:
298         What happens if a user A has an exclusive lock on a resource X,
299         and user B does a "copy as sibling Y" of X, or "create
300         new sibling Y" of X. The lock status of the resource X is exclusive
301         to A, but test implies that it would be switched to B after operation!
302         Maybe copy as / create new sibling must not be allowed if original is
303         currently locked by another user?
304
305     }
306     */

307     
308     /**
309      * Tests if setting the flags of a sibling will do any modifications to other siblings.<p>
310      *
311      * @throws Throwable if something goes wrong
312      */

313     public void testSiblingStateIssue() throws Throwable JavaDoc {
314         
315         echo("Tests issue with resource state and siblings");
316         CmsObject cms = getCmsObject();
317         
318         // part 1: changes to the fields that are part of the resource table
319
String JavaDoc resource1 = "/folder1/page1.html";
320         String JavaDoc sibling1 = "/folder1/sibling1.html";
321         
322         // create a sibling
323
cms.copyResource(resource1, sibling1, CmsResource.COPY_AS_SIBLING);
324         
325         // verify the state of the resources
326
assertState(cms, resource1, CmsResource.STATE_UNCHANGED);
327         assertState(cms, sibling1, CmsResource.STATE_NEW);
328         
329         // now set the flags for the sibling
330
cms.chflags(sibling1, 1024);
331         cms.chtype(sibling1, CmsResourceTypeBinary.getStaticTypeId());
332         
333         // verify the state of the resources after the change
334
assertState(cms, resource1, CmsResource.STATE_CHANGED);
335         assertState(cms, sibling1, CmsResource.STATE_NEW);
336
337         
338         // part 2: now the same operation with a new copy
339
String JavaDoc copy1 = "/folder1/copy1.html";
340         sibling1 = "/folder1/siblingofcopy1.html";
341
342         // create a copy
343
cms.copyResource(resource1, copy1, CmsResource.COPY_AS_NEW);
344         cms.copyResource(copy1, sibling1, CmsResource.COPY_AS_SIBLING);
345         
346         // verify the state of the resources
347
assertState(cms, copy1, CmsResource.STATE_NEW);
348         assertState(cms, sibling1, CmsResource.STATE_NEW);
349         
350         // now set the flags for the sibling
351
cms.chflags(sibling1, 1024);
352         cms.chtype(sibling1, CmsResourceTypeBinary.getStaticTypeId());
353         
354         // verify the state of the resources after the change
355
assertState(cms, copy1, CmsResource.STATE_NEW);
356         assertState(cms, sibling1, CmsResource.STATE_NEW);
357         
358         
359         // part 3: changes to the fields that are part of the structure table
360
resource1 = "/folder1/page2.html";
361         sibling1 = "/folder1/sibling2.html";
362         
363         // create a sibling
364
cms.copyResource(resource1, sibling1, CmsResource.COPY_AS_SIBLING);
365         
366         // verify the state of the resources
367
assertState(cms, resource1, CmsResource.STATE_UNCHANGED);
368         assertState(cms, sibling1, CmsResource.STATE_NEW);
369         
370         // after changes of dates the resource states must be the same
371
cms.setDateExpired(sibling1, System.currentTimeMillis() + 1000, false);
372         cms.setDateReleased(sibling1, System.currentTimeMillis() - 1000, false);
373
374         // verify the state of the resources after the change
375
assertState(cms, resource1, CmsResource.STATE_UNCHANGED);
376         assertState(cms, sibling1, CmsResource.STATE_NEW);
377         
378         
379         // step 4: changes to the fields that are part of the resource table
380
cms.setDateLastModified(sibling1, System.currentTimeMillis(), false);
381         
382         // verify the state of the resources after the change
383
assertState(cms, resource1, CmsResource.STATE_CHANGED);
384         assertState(cms, sibling1, CmsResource.STATE_NEW);
385         
386         
387         // part 5: now the same operation with a new copy
388
copy1 = "/folder1/copy2.html";
389         sibling1 = "/folder1/siblingofcopy2.html";
390         
391         // create a copy
392
cms.copyResource(resource1, copy1, CmsResource.COPY_AS_NEW);
393         cms.copyResource(copy1, sibling1, CmsResource.COPY_AS_SIBLING);
394         
395         // verify the state of the resources
396
assertState(cms, copy1, CmsResource.STATE_NEW);
397         assertState(cms, sibling1, CmsResource.STATE_NEW);
398         
399         // change date of last modification
400
cms.setDateLastModified(sibling1, System.currentTimeMillis(), false);
401         
402         // verify the state of the resources after the change
403
assertState(cms, copy1, CmsResource.STATE_NEW);
404         assertState(cms, sibling1, CmsResource.STATE_NEW);
405         
406         // modifiy release info
407
cms.setDateExpired(sibling1, System.currentTimeMillis() + 1000, false);
408         cms.setDateReleased(sibling1, System.currentTimeMillis() - 1000, false);
409         
410         // verify the state of the resources after the change
411
assertState(cms, copy1, CmsResource.STATE_NEW);
412         assertState(cms, sibling1, CmsResource.STATE_NEW);
413     }
414     
415     /**
416      * Tests deletion of a resource together with all siblings.<p>
417      *
418      * @throws Throwable if something goes wrong
419      */

420     public void testDeleteAllSiblings() throws Throwable JavaDoc {
421
422         echo("Creating a new resource with 2 siblings, then deleting it with all siblings again");
423         CmsObject cms = getCmsObject();
424         
425         String JavaDoc sib1Name = "/folder1/sib1.txt";
426         String JavaDoc sib2Name = "/folder1/sib2.txt";
427         String JavaDoc sib3Name = "/folder1/sib3.txt";
428
429         cms.createResource(sib1Name, CmsResourceTypePlain.getStaticTypeId());
430         cms.createSibling(sib1Name, sib2Name, Collections.EMPTY_LIST);
431         cms.createSibling(sib1Name, sib3Name, Collections.EMPTY_LIST);
432         
433         cms.deleteResource(sib1Name, CmsResource.DELETE_REMOVE_SIBLINGS);
434         
435         CmsResource sib2Resource = null;
436         try {
437             sib2Resource = cms.readResource(sib2Name);
438         } catch (CmsVfsResourceNotFoundException e) {
439             // intentionally left blank
440
}
441         
442         if (sib2Resource != null) {
443             fail("Sibling " + sib2Name + " has not been deleted!");
444         }
445         
446         CmsResource sib3Resource = null;
447         try {
448             sib3Resource = cms.readResource(sib3Name);
449         } catch (CmsVfsResourceNotFoundException e) {
450             // intentionally left blank
451
}
452         
453         if (sib3Resource != null) {
454             fail("Sibling " + sib3Name + " has not been deleted!");
455         }
456     }
457 }
Popular Tags