KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > synchronize > TestSynchronize


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/synchronize/TestSynchronize.java,v $
3  * Date : $Date: 2006/09/21 09:34:48 $
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.synchronize;
33
34 import org.opencms.db.CmsUserSettings;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.file.types.CmsResourceTypeJsp;
39 import org.opencms.file.types.CmsResourceTypePlain;
40 import org.opencms.file.types.CmsResourceTypeXmlPage;
41 import org.opencms.main.OpenCms;
42 import org.opencms.report.CmsShellReport;
43 import org.opencms.test.OpenCmsTestCase;
44 import org.opencms.test.OpenCmsTestProperties;
45 import org.opencms.util.CmsFileUtil;
46
47 import java.io.File JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51
52 import junit.extensions.TestSetup;
53 import junit.framework.Test;
54 import junit.framework.TestSuite;
55
56 /**
57  * JUnit test cases for the VFS/RFS synchronization.<p>
58  *
59  * @author Thomas Weckert
60  *
61  * @version $Revision: 1.21 $
62  *
63  * @since 6.0.0
64  */

65 public class TestSynchronize extends OpenCmsTestCase {
66
67     /**
68      * Default JUnit constructor.<p>
69      *
70      * @param arg0 JUnit parameters
71      */

72     public TestSynchronize(String JavaDoc arg0) {
73
74         super(arg0);
75     }
76
77     /**
78      * Test suite for this test class.<p>
79      *
80      * @return the test suite
81      */

82     public static Test suite() {
83
84         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
85
86         TestSuite suite = new TestSuite();
87         suite.setName(TestSynchronize.class.getName());
88
89         suite.addTest(new TestSynchronize("testSynchronize"));
90         suite.addTest(new TestSynchronize("testLoadSaveSynchronizeSettings"));
91         suite.addTest(new TestSynchronize("testSynchronizeSeveralFolders"));
92
93         TestSetup wrapper = new TestSetup(suite) {
94
95             protected void setUp() {
96
97                 setupOpenCms("simpletest", "/sites/default/");
98             }
99
100             protected void tearDown() {
101
102                 removeOpenCms();
103             }
104
105         };
106
107         return wrapper;
108     }
109
110     /**
111      * Tests loading and saving the user synchronize settings.<p>
112      *
113      * @throws Exception if the test fails
114      */

115     public void testLoadSaveSynchronizeSettings() throws Exception JavaDoc {
116
117         CmsObject cms = getCmsObject();
118         echo("Testing loading and saving the synchronization settings of a user");
119
120         CmsUserSettings userSettings = new CmsUserSettings(cms);
121         // default sync settings are null
122
assertNull(userSettings.getSynchronizeSettings());
123
124         String JavaDoc source = "/folder1/";
125         String JavaDoc dest = getTestDataPath("");
126         if (dest.endsWith(File.separator)) {
127             dest = dest.substring(0, dest.length() - 1);
128         }
129
130         CmsSynchronizeSettings syncSettings = new CmsSynchronizeSettings();
131         syncSettings.setEnabled(true);
132         ArrayList JavaDoc sourceList = new ArrayList JavaDoc();
133         sourceList.add(source);
134         syncSettings.setSourceListInVfs(sourceList);
135         syncSettings.setDestinationPathInRfs(dest);
136
137         // store the settings
138
userSettings.setSynchronizeSettings(syncSettings);
139         userSettings.save(cms);
140
141         // login another user
142
cms.loginUser("test1", "test1");
143
144         userSettings = new CmsUserSettings(cms);
145         // default sync settings are null for this user
146
assertNull(userSettings.getSynchronizeSettings());
147
148         // login another user
149
cms.loginUser(OpenCms.getDefaultUsers().getUserAdmin(), "admin");
150
151         userSettings = new CmsUserSettings(cms);
152         syncSettings = userSettings.getSynchronizeSettings();
153         assertNotNull(syncSettings);
154
155         assertTrue(syncSettings.isEnabled());
156         assertEquals(source, syncSettings.getSourceListInVfs().get(0));
157         assertEquals(dest, syncSettings.getDestinationPathInRfs());
158     }
159
160     /**
161      * Tests the synchronize function.<p>
162      *
163      * Synchronizes everything below "/" into the RFS, modifies .txt, .jsp and .html
164      * files in the RFS, and synchronizes everything back into the VFS.<p>
165      *
166      * @throws Exception if the test fails
167      */

168     public void testSynchronize() throws Exception JavaDoc {
169
170         String JavaDoc source = "/sites/default/";
171
172         // save what gets synchronized
173
CmsSynchronizeSettings syncSettings = new CmsSynchronizeSettings();
174
175         String JavaDoc dest = getTestDataPath("") + "sync1" + File.separator;
176         File JavaDoc destFolder = new File JavaDoc(dest);
177         if (!destFolder.exists()) {
178             destFolder.mkdirs();
179         }
180
181         syncSettings.setDestinationPathInRfs(dest);
182         ArrayList JavaDoc sourceList = new ArrayList JavaDoc();
183         sourceList.add(source);
184         syncSettings.setSourceListInVfs(sourceList);
185         syncSettings.setEnabled(true);
186
187         try {
188             CmsObject cms = getCmsObject();
189             echo("Testing synchronization of files and folders");
190
191             cms.getRequestContext().setSiteRoot("/");
192             storeResources(cms, source);
193
194             echo("Synchronizing "
195                 + syncSettings.getSourceListInVfs()
196                 + " with "
197                 + syncSettings.getDestinationPathInRfs());
198
199             // synchronize everything to the RFS
200
new CmsSynchronize(cms, syncSettings, new CmsShellReport(cms.getRequestContext().getLocale()));
201
202             // modify resources in the RFS
203
List JavaDoc tree = cms.readResources(source, CmsResourceFilter.ALL);
204             for (int i = 0, n = tree.size(); i < n; i++) {
205                 CmsResource resource = (CmsResource)tree.get(i);
206
207                 int type = resource.getTypeId();
208                 if (((type == CmsResourceTypePlain.getStaticTypeId()))
209                     || (type == CmsResourceTypeJsp.getStaticTypeId())
210                     || (type == CmsResourceTypeXmlPage.getStaticTypeId())) {
211                     // modify date last modified on resource
212
touchResourceInRfs(cms, resource, syncSettings);
213                 }
214             }
215
216             // sleep 2 seconds to avoid issues with file system timing
217
Thread.sleep(2000);
218
219             // synchronize everything back to the VFS
220
new CmsSynchronize(cms, syncSettings, new CmsShellReport(cms.getRequestContext().getLocale()));
221
222             // assert if the synchronization worked fine
223
for (int i = 0, n = tree.size(); i < n; i++) {
224                 CmsResource vfsResource = (CmsResource)tree.get(i);
225                 int type = vfsResource.getTypeId();
226                 String JavaDoc vfsname = cms.getSitePath(vfsResource);
227
228                 System.out.println("( " + i + " / " + (n - 1) + " ) Checking " + vfsname);
229                 if (((type == CmsResourceTypePlain.getStaticTypeId()))
230                     || (type == CmsResourceTypeJsp.getStaticTypeId())
231                     || (type == CmsResourceTypeXmlPage.getStaticTypeId())) {
232                     // assert the resource state
233
assertState(cms, vfsname, CmsResource.STATE_CHANGED);
234                     // assert the modification date
235
File JavaDoc rfsResource = new File JavaDoc(getRfsPath(cms, vfsResource, syncSettings));
236                     assertDateLastModifiedAfter(cms, vfsname, rfsResource.lastModified());
237                 } else {
238                     assertState(cms, vfsname, CmsResource.STATE_UNCHANGED);
239                 }
240             }
241
242         } finally {
243
244             // remove the test data
245
echo("Purging directory " + dest);
246             CmsFileUtil.purgeDirectory(new File JavaDoc(dest));
247         }
248     }
249
250     /**
251      * Tests the synchronize function with more then one source folder.<p>
252      *
253      * @throws Exception if the test fails
254      */

255     public void testSynchronizeSeveralFolders() throws Exception JavaDoc {
256
257         // save what gets synchronized
258
CmsSynchronizeSettings syncSettings = new CmsSynchronizeSettings();
259
260         String JavaDoc dest = getTestDataPath("") + "sync2" + File.separator;
261         File JavaDoc destFolder = new File JavaDoc(dest);
262         if (!destFolder.exists()) {
263             destFolder.mkdirs();
264         }
265
266         syncSettings.setDestinationPathInRfs(dest);
267         ArrayList JavaDoc sourceList = new ArrayList JavaDoc();
268         sourceList.add("/sites/default/folder1/subfolder11/");
269         sourceList.add("/sites/default/folder1/subfolder12/");
270         sourceList.add("/sites/default/folder2/subfolder21/");
271         syncSettings.setSourceListInVfs(sourceList);
272         syncSettings.setEnabled(true);
273
274         try {
275             CmsObject cms = getCmsObject();
276             echo("Testing synchronization of several folders");
277
278             cms.getRequestContext().setSiteRoot("/");
279             storeResources(cms, "/");
280
281             // synchronize everything to the RFS
282
new CmsSynchronize(cms, syncSettings, new CmsShellReport(cms.getRequestContext().getLocale()));
283
284             Iterator JavaDoc it = syncSettings.getSourceListInVfs().iterator();
285             List JavaDoc tree = new ArrayList JavaDoc();
286
287             // modify resources in the RFS
288
while (it.hasNext()) {
289                 String JavaDoc source = (String JavaDoc)it.next();
290                 List JavaDoc subTree = cms.readResources(source, CmsResourceFilter.ALL);
291                 tree.addAll(subTree);
292                 for (int i = 0, n = subTree.size(); i < n; i++) {
293                     CmsResource resource = (CmsResource)subTree.get(i);
294
295                     int type = resource.getTypeId();
296                     if (((type == CmsResourceTypePlain.getStaticTypeId()))
297                         || (type == CmsResourceTypeJsp.getStaticTypeId())
298                         || (type == CmsResourceTypeXmlPage.getStaticTypeId())) {
299                         // modify date last modified on resource
300
touchResourceInRfs(cms, resource, syncSettings);
301                     }
302                 }
303             }
304
305             // sleep 2 seconds to avoid issues with file system timing
306
Thread.sleep(2000);
307
308             // synchronize everything back to the VFS
309
new CmsSynchronize(cms, syncSettings, new CmsShellReport(cms.getRequestContext().getLocale()));
310
311             // assert if the synchronization worked fine
312
for (int i = 0, n = tree.size(); i < n; i++) {
313                 CmsResource vfsResource = (CmsResource)tree.get(i);
314                 int type = vfsResource.getTypeId();
315                 String JavaDoc vfsname = cms.getSitePath(vfsResource);
316
317                 System.out.println("( " + i + " / " + (n - 1) + " ) Checking " + vfsname);
318                 if (((type == CmsResourceTypePlain.getStaticTypeId()))
319                     || (type == CmsResourceTypeJsp.getStaticTypeId())
320                     || (type == CmsResourceTypeXmlPage.getStaticTypeId())) {
321                     // assert the resource state
322
assertState(cms, vfsname, CmsResource.STATE_CHANGED);
323                     // assert the modification date
324
File JavaDoc rfsResource = new File JavaDoc(getRfsPath(cms, vfsResource, syncSettings));
325                     assertDateLastModifiedAfter(cms, vfsname, rfsResource.lastModified());
326                 } else {
327                     assertState(cms, vfsname, CmsResource.STATE_UNCHANGED);
328                 }
329             }
330
331         } finally {
332
333             // remove the test data
334
echo("Purging directory " + dest);
335             CmsFileUtil.purgeDirectory(new File JavaDoc(dest));
336         }
337     }
338
339     /**
340      * Returns a rfs path for a given resource to be synchronized.<p>
341      *
342      * @param cms the cms context
343      * @param resource the resource to synchronize
344      * @param syncSettings the synchronization settings
345      *
346      * @return the rfs path
347      */

348     private String JavaDoc getRfsPath(CmsObject cms, CmsResource resource, CmsSynchronizeSettings syncSettings) {
349
350         String JavaDoc path = syncSettings.getDestinationPathInRfs() + cms.getSitePath(resource);
351         return CmsFileUtil.normalizePath(path);
352     }
353
354     /**
355      * "Touches" the last modification date of a resource the RFS so that it is
356      * synchronized back to the VFS as a modified resource.<p>
357      *
358      * @param cms the current user's Cms object
359      * @param resource the VFS resource to be modified in the RFS
360      * @param syncSettings the synchronization settings
361      */

362     private void touchResourceInRfs(CmsObject cms, CmsResource resource, CmsSynchronizeSettings syncSettings) {
363
364         // touch file 2 seconds in the future
365
String JavaDoc path = getRfsPath(cms, resource, syncSettings);
366         System.out.println("Touching: " + path);
367         File JavaDoc file = new File JavaDoc(path);
368         file.setLastModified(file.lastModified() + 2000);
369     }
370 }
Popular Tags