KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestDefaultResourceCollectors.java,v $
3  * Date : $Date: 2006/03/27 14:52:46 $
4  * Version: $Revision: 1.8 $
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.collectors.CmsDefaultResourceCollector;
35 import org.opencms.file.collectors.I_CmsResourceCollector;
36 import org.opencms.file.types.CmsResourceTypeFolder;
37 import org.opencms.file.types.CmsResourceTypePlain;
38 import org.opencms.main.CmsException;
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  * Tests the default resource collectors.<p>
50  */

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

58     public TestDefaultResourceCollectors(String JavaDoc arg0) {
59         super(arg0);
60     }
61     
62     /**
63      * Test suite for this test class.<p>
64      *
65      * @return the test suite
66      */

67     public static Test suite() {
68         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
69         
70         TestSuite suite = new TestSuite();
71         suite.setName(TestDefaultResourceCollectors.class.getName());
72
73         suite.addTest(new TestDefaultResourceCollectors("testCollectSingleFile"));
74         suite.addTest(new TestDefaultResourceCollectors("testCollectAllInFolder"));
75         suite.addTest(new TestDefaultResourceCollectors("testCollectAllInFolderDateReleasedDesc"));
76         
77         TestSetup wrapper = new TestSetup(suite) {
78             
79             protected void setUp() {
80                 CmsObject cms = setupOpenCms(null, null, false);
81                 try {
82                     initResources(cms);
83                 } catch (CmsException exc) {
84                     fail(exc.getMessage());
85                 }
86             }
87             
88             protected void tearDown() {
89                 removeOpenCms();
90             }
91         };
92         
93         return wrapper;
94     }
95
96     /**
97      * Initializes the resources needed for the tests.<p>
98      *
99      * @param cms the cms object
100      * @throws CmsException if something goes wrong
101      */

102     public static void initResources(CmsObject cms) throws CmsException {
103     
104         // create a file in the root directory
105
cms.createResource("/file1", CmsResourceTypePlain.getStaticTypeId(), null, null);
106         
107         // create a folder in the root directory
108
cms.createResource("/folder1", CmsResourceTypeFolder.getStaticTypeId());
109         
110         // create a file in the folder directory
111
cms.createResource("/folder1/file1", CmsResourceTypePlain.getStaticTypeId(), null, null);
112
113         // create a file in the folder directory
114
cms.createResource("/folder1/file2", CmsResourceTypePlain.getStaticTypeId(), null, null);
115     }
116     
117     /**
118      * Tests the "singleFile" resource collector.<p>
119      *
120      * @throws Throwable if something goes wrong
121      */

122     public void testCollectSingleFile() throws Throwable JavaDoc {
123         
124         CmsObject cms = getCmsObject();
125         echo("Testing singleFile resource collector");
126         
127         I_CmsResourceCollector collector = new CmsDefaultResourceCollector();
128         List JavaDoc resources = collector.getResults(cms, "singleFile", "/file1");
129         
130         CmsResource res = (CmsResource)resources.get(0);
131         assertEquals("/sites/default/file1", res.getRootPath());
132     }
133
134     /**
135      * Tests the "allInFolder" resource collector.<p>
136      *
137      * @throws Throwable if something goes wrong
138      */

139     public void testCollectAllInFolder() throws Throwable JavaDoc {
140         
141         CmsObject cms = getCmsObject();
142         int resTypeIdPlain = CmsResourceTypePlain.getStaticTypeId();
143         echo("Testing allInFolder resource collector");
144         
145         I_CmsResourceCollector collector = new CmsDefaultResourceCollector();
146         List JavaDoc resources = collector.getResults(cms, "allInFolder", "/folder1/|" + resTypeIdPlain);
147         
148         CmsResource res;
149         
150         // order descending determined by root path
151

152         res = (CmsResource)resources.get(0);
153         assertEquals("/sites/default/folder1/file2", res.getRootPath());
154         
155         res = (CmsResource)resources.get(1);
156         assertEquals("/sites/default/folder1/file1", res.getRootPath());
157     }
158     
159     /**
160      * Tests the "allInFolderDateReleasedDesc" resource collector.<p>
161      *
162      * @throws Throwable if something goes wrong
163      */

164     public void testCollectAllInFolderDateReleasedDesc() throws Throwable JavaDoc {
165         
166         CmsObject cms = getCmsObject();
167         int resTypeIdPlain = CmsResourceTypePlain.getStaticTypeId();
168         echo("Testing allInFolderDateReleasedDesc resource collector");
169         
170         I_CmsResourceCollector collector = new CmsDefaultResourceCollector();
171         List JavaDoc resources;
172         
173         CmsResource res;
174         
175         long day = 1000L * 60L * 60L * 24L;
176         long t1 = System.currentTimeMillis()-2*day, t2 = t1+day;
177         
178         cms.setDateLastModified("/folder1/file1", t1, false);
179         cms.setDateReleased("/folder1/file1", t1, false);
180         cms.setDateExpired("/folder1/file1", t1+3*day, false);
181         cms.setDateLastModified("/folder1/file2", t2, false);
182         cms.setDateReleased("/folder1/file2", t2, false);
183         cms.setDateExpired("/folder1/file2", t2+3*day, false);
184         
185         resources = collector.getResults(cms, "allInFolderDateReleasedDesc", "/folder1/|" + resTypeIdPlain);
186         
187         res = (CmsResource)resources.get(0);
188         assertEquals("/sites/default/folder1/file2", res.getRootPath());
189         
190         res = (CmsResource)resources.get(1);
191         assertEquals("/sites/default/folder1/file1", res.getRootPath());
192
193         cms.setDateLastModified("/folder1/file1", t2, false);
194         cms.setDateReleased("/folder1/file1", t2, false);
195         cms.setDateExpired("/folder1/file1", t2+3*day, false);
196         cms.setDateLastModified("/folder1/file2", t1, false);
197         cms.setDateReleased("/folder1/file2", t1, false);
198         cms.setDateExpired("/folder1/file2", t1+3*day, false);
199         
200         resources = collector.getResults(cms, "allInFolderDateReleasedDesc", "/folder1/|" + resTypeIdPlain);
201         
202         res = (CmsResource)resources.get(0);
203         assertEquals("/sites/default/folder1/file1", res.getRootPath());
204         
205         res = (CmsResource)resources.get(1);
206         assertEquals("/sites/default/folder1/file2", res.getRootPath());
207     }
208 }
209
Popular Tags