KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestPriorityResourceCollectors.java,v $
3  * Date : $Date: 2005/06/27 23:22:09 $
4  * Version: $Revision: 1.5 $
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.CmsPriorityDateResourceComparator;
35 import org.opencms.file.collectors.CmsPriorityResourceCollector;
36 import org.opencms.file.collectors.I_CmsResourceCollector;
37 import org.opencms.file.types.CmsResourceTypeFolder;
38 import org.opencms.file.types.CmsResourceTypeJsp;
39 import org.opencms.file.types.CmsResourceTypePlain;
40 import org.opencms.main.CmsException;
41 import org.opencms.test.OpenCmsTestCase;
42 import org.opencms.test.OpenCmsTestProperties;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46
47 import junit.extensions.TestSetup;
48 import junit.framework.Test;
49 import junit.framework.TestSuite;
50
51 /**
52  * Tests the priority resource collectors.<p>
53  */

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

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

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

104     public static void initResources(CmsObject cms) throws CmsException {
105         
106         List JavaDoc properties = new ArrayList JavaDoc(2);
107         CmsProperty propPrio = new CmsProperty();
108         propPrio.setName(CmsPriorityResourceCollector.PROPERTY_PRIORITY);
109         CmsProperty propDate = new CmsProperty();
110         propDate.setName(CmsPriorityDateResourceComparator.PROPERTY_DATE);
111         
112         long time = System.currentTimeMillis();
113         
114         // absolute priority order of the created files:
115
// /folder1/sub1/file5, /file1, /folder1/file3, /folder1/file2, /folder1/file1, /folder1/file4
116

117         // create a file in the root directory
118
propPrio.setStructureValue("15");
119         properties.add(propPrio);
120         propDate.setStructureValue("" + time);
121         properties.add(propDate);
122         CmsProperty.setAutoCreatePropertyDefinitions(properties, true);
123         cms.createResource("/file1", CmsResourceTypePlain.getStaticTypeId(), null, properties);
124         
125         // create a folder in the root directory
126
cms.createResource("/folder1", CmsResourceTypeFolder.getStaticTypeId());
127         
128         // create a file in the folder directory
129
properties.clear();
130         propPrio.setStructureValue("5");
131         properties.add(propPrio);
132         propDate.setStructureValue("" + (time + 20));
133         properties.add(propDate);
134         cms.createResource("/folder1/file1", CmsResourceTypePlain.getStaticTypeId(), null, properties);
135
136         // create a file in the folder directory
137
properties.clear();
138         propPrio.setStructureValue("10");
139         properties.add(propPrio);
140         propDate.setStructureValue("" + time);
141         properties.add(propDate);
142         cms.createResource("/folder1/file2", CmsResourceTypePlain.getStaticTypeId(), null, properties);
143         
144         // create a file in the folder directory
145
properties.clear();
146         propPrio.setStructureValue("10");
147         properties.add(propPrio);
148         propDate.setStructureValue("" + (time + 10));
149         properties.add(propDate);
150         cms.createResource("/folder1/file3", CmsResourceTypePlain.getStaticTypeId(), null, properties);
151         
152         // create a file in the folder directory
153
properties.clear();
154         propPrio.setStructureValue("1");
155         properties.add(propPrio);
156         propDate.setStructureValue("" + (time + 30));
157         properties.add(propDate);
158         cms.createResource("/folder1/file4", CmsResourceTypePlain.getStaticTypeId(), null, properties);
159         
160         // create a file of other type in the folder directory
161
properties.clear();
162         propPrio.setStructureValue("10");
163         properties.add(propPrio);
164         propDate.setStructureValue("" + (time + 50));
165         properties.add(propDate);
166         cms.createResource("/folder1/fileJsp", CmsResourceTypeJsp.getStaticTypeId(), null, properties);
167         
168         // create a subfolder in the folder1 directory
169
cms.createResource("/folder1/sub1", CmsResourceTypeFolder.getStaticTypeId());
170         
171         //create a file in the subfolder directory
172
properties.clear();
173         propPrio.setStructureValue("15");
174         properties.add(propPrio);
175         propDate.setStructureValue("" + time + 40);
176         properties.add(propDate);
177         cms.createResource("/folder1/sub1/file5", CmsResourceTypePlain.getStaticTypeId(), null, properties);
178     }
179     
180     /**
181      * Tests the "allInFolderPriorityDesc" resource collector.<p>
182      *
183      * @throws Throwable if something goes wrong
184      */

185     public void testCollectAllInFolderPriority() throws Throwable JavaDoc {
186         
187         CmsObject cms = getCmsObject();
188         int resTypeIdPlain = CmsResourceTypePlain.getStaticTypeId();
189         echo("Testing allInFolderPriorityDateDesc resource collector");
190         
191         I_CmsResourceCollector collector = new CmsPriorityResourceCollector();
192         List JavaDoc resources = collector.getResults(cms, "allInFolderPriorityDateDesc", "/folder1/|" + resTypeIdPlain + "|3");
193         
194         // assert that 3 files are returned
195
assertEquals(3, resources.size());
196         
197         CmsResource res;
198         
199         // order descending determined by root path
200

201         res = (CmsResource)resources.get(0);
202         assertEquals("/sites/default/folder1/file3", res.getRootPath());
203         
204         res = (CmsResource)resources.get(1);
205         assertEquals("/sites/default/folder1/file2", res.getRootPath());
206         
207         res = (CmsResource)resources.get(2);
208         assertEquals("/sites/default/folder1/file1", res.getRootPath());
209     }
210     
211     /**
212      * Tests the "allInSubTreePriorityDesc" resource collector.<p>
213      *
214      * @throws Throwable if something goes wrong
215      */

216     public void testCollectAllInSubTreePriority() throws Throwable JavaDoc {
217         
218         CmsObject cms = getCmsObject();
219         int resTypeIdPlain = CmsResourceTypePlain.getStaticTypeId();
220         echo("Testing allInSubTreePriorityDesc resource collector");
221         
222         I_CmsResourceCollector collector = new CmsPriorityResourceCollector();
223         List JavaDoc resources = collector.getResults(cms, "allInSubTreePriorityDateDesc", "/|" + resTypeIdPlain + "|4");
224         
225         // assert that 4 files are returned
226
assertEquals(4, resources.size());
227         
228         CmsResource res;
229         
230         // order descending determined by root path
231

232         res = (CmsResource)resources.get(0);
233         assertEquals("/sites/default/folder1/sub1/file5", res.getRootPath());
234         
235         res = (CmsResource)resources.get(1);
236         assertEquals("/sites/default/file1", res.getRootPath());
237         
238         res = (CmsResource)resources.get(2);
239         assertEquals("/sites/default/folder1/file3", res.getRootPath());
240         
241         res = (CmsResource)resources.get(3);
242         assertEquals("/sites/default/folder1/file2", res.getRootPath());
243         
244         resources = collector.getResults(cms, "allInSubTreePriorityDateDesc", "/|1");
245         
246         // assert that all 6 plain files are returned
247
assertEquals(6, resources.size());
248         
249         res = (CmsResource)resources.get(0);
250         assertEquals("/sites/default/folder1/sub1/file5", res.getRootPath());
251         
252         res = (CmsResource)resources.get(5);
253         assertEquals("/sites/default/folder1/file4", res.getRootPath());
254     }
255 }
256
Popular Tags