KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestMoveRename.java,v $
3  * Date : $Date: 2005/06/27 23:22:09 $
4  * Version: $Revision: 1.16 $
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.CmsResourceTypeFolder;
35 import org.opencms.file.types.CmsResourceTypePlain;
36 import org.opencms.lock.CmsLock;
37 import org.opencms.test.OpenCmsTestCase;
38 import org.opencms.test.OpenCmsTestProperties;
39 import org.opencms.test.OpenCmsTestResourceFilter;
40
41 import junit.extensions.TestSetup;
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44
45 /**
46  * Unit tests for move/reanme operation.<p>
47  *
48  * @author Alexander Kandzior
49  *
50  * @version $Revision: 1.16 $
51  */

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

59     public TestMoveRename(String JavaDoc arg0) {
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         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
70         
71         TestSuite suite = new TestSuite();
72         suite.setName(TestMoveRename.class.getName());
73                 
74         suite.addTest(new TestMoveRename("testMoveSingleResource"));
75         suite.addTest(new TestMoveRename("testMoveSingleNewResource"));
76         suite.addTest(new TestMoveRename("testMultipleMoveResource"));
77         suite.addTest(new TestMoveRename("testRenameNewFolder"));
78         suite.addTest(new TestMoveRename("testMoveFolderToOwnSubfolder"));
79         suite.addTest(new TestMoveRename("testRenameFileUpperLowerCase"));
80         suite.addTest(new TestMoveRename("testRenameFolderUpperLowerCase"));
81                 
82         TestSetup wrapper = new TestSetup(suite) {
83             
84             protected void setUp() {
85                 setupOpenCms("simpletest", "/sites/default/");
86             }
87             
88             protected void tearDown() {
89                 removeOpenCms();
90             }
91         };
92         
93         return wrapper;
94     }
95     
96     
97     /**
98      * Tests a "multiple move" on a resource.<p>
99      *
100      * @throws Throwable if something goes wrong
101      */

102     public void testMultipleMoveResource() throws Throwable JavaDoc {
103         
104         CmsObject cms = getCmsObject();
105         echo("Testing multiple move of a resource");
106         
107         // switch to the root context
108
cms.getRequestContext().setSiteRoot("/");
109         
110         String JavaDoc source = "/sites/default/folder1/page1.html";
111         String JavaDoc destination1 = "/sites/default/folder1/page1_move.html";
112         String JavaDoc destination2 = "/page1_move.html";
113
114         storeResources(cms, source);
115         
116         cms.lockResource(source);
117         cms.moveResource(source, destination1);
118         cms.moveResource(destination1, destination2);
119         
120         // source resource:
121

122         // project must be current project
123
assertProject(cms, source, cms.getRequestContext().currentProject());
124         // state must be "deelted"
125
assertState(cms, source, CmsResource.STATE_DELETED);
126         // assert lock state
127
assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
128         // "internal" property must have been added
129
assertPropertyNew(cms, source, new CmsProperty(CmsPropertyDefinition.PROPERTY_INTERNAL,
130             String.valueOf(cms.getRequestContext().currentProject().getId()), null));
131         // one sibling must have been added
132
assertSiblingCountIncremented(cms, source, 1);
133         // now assert the filter for the rest of the attributes
134
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_MOVE_SOURCE);
135         
136         // destination resource
137

138         // project must be current project
139
assertProject(cms, destination2, cms.getRequestContext().currentProject());
140         // state must be "new"
141
assertState(cms, destination2, CmsResource.STATE_NEW);
142         // assert lock state
143
assertLock(cms, destination2, CmsLock.TYPE_EXCLUSIVE);
144         // set filter mapping
145
setMapping(destination2, source);
146         // one sibling must have been added
147
assertSiblingCountIncremented(cms, destination2, 1);
148         // now assert the filter for the rest of the attributes
149
assertFilter(cms, destination2, OpenCmsTestResourceFilter.FILTER_MOVE_DESTINATION);
150         
151         
152         // just for fun try to undo changes on the source resource
153
resetMapping();
154         cms.changeLock(source);
155         assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
156         assertLock(cms, destination2, CmsLock.TYPE_SHARED_EXCLUSIVE);
157         cms.undoChanges(source, false);
158         assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING);
159     }
160     
161     /**
162      * Tests the "move single resource" operation.<p>
163      *
164      * @throws Throwable if something goes wrong
165      */

166     public void testMoveSingleResource() throws Throwable JavaDoc {
167
168         CmsObject cms = getCmsObject();
169         echo("Testing move of a file");
170         
171         String JavaDoc source = "/folder1/index.html";
172         String JavaDoc destination = "/folder1/index_move.html";
173
174         storeResources(cms, source);
175         
176         cms.lockResource(source);
177         cms.moveResource(source, destination);
178                 
179         // source resource must still be available if reading with "include deleted" filter
180
cms.readResource(source, CmsResourceFilter.ALL);
181         
182         // source resource
183

184         // project must be current project
185
assertProject(cms, source, cms.getRequestContext().currentProject());
186         // state must be "deelted"
187
assertState(cms, source, CmsResource.STATE_DELETED);
188         // assert lock state
189
assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
190         // "internal" property must have been added
191
assertPropertyNew(cms, source, new CmsProperty(CmsPropertyDefinition.PROPERTY_INTERNAL,
192             String.valueOf(cms.getRequestContext().currentProject().getId()), null));
193         // one sibling must have been added
194
assertSiblingCountIncremented(cms, source, 1);
195         // now assert the filter for the rest of the attributes
196
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_MOVE_SOURCE);
197         
198         // destination resource
199

200         // project must be current project
201
assertProject(cms, destination, cms.getRequestContext().currentProject());
202         // state must be "new"
203
assertState(cms, destination, CmsResource.STATE_NEW);
204         // assert lock state
205
assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
206         // set filter mapping
207
setMapping(destination, source);
208         // one sibling must have been added
209
assertSiblingCountIncremented(cms, destination, 1);
210         // now assert the filter for the rest of the attributes
211
assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_MOVE_DESTINATION);
212     }
213     
214     /**
215      * Tests the "move a single new resource" operation.<p>
216      *
217      * @throws Throwable if something goes wrong
218      */

219     public void testMoveSingleNewResource() throws Throwable JavaDoc {
220
221         CmsObject cms = getCmsObject();
222         echo("Testing move of a new file");
223         
224         String JavaDoc source = "/folder1/new.html";
225         String JavaDoc destination = "/folder1/new_move.html";
226
227         // create a new, plain resource
228
cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
229         assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
230         
231         storeResources(cms, source);
232         
233         cms.moveResource(source, destination);
234         
235         // source resource must be gone
236
CmsResource res = null;
237         try {
238             res = cms.readResource(source);
239         } catch (CmsVfsResourceNotFoundException e) {
240             // this is expected
241
}
242         if (res != null) {
243             fail("New resource still available after move operation!");
244         }
245
246         // destination resource
247

248         // project must be current project
249
assertProject(cms, destination, cms.getRequestContext().currentProject());
250         // state must be "new"
251
assertState(cms, destination, CmsResource.STATE_NEW);
252         // assert lock state
253
assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
254         // set filter mapping
255
setMapping(destination, source);
256         // no siblings on the new resource
257
assertSiblingCount(cms, destination, 1);
258         // now assert the filter for the rest of the attributes
259
assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_MOVE_DESTINATION);
260     }
261
262     /**
263      * Tests a "multiple move" on a resource.<p>
264      *
265      * @throws Throwable if something goes wrong
266      */

267     public void testRenameNewFolder() throws Throwable JavaDoc {
268         
269         CmsObject cms = getCmsObject();
270         echo("Testing rename a new folder with content");
271         
272         // switch to the root context
273
cms.getRequestContext().setSiteRoot("/");
274         
275         String JavaDoc source = "/sites/default/folder1";
276         String JavaDoc newFolder = "/sites/default/newfolder";
277         String JavaDoc destination = newFolder + "/folder1";
278         String JavaDoc newFolder2 = "/sites/default/testfolder";
279
280         cms.createResource(newFolder, CmsResourceTypeFolder.getStaticTypeId());
281         
282         cms.lockResource(source);
283         cms.moveResource(source, destination);
284         cms.moveResource(newFolder, newFolder2);
285         
286         try {
287             cms.readResource(newFolder2, CmsResourceFilter.ALL);
288         } catch (CmsVfsResourceNotFoundException e) {
289             echo("ERROR: folder not found, try to create it.");
290             cms.createResource(newFolder2, CmsResourceTypeFolder.getStaticTypeId());
291         }
292         
293         assertState(cms, newFolder2, CmsResource.STATE_NEW);
294         
295         cms.undoChanges(source, false);
296     }
297     
298     /**
299      * Tests to move a folder in its own subfolder.<p>
300      *
301      * @throws Exception if the test fails
302      */

303     public void testMoveFolderToOwnSubfolder() throws Exception JavaDoc {
304
305         CmsObject cms = getCmsObject();
306         echo("Testing to move a folder in its own subfolder");
307
308         // Creating paths
309
String JavaDoc source = "/folder1/";
310         String JavaDoc destination = "/folder1/subfolder11/folder1/";
311
312         cms.lockResource(source);
313         CmsVfsException error = null;
314         try {
315             // moving a folder to it's own subfolder must cause an exception
316
cms.moveResource(source, destination);
317         } catch (CmsVfsException e) {
318             error = e;
319         }
320         
321         // an exception must have been thrown
322
assertNotNull(error);
323         // check for the right error message
324
assertSame(error.getMessageContainer().getKey(), Messages.ERR_MOVE_SAME_FOLDER_2);
325     }
326     
327     /**
328      * Tests renaming a file to the same name with a different case.<p>
329      *
330      * @throws Exception if the test fails
331      */

332     public void testRenameFileUpperLowerCase() throws Exception JavaDoc {
333         
334         CmsObject cms = getCmsObject();
335         echo("Testing to rename a file to the same name with a different case");
336         
337         // Creating paths
338
String JavaDoc source = "/folder2/image1.gif";
339         String JavaDoc destination = "/folder2/Image1.GIF";
340         
341         storeResources(cms, source);
342         
343         // now move from the old to the new name
344
cms.lockResource(source);
345         cms.moveResource(source, destination);
346         
347         // source resource must be gone for default read
348
CmsResource res = null;
349         try {
350             res = cms.readResource(source);
351         } catch (CmsVfsResourceNotFoundException e) {
352             // this is expected
353
}
354         if (res != null) {
355             fail("New resource still available after move operation!");
356         }
357         
358         // source resource must still be available if reading with "include deleted" filter
359
cms.readResource(source, CmsResourceFilter.ALL);
360         
361         // source resource
362

363         // project must be current project
364
assertProject(cms, source, cms.getRequestContext().currentProject());
365         // state must be "deelted"
366
assertState(cms, source, CmsResource.STATE_DELETED);
367         // assert lock state
368
assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
369         // "internal" property must have been added
370
assertPropertyNew(cms, source, new CmsProperty(CmsPropertyDefinition.PROPERTY_INTERNAL,
371             String.valueOf(cms.getRequestContext().currentProject().getId()), null));
372         // one sibling must have been added
373
assertSiblingCountIncremented(cms, source, 1);
374         // now assert the filter for the rest of the attributes
375
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_MOVE_SOURCE);
376         
377         // destination resource
378

379         // project must be current project
380
assertProject(cms, destination, cms.getRequestContext().currentProject());
381         // state must be "new"
382
assertState(cms, destination, CmsResource.STATE_NEW);
383         // assert lock state
384
assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
385         // set filter mapping
386
setMapping(destination, source);
387         // one sibling must have been added
388
assertSiblingCountIncremented(cms, destination, 1);
389         // now assert the filter for the rest of the attributes
390
assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_MOVE_DESTINATION);
391     }
392     
393     /**
394      * Tests renaming a folder to the same name with a different case.<p>
395      *
396      * @throws Exception if the test fails
397      */

398     public void testRenameFolderUpperLowerCase() throws Exception JavaDoc {
399         
400         CmsObject cms = getCmsObject();
401         echo("Testing to rename a folder to the same name with a different case");
402         
403         // Creating paths
404
String JavaDoc source = "/xmlcontent";
405         String JavaDoc destination = "/XMLcontent";
406         
407         storeResources(cms, source);
408         
409         // now move from the old to the new name
410
cms.lockResource(source);
411         cms.moveResource(source, destination);
412         
413         // source resource must be gone for default read
414
CmsResource res = null;
415         try {
416             res = cms.readResource(source);
417         } catch (CmsVfsResourceNotFoundException e) {
418             // this is expected
419
}
420         if (res != null) {
421             fail("New resource still available after move operation!");
422         }
423         
424         // source resource must still be available if reading with "include deleted" filter
425
cms.readResource(source, CmsResourceFilter.ALL);
426         // try to read the destination folder
427
cms.readResource(destination);
428                 
429         // source resource
430

431         // project must be current project
432
assertProject(cms, source, cms.getRequestContext().currentProject());
433         // state must be "deelted"
434
assertState(cms, source, CmsResource.STATE_DELETED);
435         // assert lock state
436
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
437         // folders don't have siblings
438
assertSiblingCount(cms, source, 1);
439         
440         // destination resource
441

442         // project must be current project
443
assertProject(cms, destination, cms.getRequestContext().currentProject());
444         // state must be "new"
445
assertState(cms, destination, CmsResource.STATE_NEW);
446         // assert lock state
447
assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
448         // folders don't have siblings
449
assertSiblingCount(cms, source, 1);
450     }
451 }
Popular Tags