📄 testcmsimportexport.java
字号:
cms.readResource(filename1); // check resource by name
try {
cms.readResource(resBefore.getStructureId()); // check resource by id
fail("should not be found");
} catch (Exception e) {
// ok
}
// re-import the exported file
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// publish the imported file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
// read the imported file
CmsResource resAfter = cms.readResource(filename1);
// check it against the saved version
assertFilter(cms, resAfter, OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT_OVERWRITE);
} 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 sibling that has been recreated.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportRecreatedSibling() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing the import of a sibling that has been recreated.");
String filename1 = "/newfile3.html";
String filename2 = "sibling2.html";
String zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
"packages/testImportRecreatedFile.zip");
try {
cms.getRequestContext().setSiteRoot("/");
// create files
CmsResource res1 = cms.createResource(filename1, CmsResourceTypePlain.getStaticTypeId());
cms.createSibling(filename1, filename2, null);
// publish the files
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
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()));
cms.lockResource(filename1);
cms.deleteResource(filename1, CmsResource.DELETE_PRESERVE_SIBLINGS);
// publish the file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
assertFalse(cms.existsResource(filename1));
// create new sibling
cms.createSibling(filename2, filename1, null);
// publish the file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
cms.readResource(filename1); // check resource by name
try {
cms.readResource(res1.getStructureId()); // check resource by id
fail("should not be found");
} catch (Exception e) {
// ok
}
// try to re-import the exported files, should fail
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// publish the file
cms.unlockResource(filename1);
OpenCms.getPublishManager().publishResource(cms, filename1);
OpenCms.getPublishManager().waitWhileRunning();
// read file
CmsResource resAfter = cms.readResource(filename1);
// since nothing change
assertFilter(cms, resAfter, OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT_OVERWRITE);
} 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 edited.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportRelations() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing the import of a resource with relations.");
String filename = "/index.html";
String zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
"packages/testImportRelations.zip");
try {
// first there are no relations
assertTrue(cms.getRelationsForResource(filename, CmsRelationFilter.TARGETS.filterNotDefinedInContent()).isEmpty());
// add relation
cms.lockResource(filename);
CmsCategoryService catService = CmsCategoryService.getInstance();
CmsCategory cat = catService.createCategory(cms, null, "abc", "title", "description");
catService.addResourceToCategory(cms, filename, cat.getPath());
// now check the new relation
List relations = cms.getRelationsForResource(
filename,
CmsRelationFilter.TARGETS.filterNotDefinedInContent());
assertEquals(1, relations.size());
assertRelation(new CmsRelation(
cms.readResource(filename),
cms.readResource(cat.getId()),
CmsRelationType.CATEGORY), (CmsRelation)relations.get(0));
// export the file
CmsVfsImportExportHandler vfsExportHandler = new CmsVfsImportExportHandler();
vfsExportHandler.setFileName(zipExportFilename);
List exportPaths = new ArrayList(1);
exportPaths.add(filename);
vfsExportHandler.setExportPaths(exportPaths);
vfsExportHandler.setIncludeSystem(false);
vfsExportHandler.setIncludeUnchanged(true);
vfsExportHandler.setExportUserdata(false);
OpenCms.getImportExportManager().exportData(
cms,
vfsExportHandler,
new CmsShellReport(cms.getRequestContext().getLocale()));
// delete resource
cms.deleteResource(filename, CmsResource.DELETE_PRESERVE_SIBLINGS);
// publish
OpenCms.getPublishManager().publishResource(cms, filename);
OpenCms.getPublishManager().waitWhileRunning();
// create new res
cms.createResource(filename, CmsResourceTypePlain.getStaticTypeId());
// recheck that there are no relations
assertTrue(cms.getRelationsForResource(filename, CmsRelationFilter.TARGETS.filterNotDefinedInContent()).isEmpty());
// re-import the exported files
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// now check the imported relation
relations = cms.getRelationsForResource(filename, CmsRelationFilter.TARGETS.filterNotDefinedInContent());
assertEquals(1, relations.size());
assertRelation(new CmsRelation(
cms.readResource(filename),
cms.readResource(cat.getId()),
CmsRelationType.CATEGORY), (CmsRelation)relations.get(0));
} finally {
try {
if (zipExportFilename != null) {
File file = new File(zipExportFilename);
if (file.exists()) {
file.delete();
}
}
} catch (Throwable t) {
// intentionally left blank
}
}
}
/**
* Tests the resource translation during import.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportResourceTranslator() throws Exception {
echo("Testing resource translator for import");
CmsObject cms = OpenCms.initCmsObject(getCmsObject());
cms.getRequestContext().setSiteRoot("/");
// need to create the "galleries" folder manually
cms.createResource("/system/galleries", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
cms.unlockResource("/system/galleries");
CmsResourceTranslator oldFolderTranslator = OpenCms.getResourceManager().getFolderTranslator();
CmsResourceTranslator folderTranslator = new CmsResourceTranslator(new String[] {
"s#^/sites/default/content/bodys(.*)#/system/bodies$1#",
"s#^/sites/default/pics/system(.*)#/system/workplace/resources$1#",
"s#^/sites/default/pics(.*)#/system/galleries/pics$1#",
"s#^/sites/default/download(.*)#/system/galleries/download$1#",
"s#^/sites/default/externallinks(.*)#/system/galleries/externallinks$1#",
"s#^/sites/default/htmlgalleries(.*)#/system/galleries/htmlgalleries$1#",
"s#^/sites/default/content(.*)#/system$1#"}, false);
// set modified folder translator
OpenCms.getResourceManager().setTranslators(folderTranslator, OpenCms.getResourceManager().getFileTranslator());
// update OpenCms context to ensure new translator is used
cms = getCmsObject();
cms.getRequestContext().setSiteRoot("/sites/default");
// import the files
String importFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testimport01.zip");
OpenCms.getImportExportManager().importData(
cms,
importFile,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// check the results of the import
CmsXmlPage page;
CmsFile file;
CmsLinkTable table;
List links;
Iterator i;
List siblings;
// test "/importtest/index.html"
file = cms.readFile("/importtest/index.html");
page = CmsXmlPageFactory.unmarshal(cms, file);
table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
links = new ArrayList();
i = table.iterator();
while (i.hasNext()) {
CmsLink link = (CmsLink)i.next();
links.add(link.toString());
}
assertTrue(links.size() == 2);
assertTrue(links.contains("/sites/default/importtest/page2.html"));
assertTrue(links.contains("/sites/default/importtest/page3.html"));
siblings = cms.readSiblings("/importtest/index.html", CmsResourceFilter.ALL);
i = siblings.iterator();
links = new ArrayList();
while (i.hasNext()) {
CmsResource sibling = (CmsResource)i.next();
links.add(sibling.getRootPath());
}
assertEquals(2, links.size());
assertTrue(links.contains("/sites/default/importtest/index.html"));
assertTrue(links.contains("/sites/default/importtest/linktest.html"));
// test "/importtest/page2.html"
file = cms.readFile("/importtest/page2.html");
page = CmsXmlPageFactory.unmarshal(cms, file);
table = page.getLinkTable("body", CmsLocaleManager.getDefaultLocale());
links = new ArrayList();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -