1 31 32 package org.opencms.file; 33 34 import org.opencms.file.types.CmsResourceTypePlain; 35 import org.opencms.test.OpenCmsTestCase; 36 import org.opencms.test.OpenCmsTestProperties; 37 38 import java.util.ArrayList ; 39 import java.util.List ; 40 41 import junit.extensions.TestSetup; 42 import junit.framework.Test; 43 import junit.framework.TestSuite; 44 45 51 public class TestRestoreFromHistory extends OpenCmsTestCase { 52 53 58 public TestRestoreFromHistory(String arg0) { 59 super(arg0); 60 } 61 62 67 public static Test suite() { 68 OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); 69 70 TestSuite suite = new TestSuite(); 71 suite.setName(TestRestoreFromHistory.class.getName()); 72 73 suite.addTest(new TestRestoreFromHistory("testRestoreResource")); 74 suite.addTest(new TestRestoreFromHistory("testRestoreDeletedResource")); 75 suite.addTest(new TestRestoreFromHistory("testHistoryOverflow")); 76 77 TestSetup wrapper = new TestSetup(suite) { 78 79 protected void setUp() { 80 setupOpenCms("simpletest", "/sites/default/"); 81 } 82 83 protected void tearDown() { 84 removeOpenCms(); 85 } 86 }; 87 88 return wrapper; 89 } 90 91 96 public void testHistoryOverflow() throws Throwable { 97 98 final int C_MAX_VERSIONS = 10; 99 100 CmsObject cms = getCmsObject(); 101 echo("Testing history overflow"); 102 103 String resourcename ="/test-overflow1.txt"; 104 String contentStr = "1"; 105 106 cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null); 107 this.storeResources(cms, resourcename); 108 109 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 111 cms.publishProject(); 112 113 int version; 114 for (version = 1; version < 20; version++) { 115 116 cms.lockResource(resourcename); 117 118 List allFiles = cms.readAllBackupFileHeaders(resourcename); 120 if (version <= C_MAX_VERSIONS) { 121 if (allFiles.size() != version) { 122 fail("Number of backup files found = " + allFiles.size() + " != " + version + " expected"); 123 } 124 } else { 125 if (allFiles.size() != C_MAX_VERSIONS) { 126 fail("Number of backup files found = " + allFiles.size() + " != " + C_MAX_VERSIONS + " expected"); 127 } 128 } 129 130 if (version > 1) { 132 CmsBackupResource backup = (CmsBackupResource)allFiles.get(1); 133 cms.restoreResourceBackup(resourcename, backup.getTagId()); 134 135 assertContent(cms, resourcename, Integer.toString(version-1).getBytes()); 137 } 138 139 contentStr = Integer.toString(version+1); 141 CmsFile update = cms.readFile(resourcename); 142 update.setContents(contentStr.getBytes()); 143 cms.writeFile(update); 144 this.storeResources(cms, resourcename); 145 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 146 cms.publishProject(); 147 } 148 } 149 150 155 public void testRestoreResource() throws Throwable { 156 157 CmsObject cms = getCmsObject(); 158 echo("Testing restore resource"); 159 160 String resourcename = "/test-restore1.txt"; 161 162 String contentStr1 = "Hello this is content version 1"; 163 String contentStr2 = "Hello this is content version 2"; 164 165 166 CmsProperty sProp1 = new CmsProperty("StructureProp", "Structure property value version 1", null, true); 167 CmsProperty rProp1 = new CmsProperty("ResourceProp", null, "Resource property value version 1", true); 168 List props1 = new ArrayList (); 169 props1.add(sProp1); 170 props1.add(rProp1); 171 172 CmsProperty sProp2 = new CmsProperty("StructureProp", "Structure property value version 2", null, true); 173 CmsProperty rProp2 = new CmsProperty("ResourceProp", null, "Resource property value version 2", true); 174 List props2 = new ArrayList (); 175 props2.add(sProp2); 176 props2.add(rProp2); 177 178 cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr1.getBytes(), null); 180 this.storeResources(cms, resourcename); 181 182 cms.writePropertyObject(resourcename, sProp1); 184 cms.writePropertyObject(resourcename, rProp1); 185 186 assertContent(cms, resourcename, contentStr1.getBytes()); 188 assertPropertyNew(cms, resourcename, props1); 189 190 List allFiles = cms.readAllBackupFileHeaders(resourcename); 192 if (!allFiles.isEmpty()) { 193 fail("Unexpected backup files for new created resource found."); 194 } 195 196 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 198 cms.publishProject(); 199 200 allFiles = cms.readAllBackupFileHeaders(resourcename); 202 if (allFiles.size() != 1) { 203 fail("Unexpected number of backup files for published resource found (one expected)"); 204 } 205 206 this.storeResources(cms, resourcename); 208 209 cms.lockResource(resourcename); 211 CmsFile update = cms.readFile(resourcename); 212 update.setContents(contentStr2.getBytes()); 213 cms.writeFile(update); 214 215 cms.writePropertyObject(resourcename, sProp2); 217 cms.writePropertyObject(resourcename, rProp2); 218 219 assertContent(cms, resourcename, contentStr2.getBytes()); 221 222 assertPropertyChanged(cms, resourcename, props2); 224 225 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 227 cms.publishProject(); 228 229 allFiles = cms.readAllBackupFileHeaders(resourcename); 231 if (allFiles.size() != 2) { 232 fail("Unexpected number of backup files for published resource found (two expected)"); 233 } 234 235 CmsBackupResource backup = (CmsBackupResource)allFiles.get(1); 237 238 this.storeResources(cms, resourcename); 240 241 cms.lockResource(resourcename); 243 cms.restoreResourceBackup(resourcename, backup.getTagId()); 244 245 assertContent(cms, resourcename, contentStr1.getBytes()); 247 248 assertPropertyChanged(cms, resourcename, props1); 250 } 251 252 259 public void testRestoreDeletedResource() throws Throwable { 260 261 CmsObject cms = getCmsObject(); 262 echo("Testing restoring deleted resources"); 263 264 String resourcename = "/test-restore3.txt"; 265 266 String contentStr = "Hello this is the content"; 267 268 cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null); 270 271 assertContent(cms, resourcename, contentStr.getBytes()); 273 274 List allFiles = cms.readAllBackupFileHeaders(resourcename); 276 if (!allFiles.isEmpty()) { 277 fail("Unexpected backup files for new created resource found."); 278 } 279 280 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 282 cms.publishProject(); 283 284 allFiles = cms.readAllBackupFileHeaders(resourcename); 286 if (allFiles.size() != 1) { 287 fail("Unexpected number of backup files for published resource found (one expected)"); 288 } 289 290 cms.lockResource(resourcename); 292 cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS); 293 cms.unlockProject(cms.getRequestContext().currentProject().getId()); 294 cms.publishProject(); 295 296 cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), null, null); 298 299 allFiles = cms.readAllBackupFileHeaders(resourcename); 301 if (allFiles.size() != 1) { 302 fail("Unexpected number of backup files for published resource found (one expected)"); 303 } 304 305 CmsBackupResource backup = (CmsBackupResource)allFiles.get(0); 307 308 cms.restoreResourceBackup(resourcename, backup.getTagId()); 310 311 assertContent(cms, resourcename, contentStr.getBytes()); 313 } 314 } 315 | Popular Tags |