1 31 32 package org.opencms.file; 33 34 import org.opencms.file.types.CmsResourceTypeBinary; 35 import org.opencms.file.types.CmsResourceTypeFolder; 36 import org.opencms.file.types.CmsResourceTypePlain; 37 import org.opencms.lock.CmsLock; 38 import org.opencms.main.OpenCms; 39 import org.opencms.report.CmsShellReport; 40 import org.opencms.test.OpenCmsTestCase; 41 import org.opencms.test.OpenCmsTestProperties; 42 import org.opencms.test.OpenCmsTestResourceFilter; 43 import org.opencms.util.CmsResourceTranslator; 44 45 import java.util.Collections ; 46 import java.util.List ; 47 48 import junit.extensions.TestSetup; 49 import junit.framework.Test; 50 import junit.framework.TestSuite; 51 52 58 public class TestSiblings extends OpenCmsTestCase { 59 60 65 public TestSiblings(String arg0) { 66 67 super(arg0); 68 } 69 70 75 public static Test suite() { 76 OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); 77 78 TestSuite suite = new TestSuite(); 79 suite.setName(TestSiblings.class.getName()); 80 81 suite.addTest(new TestSiblings("testSiblingsCopy")); 82 suite.addTest(new TestSiblings("testSiblingsCreate")); 83 suite.addTest(new TestSiblings("testSiblingIssueAfterImport")); 84 suite.addTest(new TestSiblings("testDeleteAllSiblings")); 85 suite.addTest(new TestSiblings("testSiblingStateIssue")); 86 87 TestSetup wrapper = new TestSetup(suite) { 88 89 protected void setUp() { 90 setupOpenCms("simpletest", "/sites/default/"); 91 } 92 93 protected void tearDown() { 94 removeOpenCms(); 95 } 96 }; 97 98 return wrapper; 99 } 100 101 110 public static void copyResourceAsSibling(OpenCmsTestCase tc, CmsObject cms, String source, String target) 111 throws Throwable { 112 113 tc.storeResources(cms, source); 115 116 cms.copyResource(source, target, CmsResource.COPY_AS_SIBLING); 118 119 121 tc.assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING); 123 tc.assertProject(cms, source, cms.getRequestContext().currentProject()); 125 tc.assertSiblingCountIncremented(cms, source, 1); 127 tc.assertModifiedInCurrentProject(cms, source, false); 129 tc.assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 131 132 134 tc.assertFilter(cms, source, target, OpenCmsTestResourceFilter.FILTER_EXISTING_AND_NEW_SIBLING); 136 tc.assertState(cms, target, CmsResource.STATE_NEW); 138 tc.assertModifiedInCurrentProject(cms, target, true); 140 tc.assertLock(cms, target, CmsLock.TYPE_EXCLUSIVE); 142 } 143 144 153 public static void createSibling(OpenCmsTestCase tc, CmsObject cms, String source, String target) throws Throwable { 154 155 tc.storeResources(cms, source); 157 158 List properties = cms.readPropertyObjects(source, false); 160 cms.createSibling(source, target, properties); 161 162 164 tc.assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING); 166 tc.assertProject(cms, source, cms.getRequestContext().currentProject()); 168 tc.assertSiblingCountIncremented(cms, source, 1); 170 tc.assertModifiedInCurrentProject(cms, source, false); 172 tc.assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 174 175 177 tc.assertFilter(cms, source, target, OpenCmsTestResourceFilter.FILTER_EXISTING_AND_NEW_SIBLING); 179 tc.assertState(cms, target, CmsResource.STATE_NEW); 181 tc.assertModifiedInCurrentProject(cms, target, true); 183 tc.assertLock(cms, target, CmsLock.TYPE_EXCLUSIVE); 185 } 186 187 192 public void testSiblingsCopy() throws Throwable { 193 194 CmsObject cms = getCmsObject(); 195 String source = "/index.html"; 196 String target = "/index_sibling.html"; 197 echo("Copying " + source + " as a new sibling to " + target); 198 copyResourceAsSibling(this, cms, source, target); 199 } 200 201 206 public void testSiblingsCreate() throws Throwable { 207 208 CmsObject cms = getCmsObject(); 209 String source = "/folder1/image1.gif"; 210 String target = "/folder1/image1_sibling.gif"; 211 echo("Creating a new sibling " + target + " from " + source); 212 createSibling(this, cms, source, target); 213 } 214 215 220 public void testSiblingIssueAfterImport() throws Exception { 221 222 echo("Testing sibling issue after import"); 223 224 CmsResourceTranslator oldFolderTranslator = OpenCms.getResourceManager().getFolderTranslator(); 225 226 CmsResourceTranslator folderTranslator = new CmsResourceTranslator( 227 new String [] { 228 "s#^/sites/default/content/bodys(.*)#/system/bodies$1#", 229 "s#^/sites/default/pics/system(.*)#/system/workplace/resources$1#", 230 "s#^/sites/default/pics(.*)#/system/galleries/pics$1#", 231 "s#^/sites/default/download(.*)#/system/galleries/download$1#", 232 "s#^/sites/default/externallinks(.*)#/system/galleries/externallinks$1#", 233 "s#^/sites/default/htmlgalleries(.*)#/system/galleries/htmlgalleries$1#", 234 "s#^/sites/default/content(.*)#/system$1#" 235 }, 236 false); 237 238 OpenCms.getResourceManager().setTranslators( 240 folderTranslator, 241 OpenCms.getResourceManager().getFileTranslator()); 242 243 try { 244 245 CmsObject cms = getCmsObject(); 246 247 cms.getRequestContext().setSiteRoot("/"); 248 249 cms.createResource("/system/galleries", CmsResourceTypeFolder.RESOURCE_TYPE_ID); 251 cms.unlockResource("/system/galleries"); 252 253 cms.getRequestContext().setSiteRoot("/sites/default"); 254 255 String importFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testimport01.zip"); 257 OpenCms.getImportExportManager().importData(cms, importFile, "/", new CmsShellReport(cms.getRequestContext().getLocale())); 258 259 cms.getRequestContext().setSiteRoot("/"); 261 cms.lockResource("/sites/default"); 262 cms.lockResource("/system"); 263 264 cms.deleteResource("/sites/default/importtest", CmsResource.DELETE_REMOVE_SIBLINGS); 266 cms.deleteResource("/system/bodies", CmsResource.DELETE_REMOVE_SIBLINGS); 267 cms.deleteResource("/system/galleries/pics", CmsResource.DELETE_REMOVE_SIBLINGS); 268 269 cms.unlockResource("/sites/default"); 270 cms.unlockResource("/system"); 271 cms.publishProject(); 272 273 } finally { 274 275 OpenCms.getResourceManager().setTranslators( 277 oldFolderTranslator, 278 OpenCms.getResourceManager().getFileTranslator()); 279 280 } 281 } 282 283 286 307 308 313 public void testSiblingStateIssue() throws Throwable { 314 315 echo("Tests issue with resource state and siblings"); 316 CmsObject cms = getCmsObject(); 317 318 String resource1 = "/folder1/page1.html"; 320 String sibling1 = "/folder1/sibling1.html"; 321 322 cms.copyResource(resource1, sibling1, CmsResource.COPY_AS_SIBLING); 324 325 assertState(cms, resource1, CmsResource.STATE_UNCHANGED); 327 assertState(cms, sibling1, CmsResource.STATE_NEW); 328 329 cms.chflags(sibling1, 1024); 331 cms.chtype(sibling1, CmsResourceTypeBinary.getStaticTypeId()); 332 333 assertState(cms, resource1, CmsResource.STATE_CHANGED); 335 assertState(cms, sibling1, CmsResource.STATE_NEW); 336 337 338 String copy1 = "/folder1/copy1.html"; 340 sibling1 = "/folder1/siblingofcopy1.html"; 341 342 cms.copyResource(resource1, copy1, CmsResource.COPY_AS_NEW); 344 cms.copyResource(copy1, sibling1, CmsResource.COPY_AS_SIBLING); 345 346 assertState(cms, copy1, CmsResource.STATE_NEW); 348 assertState(cms, sibling1, CmsResource.STATE_NEW); 349 350 cms.chflags(sibling1, 1024); 352 cms.chtype(sibling1, CmsResourceTypeBinary.getStaticTypeId()); 353 354 assertState(cms, copy1, CmsResource.STATE_NEW); 356 assertState(cms, sibling1, CmsResource.STATE_NEW); 357 358 359 resource1 = "/folder1/page2.html"; 361 sibling1 = "/folder1/sibling2.html"; 362 363 cms.copyResource(resource1, sibling1, CmsResource.COPY_AS_SIBLING); 365 366 assertState(cms, resource1, CmsResource.STATE_UNCHANGED); 368 assertState(cms, sibling1, CmsResource.STATE_NEW); 369 370 cms.setDateExpired(sibling1, System.currentTimeMillis() + 1000, false); 372 cms.setDateReleased(sibling1, System.currentTimeMillis() - 1000, false); 373 374 assertState(cms, resource1, CmsResource.STATE_UNCHANGED); 376 assertState(cms, sibling1, CmsResource.STATE_NEW); 377 378 379 cms.setDateLastModified(sibling1, System.currentTimeMillis(), false); 381 382 assertState(cms, resource1, CmsResource.STATE_CHANGED); 384 assertState(cms, sibling1, CmsResource.STATE_NEW); 385 386 387 copy1 = "/folder1/copy2.html"; 389 sibling1 = "/folder1/siblingofcopy2.html"; 390 391 cms.copyResource(resource1, copy1, CmsResource.COPY_AS_NEW); 393 cms.copyResource(copy1, sibling1, CmsResource.COPY_AS_SIBLING); 394 395 assertState(cms, copy1, CmsResource.STATE_NEW); 397 assertState(cms, sibling1, CmsResource.STATE_NEW); 398 399 cms.setDateLastModified(sibling1, System.currentTimeMillis(), false); 401 402 assertState(cms, copy1, CmsResource.STATE_NEW); 404 assertState(cms, sibling1, CmsResource.STATE_NEW); 405 406 cms.setDateExpired(sibling1, System.currentTimeMillis() + 1000, false); 408 cms.setDateReleased(sibling1, System.currentTimeMillis() - 1000, false); 409 410 assertState(cms, copy1, CmsResource.STATE_NEW); 412 assertState(cms, sibling1, CmsResource.STATE_NEW); 413 } 414 415 420 public void testDeleteAllSiblings() throws Throwable { 421 422 echo("Creating a new resource with 2 siblings, then deleting it with all siblings again"); 423 CmsObject cms = getCmsObject(); 424 425 String sib1Name = "/folder1/sib1.txt"; 426 String sib2Name = "/folder1/sib2.txt"; 427 String sib3Name = "/folder1/sib3.txt"; 428 429 cms.createResource(sib1Name, CmsResourceTypePlain.getStaticTypeId()); 430 cms.createSibling(sib1Name, sib2Name, Collections.EMPTY_LIST); 431 cms.createSibling(sib1Name, sib3Name, Collections.EMPTY_LIST); 432 433 cms.deleteResource(sib1Name, CmsResource.DELETE_REMOVE_SIBLINGS); 434 435 CmsResource sib2Resource = null; 436 try { 437 sib2Resource = cms.readResource(sib2Name); 438 } catch (CmsVfsResourceNotFoundException e) { 439 } 441 442 if (sib2Resource != null) { 443 fail("Sibling " + sib2Name + " has not been deleted!"); 444 } 445 446 CmsResource sib3Resource = null; 447 try { 448 sib3Resource = cms.readResource(sib3Name); 449 } catch (CmsVfsResourceNotFoundException e) { 450 } 452 453 if (sib3Resource != null) { 454 fail("Sibling " + sib3Name + " has not been deleted!"); 455 } 456 } 457 } | Popular Tags |