📄 testhistory.java
字号:
assertHistory(cms, s1name, 3);
histRes = cms.readResource(s1.getStructureId(), 1);
assertEquals(0, histRes.getStructureVersion());
assertEquals(1, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 2);
assertEquals(0, histRes.getStructureVersion());
assertEquals(2, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag + 1, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
// 12. delete s1
cms.lockResource(s1name);
cms.deleteResource(s1name, CmsResource.DELETE_PRESERVE_SIBLINGS);
// assert version
assertEquals(3, cms.readResource(s1name, CmsResourceFilter.ALL).getVersion());
// 13. publish s1
OpenCms.getPublishManager().publishResource(cms, s1name);
OpenCms.getPublishManager().waitWhileRunning();
// 14. restore s1
cms.restoreDeletedResource(s1.getStructureId());
// check history
assertHistoryForRestored(cms, s1name, 4);
// 15. publish s1
OpenCms.getPublishManager().publishResource(cms, s1name);
OpenCms.getPublishManager().waitWhileRunning();
// check the history for s1
assertHistory(cms, s1name, 4);
histRes = cms.readResource(s1.getStructureId(), 1);
assertEquals(0, histRes.getStructureVersion());
assertEquals(1, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 2);
assertEquals(0, histRes.getStructureVersion());
assertEquals(2, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag + 1, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 3);
assertEquals(0, histRes.getStructureVersion());
assertEquals(3, histRes.getResourceVersion());
assertTrue(histRes.getState().isDeleted());
assertEquals(basePubTag + 4, histRes.getPublishTag());
assertEquals(txt2, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 4);
assertEquals(0, histRes.getStructureVersion());
assertEquals(4, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag + 5, histRes.getPublishTag());
assertEquals(txt2, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
// 16. restore s2
cms.restoreDeletedResource(s2.getStructureId());
// check history
assertHistory(cms, s1name, 5);
assertHistoryForRestored(cms, s2name, 6);
// 17. publish s2
OpenCms.getPublishManager().publishResource(cms, s2name);
OpenCms.getPublishManager().waitWhileRunning();
// check the history for s1
assertHistory(cms, s1name, 5);
histRes = cms.readResource(s1.getStructureId(), 1);
assertEquals(0, histRes.getStructureVersion());
assertEquals(1, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 2);
assertEquals(0, histRes.getStructureVersion());
assertEquals(2, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag + 1, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 3);
assertEquals(0, histRes.getStructureVersion());
assertEquals(3, histRes.getResourceVersion());
assertTrue(histRes.getState().isDeleted());
assertEquals(basePubTag + 4, histRes.getPublishTag());
assertEquals(txt2, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 4);
assertEquals(0, histRes.getStructureVersion());
assertEquals(4, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag + 5, histRes.getPublishTag());
assertEquals(txt2, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s1.getStructureId(), 5);
assertEquals(0, histRes.getStructureVersion());
assertEquals(5, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag + 6, histRes.getPublishTag());
// we deleted s2 before publishing the contents, so the restored version has the old content
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
// check the history for s2
assertHistory(cms, s2name, 6);
histRes = cms.readResource(s1.getStructureId(), 1);
assertEquals(0, histRes.getStructureVersion());
assertEquals(1, histRes.getResourceVersion());
assertEquals(basePubTag, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s2.getStructureId(), 2);
assertEquals(0, histRes.getStructureVersion());
assertEquals(2, histRes.getResourceVersion());
assertEquals(basePubTag + 1, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s2.getStructureId(), 3);
assertEquals(1, histRes.getStructureVersion());
assertEquals(2, histRes.getResourceVersion());
assertEquals(basePubTag + 3, histRes.getPublishTag());
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s2.getStructureId(), 4);
assertEquals(1, histRes.getStructureVersion());
assertEquals(3, histRes.getResourceVersion());
assertTrue(histRes.getState().isDeleted());
assertEquals(basePubTag + 4, histRes.getPublishTag());
assertEquals(txt2, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s2.getStructureId(), 5);
assertEquals(1, histRes.getStructureVersion());
assertEquals(4, histRes.getResourceVersion());
assertTrue(histRes.getState().isDeleted());
assertEquals(basePubTag + 5, histRes.getPublishTag());
assertEquals(txt2, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
histRes = cms.readResource(s2.getStructureId(), 6);
assertEquals(1, histRes.getStructureVersion());
assertEquals(5, histRes.getResourceVersion());
assertFalse(histRes.getState().isDeleted());
assertEquals(basePubTag + 6, histRes.getPublishTag());
// we deleted s2 before publishing the contents, so the restored version has the old content
assertEquals(txt1, new String(cms.readFile((CmsHistoryFile)histRes).getContents()));
}
/**
* Creates and deletes a file n-times and tests if the historical data
* are correct and if the content can be properly restored.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCreateAndDeleteFile() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing history creating, modifying and deleting a file");
String filename = "/dummy1.txt";
int counter = 2;
// create a plain text file
CmsResource res = cms.createResource(
filename,
CmsResourceTypePlain.getStaticTypeId(),
"content version 0".getBytes(),
null);
cms.unlockResource(filename);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 1; i <= counter; i++) {
// create a plain text file
String contentStr = "content version " + i;
CmsFile file = cms.readFile(filename);
file.setContents(contentStr.getBytes());
cms.lockResource(filename);
cms.writeFile(file);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
}
CmsFile file = cms.readFile(filename);
file.setContents(("content version " + (counter + 1)).getBytes());
cms.lockResource(filename);
cms.writeFile(file);
// delete the file
cms.deleteResource(filename, CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.unlockResource(filename);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId(), null, null);
// the history works only by ID so the expected number of versions is ZERO
List historyResources = cms.readAllAvailableVersions(filename);
assertTrue(historyResources.isEmpty());
// re-create the resource to be able to read all versions from the history
// assert that we have the expected number of versions in the history
String importFile = "import.txt";
cms.importResource(importFile, res, "blah-blah".getBytes(), null);
historyResources = cms.readAllAvailableVersions(importFile);
assertEquals(counter + 2, historyResources.size()); // counter + created + deleted
for (int i = 0; i < counter + 2; i++) {
// the list of historical resources contains at index 0 the
// resource with the highest version and tag ID
int version = counter + 2 - i;
String contentStr = "content version " + (version - 1);
// assert that the historical resource has the correct version
I_CmsHistoryResource historyResource = (I_CmsHistoryResource)historyResources.get(i);
assertEquals(version, historyResource.getVersion());
cms.restoreResourceVersion(historyResource.getStructureId(), historyResource.getVersion());
file = cms.readFile(importFile);
// assert that the content and version fit together
String restoredContent = getContentString(cms, file.getContents());
assertEquals(contentStr, restoredContent);
}
}
/**
* Creates and deletes a folder n-times and tests if the historical data
* are correct and if the properties can be properly restored.<p>
*
* @throws Throwable if something goes wrong
*/
public void testCreateAndDeleteFolder() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing history creating, modifying and deleting a folder");
String folderName = "/dummy1/";
int counter = 2;
// create a folder
CmsResource res = cms.createResource(folderName, CmsResourceTypeFolder.getStaticTypeId(), null, null);
cms.writePropertyObject(folderName, new CmsProperty(
CmsPropertyDefinition.PROPERTY_TITLE,
"title version 0",
null));
cms.unlockResource(folderName);
OpenCms.getPublishManager().publishResource(cms, folderName);
OpenCms.getPublishManager().waitWhileRunning();
// check
assertHistory(cms, folderName, 1);
for (int i = 1; i <= counter; i++) {
cms.lockResource(folderName);
cms.writePropertyObject(folderName, new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "title version "
+ i, null));
// check
assertHistory(cms, folderName, 1 + i);
OpenCms.getPublishManager().publishResource(cms, folderName);
OpenCms.getPublishManager().waitWhileRunning();
// check
assertHistory(cms, folderName, 1 + i);
}
cms.lockResource(folderName);
cms.writePropertyObject(folderName, new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "title version "
+ (counter + 1), null));
// delete the folder
cms.deleteResource(folderName, CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.unlockResource(folderName);
OpenCms.getPublishManager().publishResource(cms, folderName);
OpenCms.getPublishManager().waitWhileRunning();
// the history works only by ID so the expected number of versions is ZERO
cms.createResource(folderName, CmsResourceTypeFolder.getStaticTypeId(), null, null);
List historyResources = cms.readAllAvailableVersions(folderName);
assertTrue(historyResources.isEmpty());
// re-create the folder to be able to read all versions from the history
// assert that we have the expected number of versions in the history
String importFolder = "import/";
cms.importResource(importFolder, res, null, null);
historyResources = cms.readAllAvailableVersions(importFolder);
assertEquals(counter + 2, historyResources.size()); // counter + created + deleted
for (int i = 0; i < counter + 2; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -