KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestReadResourceTree.java,v $
3  * Date : $Date: 2006/03/27 14:52:46 $
4  * Version: $Revision: 1.12 $
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.test.OpenCmsTestCase;
36 import org.opencms.test.OpenCmsTestProperties;
37 import org.opencms.test.OpenCmsTestResourceFilter;
38
39 import java.util.List JavaDoc;
40
41 import junit.extensions.TestSetup;
42 import junit.framework.Test;
43 import junit.framework.TestSuite;
44
45 /**
46  * Unit test for the "readResources" method of the CmsObject to test reading resource lists within a subtree.<p>
47  *
48  * @author Carsten Weinholz
49  * @version $Revision: 1.12 $
50  */

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

58     public TestReadResourceTree(String JavaDoc arg0) {
59
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(TestReadResourceTree.class.getName());
73
74         suite.addTest(new TestReadResourceTree("testReadSubtree"));
75         suite.addTest(new TestReadResourceTree("testReadChildren"));
76         suite.addTest(new TestReadResourceTree("testReadFolders"));
77         suite.addTest(new TestReadResourceTree("testReadFiles"));
78         suite.addTest(new TestReadResourceTree("testReadResources"));
79         suite.addTest(new TestReadResourceTree("testReadModifiedResources"));
80         suite.addTest(new TestReadResourceTree("testReadResourcesInTimerange"));
81
82         TestSetup wrapper = new TestSetup(suite) {
83
84             protected void setUp() {
85
86                 setupOpenCms("simpletest", "/sites/default/");
87             }
88
89             protected void tearDown() {
90
91                 removeOpenCms();
92             }
93         };
94
95         return wrapper;
96     }
97
98     /**
99      * Test readResources for reading a subtree below a given path.<p>
100      *
101      * @throws Throwable if something goes wrong
102      */

103     public void testReadSubtree() throws Throwable JavaDoc {
104
105         CmsObject cms = getCmsObject();
106         echo("Testing readResources: reading a subtree");
107
108         cms.getRequestContext().setSiteRoot("/");
109
110         String JavaDoc path = "/sites/default/folder1/subfolder12";
111
112         // store all resources of the expected result
113
// storeResources(cms, path, false);
114
storeResources(cms, path + "/subsubfolder121", false);
115         storeResources(cms, path + "/subsubfolder121/image1.gif", false);
116         storeResources(cms, path + "/subsubfolder121/index.html", false);
117         storeResources(cms, path + "/subsubfolder121/page1.html", false);
118         storeResources(cms, path + "/index.html", false);
119         storeResources(cms, path + "/page1.html", false);
120         storeResources(cms, path + "/page2.html", false);
121
122         // read each resource below folder1/subfolder12
123
List JavaDoc result = cms.readResources(path, CmsResourceFilter.ALL);
124
125         // check each resource in the result
126
int i;
127         for (i = 0; i < result.size(); i++) {
128             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
129         }
130
131         // check the number of resources
132
assertEquals(this.m_currentResourceStrorage.size(), i);
133     }
134
135     /**
136      * Test readResources for reading immediate child resources below a given path.<p>
137      *
138      * @throws Throwable if something goes wrong
139      */

140     public void testReadChildren() throws Throwable JavaDoc {
141
142         CmsObject cms = getCmsObject();
143         echo("Testing readResources: reading child resources");
144
145         cms.getRequestContext().setSiteRoot("/");
146
147         String JavaDoc path = "/sites/default/folder1/subfolder12";
148
149         // store all resources of the expected result
150
// storeResources(cms, path, false);
151
storeResources(cms, path + "/subsubfolder121", false);
152         storeResources(cms, path + "/index.html", false);
153         storeResources(cms, path + "/page1.html", false);
154         storeResources(cms, path + "/page2.html", false);
155
156         // read each resource below folder1/subfolder12
157
List JavaDoc result = cms.readResources(path, CmsResourceFilter.ALL, false);
158
159         // check each resource in the result
160
int i;
161         for (i = 0; i < result.size(); i++) {
162             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
163         }
164
165         // check the number of resources
166
assertEquals(this.m_currentResourceStrorage.size(), i);
167     }
168
169     /**
170      * Test readResources for reading folder resources.<p>
171      *
172      * @throws Throwable if something goes wrong
173      */

174     public void testReadFolders() throws Throwable JavaDoc {
175
176         CmsObject cms = getCmsObject();
177         echo("Testing readResources: reading folder resources");
178
179         cms.getRequestContext().setSiteRoot("/");
180
181         String JavaDoc path = "/sites/default/folder2";
182
183         // store all resources of the expected result
184
// storeResources(cms, path, false);
185
storeResources(cms, path + "/subfolder21", false);
186         storeResources(cms, path + "/subfolder21/subsubfolder211", false);
187         storeResources(cms, path + "/subfolder22", false);
188         storeResources(cms, path + "/subfolder22/subsubfolder221", false);
189
190         // read each resource below folder2
191
List JavaDoc result = cms.readResources(path, CmsResourceFilter.DEFAULT_FOLDERS);
192
193         // check each resource in the result
194
int i;
195         for (i = 0; i < result.size(); i++) {
196             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
197         }
198
199         // check the number of resources
200
assertEquals(this.m_currentResourceStrorage.size(), i);
201     }
202
203     /**
204      * Test readResources for reading file resources.<p>
205      *
206      * @throws Throwable if something goes wrong
207      */

208     public void testReadFiles() throws Throwable JavaDoc {
209
210         CmsObject cms = getCmsObject();
211         echo("Testing readResources: reading file resources");
212
213         cms.getRequestContext().setSiteRoot("/");
214         String JavaDoc path = "/sites/default/folder2";
215
216         // store all resources of the expected result
217
storeResources(cms, path + "/subfolder21/subsubfolder211/jsp1.jsp", false);
218         storeResources(cms, path + "/subfolder21/subsubfolder211/jsp2.jsp", false);
219         storeResources(cms, path + "/subfolder21/image1.gif", false);
220         storeResources(cms, path + "/subfolder21/index.html", false);
221         storeResources(cms, path + "/subfolder21/page1.html", false);
222         storeResources(cms, path + "/subfolder22/subsubfolder221/index.html", false);
223         storeResources(cms, path + "/subfolder22/subsubfolder221/jsp1.jsp", false);
224         storeResources(cms, path + "/subfolder22/subsubfolder221/page1.html", false);
225         storeResources(cms, path + "/subfolder22/index.html", false);
226         storeResources(cms, path + "/subfolder22/page1.html", false);
227         storeResources(cms, path + "/subfolder22/page2.html", false);
228         storeResources(cms, path + "/subfolder22/page3.html", false);
229         storeResources(cms, path + "/image1.gif", false);
230         storeResources(cms, path + "/image2.gif", false);
231         storeResources(cms, path + "/index.html", false);
232         storeResources(cms, path + "/page1.html", false);
233         storeResources(cms, path + "/page2.html", false);
234
235         // read each non-folder resource below folder2
236
List JavaDoc result = cms.readResources(path, CmsResourceFilter.DEFAULT_FILES);
237
238         // check each resource in the result
239
int i;
240         for (i = 0; i < result.size(); i++) {
241             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
242         }
243
244         // check the number of resources
245
assertEquals(this.m_currentResourceStrorage.size(), i);
246     }
247
248     /**
249      * Test the method that read the direct sub-resources of a folder.<p>
250      *
251      * @throws Throwable if something goes wrong
252      */

253     public void testReadResources() throws Throwable JavaDoc {
254
255         CmsObject cms = getCmsObject();
256         echo("Testing readResources: reading child resources");
257
258         cms.getRequestContext().setSiteRoot("/");
259
260         String JavaDoc path = "/sites/default/";
261
262         List JavaDoc result;
263         
264         result = cms.getResourcesInFolder(path, CmsResourceFilter.ALL);
265         assertEquals(5, result.size());
266         
267         result = cms.getSubFolders(path, CmsResourceFilter.ALL);
268         assertEquals(4, result.size());
269         
270         result = cms.getFilesInFolder(path, CmsResourceFilter.ALL);
271         assertEquals(1, result.size());
272     }
273     
274     /**
275      * Test readResources for reading modified resources.<p>
276      *
277      * @throws Throwable if something goes wrong
278      */

279     public void testReadModifiedResources() throws Throwable JavaDoc {
280
281         CmsObject cms = getCmsObject();
282         echo("Testing readResources: reading modified resources");
283
284         cms.getRequestContext().setSiteRoot("/");
285
286         String JavaDoc path = "/sites/default/folder2";
287         String JavaDoc resourcename;
288
289         // touch/create/delete some resources
290
resourcename = path + "/subfolder21/subsubfolder211/jsp1.jsp";
291         cms.lockResource(resourcename);
292         cms.setDateLastModified(resourcename, System.currentTimeMillis(), false);
293
294         resourcename = path + "/subfolder22/subsubfolder221/page1.html";
295         cms.lockResource(resourcename);
296         cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
297
298         resourcename = path + "/newpage.html";
299         cms.createResource(resourcename, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
300
301         // store all resources of the expected result
302
storeResources(cms, path + "/newpage.html", false);
303
304         // read new resources
305
List JavaDoc result = cms.readResources("/", CmsResourceFilter.ALL.addRequireState(CmsResource.STATE_NEW));
306
307         // check each resource in the result
308
int i;
309         for (i = 0; i < result.size(); i++) {
310             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
311         }
312
313         // check the number of resources
314
assertEquals(this.m_currentResourceStrorage.size(), i);
315
316         // store all resources of the expected result
317
storeResources(cms, path + "/subfolder21/subsubfolder211/jsp1.jsp", false);
318         storeResources(cms, path + "/subfolder22/subsubfolder221/jsp1.jsp", false); // sibling
319
storeResources(cms, path + "/subfolder22/subsubfolder221/page1.html", false);
320
321         // read each resource with a modified state ("not unchanged")
322
result = cms.readResources("/", CmsResourceFilter.ALL_MODIFIED);
323
324         // check each resource in the result
325
for (i = 0; i < result.size(); i++) {
326             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
327         }
328
329         // check the number of resources
330
assertEquals(this.m_currentResourceStrorage.size(), i);
331     }
332
333     /**
334      * Test readResources for reading resources modified within a timerange.<p>
335      *
336      * @throws Throwable if something goes wrong
337      */

338     public void testReadResourcesInTimerange() throws Throwable JavaDoc {
339
340         CmsObject cms = getCmsObject();
341         echo("Testing readResources: reading resources modified within a timerange");
342
343         long timestamp1 = CmsResource.DATE_RELEASED_DEFAULT + 1;
344         long timestamp2 = System.currentTimeMillis();
345         List JavaDoc result = null;
346
347         cms.getRequestContext().setSiteRoot("/");
348
349         String JavaDoc path = "/sites/default/folder1";
350         String JavaDoc resourcename;
351
352         // ensure no resource was modified before timestamp1
353
result = cms.readResources("/", CmsResourceFilter.ALL.addRequireLastModifiedBefore(timestamp1));
354
355         if (result.size() > 0) {
356             fail("Unexpected modification dates in resources found");
357         }
358
359         // now touch a resource with a timestamp before timestamp1 and check that it is now modified before timestamp1
360
resourcename = path + "/subfolder12/index.html";
361         cms.lockResource(resourcename);
362         cms.setDateLastModified(resourcename, timestamp1 - 1, false);
363
364         // store all resources of the expected result
365
storeResources(cms, resourcename, false);
366
367         // ensure this resource was modified before timestamp1
368
result = cms.readResources("/", CmsResourceFilter.ALL.addRequireLastModifiedBefore(timestamp1));
369
370         // check each resource in the result
371
int i;
372         for (i = 0; i < result.size(); i++) {
373             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
374         }
375
376         // check the number of resources
377
assertEquals(this.m_currentResourceStrorage.size(), i);
378
379         // ensure no resource was modified after timestamp2
380
result = cms.readResources("/", CmsResourceFilter.ALL.addRequireLastModifiedAfter(timestamp2));
381
382         if (result.size() > 0) {
383             fail("Unexpected modification dates in resources found");
384         }
385
386         // now touch the resource with a timestamp after timestamp2 and check that it is now modified after timestamp2
387
cms.lockResource(resourcename);
388         cms.setDateLastModified(resourcename, timestamp2 + 1, false);
389
390         // store all resources of the expected result
391
storeResources(cms, resourcename, false);
392
393         // ensure this resource was modified after timestamp2
394
result = cms.readResources("/", CmsResourceFilter.ALL.addRequireLastModifiedAfter(timestamp2));
395
396         // check each resource in the result
397
for (i = 0; i < result.size(); i++) {
398             assertFilter(cms, (CmsResource)result.get(i), OpenCmsTestResourceFilter.FILTER_EQUAL);
399         }
400
401         // check the number of resources
402
assertEquals(this.m_currentResourceStrorage.size(), i);
403     }
404 }
Popular Tags