KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestBackup.java,v $
3  * Date : $Date: 2005/07/06 10:23:38 $
4  * Version: $Revision: 1.7 $
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.io.UnsupportedEncodingException 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 backup operation.<p>
47  *
48  * @author Thomas Weckert
49  * @version $Revision: 1.7 $
50  */

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

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

68     public static Test suite() {
69
70         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
71
72         TestSuite suite = new TestSuite();
73         suite.setName(TestCopy.class.getName());
74
75         //suite.addTest(new TestBackup("testCreateAndDeleteResources"));
76
suite.addTest(new TestBackup("testFileHistory"));
77
78         TestSetup wrapper = new TestSetup(suite) {
79
80             protected void setUp() {
81
82                 setupOpenCms("simpletest", "/sites/default/");
83             }
84
85             protected void tearDown() {
86
87                 removeOpenCms();
88             }
89         };
90
91         return wrapper;
92     }
93
94     /**
95      * Creates and deletes a resource n-times and tests if the version ID of the backup resources
96      * are correct and if the content could be restored for a specified version ID.<p>
97      *
98      * @throws Throwable if something goes wrong
99      */

100     public void testCreateAndDeleteResources() throws Throwable JavaDoc {
101
102         CmsObject cms = getCmsObject();
103         String JavaDoc filename = "/dummy1.txt";
104         CmsProject offlineProject = cms.getRequestContext().currentProject();
105         int counter = 5;
106
107         try {
108
109             // switch to the default site in the offline project
110
cms.getRequestContext().saveSiteRoot();
111             cms.getRequestContext().setSiteRoot("/sites/default/");
112             cms.getRequestContext().setCurrentProject(offlineProject);
113
114             for (int i = 1; i <= counter; i++) {
115
116                 // create a plain text file
117
String JavaDoc contentStr = "content version " + i;
118                 cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null);
119                 cms.unlockResource(filename);
120                 cms.publishResource(filename);
121
122                 // delete the resource again
123
cms.lockResource(filename);
124                 cms.deleteResource(filename, CmsResource.DELETE_PRESERVE_SIBLINGS);
125                 cms.unlockResource(filename);
126                 cms.publishResource(filename);
127             }
128
129             // re-create the resource to be able to read all versions from the history
130
// assert that we have the expected number of versions in the history
131
cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId(), null, null);
132             List JavaDoc backupResources = cms.readAllBackupFileHeaders(filename);
133             assertEquals(counter, backupResources.size());
134
135             for (int i = 0; i < counter; i++) {
136
137                 // the list of backup resources contains at index 0 the
138
// resource with the highest version and tag ID
139
int version = counter - i;
140                 String JavaDoc contentStr = "content version " + version;
141
142                 // assert that the backup resource has the correct version
143
CmsBackupResource backupResource = (CmsBackupResource)backupResources.get(i);
144                 assertEquals(version, backupResource.getVersionId());
145
146                 cms.restoreResourceBackup(filename, backupResource.getTagId());
147                 CmsFile file = cms.readFile(filename);
148
149                 // assert that the content and version fit together
150
String JavaDoc restoredContent = getContentString(cms, file.getContents());
151                 assertEquals(contentStr, restoredContent);
152             }
153         } finally {
154
155             cms.getRequestContext().restoreSiteRoot();
156         }
157
158     }
159
160     /**
161      * creates a file, modifies and publishes it n-times, create a sibling,
162      * publishes both and compares the histories.<p>
163      *
164      * @throws Throwable if something goes wrong
165      */

166     public void testFileHistory() throws Throwable JavaDoc {
167
168         CmsObject cms = getCmsObject();
169         String JavaDoc filename = "/testFileHistory1.txt";
170         String JavaDoc siblingname = "/testFileHistory2.txt";
171         CmsProject offlineProject = cms.getRequestContext().currentProject();
172         int counter = 5;
173
174         try {
175
176             // switch to the default site in the offline project
177
cms.getRequestContext().saveSiteRoot();
178             cms.getRequestContext().setSiteRoot("/sites/default/");
179             cms.getRequestContext().setCurrentProject(offlineProject);
180
181             // create a plain text file
182
String JavaDoc contentStr = "content version " + 0;
183             cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null);
184
185             for (int i = 1; i <= counter; i++) {
186
187                 // modify the plain text file
188
contentStr = "content version " + i;
189                 CmsFile file = cms.readFile(filename);
190                 file.setContents(contentStr.getBytes());
191                 cms.lockResource(filename);
192                 cms.writeFile(file);
193                 cms.unlockResource(filename);
194                 cms.publishResource(filename);
195             }
196
197             // create a sibling
198
cms.copyResource(filename, siblingname, CmsResource.COPY_AS_SIBLING);
199             cms.unlockResource(siblingname);
200             cms.publishResource(siblingname);
201
202             for (int i = 1; i <= counter; i++) {
203
204                 // modify the sibling text file
205
contentStr = "sibling content version " + (counter + i);
206                 CmsFile file = cms.readFile(siblingname);
207                 file.setContents(contentStr.getBytes());
208                 cms.lockResource(siblingname);
209                 cms.writeFile(file);
210                 cms.unlockResource(siblingname);
211                 cms.publishResource(siblingname);
212             }
213
214             List JavaDoc backupResourcesForFile = cms.readAllBackupFileHeaders(filename);
215             List JavaDoc backupResourcesForSibling = cms.readAllBackupFileHeaders(siblingname);
216             assertEquals(backupResourcesForFile, backupResourcesForSibling);
217             assertEquals(2 * counter + 1, backupResourcesForFile.size());
218         } finally {
219             cms.getRequestContext().restoreSiteRoot();
220         }
221
222     }
223
224     /**
225      * Turns the byte content of a resource into a string.<p>
226      *
227      * @param cms the current user's Cms object
228      * @param content the byte content of a resource
229      * @return the content as a string
230      */

231     protected String JavaDoc getContentString(CmsObject cms, byte[] content) {
232
233         try {
234             return new String JavaDoc(content, cms.getRequestContext().getEncoding());
235         } catch (UnsupportedEncodingException JavaDoc e) {
236             return new String JavaDoc(content);
237         }
238     }
239
240 }
241
Popular Tags