📄 testcmsimportexport.java
字号:
// move the folder
cms.lockResource(foldername);
cms.moveResource(foldername, folder2);
// publish the file
cms.unlockResource(folder2);
OpenCms.getPublishManager().publishResource(cms, folder2);
OpenCms.getPublishManager().waitWhileRunning();
assertFalse(cms.existsResource(foldername));
assertFalse(cms.existsResource(foldername + filename));
assertTrue(cms.existsResource(folder2));
assertTrue(cms.existsResource(folder2 + filename));
cms.readResource(folderBefore.getStructureId()); // check resource by id
try {
cms.readResource(foldername); // check resource by name
fail("should not be found");
} catch (Exception e) {
// ok
}
// re-import the exported files
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// publish the file
cms.unlockResource(foldername);
OpenCms.getPublishManager().publishResource(cms, foldername);
OpenCms.getPublishManager().waitWhileRunning();
assertFalse(cms.existsResource(folder2));
assertFalse(cms.existsResource(folder2 + filename));
assertFilter(cms, foldername, OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT);
assertFilter(cms, foldername + filename, OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT);
// check locks
List lockedResources = cms.getLockedResources("/", CmsLockFilter.FILTER_ALL);
assertFalse(lockedResources.contains(foldername));
assertFalse(lockedResources.contains(folder2));
// check content
String offlineContent = new String(cms.readFile(foldername + filename).getContents());
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
assertEquals(offlineContent, new String(cms.readFile(foldername + filename).getContents()));
} finally {
try {
if (zipExportFilename != null) {
File file = new File(zipExportFilename);
if (file.exists()) {
file.delete();
}
}
} catch (Throwable t) {
// intentionally left blank
}
}
}
/**
* Tests the import of a folder that has been moved without publishing.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportMovedFolder2() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing the import of a folder that has been moved without publishing.");
String filename = "newtestfile2.html";
String foldername = "/folderToMove2/";
String folder2 = "/movedFolder2/";
String zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
"packages/testImportExportMovedFolder.zip");
try {
cms.getRequestContext().setSiteRoot("/");
// create test file & folder
CmsResource folderBefore = cms.createResource(foldername, CmsResourceTypeFolder.getStaticTypeId());
cms.createResource(foldername + filename, CmsResourceTypePlain.getStaticTypeId());
// publish the folder
cms.unlockResource(foldername);
OpenCms.getPublishManager().publishResource(cms, foldername);
OpenCms.getPublishManager().waitWhileRunning();
storeResources(cms, foldername, true);
// export the folder
CmsVfsImportExportHandler vfsExportHandler = new CmsVfsImportExportHandler();
vfsExportHandler.setFileName(zipExportFilename);
List exportPaths = new ArrayList(1);
exportPaths.add(foldername);
vfsExportHandler.setExportPaths(exportPaths);
vfsExportHandler.setIncludeSystem(false);
vfsExportHandler.setIncludeUnchanged(true);
vfsExportHandler.setExportUserdata(false);
OpenCms.getImportExportManager().exportData(
cms,
vfsExportHandler,
new CmsShellReport(cms.getRequestContext().getLocale()));
// move the folder
cms.lockResource(foldername);
cms.moveResource(foldername, folder2);
assertFalse(cms.existsResource(foldername));
assertFalse(cms.existsResource(foldername + filename));
assertTrue(cms.existsResource(folder2));
assertTrue(cms.existsResource(folder2 + filename));
cms.readResource(folderBefore.getStructureId()); // check resource by id
try {
cms.readResource(foldername); // check resource by name
fail("should not be found");
} catch (Exception e) {
// ok
}
// re-import the exported files
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// publish the file
cms.unlockResource(foldername);
OpenCms.getPublishManager().publishResource(cms, foldername);
OpenCms.getPublishManager().waitWhileRunning();
assertFalse(cms.existsResource(folder2));
assertFalse(cms.existsResource(folder2 + filename));
assertFilter(cms, foldername, OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT);
assertFilter(cms, foldername + filename, OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT);
// check locks
List lockedResources = cms.getLockedResources("/", CmsLockFilter.FILTER_ALL);
assertFalse(lockedResources.contains(foldername));
assertFalse(lockedResources.contains(folder2));
// check content
String offlineContent = new String(cms.readFile(foldername + filename).getContents());
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
assertEquals(offlineContent, new String(cms.readFile(foldername + filename).getContents()));
} finally {
try {
if (zipExportFilename != null) {
File file = new File(zipExportFilename);
if (file.exists()) {
file.delete();
}
}
} catch (Throwable t) {
// intentionally left blank
}
}
}
/**
* Tests the import of a resource that has been moved.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportMovedResource() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing the import of a resource that has been moved.");
String filename1 = "/newfile4.html";
String filename2 = "/movedfile.html";
String zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
"packages/testImportExportMovedResource.zip");
try {
cms.getRequestContext().setSiteRoot("/");
// create file
CmsResource resBefore = cms.createResource(filename1, CmsResourceTypePlain.getStaticTypeId());
// publish the file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
storeResources(cms, filename1);
// export the file
CmsVfsImportExportHandler vfsExportHandler = new CmsVfsImportExportHandler();
vfsExportHandler.setFileName(zipExportFilename);
List exportPaths = new ArrayList(1);
exportPaths.add(filename1);
vfsExportHandler.setExportPaths(exportPaths);
vfsExportHandler.setIncludeSystem(false);
vfsExportHandler.setIncludeUnchanged(true);
vfsExportHandler.setExportUserdata(false);
OpenCms.getImportExportManager().exportData(
cms,
vfsExportHandler,
new CmsShellReport(cms.getRequestContext().getLocale()));
// move the file
cms.lockResource(filename1);
cms.moveResource(filename1, filename2);
// publish the file
cms.unlockResource(filename2);
OpenCms.getPublishManager().publishResource(cms, filename2);
OpenCms.getPublishManager().waitWhileRunning();
assertFalse(cms.existsResource(filename1));
assertTrue(cms.existsResource(filename2));
cms.readResource(resBefore.getStructureId()); // check resource by id
try {
cms.readResource(filename1); // check resource by name
fail("should not be found");
} catch (Exception e) {
// ok
}
// re-import the exported files
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// publish the file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
assertFalse(cms.existsResource(filename2));
assertFilter(cms, cms.readResource(filename1), OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT);
} finally {
try {
if (zipExportFilename != null) {
File file = new File(zipExportFilename);
if (file.exists()) {
file.delete();
}
}
} catch (Throwable t) {
// intentionally left blank
}
}
}
/**
* Tests the import of a resource that has been recreated.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportRecreatedFile() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing the import of a resource that has been recreated.");
String filename1 = "/newfile5.html";
String zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
"packages/testImportRecreatedFile.zip");
try {
cms.getRequestContext().setSiteRoot("/");
// create file
CmsResource resBefore = cms.createResource(filename1, CmsResourceTypePlain.getStaticTypeId());
// publish the file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
// save the file state for later comparison
storeResources(cms, filename1);
// export the file
CmsVfsImportExportHandler vfsExportHandler = new CmsVfsImportExportHandler();
vfsExportHandler.setFileName(zipExportFilename);
List exportPaths = new ArrayList(1);
exportPaths.add(filename1);
vfsExportHandler.setExportPaths(exportPaths);
vfsExportHandler.setIncludeSystem(false);
vfsExportHandler.setIncludeUnchanged(true);
vfsExportHandler.setExportUserdata(false);
OpenCms.getImportExportManager().exportData(
cms,
vfsExportHandler,
new CmsShellReport(cms.getRequestContext().getLocale()));
// delete the file
cms.lockResource(filename1);
cms.deleteResource(filename1, CmsResource.DELETE_REMOVE_SIBLINGS);
// publish the deleted file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
assertFalse(cms.existsResource(filename1));
// create new file at the same location
cms.createResource(filename1, CmsResourceTypeImage.getStaticTypeId());
// publish the new file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -