KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestCopy.java,v $
3  * Date : $Date: 2006/09/21 09:34:47 $
4  * Version: $Revision: 1.17 $
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.lock.CmsLock;
35 import org.opencms.main.OpenCms;
36 import org.opencms.test.OpenCmsTestCase;
37 import org.opencms.test.OpenCmsTestProperties;
38 import org.opencms.test.OpenCmsTestResourceConfigurableFilter;
39 import org.opencms.test.OpenCmsTestResourceFilter;
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 tests for copy operation.<p>
50  *
51  * @author Alexander Kandzior
52  *
53  * @version $Revision: 1.17 $
54  */

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

62     public TestCopy(String JavaDoc arg0) {
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         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
73         
74         TestSuite suite = new TestSuite();
75         suite.setName(TestCopy.class.getName());
76                 
77         // suite.addTest(new TestCopy("testCopyFileUserCreatedIssue"));
78
suite.addTest(new TestCopy("testCopySingleResourceAsNew"));
79         suite.addTest(new TestCopy("testCopyFolderDateIssue"));
80         suite.addTest(new TestCopy("testCopyFolderAsNew"));
81         suite.addTest(new TestCopy("testCopyOverwriteDeletedFile"));
82         
83         TestSetup wrapper = new TestSetup(suite) {
84             
85             protected void setUp() {
86                 setupOpenCms("simpletest", "/sites/default/");
87             }
88             
89             protected void tearDown() {
90                 removeOpenCms();
91             }
92         };
93         
94         return wrapper;
95     }
96     
97     /**
98      * Tests the "copy single resource as new" operation.<p>
99      *
100      * @throws Exception if the test fails
101      */

102     public void testCopySingleResourceAsNew() throws Exception JavaDoc {
103
104         CmsObject cms = getCmsObject();
105         echo("Testing copy of a file as new");
106         
107         // create a project for all tests in this suite
108
cms.createProject("testproject", "a test project", "Users", "Users", CmsProject.PROJECT_TYPE_NORMAL);
109         cms.copyResourceToProject("/");
110         cms.addUserToGroup("test1", OpenCms.getDefaultUsers().getGroupAdministrators());
111         
112         cms.loginUser("test1", "test1");
113         cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));
114
115         
116         String JavaDoc source = "/index.html";
117         String JavaDoc destination = "/index_copy.html";
118         long timestamp = System.currentTimeMillis();
119         
120         CmsUser admin = cms.readUser("Admin");
121         // some assertions about the original state of the resource
122
assertUserCreated(cms, source, admin);
123         assertUserLastModified(cms, source, admin);
124         CmsResource original = cms.readResource(source);
125         
126         storeResources(cms, source);
127         cms.copyResource(source, destination);
128                         
129         assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);
130         
131         // project must be current project
132
assertProject(cms, destination, cms.getRequestContext().currentProject());
133         // state must be "new"
134
assertState(cms, destination, CmsResource.STATE_NEW);
135         // date created must be new
136
assertDateCreatedAfter(cms, destination, timestamp);
137         // date last modified must be original date
138
assertDateLastModified(cms, destination, original.getDateLastModified());
139         // user created must be current user
140
assertUserCreated(cms, destination, cms.getRequestContext().currentUser());
141         // user last modified must be original user
142
assertUserLastModified(cms, destination, cms.readUser(original.getUserLastModified()));
143         // now assert the filter for the rest of the attributes
144
setMapping(destination, source);
145         assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_COPY_FILE_AS_NEW);
146         // assert lock state
147
assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
148     }
149     
150     /**
151      * Tests the copy operation for a folder, checking if the date of the new folder is the current date.<p>
152      *
153      * @throws Exception if the test fails
154      */

155     public void testCopyFolderDateIssue() throws Exception JavaDoc {
156
157         
158         CmsObject cms = getCmsObject();
159         echo("Testing copy operation for a folder, checking if the date of the new folder is the current date");
160         
161         cms.loginUser("test1", "test1");
162         cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));
163         
164         String JavaDoc source = "/xmlcontent/";
165         String JavaDoc destination = "/xmlcontent_copy/";
166         long timestamp = System.currentTimeMillis();
167         
168         storeResources(cms, source);
169         cms.copyResource(source, destination);
170                 
171         assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);
172         
173         // project must be current project
174
assertProject(cms, destination, cms.getRequestContext().currentProject());
175         // state must be "new"
176
assertState(cms, destination, CmsResource.STATE_NEW);
177         // date created must be new
178
assertDateCreatedAfter(cms, destination, timestamp);
179         // for a copied folder, also the date last modified must be new
180
assertDateLastModifiedAfter(cms, destination, timestamp);
181         // user created must be current user
182
assertUserCreated(cms, destination, cms.getRequestContext().currentUser());
183         // user last modified must be current user
184
assertUserLastModified(cms, destination, cms.getRequestContext().currentUser());
185         // assert lock state
186
assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
187         // now assert the filter for the rest of the attributes
188
setMapping(destination, source);
189         assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_COPY_FOLDER);
190     }
191     
192     /**
193      * Tests the to copy a single resource to a destination that already exists but is
194      * marked as deleted.<p>
195      *
196      * @throws Exception if the test fails
197      */

198     public void testCopyOverwriteDeletedFile() throws Exception JavaDoc {
199
200         CmsObject cms = getCmsObject();
201         echo("Testing overwriting a deleted file");
202
203         cms.loginUser("test1", "test1");
204         cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));
205         
206         String JavaDoc source1 = "/folder1/page2.html";
207         String JavaDoc source2 = "/folder1/image1.gif";
208         String JavaDoc source3 = "/folder1/page3.html";
209         String JavaDoc destination = "/folder1/page1.html";
210            
211         storeResources(cms, source1);
212         storeResources(cms, source2);
213         storeResources(cms, source3);
214         storeResources(cms, destination);
215         
216         cms.lockResource(destination);
217         
218         // delete and owerwrite with a sibling of source 1
219
cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
220         assertState(cms, destination, CmsResource.STATE_DELETED);
221         
222         cms.copyResource(source1, destination, CmsResource.COPY_AS_SIBLING);
223         
224         assertState(cms, destination, CmsResource.STATE_CHANGED);
225         assertSiblingCount(cms, destination, 2);
226         assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
227         
228         assertSiblingCountIncremented(cms, source1, 1);
229         assertLock(cms, source1, CmsLock.TYPE_SHARED_EXCLUSIVE);
230         
231         assertFilter(cms, source1, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING);
232         assertFilter(cms, source1, destination, OpenCmsTestResourceFilter.FILTER_COPY_SOURCE_DESTINATION_AS_SIBLING);
233         
234         
235         // delete again and owerwrite with a sibling of source 2
236
cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
237         assertState(cms, destination, CmsResource.STATE_DELETED);
238
239         cms.copyResource(source2, destination, CmsResource.COPY_AS_SIBLING);
240
241         assertSiblingCountIncremented(cms, source1, 0);
242         assertLock(cms, source1, CmsLock.TYPE_UNLOCKED);
243         
244         assertState(cms, destination, CmsResource.STATE_CHANGED);
245         assertSiblingCount(cms, destination, 2);
246         assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
247         
248         assertSiblingCountIncremented(cms, source2, 1);
249         assertLock(cms, source2, CmsLock.TYPE_SHARED_EXCLUSIVE);
250         
251         assertFilter(cms, source1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
252         assertFilter(cms, source2, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING);
253         assertFilter(cms, source2, destination, OpenCmsTestResourceFilter.FILTER_COPY_SOURCE_DESTINATION_AS_SIBLING);
254
255         cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
256         assertState(cms, destination, CmsResource.STATE_DELETED);
257
258         // delete yet again and overwrite with content of source 3 (not a sibling)
259
cms.copyResource(source3, destination, CmsResource.COPY_AS_NEW);
260
261         assertSiblingCountIncremented(cms, source1, 0);
262         assertLock(cms, source1, CmsLock.TYPE_UNLOCKED);
263         assertSiblingCountIncremented(cms, source2, 0);
264         assertLock(cms, source2, CmsLock.TYPE_UNLOCKED);
265         
266         assertState(cms, destination, CmsResource.STATE_CHANGED);
267         assertSiblingCount(cms, destination, 1);
268         assertSiblingCount(cms, source3, 1);
269         assertLock(cms, destination, CmsLock.TYPE_EXCLUSIVE);
270         
271         assertFilter(cms, source1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
272         assertFilter(cms, source2, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
273         assertFilter(cms, source3, OpenCmsTestResourceFilter.FILTER_EQUAL);
274         assertFilter(cms, source3, destination, OpenCmsTestResourceFilter.FILTER_COPY_FILE_AS_NEW);
275     }
276         
277     /**
278      * Tests the "copy a folder as new" operation.<p>
279      *
280      * @throws Exception if the test fails
281      */

282     public void testCopyFolderAsNew() throws Exception JavaDoc {
283
284         CmsObject cms = getCmsObject();
285         echo("Testing copy of a folder as new (i.e. no siblings)");
286         
287         cms.loginUser("test1", "test1");
288         cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));
289         
290         String JavaDoc source = "/folder2/";
291         String JavaDoc destination = "/folder2_copy/";
292         long timestamp = System.currentTimeMillis();
293         
294         storeResources(cms, source);
295         cms.copyResource(source, destination, CmsResource.COPY_AS_NEW);
296
297         List JavaDoc subresources;
298         Iterator JavaDoc i;
299         
300         subresources = cms.readResources(source, CmsResourceFilter.ALL);
301         
302         // iterate through the subresources
303
i = subresources.iterator();
304         while (i.hasNext()) {
305             CmsResource res = (CmsResource)i.next();
306             String JavaDoc resName = cms.getSitePath(res);
307             assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_EQUAL);
308         }
309         
310         subresources = cms.readResources(destination, CmsResourceFilter.ALL);
311         setMapping(destination, source);
312         
313         // prepare filter without sibling count
314
OpenCmsTestResourceConfigurableFilter filter =
315             new OpenCmsTestResourceConfigurableFilter(OpenCmsTestResourceFilter.FILTER_COPY_FOLDER);
316
317         filter.disableSiblingCountTest();
318         
319         // iterate through the subresources
320
i = subresources.iterator();
321         while (i.hasNext()) {
322             CmsResource res = (CmsResource)i.next();
323             String JavaDoc resName = cms.getSitePath(res);
324             
325             // project must be current project
326
assertProject(cms, resName, cms.getRequestContext().currentProject());
327             // state must be "new"
328
assertState(cms, resName, CmsResource.STATE_NEW);
329             // date created must be new
330
assertDateCreatedAfter(cms, resName, timestamp);
331             // user created must be current user
332
assertUserCreated(cms, resName, cms.getRequestContext().currentUser());
333             // assert lock state
334
assertLock(cms, resName);
335             // must have sibling count of 1
336
assertSiblingCount(cms, resName, 1);
337             // now assert the filter for the rest of the attributes
338
assertFilter(cms, resName, filter);
339         }
340     }
341 }
Popular Tags