📄 testrestorefromhistory.java
字号:
CmsResource resource = cms.readResource(resourcename);
// now delete and publish the resource
cms.lockResource(resourcename);
cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// create a new empty resource, this is no longer supported
cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), "".getBytes(), null);
allFiles = cms.readAllAvailableVersions(resourcename);
assertEquals("Unexpected number of historical files for published resource found (zero expected)", 0, allFiles.size());
cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
// check that there is one historical file available, again
cms.importResource(resourcename, resource, "test".getBytes(), null);
allFiles = cms.readAllAvailableVersions(resourcename);
assertEquals("Unexpected number of historical files for published resource found (two expected)", 2, allFiles.size());
// read the tag id
I_CmsHistoryResource history = (I_CmsHistoryResource)allFiles.get(0);
// and restore it from history
cms.restoreResourceVersion(history.getStructureId(), history.getVersion());
// check the content
assertContent(cms, resourcename, contentStr.getBytes());
}
/**
* Tests the re-creation of moved resources.<p>
*
* @throws Throwable if something goes wrong
*/
public void testRestoreMovedResource() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing restoring moved resources");
String resourcename = "/test-restore4.txt";
String newresname = "/test-restore4moved.txt";
String contentStr = "Hello this is the content";
// create the resource with content
cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null);
// check the content
assertContent(cms, resourcename, contentStr.getBytes());
// check that there are no historical versions available
List allFiles = cms.readAllAvailableVersions(resourcename);
if (!allFiles.isEmpty()) {
fail("Unexpected historical files for new created resource found.");
}
// publish the project
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// check that there is exactly one historical file available
allFiles = cms.readAllAvailableVersions(resourcename);
if (allFiles.size() != 1) {
fail("Unexpected number of historical files for published resource found (one expected)");
}
// now move and publish the resource
cms.lockResource(resourcename);
cms.moveResource(resourcename, newresname);
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// check that there is one historical file available, again
allFiles = cms.readAllAvailableVersions(newresname);
if (allFiles.size() != 2) {
fail("Unexpected number of historical files for published resource found (two expected)");
}
// read the tag id
I_CmsHistoryResource history = (I_CmsHistoryResource)allFiles.get(0);
// and restore it from history
cms.lockResource(newresname);
cms.restoreResourceVersion(history.getStructureId(), history.getVersion());
// check the content
assertContent(cms, newresname, contentStr.getBytes());
}
/**
* Test the restore resource method.<p>
*
* @throws Throwable if something goes wrong
*/
public void testRestoreResource() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing restore resource");
String resourcename = "/test-restore1.txt";
String contentStr1 = "Hello this is content version 1";
String contentStr2 = "Hello this is content version 2";
CmsProperty sProp1 = new CmsProperty("StructureProp", "Structure property value version 1", null, true);
CmsProperty rProp1 = new CmsProperty("ResourceProp", null, "Resource property value version 1", true);
List props1 = new ArrayList();
props1.add(sProp1);
props1.add(rProp1);
CmsProperty sProp2 = new CmsProperty("StructureProp", "Structure property value version 2", null, true);
CmsProperty rProp2 = new CmsProperty("ResourceProp", null, "Resource property value version 2", true);
List props2 = new ArrayList();
props2.add(sProp2);
props2.add(rProp2);
// create the resource with content version 1
cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), contentStr1.getBytes(), null);
this.storeResources(cms, resourcename);
// set the properties
cms.writePropertyObject(resourcename, sProp1);
cms.writePropertyObject(resourcename, rProp1);
// check the content
assertContent(cms, resourcename, contentStr1.getBytes());
assertPropertyNew(cms, resourcename, props1);
// check that there are no historical versions available
List allFiles = cms.readAllAvailableVersions(resourcename);
if (!allFiles.isEmpty()) {
fail("Unexpected historical files for new created resource found.");
}
// publish the project
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// check that there is exactly one historical file available
allFiles = cms.readAllAvailableVersions(resourcename);
if (allFiles.size() != 1) {
fail("Unexpected number of historical files for published resource found (one expected)");
}
// store current resource contents
this.storeResources(cms, resourcename);
// change to content of the file to version 2 and publish it again
cms.lockResource(resourcename);
CmsFile update = cms.readFile(resourcename);
update.setContents(contentStr2.getBytes());
cms.writeFile(update);
// change the properties
cms.writePropertyObject(resourcename, sProp2);
cms.writePropertyObject(resourcename, rProp2);
// check the content - must be version 2
assertContent(cms, resourcename, contentStr2.getBytes());
// check the properties - must be version 2
assertPropertyChanged(cms, resourcename, props2);
// publish the project
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// check that there are exactly two historical files available
allFiles = cms.readAllAvailableVersions(resourcename);
if (allFiles.size() != 2) {
fail("Unexpected number of historical files for published resource found (two expected)");
}
// read the tag id
I_CmsHistoryResource history = (I_CmsHistoryResource)allFiles.get(1);
// store current resource contents
this.storeResources(cms, resourcename);
// now restore the first version
cms.lockResource(resourcename);
cms.restoreResourceVersion(history.getStructureId(), history.getVersion());
// check the content - must be version 1
assertContent(cms, resourcename, contentStr1.getBytes());
// check the properties - must be version 1
assertPropertyChanged(cms, resourcename, props1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -