| 1 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 ; 45 import java.util.List ; 46 47 import junit.extensions.TestSetup; 48 import junit.framework.Test; 49 import junit.framework.TestSuite; 50 51 54 public class TestPriorityResourceCollectors extends OpenCmsTestCase { 55 56 61 public TestPriorityResourceCollectors(String arg0) { 62 super(arg0); 63 } 64 65 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 104 public static void initResources(CmsObject cms) throws CmsException { 105 106 List properties = new ArrayList (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 117 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 cms.createResource("/folder1", CmsResourceTypeFolder.getStaticTypeId()); 127 128 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 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 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 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 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 cms.createResource("/folder1/sub1", CmsResourceTypeFolder.getStaticTypeId()); 170 171 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 185 public void testCollectAllInFolderPriority() throws Throwable { 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 resources = collector.getResults(cms, "allInFolderPriorityDateDesc", "/folder1/|" + resTypeIdPlain + "|3"); 193 194 assertEquals(3, resources.size()); 196 197 CmsResource res; 198 199 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 216 public void testCollectAllInSubTreePriority() throws Throwable { 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 resources = collector.getResults(cms, "allInSubTreePriorityDateDesc", "/|" + resTypeIdPlain + "|4"); 224 225 assertEquals(4, resources.size()); 227 228 CmsResource res; 229 230 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 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 |