KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestUndoChanges.java,v $
3  * Date : $Date: 2006/09/21 09:34:47 $
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.CmsResourceTypePlain;
35 import org.opencms.lock.CmsLock;
36 import org.opencms.test.OpenCmsTestCase;
37 import org.opencms.test.OpenCmsTestProperties;
38 import org.opencms.test.OpenCmsTestResourceFilter;
39 import org.opencms.test.OpenCmsTestResourceStorage;
40
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43
44 import junit.extensions.TestSetup;
45 import junit.framework.Test;
46 import junit.framework.TestSuite;
47
48 /**
49  * Unit test for the "undoChanges" method of the CmsObject.<p>
50  *
51  * @author Michael Emmerich
52  * @version $Revision: 1.21 $
53  */

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

61     public TestUndoChanges(String JavaDoc arg0) {
62
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
73         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
74
75         TestSuite suite = new TestSuite();
76         suite.setName(TestUndoChanges.class.getName());
77
78         suite.addTest(new TestUndoChanges("testUndoChangesResource"));
79         suite.addTest(new TestUndoChanges("testUndoChangesOnNewResource"));
80         suite.addTest(new TestUndoChanges("testUndoChangesFolder"));
81         suite.addTest(new TestUndoChanges("testUndoChangesFolderRecursive"));
82         suite.addTest(new TestUndoChanges("testUndoChangesAfterCopyNewOverDeleted"));
83         suite.addTest(new TestUndoChanges("testUndoChangesAfterCopySiblingOverDeleted"));
84         suite.addTest(new TestUndoChanges("testUndoChangesWithAce"));
85         suite.addTest(new TestUndoChanges("testUndoChangesSharedProperty"));
86
87         TestSetup wrapper = new TestSetup(suite) {
88
89             protected void setUp() {
90
91                 setupOpenCms("simpletest", "/sites/default/");
92             }
93
94             protected void tearDown() {
95
96                 removeOpenCms();
97             }
98         };
99
100         return wrapper;
101     }
102
103     /**
104      * Tests undo changes on a new resource, this must lead to an exception!<p>
105      *
106      * @throws Throwable if something goes wrong
107      */

108     public void testUndoChangesOnNewResource() throws Throwable JavaDoc {
109
110         CmsObject cms = getCmsObject();
111         echo("Testing for exception when trying undo changes on a new resource");
112
113         String JavaDoc source = "/types/new.html";
114
115         // create a new, plain resource
116
cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
117         assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
118
119         try {
120             cms.undoChanges(source, false);
121         } catch (CmsVfsException e) {
122             if (e.getMessageContainer().getKey().equals(org.opencms.db.Messages.ERR_UNDO_CHANGES_FOR_RESOURCE_1)) {
123                 // this is the expected result, so the test is successful
124
return;
125             }
126         }
127         fail("Did not catch expected exception trying undo changes on a new resource!");
128     }
129
130     /**
131      * Tests undo changes after a resource was deleted and another
132      * resource was copied over the deleted file "as new".<p>
133      *
134      * @throws Throwable if something goes wrong
135      */

136     public void testUndoChangesAfterCopyNewOverDeleted() throws Throwable JavaDoc {
137
138         CmsObject cms = getCmsObject();
139         echo("Testing undo changes after overwriting a deleted file with a new file");
140
141         String JavaDoc source = "/folder1/page2.html";
142         String JavaDoc destination = "/folder1/page1.html";
143
144         storeResources(cms, source);
145         storeResources(cms, destination);
146
147         cms.lockResource(destination);
148
149         // delete and owerwrite
150
cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
151         assertState(cms, destination, CmsResource.STATE_DELETED);
152
153         cms.copyResource(source, destination, CmsResource.COPY_AS_NEW);
154
155         // now undo all changes on the resource
156
cms.undoChanges(destination, false);
157
158         // now ensure source and destionation are in the original state
159
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);
160         assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
161
162         // publishing may reveal problems with the id's
163
cms.publishProject();
164     }
165
166     /**
167      * Tests undo changes after a resource was deleted and another
168      * resource was copied over the deleted file "as sibling".<p>
169      *
170      * @throws Throwable if something goes wrong
171      */

172     public void testUndoChangesAfterCopySiblingOverDeleted() throws Throwable JavaDoc {
173
174         CmsObject cms = getCmsObject();
175         echo("Testing undo changes after overwriting a deleted file with a sibling");
176
177         String JavaDoc source = "/folder1/page2.html";
178         String JavaDoc destination = "/folder1/page1.html";
179
180         storeResources(cms, source);
181         storeResources(cms, destination);
182
183         cms.lockResource(destination);
184
185         // delete and owerwrite with a sibling
186
cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
187         assertState(cms, destination, CmsResource.STATE_DELETED);
188
189         cms.copyResource(source, destination, CmsResource.COPY_AS_SIBLING);
190
191         // now undo all changes on the resource
192
cms.undoChanges(destination, false);
193
194         // now ensure source and destionation are in the original state
195
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
196         assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
197
198         // publishing may reveal problems with the id's
199
cms.publishProject();
200     }
201
202     /**
203      * Test the touch method to touch a single resource.<p>
204      * @param tc the OpenCmsTestCase
205      * @param cms the CmsObject
206      * @param resource1 the resource to touch
207      * @throws Throwable if something goes wrong
208      */

209     public static void undoChanges(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1) throws Throwable JavaDoc {
210
211         // create a global storage and store the resource
212
tc.createStorage("undoChanges");
213         tc.switchStorage("undoChanges");
214         tc.storeResources(cms, resource1);
215         tc.switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE);
216
217         // now do a touch on the resource
218
TestTouch.touchResource(tc, cms, resource1);
219
220         // change a property
221
CmsProperty property1 = new CmsProperty("Title", "undoChanges", null);
222         TestProperty.writeProperty(tc, cms, resource1, property1);
223
224         // now undo everything
225
cms.lockResource(resource1);
226         cms.undoChanges(resource1, false);
227         cms.unlockResource(resource1);
228
229         tc.switchStorage("undoChanges");
230
231         // now evaluate the result
232
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
233         // project must be current project
234
tc.assertProject(cms, resource1, cms.getRequestContext().currentProject());
235     }
236
237     /**
238      * Test undoChanges method to a single folder.<p>
239      * @param tc the OpenCmsTestCase
240      * @param cms the CmsObject
241      * @param resource1 the resource to touch
242      * @throws Throwable if something goes wrong
243      */

244     public static void undoChangesFolder(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1) throws Throwable JavaDoc {
245
246         // create a global storage and store the resource
247
tc.createStorage("undoChanges");
248         tc.switchStorage("undoChanges");
249         tc.storeResources(cms, resource1);
250         tc.switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE);
251
252         long timestamp = System.currentTimeMillis();
253
254         // change a property
255
CmsProperty property1 = new CmsProperty("Title", "undoChanges", null);
256         TestProperty.writeProperty(tc, cms, resource1, property1);
257
258         // change the property on all subresources
259
List JavaDoc subresources = cms.readResources(resource1, CmsResourceFilter.ALL);
260         Iterator JavaDoc i = subresources.iterator();
261         while (i.hasNext()) {
262             CmsResource res = (CmsResource)i.next();
263             String JavaDoc resName = cms.getSitePath(res);
264             TestProperty.writeProperty(tc, cms, resName, property1);
265         }
266
267         // now undo everything
268
cms.lockResource(resource1);
269         cms.undoChanges(resource1, false);
270         cms.unlockResource(resource1);
271
272         tc.switchStorage("undoChanges");
273
274         // now evaluate the result, the folder must be unchanged now
275
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
276         // project must be current project
277
tc.assertProject(cms, resource1, cms.getRequestContext().currentProject());
278
279         // all resources within the folder must keep their changes
280
Iterator JavaDoc j = subresources.iterator();
281         while (j.hasNext()) {
282             CmsResource res = (CmsResource)j.next();
283             String JavaDoc resName = cms.getSitePath(res);
284             tc.assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY);
285             // project must be current project
286
tc.assertProject(cms, resName, cms.getRequestContext().currentProject());
287             // state must be "changed"
288
tc.assertState(cms, resName, tc.getPreCalculatedState(resource1));
289             // date last modified must be after the test timestamp
290
tc.assertDateLastModifiedAfter(cms, resName, timestamp);
291             // the user last modified must be the current user
292
tc.assertUserLastModified(cms, resName, cms.getRequestContext().currentUser());
293             // the property must have the new value
294
tc.assertPropertyChanged(cms, resName, property1);
295         }
296     }
297
298     /**
299      * Test undoChanges method to a single folder and all resources within the folder.<p>
300      * @param tc the OpenCmsTestCase
301      * @param cms the CmsObject
302      * @param resource1 the resource to touch
303      * @throws Throwable if something goes wrong
304      */

305     public static void undoChangesFolderRecursive(OpenCmsTestCase tc, CmsObject cms, String JavaDoc resource1) throws Throwable JavaDoc {
306
307         // create a global storage and store the resource
308
tc.createStorage("undoChanges");
309         tc.switchStorage("undoChanges");
310         tc.storeResources(cms, resource1);
311         tc.switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE);
312
313         // change a property
314
CmsProperty property1 = new CmsProperty("Title", "undoChanges", null);
315         TestProperty.writeProperty(tc, cms, resource1, property1);
316
317         // change the property on all subresources
318
List JavaDoc subresources = cms.readResources(resource1, CmsResourceFilter.ALL);
319         Iterator JavaDoc i = subresources.iterator();
320         while (i.hasNext()) {
321             CmsResource res = (CmsResource)i.next();
322             String JavaDoc resName = cms.getSitePath(res);
323             TestProperty.writeProperty(tc, cms, resName, property1);
324         }
325
326         // now undo everything
327
cms.lockResource(resource1);
328         cms.undoChanges(resource1, true);
329         cms.unlockResource(resource1);
330
331         tc.switchStorage(OpenCmsTestResourceStorage.GLOBAL_STORAGE);
332
333         // now evaluate the result, the folder must be unchanged now
334
tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
335         // project must be current project
336
tc.assertProject(cms, resource1, cms.getRequestContext().currentProject());
337
338         // all resources within the folder must be unchanged now
339
Iterator JavaDoc j = subresources.iterator();
340         while (j.hasNext()) {
341             CmsResource res = (CmsResource)j.next();
342             String JavaDoc resName = cms.getSitePath(res);
343
344             // now evaluate the result
345
tc.assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
346             // project must be current project
347
tc.assertProject(cms, resName, cms.getRequestContext().currentProject());
348         }
349     }
350
351     /**
352      * Test undoChanges method to a single file.<p>
353      *
354      * @throws Throwable if something goes wrong
355      */

356     public void testUndoChangesResource() throws Throwable JavaDoc {
357
358         CmsObject cms = getCmsObject();
359
360         // this is the first test, so set up the global storage used for all other
361
// tests
362
createStorage(OpenCmsTestResourceStorage.GLOBAL_STORAGE);
363         switchStorage(OpenCmsTestResourceStorage.GLOBAL_STORAGE);
364         storeResources(cms, "/");
365         switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE);
366
367         echo("Testing undoChanges on a file");
368         undoChanges(this, cms, "/index.html");
369     }
370
371     /**
372      * Test undoChanges method to a single folder.<p>
373      *
374      * @throws Throwable if something goes wrong
375      */

376     public void testUndoChangesFolder() throws Throwable JavaDoc {
377
378         CmsObject cms = getCmsObject();
379         echo("Testing undoChanges on a folder without recursion");
380         undoChangesFolder(this, cms, "/folder2/");
381     }
382
383     /**
384      * Test undoChanges method to a single folder.<p>
385      *
386      * @throws Throwable if something goes wrong
387      */

388     public void testUndoChangesFolderRecursive() throws Throwable JavaDoc {
389
390         CmsObject cms = getCmsObject();
391         echo("Testing undoChanges on a folder _with_ recursion");
392         undoChangesFolderRecursive(this, cms, "/folder1/");
393     }
394
395     /**
396      * Test undoChanges method to a resource with an ace.<p>
397      *
398      * @throws Throwable if something goes wrong
399      */

400     public void testUndoChangesWithAce() throws Throwable JavaDoc {
401
402         CmsObject cms = getCmsObject();
403         echo("Testing undoChanges on a resource with an ACE");
404         undoChanges(this, cms, "/folder2/index.html");
405     }
406
407     /**
408      * Test undoChanges method to a resource with an ace.<p>
409      *
410      * @throws Throwable if something goes wrong
411      */

412     public void testUndoChangesSharedProperty() throws Throwable JavaDoc {
413
414         CmsObject cms = getCmsObject();
415         echo("Testing undoChanges on shared property");
416         
417         // create the files
418
String JavaDoc file = "/a";
419         cms.createResource(file, CmsResourceTypePlain.getStaticTypeId());
420         // publish the project
421
cms.unlockProject(cms.getRequestContext().currentProject().getId());
422         cms.publishProject();
423
424         String JavaDoc sibling = "/b";
425         TestSiblings.createSibling(this, cms, file, sibling);
426         // write a persistent no-shared property to test with
427
CmsProperty property = new CmsProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, "undoChanges navText", null);
428         cms.writePropertyObject(sibling, property);
429         // write a persistent shared property to test with
430
CmsProperty property1 = new CmsProperty(CmsPropertyDefinition.PROPERTY_DESCRIPTION, null, "undoChanges description");
431         cms.writePropertyObject(sibling, property1);
432         // publish the project
433
cms.unlockProject(cms.getRequestContext().currentProject().getId());
434         cms.publishProject();
435
436         // create a global storage and store the resource
437
createStorage("undoChanges");
438         switchStorage("undoChanges");
439         storeResources(cms, file);
440         storeResources(cms, sibling);
441         switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE);
442
443         // change a shared property
444
CmsProperty property2 = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, null, "undoChanges title");
445         cms.lockResource(file);
446         cms.writePropertyObject(file, property2);
447         cms.unlockResource(file);
448         //TestProperty.writeProperty(this, cms, file1, property);
449

450         // now undo everything
451
cms.lockResource(file);
452         cms.undoChanges(file, false);
453         cms.unlockResource(file);
454
455         switchStorage("undoChanges");
456
457         // now evaluate the result
458
assertFilter(cms, file, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
459         // project must be current project
460
assertProject(cms, file, cms.getRequestContext().currentProject());
461     }
462
463 }
464
Popular Tags