KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestProjects.java,v $
3  * Date : $Date: 2006/03/27 14:52:46 $
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.main.OpenCms;
37 import org.opencms.test.OpenCmsTestCase;
38 import org.opencms.test.OpenCmsTestProperties;
39 import org.opencms.test.OpenCmsTestResourceFilter;
40
41 import java.util.Collections JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44
45 import junit.extensions.TestSetup;
46 import junit.framework.Test;
47 import junit.framework.TestSuite;
48
49 /**
50  * Unit tests OpenCms projects.<p>
51  *
52  * @author Alexander Kandzior
53  *
54  * @version $Revision: 1.16 $
55  */

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

63     public TestProjects(String JavaDoc arg0) {
64         super(arg0);
65     }
66     
67     /**
68      * Test suite for this test class.<p>
69      *
70      * @return the test suite
71      */

72     public static Test suite() {
73         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
74         
75         TestSuite suite = new TestSuite();
76         suite.setName(TestProjects.class.getName());
77                 
78         suite.addTest(new TestProjects("testCreateDeleteProject"));
79         suite.addTest(new TestProjects("testCopyResourceToProject"));
80         suite.addTest(new TestProjects("testDeleteProjectWithResources"));
81         suite.addTest(new TestProjects("testReadProjectResources"));
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      * Test the "createProject" and "deleteProject" methods.<p>
99      *
100      * @throws Exception if the test fails
101      */

102     public void testCreateDeleteProject() throws Exception JavaDoc {
103         
104         CmsObject cms = getCmsObject();
105         
106         echo("Testing creating a project");
107         
108         String JavaDoc projectName = "UnitTest2";
109         
110         CmsProject project = cms.createProject(
111             projectName,
112             "Unit test project 2",
113             OpenCms.getDefaultUsers().getGroupUsers(),
114             OpenCms.getDefaultUsers().getGroupProjectmanagers(),
115             CmsProject.PROJECT_TYPE_NORMAL
116         );
117         
118         // some basic project tests
119
assertEquals(projectName, project.getName());
120         assertFalse(project.isOnlineProject());
121         
122         // ensure the project is now accessible
123
List JavaDoc projects = cms.getAllAccessibleProjects();
124         int i;
125         for (i = 0; i < projects.size(); i++) {
126             if (((CmsProject)projects.get(i)).getId() == project.getId()) {
127                 break;
128             }
129         }
130         if (i >= projects.size()) {
131             fail ("Project " + project.getName() + "not accessible");
132         }
133         
134         // ensure the project is manageable
135
projects = cms.getAllManageableProjects();
136         for (i = 0; i < projects.size(); i++) {
137             if (((CmsProject)projects.get(i)).getId() == project.getId()) {
138                 break;
139             }
140         }
141         if (i >= projects.size()) {
142             fail ("Project " + project.getName() + "not manageable");
143         }
144         
145         echo("Testing deleting a project");
146         
147         // try to delete the project
148
cms.deleteProject(project.getId());
149         
150         // ensure the project is not accessible anymore
151
projects = cms.getAllAccessibleProjects();
152         for (i = 0; i < projects.size(); i++) {
153             if (((CmsProject)projects.get(i)).getId() == project.getId()) {
154                 fail ("Project " + project.getName() + "not deleted");
155             }
156         }
157     }
158     
159     /**
160      * Test the "delete project with resources" function.<p>
161      *
162      * @throws Exception if the test fails
163      */

164     public void testDeleteProjectWithResources() throws Exception JavaDoc {
165         
166         CmsObject cms = getCmsObject();
167         
168         echo("Creating a project for deletion test with resources");
169         
170         String JavaDoc projectName = "UnitTest3";
171         
172         CmsProject project = cms.createProject(
173             projectName,
174             "Unit test project 3",
175             OpenCms.getDefaultUsers().getGroupUsers(),
176             OpenCms.getDefaultUsers().getGroupProjectmanagers(),
177             CmsProject.PROJECT_TYPE_NORMAL
178         );
179         
180         // use the main folder as start folder for the project
181
String JavaDoc resource = "/";
182         
183         // store the resource
184
storeResources(cms, resource);
185
186         // switch to the project
187
cms.getRequestContext().setCurrentProject(project);
188         
189         // copy the main site folder to the project
190
cms.copyResourceToProject("/");
191         
192         // some basic project tests
193
assertEquals(projectName, project.getName());
194         assertFalse(project.isOnlineProject());
195         
196         // do some changes to the project
197
cms.lockResource(resource);
198         cms.setDateLastModified("/folder1/", System.currentTimeMillis(), true);
199         cms.deleteResource("/folder2/", CmsResource.DELETE_REMOVE_SIBLINGS);
200         cms.createResource("/folder3/", CmsResourceTypeFolder.getStaticTypeId(), null, Collections.EMPTY_LIST);
201         cms.createResource("/folder3/test.txt", CmsResourceTypePlain.getStaticTypeId(), "".getBytes(), Collections.EMPTY_LIST);
202         cms.unlockResource(resource);
203                 
204         // switch to the offline project
205
CmsProject offlineProject = cms.readProject("Offline");
206         cms.getRequestContext().setCurrentProject(offlineProject);
207         
208         // now delete the project - all changes in the project must be undone
209
cms.deleteProject(project.getId());
210
211         // ensure that the original resources are unchanged
212
assertFilter(cms, resource, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
213         
214         // all resources within the folder must be unchanged now
215
Iterator JavaDoc j = cms.readResources(resource, CmsResourceFilter.ALL, true).iterator();
216         while (j.hasNext()) {
217             CmsResource res = (CmsResource)j.next();
218             String JavaDoc resName = cms.getSitePath(res);
219                         
220             // now evaluate the result
221
assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
222         }
223     }
224     
225     /**
226      * Test the "copy resource to project" function.<p>
227      *
228      * @throws Exception if the test fails
229      */

230     public void testCopyResourceToProject() throws Exception JavaDoc {
231         
232         CmsObject cms = getCmsObject();
233         echo("Testing copying a resource to a project");
234         
235         String JavaDoc projectName = "UnitTest1";
236         
237         cms.getRequestContext().saveSiteRoot();
238         cms.getRequestContext().setSiteRoot("/");
239         try {
240             CmsProject project = cms.createProject(
241                 projectName,
242                 "Unit test project 1",
243                 OpenCms.getDefaultUsers().getGroupUsers(),
244                 OpenCms.getDefaultUsers().getGroupProjectmanagers(),
245                 CmsProject.PROJECT_TYPE_NORMAL
246             );
247             cms.getRequestContext().setCurrentProject(project);
248             cms.copyResourceToProject("/sites/default/index.html");
249             cms.copyResourceToProject("/sites/default/folder1/");
250         } finally {
251             cms.getRequestContext().restoreSiteRoot();
252         }
253         
254         CmsProject current = cms.readProject(projectName);
255         cms.getRequestContext().setCurrentProject(current);
256         
257         // some basic project tests
258
assertEquals(projectName, current.getName());
259         assertFalse(current.isOnlineProject());
260         
261         // check the project resources
262
List JavaDoc currentResources = cms.readProjectResources(current);
263         assertTrue(CmsProject.isInsideProject(currentResources, "/sites/default/index.html"));
264         assertTrue(CmsProject.isInsideProject(currentResources, "/sites/default/folder1/"));
265         assertTrue(CmsProject.isInsideProject(currentResources, "/sites/default/folder1/subfolder11/index.html"));
266         assertFalse(CmsProject.isInsideProject(currentResources, "/sites/default/"));
267         assertFalse(CmsProject.isInsideProject(currentResources, "/"));
268         assertFalse(CmsProject.isInsideProject(currentResources, "/sites/default/folder2/index.html"));
269     }
270     
271     /**
272      * Test the "readProjectResources" method.<p>
273      *
274      * @throws Exception if the test fails
275      */

276     public void testReadProjectResources() throws Exception JavaDoc {
277         
278         CmsObject cms = getCmsObject();
279         
280         echo("Testing to read all project resources");
281                 
282         String JavaDoc projectName = "UnitTest4";
283         
284         cms.getRequestContext().saveSiteRoot();
285         cms.getRequestContext().setSiteRoot("/");
286         try {
287             CmsProject project = cms.createProject(
288                 projectName,
289                 "Unit test project 4",
290                 OpenCms.getDefaultUsers().getGroupUsers(),
291                 OpenCms.getDefaultUsers().getGroupProjectmanagers(),
292                 CmsProject.PROJECT_TYPE_NORMAL
293             );
294             cms.getRequestContext().setCurrentProject(project);
295             cms.copyResourceToProject("/sites/default/index.html");
296             cms.copyResourceToProject("/sites/default/folder1/");
297         } finally {
298             cms.getRequestContext().restoreSiteRoot();
299         }
300         
301         CmsProject current = cms.readProject(projectName);
302         cms.getRequestContext().setCurrentProject(current);
303         
304         // some basic project tests
305
assertEquals(projectName, current.getName());
306         assertFalse(current.isOnlineProject());
307         
308         // check the project resources
309
List JavaDoc projectResources = cms.readProjectResources(current);
310         
311         // check the project resource list
312
assertEquals(2, projectResources.size());
313         assertTrue(projectResources.contains("/sites/default/index.html"));
314         assertTrue(projectResources.contains("/sites/default/folder1/"));
315     }
316 }
317
Popular Tags