📄 testhistory.java
字号:
// the list of historical resources contains at index 0 the
// folder with the highest version and tag ID
int version = counter + 2 - i;
String title = "title 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());
CmsProperty property = cms.readPropertyObject(
cms.readResource(importFolder),
CmsPropertyDefinition.PROPERTY_TITLE,
false);
// assert that the title and version fit together
assertEquals(title, property.getStructureValue());
}
}
/**
* Creates a file, modifies and publishes it n-times, create a sibling,
* publishes both and compares the histories.<p>
*
* @throws Throwable if something goes wrong
*/
public void testFileHistory() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing history with one sibling");
String filename = "/testFileHistory1.txt";
String siblingname = "/testFileHistory2.txt";
int counter = 2;
// create a plain text file
String contentStr = "content version " + 0;
cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 1; i <= counter; i++) {
// modify the plain text file
contentStr = "content version " + i;
CmsFile file = cms.readFile(filename);
file.setContents(contentStr.getBytes());
cms.lockResource(filename);
cms.writeFile(file);
cms.unlockResource(filename);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
}
// create a sibling
cms.copyResource(filename, siblingname, CmsResource.COPY_AS_SIBLING);
cms.unlockResource(siblingname);
OpenCms.getPublishManager().publishResource(cms, siblingname);
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 1; i <= counter; i++) {
// modify the sibling text file
contentStr = "sibling content version " + (counter + i);
CmsFile file = cms.readFile(siblingname);
file.setContents(contentStr.getBytes());
cms.lockResource(siblingname);
cms.writeFile(file);
cms.unlockResource(siblingname);
OpenCms.getPublishManager().publishResource(cms, siblingname);
OpenCms.getPublishManager().waitWhileRunning();
}
List historyResourcesForFile = cms.readAllAvailableVersions(filename);
List historyResourcesForSibling = cms.readAllAvailableVersions(siblingname);
// 1 creation
// counter modifications of original file
// 0 sibling creation, does not affects the history for the original file
// counter modifications of sibling
assertEquals(1 + counter + 0 + counter, historyResourcesForFile.size());
// 1 creation
// counter modifications of original file
// 1 sibling creation
// counter modifications of sibling
assertEquals(1 + counter + 1 + counter, historyResourcesForSibling.size());
}
/**
* creates a file, modifies and publishes it n-times, create 2 siblings,
* delete file and sibling N2, delete some versions from history, restore
* file and sibling from history and compare contents.<p>
*
* @throws Throwable if something goes wrong
*/
public void testFileHistoryFileWithSibling() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing history with several siblings");
String filename = "/testFileRoot.txt";
String siblingname = "/testFileSibling1.txt";
String siblingname2 = "/testFileSibling2.txt";
int counter = 3;
int counterSibl = 4;
int counterSibl2 = 5;
// create a plain text file
String contentStr = "content version " + 0;
cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId(), contentStr.getBytes(), null);
for (int i = 1; i <= counter; i++) {
// modify the plain text file
contentStr = "content version " + i;
CmsFile file = cms.readFile(filename);
file.setContents(contentStr.getBytes());
cms.lockResource(filename);
cms.writeFile(file);
CmsProperty property = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "RootTitle" + i, null);
cms.writePropertyObject(filename, property);
cms.unlockResource(filename);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
}
// create a sibling
cms.copyResource(filename, siblingname, CmsResource.COPY_AS_SIBLING);
cms.unlockResource(siblingname);
OpenCms.getPublishManager().publishResource(cms, siblingname);
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 1; i <= counterSibl; i++) {
// modify the sibling text file
contentStr = "sibling content version " + (counter + i);
CmsFile file = cms.readFile(siblingname);
file.setContents(contentStr.getBytes());
cms.lockResource(siblingname);
cms.writeFile(file);
CmsProperty property = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "SiblingTitle" + i, null);
cms.writePropertyObject(siblingname, property);
cms.unlockResource(siblingname);
OpenCms.getPublishManager().publishResource(cms, siblingname);
OpenCms.getPublishManager().waitWhileRunning();
}
// create a sibling2
cms.copyResource(filename, siblingname2, CmsResource.COPY_AS_SIBLING);
cms.unlockResource(siblingname2);
OpenCms.getPublishManager().publishResource(cms, siblingname2);
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 1; i <= counterSibl2; i++) {
// modify the sibling text file
contentStr = "sibling2 content version " + (counter + counterSibl2 + i);
CmsFile file = cms.readFile(siblingname2);
file.setContents(contentStr.getBytes());
cms.lockResource(siblingname2);
cms.writeFile(file);
CmsProperty property = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "Sibling2Title" + i, null);
cms.writePropertyObject(siblingname2, property);
cms.unlockResource(siblingname2);
OpenCms.getPublishManager().publishResource(cms, siblingname2);
OpenCms.getPublishManager().waitWhileRunning();
}
// now delete and publish the root
cms.lockResource(filename);
cms.deleteResource(filename, CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.unlockResource(filename);
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
// now delete and publish second sibling
cms.lockResource(siblingname2);
cms.deleteResource(siblingname2, CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.unlockResource(siblingname2);
OpenCms.getPublishManager().publishResource(cms, siblingname2);
OpenCms.getPublishManager().waitWhileRunning();
List allFiles = cms.readAllAvailableVersions(siblingname);
// 0 creation is not published
// 'counter' modifications to the original file
// '1' first sibling creation
// 'counterSibl' first sibling modifications
// '0' second sibling creation, affects just the given sibling
// 'counterSibl2' second sibling modifications
// '0' original file deletion, affects just the given sibling
// '0' second sibling deletion, affects just the given sibling
// -3 versions deleted due to history overflow, while deleting original file
assertEquals(0 + counter + 1 + counterSibl + 0 + counterSibl2 + 0 + 0 - 3, allFiles.size());
//Delete historical entries, keep only 3 latest versions.
cms.deleteHistoricalVersions("/", 3, 3, -1, new CmsShellReport(cms.getRequestContext().getLocale()));
allFiles = cms.readAllAvailableVersions(siblingname);
// 3 the number of versions that should remain for the sibling
// 3 the number of versions that should remain for the second sibling
// 2 additional remaining versions of the original file (one overlap)
assertEquals(3 + counterSibl2, allFiles.size()); // it is not 3 since there are more versions coming from the siblings!
I_CmsHistoryResource history = (I_CmsHistoryResource)allFiles.get(1);
cms.lockResource(siblingname);
//and restore it from history
cms.restoreResourceVersion(history.getStructureId(), history.getVersion());
cms.unlockResource(siblingname);
CmsFile file = cms.readFile(siblingname);
// assert that the content and version fit together
String restoredContent = getContentString(cms, file.getContents());
// the content is coming from a sibling2 modification
assertEquals("sibling2 content version 12", restoredContent);
CmsProperty prop = cms.readPropertyObject(siblingname, CmsPropertyDefinition.PROPERTY_TITLE, false);
// the property is coming from the sibling's last set title
assertEquals("SiblingTitle4", prop.getValue());
// create a new empty resource
cms.createResource(siblingname2, CmsResourceTypePlain.getStaticTypeId(), null, null);
allFiles = cms.readAllAvailableVersions(siblingname2);
assertTrue(allFiles.isEmpty());
}
/**
* Test restoring a file also possible missing folders are restored.<p>
*
* @throws Throwable if something goes wrong
*/
public void testFileRestore() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing restoring a file also possible missing folders are restored");
// initial check
List deletedResources = cms.readDeletedResources("/", true);
assertTrue(deletedResources.isEmpty());
// create a new folder and resource
CmsResource folder = cms.createResource("testFolder", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
// create a relation
cms.lockResource("index.html");
cms.addRelationToResource("index.html", "testFolder", CmsRelationType.CATEGORY.getName());
// write props
cms.writePropertyObject("testFolder", new CmsProperty(
CmsPropertyDefinition.PROPERTY_TITLE,
"strFolder",
"resFolder"));
CmsResource res = cms.createResource(
"testFolder/test.txt",
CmsResourceTypePlain.getStaticTypeId(),
"test".getBytes(),
null);
// create a relation
cms.addRelationToResource("index.html", "testFolder/test.txt", CmsRelationType.CATEGORY.getName());
// write props
cms.writePropertyObject("testFolder/test.txt", new CmsProperty(
CmsPropertyDefinition.PROPERTY_TITLE,
"strFile",
"resFile"));
storeResources(cms, "testFolder", true);
// publish
OpenCms.getPublishManager().publishResource(cms, "testFolder");
OpenCms.getPublishManager().waitWhileRunning();
// delete the folder and file
cms.lockResource("testFolder");
cms.deleteResource("testFolder/test.txt", CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.deleteResource("testFolder", CmsResource.DELETE_PRESERVE_SIBLINGS);
// publish again
OpenCms.getPublishManager().publishResource(cms, "testFolder");
OpenCms.getPublishManager().waitWhileRunning();
// be sure the files were deleted, this could fail with enabled cache
assertFalse(cms.existsResource("testFolder", CmsResourceFilter.ALL));
assertFalse(cms.existsResource("testFolder/test.txt", CmsResourceFilter.ALL));
// be sure the files were deleted
CmsProject offline = cms.get
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -