KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestRestoreFromHistory.java,v $
3  * Date : $Date: 2005/06/27 23:22:09 $
4  * Version: $Revision: 1.14 $
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.test.OpenCmsTestCase;
36 import org.opencms.test.OpenCmsTestProperties;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40
41 import junit.extensions.TestSetup;
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44
45 /**
46  * Unit tests for the history restore method.<p>
47  *
48  * @author Carsten Weinholz
49  * @version $Revision: 1.14 $
50  */

51 public class TestRestoreFromHistory extends OpenCmsTestCase {
52     
53     /**
54      * Default JUnit constructor.<p>
55      *
56      * @param arg0 JUnit parameters
57      */

58     public TestRestoreFromHistory(String JavaDoc arg0) {
59         super(arg0);
60     }
61     
62     /**
63      * Test suite for this test class.<p>
64      *
65      * @return the test suite
66      */

67     public static Test suite() {
68         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
69         
70         TestSuite suite = new TestSuite();
71         suite.setName(TestRestoreFromHistory.class.getName());
72         
73         suite.addTest(new TestRestoreFromHistory("testRestoreResource"));
74         suite.addTest(new TestRestoreFromHistory("testRestoreDeletedResource"));
75         suite.addTest(new TestRestoreFromHistory("testHistoryOverflow"));
76         
77         TestSetup wrapper = new TestSetup(suite) {
78             
79             protected void setUp() {
80                 setupOpenCms("simpletest", "/sites/default/");
81             }
82             
83             protected void tearDown() {
84                 removeOpenCms();
85             }
86         };
87         
88         return wrapper;
89     }
90
91     /**
92      * Tests the history overflow function.<p>
93      *
94      * @throws Throwable if something goes wrong
95      */

96     public void testHistoryOverflow() throws Throwable JavaDoc {
97         
98         final int C_MAX_VERSIONS = 10;
99         
100         CmsObject cms = getCmsObject();
101         echo("Testing history overflow");
102         
103         String JavaDoc resourcename ="/test-overflow1.txt";
104         String JavaDoc contentStr = "1";
105         
106         cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null);
107         this.storeResources(cms, resourcename);
108         
109         // publish the project
110
cms.unlockProject(cms.getRequestContext().currentProject().getId());
111         cms.publishProject();
112         
113         int version;
114         for (version = 1; version < 20; version++) {
115             
116             cms.lockResource(resourcename);
117             
118             // check that there is the appropriate number of backup files
119
List JavaDoc allFiles = cms.readAllBackupFileHeaders(resourcename);
120             if (version <= C_MAX_VERSIONS) {
121                 if (allFiles.size() != version) {
122                     fail("Number of backup files found = " + allFiles.size() + " != " + version + " expected");
123                 }
124             } else {
125                 if (allFiles.size() != C_MAX_VERSIONS) {
126                     fail("Number of backup files found = " + allFiles.size() + " != " + C_MAX_VERSIONS + " expected");
127                 }
128             }
129             
130             // now check the previous version if available
131
if (version > 1) {
132                 CmsBackupResource backup = (CmsBackupResource)allFiles.get(1);
133                 cms.restoreResourceBackup(resourcename, backup.getTagId());
134             
135                 // check the content - must be version-1
136
assertContent(cms, resourcename, Integer.toString(version-1).getBytes());
137             }
138             
139             // change to content of the file to next version and publish it again
140
contentStr = Integer.toString(version+1);
141             CmsFile update = cms.readFile(resourcename);
142             update.setContents(contentStr.getBytes());
143             cms.writeFile(update);
144             this.storeResources(cms, resourcename);
145             cms.unlockProject(cms.getRequestContext().currentProject().getId());
146             cms.publishProject();
147         }
148     }
149             
150     /**
151      * Test the restore resource method.<p>
152      *
153      * @throws Throwable if something goes wrong
154      */

155     public void testRestoreResource() throws Throwable JavaDoc {
156         
157         CmsObject cms = getCmsObject();
158         echo("Testing restore resource");
159         
160         String JavaDoc resourcename = "/test-restore1.txt";
161         
162         String JavaDoc contentStr1 = "Hello this is content version 1";
163         String JavaDoc contentStr2 = "Hello this is content version 2";
164         
165         
166         CmsProperty sProp1 = new CmsProperty("StructureProp", "Structure property value version 1", null, true);
167         CmsProperty rProp1 = new CmsProperty("ResourceProp", null, "Resource property value version 1", true);
168         List JavaDoc props1 = new ArrayList JavaDoc();
169         props1.add(sProp1);
170         props1.add(rProp1);
171         
172         CmsProperty sProp2 = new CmsProperty("StructureProp", "Structure property value version 2", null, true);
173         CmsProperty rProp2 = new CmsProperty("ResourceProp", null, "Resource property value version 2", true);
174         List JavaDoc props2 = new ArrayList JavaDoc();
175         props2.add(sProp2);
176         props2.add(rProp2);
177         
178         // create the resource with content version 1
179
cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr1.getBytes(), null);
180         this.storeResources(cms, resourcename);
181         
182         // set the properties
183
cms.writePropertyObject(resourcename, sProp1);
184         cms.writePropertyObject(resourcename, rProp1);
185         
186         // check the content
187
assertContent(cms, resourcename, contentStr1.getBytes());
188         assertPropertyNew(cms, resourcename, props1);
189         
190         // check that there are no backups available
191
List JavaDoc allFiles = cms.readAllBackupFileHeaders(resourcename);
192         if (!allFiles.isEmpty()) {
193             fail("Unexpected backup files for new created resource found.");
194         }
195         
196         // publish the project
197
cms.unlockProject(cms.getRequestContext().currentProject().getId());
198         cms.publishProject();
199         
200         // check that there is exactly one backup file available
201
allFiles = cms.readAllBackupFileHeaders(resourcename);
202         if (allFiles.size() != 1) {
203             fail("Unexpected number of backup files for published resource found (one expected)");
204         }
205         
206         // store current resource contents
207
this.storeResources(cms, resourcename);
208         
209         // change to content of the file to version 2 and publish it again
210
cms.lockResource(resourcename);
211         CmsFile update = cms.readFile(resourcename);
212         update.setContents(contentStr2.getBytes());
213         cms.writeFile(update);
214         
215         // change the properties
216
cms.writePropertyObject(resourcename, sProp2);
217         cms.writePropertyObject(resourcename, rProp2);
218         
219         // check the content - must be version 2
220
assertContent(cms, resourcename, contentStr2.getBytes());
221         
222         // check the properties - must be version 2
223
assertPropertyChanged(cms, resourcename, props2);
224
225         // publish the project
226
cms.unlockProject(cms.getRequestContext().currentProject().getId());
227         cms.publishProject();
228         
229         // check that there are exactly two backup files available
230
allFiles = cms.readAllBackupFileHeaders(resourcename);
231         if (allFiles.size() != 2) {
232             fail("Unexpected number of backup files for published resource found (two expected)");
233         }
234         
235         // read the tag id
236
CmsBackupResource backup = (CmsBackupResource)allFiles.get(1);
237         
238         // store current resource contents
239
this.storeResources(cms, resourcename);
240         
241         // now restore the first version
242
cms.lockResource(resourcename);
243         cms.restoreResourceBackup(resourcename, backup.getTagId());
244         
245         // check the content - must be version 1
246
assertContent(cms, resourcename, contentStr1.getBytes());
247         
248         // check the properties - must be version 1
249
assertPropertyChanged(cms, resourcename, props1);
250     }
251     
252     /**
253      * Tests the re-creation of already deleted resources.<p>
254      * A deleted resource can be restored by creating a new one with the same path
255      * and then restoring its contents from history.
256      *
257      * @throws Throwable if something goes wrong
258      */

259     public void testRestoreDeletedResource() throws Throwable JavaDoc {
260         
261         CmsObject cms = getCmsObject();
262         echo("Testing restoring deleted resources");
263         
264         String JavaDoc resourcename = "/test-restore3.txt";
265         
266         String JavaDoc contentStr = "Hello this is the content";
267         
268         // create the resource with content
269
cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null);
270         
271         // check the content
272
assertContent(cms, resourcename, contentStr.getBytes());
273         
274         // check that there are no backups available
275
List JavaDoc allFiles = cms.readAllBackupFileHeaders(resourcename);
276         if (!allFiles.isEmpty()) {
277             fail("Unexpected backup files for new created resource found.");
278         }
279         
280         // publish the project
281
cms.unlockProject(cms.getRequestContext().currentProject().getId());
282         cms.publishProject();
283         
284         // check that there is exactly one backup file available
285
allFiles = cms.readAllBackupFileHeaders(resourcename);
286         if (allFiles.size() != 1) {
287             fail("Unexpected number of backup files for published resource found (one expected)");
288         }
289         
290         // now delete and publish the resource
291
cms.lockResource(resourcename);
292         cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
293         cms.unlockProject(cms.getRequestContext().currentProject().getId());
294         cms.publishProject();
295         
296         // create a new empty resource
297
cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), null, null);
298         
299         // check that there is one backup file available, again
300
allFiles = cms.readAllBackupFileHeaders(resourcename);
301         if (allFiles.size() != 1) {
302             fail("Unexpected number of backup files for published resource found (one expected)");
303         }
304         
305         // read the tag id
306
CmsBackupResource backup = (CmsBackupResource)allFiles.get(0);
307         
308         // and restore it from history
309
cms.restoreResourceBackup(resourcename, backup.getTagId());
310         
311         // check the content
312
assertContent(cms, resourcename, contentStr.getBytes());
313     }
314 }
315
Popular Tags