KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestResourceOperations.java,v $
3  * Date : $Date: 2005/06/23 11:11:43 $
4  * Version: $Revision: 1.14 $
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.CmsException;
37 import org.opencms.main.CmsIllegalArgumentException;
38 import org.opencms.main.CmsRuntimeException;
39 import org.opencms.test.OpenCmsTestCase;
40 import org.opencms.test.OpenCmsTestProperties;
41
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 basic resource operations without test import.<p>
50  *
51  * @author Carsten Weinholz
52  *
53  * @version $Revision: 1.14 $
54  */

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

62     public TestResourceOperations(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(TestResourceOperations.class.getName());
76
77         suite.addTest(new TestResourceOperations("testGetFolderPath"));
78         suite.addTest(new TestResourceOperations("testGetName"));
79         suite.addTest(new TestResourceOperations("testGetParentFolder"));
80         suite.addTest(new TestResourceOperations("testGetPathLevel"));
81         suite.addTest(new TestResourceOperations("testGetPathPart"));
82         suite.addTest(new TestResourceOperations("testIsFolder"));
83         suite.addTest(new TestResourceOperations("testGetFolderPath"));
84         suite.addTest(new TestResourceOperations("testResourceNames"));
85         suite.addTest(new TestResourceOperations("testCreateResources"));
86         suite.addTest(new TestResourceOperations("testCreateReadFile"));
87         suite.addTest(new TestResourceOperations("testPublishFile"));
88         suite.addTest(new TestResourceOperations("testCreateSibling"));
89         suite.addTest(new TestResourceOperations("testCreateAccessFolders"));
90         
91         TestSetup wrapper = new TestSetup(suite) {
92             
93             protected void setUp() {
94                 setupOpenCms(null, null, false);
95             }
96             
97             protected void tearDown() {
98                 removeOpenCms();
99             }
100         };
101         
102         return wrapper;
103     }
104     
105     /**
106      * Tests the static "getFolderPath" method.<p>
107      *
108      * @throws Throwable if something goes wrong
109      */

110     public void testGetFolderPath() throws Throwable JavaDoc {
111         
112         echo("Testing testGetFolderPath");
113         
114         assertEquals(CmsResource.getFolderPath("/system/def/file.html"), "/system/def/");
115         
116         assertEquals(CmsResource.getFolderPath("/system/def/"), "/system/def/");
117     }
118     
119     /**
120      * Tests the static "getName" method.<p>
121      *
122      * @throws Throwable if something goes wrong
123      */

124     public void testGetName() throws Throwable JavaDoc {
125         
126         echo("Testing testGetName");
127         
128         assertEquals(CmsResource.getName("/system/workplace/"), "workplace/");
129     }
130     
131     /**
132      * Tests the static "getParentFolder" method.<p>
133      *
134      * @throws Throwable if something goes wrong
135      */

136     public void testGetParentFolder() throws Throwable JavaDoc {
137         
138         echo("Testing testGetParentFolder");
139         
140         assertEquals(CmsResource.getParentFolder("/system/workplace/"), "/system/");
141     }
142     
143     /**
144      * Tests the static "getPathLevel" method.<p>
145      *
146      * @throws Throwable if something goes wrong
147      */

148     public void testGetPathLevel() throws Throwable JavaDoc {
149         
150         echo("Testing testGetPathLevel");
151         
152         assertEquals(CmsResource.getPathLevel("/"), 0);
153         
154         assertEquals(CmsResource.getPathLevel("/foo/"), 1);
155         
156         assertEquals(CmsResource.getPathLevel("/foo/bar/"), 2);
157     }
158     
159     /**
160      * Tests the static "getPathPart" method.<p>
161      *
162      * @throws Throwable if something goes wrong
163      */

164     public void testGetPathPart() throws Throwable JavaDoc {
165         
166         echo("Testing testGetPathPart");
167         
168         assertEquals(CmsResource.getPathPart("/foo/bar/", 0), "/");
169         assertEquals(CmsResource.getPathPart("/foo/bar/", 1), "/foo/");
170         assertEquals(CmsResource.getPathPart("/foo/bar/", 2), "/foo/bar/");
171         // TODO: CW - unexpected behaviour ???
172
assertEquals(CmsResource.getPathPart("/foo/bar/", -1), "/foo/bar/");
173         assertEquals(CmsResource.getPathPart("/foo/bar/", -2), "/foo/");
174     }
175     
176     /**
177      * Tests the static "isFolder" method.<p>
178      *
179      * @throws Throwable if something goes wrong
180      */

181     public void testIsFolder() throws Throwable JavaDoc {
182         
183         echo("Testing testIsFolder");
184     }
185
186     /**
187      * Tests the check for valid resource names.<p>
188      *
189      * @throws Throwable if something goes wrong
190      */

191     public void testResourceNames() throws Throwable JavaDoc {
192         
193         CmsObject cms = getCmsObject();
194         echo("Testing invalid resource names");
195         
196         CmsRuntimeException exc;
197         
198         // resource name must not contain blanks
199
exc = null;
200         try {
201             cms.createResource("/Resource Name", CmsResourceTypePlain.getStaticTypeId(), null, null);
202         } catch (CmsIllegalArgumentException e) {
203             exc = e;
204         }
205
206         assertTrue(exc instanceof CmsIllegalArgumentException);
207         
208         // resource name must not contain leading blanks
209
exc = null;
210         try {
211             cms.createResource("/ ResourceName", CmsResourceTypePlain.getStaticTypeId(), null, null);
212         } catch (CmsIllegalArgumentException e) {
213             exc = e;
214         }
215
216         assertTrue(exc instanceof CmsIllegalArgumentException);
217         
218         // resource name must not contain trailing blanks
219
exc = null;
220         try {
221             cms.createResource("/ResourceName ", CmsResourceTypePlain.getStaticTypeId(), null, null);
222         } catch (CmsIllegalArgumentException e) {
223             exc = e;
224         }
225
226         assertTrue(exc instanceof CmsIllegalArgumentException);
227         
228         // resource name must not contain other characters
229
exc = null;
230         try {
231             cms.createResource("/Resource#Name", CmsResourceTypePlain.getStaticTypeId(), null, null);
232         } catch (CmsIllegalArgumentException e) {
233             exc = e;
234         }
235
236         assertTrue(exc instanceof CmsIllegalArgumentException);
237     }
238     
239     /**
240      * Tests the "createResource" operation.<p>
241      *
242      * @throws Throwable if something goes wrong
243      */

244     public void testCreateResources() throws Throwable JavaDoc {
245
246         CmsObject cms = getCmsObject();
247         echo("Testing resource creation");
248         
249         // create a folder in the root directory
250
cms.createResource("/folder1", CmsResourceTypeFolder.getStaticTypeId());
251         
252         // create an empty file in the root directory
253
cms.createResource("/resource2", CmsResourceTypePlain.getStaticTypeId());
254         
255         // create an empty file in the created folder
256
cms.createResource("/folder1/resource3", CmsResourceTypePlain.getStaticTypeId());
257         
258         // ensure first created resource is a folder
259
assertIsFolder(cms, "/folder1/");
260         
261         // ensure second created resource is a plain text file
262
assertResourceType(cms, "/resource2", CmsResourceTypePlain.getStaticTypeId());
263         
264         // ensure third created resource is a plain text file
265
assertResourceType(cms, "/folder1/resource3", CmsResourceTypePlain.getStaticTypeId());
266
267         
268     }
269     
270     /**
271      * Test folder creation and reading with and without trailing "/".<p>
272      *
273      * @throws Throwable if something goes wrong
274      */

275     public void testCreateAccessFolders() throws Throwable JavaDoc {
276         
277         CmsObject cms = getCmsObject();
278         echo("Testing folder creation and access");
279         
280         CmsException exc;
281         
282         // create a folder without trailing / in the resource name
283
cms.createResource("/cafolder1", CmsResourceTypeFolder.getStaticTypeId());
284
285         // create a folder with trailing / in the resource name
286
cms.createResource("/cafolder2/", CmsResourceTypeFolder.getStaticTypeId());
287                 
288         // access a folder without trailing / in the resource name
289
// and ensure that its root path is a valid folder path (i.e. with trailing /)
290
assertTrue(CmsResource.isFolder(cms.readResource("/cafolder2").getRootPath()));
291
292         // access a folder with trailing / in the resource name
293
// and ensure that its root path is a valid folder path (i.e. with trailing /)
294
assertTrue(CmsResource.isFolder(cms.readResource("/cafolder1/").getRootPath()));
295
296         // check the folder access using another query
297
// and ensure that the root paths are valid folder paths
298
List JavaDoc l;
299         l = cms.getSubFolders("/");
300         for (int i=0; i<l.size(); i++) {
301             CmsResource r = (CmsResource)l.get(i);
302             if (!(CmsResource.isFolder(r.getRootPath()))) {
303                 fail("Invalid folder name returned via getRootPath (" + r.getRootPath() + ")");
304             }
305         }
306         
307         // try to create another resource with the same name - must fail
308
exc = null;
309         try {
310             cms.createResource("/cafolder1", CmsResourceTypePlain.getStaticTypeId());
311         } catch (CmsException e) {
312             exc = e;
313         }
314         assertTrue(exc instanceof CmsVfsResourceAlreadyExistsException);
315     }
316
317     /**
318      * Tests the create and read file methods.<p>
319      *
320      * @throws Throwable if something goes wrong
321      */

322     public void testCreateReadFile() throws Throwable JavaDoc {
323         
324         CmsObject cms = getCmsObject();
325         echo("Testing file creation");
326         
327         String JavaDoc content = "this is a test content";
328         
329         // create a file in the root directory
330
cms.createResource("/file1", CmsResourceTypePlain.getStaticTypeId(), content.getBytes(), null);
331         
332         // read and check the content
333
this.assertContent(cms, "/file1" , content.getBytes());
334     }
335
336     /**
337      * Tests the publish resource method for file.<p>
338      *
339      * @throws Throwable if something goes wrong
340      */

341     public void testPublishFile() throws Throwable JavaDoc {
342
343         CmsObject cms = getCmsObject();
344         echo("Testing file publishing");
345
346         String JavaDoc content = "this is a test content";
347                 
348         // set the site root
349
cms.getRequestContext().setSiteRoot("/");
350
351         // create a file in the root directory
352
cms.createResource("/file2", CmsResourceTypePlain.getStaticTypeId(), content.getBytes(), null);
353         
354         // the reosurce must unlocked, otherwise it will not be published
355
cms.unlockResource("/file2");
356         
357         // now publish the file
358
cms.publishResource("/file2");
359         
360         // change the project to online
361
cms.getRequestContext().setCurrentProject(cms.readProject("Online"));
362         
363         // read and check the content
364
this.assertContent(cms, "/file2" , content.getBytes());
365         
366         // switch back to offline project
367
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
368     }
369     
370     /**
371      * Tests the "createSibling" operation.<p>
372      *
373      * @throws Throwable if something goes wrong
374      */

375     public void testCreateSibling() throws Throwable JavaDoc {
376         
377         CmsObject cms = getCmsObject();
378         echo("Testing sibling creation");
379         
380         // create an empty file in the root directory
381
cms.createResource("/resource4", CmsResourceTypePlain.getStaticTypeId());
382         
383         // ensure that sibling count is zero
384
assertSiblingCount(cms, "/resource4", 1);
385         
386         // create a sibling of res3 in root folder
387
cms.createSibling("/resource4", "/sibling1", null);
388
389         // ensure first created resource is a plain text file
390
assertResourceType(cms, "/resource4", CmsResourceTypePlain.getStaticTypeId());
391         
392         // ensure sibling is also a plain text file
393
assertResourceType(cms, "/sibling1", CmsResourceTypePlain.getStaticTypeId());
394         
395         // check the sibling count
396
assertSiblingCount(cms, "/resource4", 2);
397         assertSiblingCount(cms, "/sibling1", 2);
398     }
399  
400 }
Popular Tags