1 31 32 package org.opencms.file; 33 34 import org.opencms.file.types.CmsResourceTypePlain; 35 import org.opencms.lock.CmsLock; 36 import org.opencms.test.OpenCmsTestCase; 37 import org.opencms.test.OpenCmsTestProperties; 38 import org.opencms.test.OpenCmsTestResourceFilter; 39 import org.opencms.test.OpenCmsTestResourceStorage; 40 41 import java.util.Iterator ; 42 import java.util.List ; 43 44 import junit.extensions.TestSetup; 45 import junit.framework.Test; 46 import junit.framework.TestSuite; 47 48 54 public class TestUndoChanges extends OpenCmsTestCase { 55 56 61 public TestUndoChanges(String arg0) { 62 63 super(arg0); 64 } 65 66 71 public static Test suite() { 72 73 OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); 74 75 TestSuite suite = new TestSuite(); 76 suite.setName(TestUndoChanges.class.getName()); 77 78 suite.addTest(new TestUndoChanges("testUndoChangesResource")); 79 suite.addTest(new TestUndoChanges("testUndoChangesOnNewResource")); 80 suite.addTest(new TestUndoChanges("testUndoChangesFolder")); 81 suite.addTest(new TestUndoChanges("testUndoChangesFolderRecursive")); 82 suite.addTest(new TestUndoChanges("testUndoChangesAfterCopyNewOverDeleted")); 83 suite.addTest(new TestUndoChanges("testUndoChangesAfterCopySiblingOverDeleted")); 84 suite.addTest(new TestUndoChanges("testUndoChangesWithAce")); 85 suite.addTest(new TestUndoChanges("testUndoChangesSharedProperty")); 86 87 TestSetup wrapper = new TestSetup(suite) { 88 89 protected void setUp() { 90 91 setupOpenCms("simpletest", "/sites/default/"); 92 } 93 94 protected void tearDown() { 95 96 removeOpenCms(); 97 } 98 }; 99 100 return wrapper; 101 } 102 103 108 public void testUndoChangesOnNewResource() throws Throwable { 109 110 CmsObject cms = getCmsObject(); 111 echo("Testing for exception when trying undo changes on a new resource"); 112 113 String source = "/types/new.html"; 114 115 cms.createResource(source, CmsResourceTypePlain.getStaticTypeId()); 117 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 118 119 try { 120 cms.undoChanges(source, false); 121 } catch (CmsVfsException e) { 122 if (e.getMessageContainer().getKey().equals(org.opencms.db.Messages.ERR_UNDO_CHANGES_FOR_RESOURCE_1)) { 123 return; 125 } 126 } 127 fail("Did not catch expected exception trying undo changes on a new resource!"); 128 } 129 130 136 public void testUndoChangesAfterCopyNewOverDeleted() throws Throwable { 137 138 CmsObject cms = getCmsObject(); 139 echo("Testing undo changes after overwriting a deleted file with a new file"); 140 141 String source = "/folder1/page2.html"; 142 String destination = "/folder1/page1.html"; 143 144 storeResources(cms, source); 145 storeResources(cms, destination); 146 147 cms.lockResource(destination); 148 149 cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS); 151 assertState(cms, destination, CmsResource.STATE_DELETED); 152 153 cms.copyResource(source, destination, CmsResource.COPY_AS_NEW); 154 155 cms.undoChanges(destination, false); 157 158 assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL); 160 assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 161 162 cms.publishProject(); 164 } 165 166 172 public void testUndoChangesAfterCopySiblingOverDeleted() throws Throwable { 173 174 CmsObject cms = getCmsObject(); 175 echo("Testing undo changes after overwriting a deleted file with a sibling"); 176 177 String source = "/folder1/page2.html"; 178 String destination = "/folder1/page1.html"; 179 180 storeResources(cms, source); 181 storeResources(cms, destination); 182 183 cms.lockResource(destination); 184 185 cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS); 187 assertState(cms, destination, CmsResource.STATE_DELETED); 188 189 cms.copyResource(source, destination, CmsResource.COPY_AS_SIBLING); 190 191 cms.undoChanges(destination, false); 193 194 assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 196 assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 197 198 cms.publishProject(); 200 } 201 202 209 public static void undoChanges(OpenCmsTestCase tc, CmsObject cms, String resource1) throws Throwable { 210 211 tc.createStorage("undoChanges"); 213 tc.switchStorage("undoChanges"); 214 tc.storeResources(cms, resource1); 215 tc.switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE); 216 217 TestTouch.touchResource(tc, cms, resource1); 219 220 CmsProperty property1 = new CmsProperty("Title", "undoChanges", null); 222 TestProperty.writeProperty(tc, cms, resource1, property1); 223 224 cms.lockResource(resource1); 226 cms.undoChanges(resource1, false); 227 cms.unlockResource(resource1); 228 229 tc.switchStorage("undoChanges"); 230 231 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 233 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 235 } 236 237 244 public static void undoChangesFolder(OpenCmsTestCase tc, CmsObject cms, String resource1) throws Throwable { 245 246 tc.createStorage("undoChanges"); 248 tc.switchStorage("undoChanges"); 249 tc.storeResources(cms, resource1); 250 tc.switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE); 251 252 long timestamp = System.currentTimeMillis(); 253 254 CmsProperty property1 = new CmsProperty("Title", "undoChanges", null); 256 TestProperty.writeProperty(tc, cms, resource1, property1); 257 258 List subresources = cms.readResources(resource1, CmsResourceFilter.ALL); 260 Iterator i = subresources.iterator(); 261 while (i.hasNext()) { 262 CmsResource res = (CmsResource)i.next(); 263 String resName = cms.getSitePath(res); 264 TestProperty.writeProperty(tc, cms, resName, property1); 265 } 266 267 cms.lockResource(resource1); 269 cms.undoChanges(resource1, false); 270 cms.unlockResource(resource1); 271 272 tc.switchStorage("undoChanges"); 273 274 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 276 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 278 279 Iterator j = subresources.iterator(); 281 while (j.hasNext()) { 282 CmsResource res = (CmsResource)j.next(); 283 String resName = cms.getSitePath(res); 284 tc.assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_WRITEPROPERTY); 285 tc.assertProject(cms, resName, cms.getRequestContext().currentProject()); 287 tc.assertState(cms, resName, tc.getPreCalculatedState(resource1)); 289 tc.assertDateLastModifiedAfter(cms, resName, timestamp); 291 tc.assertUserLastModified(cms, resName, cms.getRequestContext().currentUser()); 293 tc.assertPropertyChanged(cms, resName, property1); 295 } 296 } 297 298 305 public static void undoChangesFolderRecursive(OpenCmsTestCase tc, CmsObject cms, String resource1) throws Throwable { 306 307 tc.createStorage("undoChanges"); 309 tc.switchStorage("undoChanges"); 310 tc.storeResources(cms, resource1); 311 tc.switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE); 312 313 CmsProperty property1 = new CmsProperty("Title", "undoChanges", null); 315 TestProperty.writeProperty(tc, cms, resource1, property1); 316 317 List subresources = cms.readResources(resource1, CmsResourceFilter.ALL); 319 Iterator i = subresources.iterator(); 320 while (i.hasNext()) { 321 CmsResource res = (CmsResource)i.next(); 322 String resName = cms.getSitePath(res); 323 TestProperty.writeProperty(tc, cms, resName, property1); 324 } 325 326 cms.lockResource(resource1); 328 cms.undoChanges(resource1, true); 329 cms.unlockResource(resource1); 330 331 tc.switchStorage(OpenCmsTestResourceStorage.GLOBAL_STORAGE); 332 333 tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 335 tc.assertProject(cms, resource1, cms.getRequestContext().currentProject()); 337 338 Iterator j = subresources.iterator(); 340 while (j.hasNext()) { 341 CmsResource res = (CmsResource)j.next(); 342 String resName = cms.getSitePath(res); 343 344 tc.assertFilter(cms, resName, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 346 tc.assertProject(cms, resName, cms.getRequestContext().currentProject()); 348 } 349 } 350 351 356 public void testUndoChangesResource() throws Throwable { 357 358 CmsObject cms = getCmsObject(); 359 360 createStorage(OpenCmsTestResourceStorage.GLOBAL_STORAGE); 363 switchStorage(OpenCmsTestResourceStorage.GLOBAL_STORAGE); 364 storeResources(cms, "/"); 365 switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE); 366 367 echo("Testing undoChanges on a file"); 368 undoChanges(this, cms, "/index.html"); 369 } 370 371 376 public void testUndoChangesFolder() throws Throwable { 377 378 CmsObject cms = getCmsObject(); 379 echo("Testing undoChanges on a folder without recursion"); 380 undoChangesFolder(this, cms, "/folder2/"); 381 } 382 383 388 public void testUndoChangesFolderRecursive() throws Throwable { 389 390 CmsObject cms = getCmsObject(); 391 echo("Testing undoChanges on a folder _with_ recursion"); 392 undoChangesFolderRecursive(this, cms, "/folder1/"); 393 } 394 395 400 public void testUndoChangesWithAce() throws Throwable { 401 402 CmsObject cms = getCmsObject(); 403 echo("Testing undoChanges on a resource with an ACE"); 404 undoChanges(this, cms, "/folder2/index.html"); 405 } 406 407 412 public void testUndoChangesSharedProperty() throws Throwable { 413 414 CmsObject cms = getCmsObject(); 415 echo("Testing undoChanges on shared property"); 416 417 String file = "/a"; 419 cms.createResource(file, CmsResourceTypePlain.getStaticTypeId()); 420 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 422 cms.publishProject(); 423 424 String sibling = "/b"; 425 TestSiblings.createSibling(this, cms, file, sibling); 426 CmsProperty property = new CmsProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, "undoChanges navText", null); 428 cms.writePropertyObject(sibling, property); 429 CmsProperty property1 = new CmsProperty(CmsPropertyDefinition.PROPERTY_DESCRIPTION, null, "undoChanges description"); 431 cms.writePropertyObject(sibling, property1); 432 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 434 cms.publishProject(); 435 436 createStorage("undoChanges"); 438 switchStorage("undoChanges"); 439 storeResources(cms, file); 440 storeResources(cms, sibling); 441 switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE); 442 443 CmsProperty property2 = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, null, "undoChanges title"); 445 cms.lockResource(file); 446 cms.writePropertyObject(file, property2); 447 cms.unlockResource(file); 448 450 cms.lockResource(file); 452 cms.undoChanges(file, false); 453 cms.unlockResource(file); 454 455 switchStorage("undoChanges"); 456 457 assertFilter(cms, file, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 459 assertProject(cms, file, cms.getRequestContext().currentProject()); 461 } 462 463 } 464 | Popular Tags |