📄 testcmsimportexport.java
字号:
i = table.iterator();
while (i.hasNext()) {
CmsLink link = (CmsLink)i.next();
links.add(link.toString());
System.out.println("Link: " + link.toString());
}
assertEquals(2, links.size());
assertTrue(links.contains("/system/galleries/pics/_anfang/bg_teaser_test2.jpg"));
assertTrue(links.contains("/sites/default/importtest/index.html"));
// test "/importtest/linktest.html" (sibling of "/importtest/index.html")
file = cms.readFile("/importtest/linktest.html");
assertEquals(CmsResourceTypeXmlPage.getStaticTypeId(), file.getTypeId());
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());
System.out.println("Link: " + link.toString());
}
assertEquals(2, links.size());
assertTrue(links.contains("/sites/default/importtest/page2.html"));
assertTrue(links.contains("/sites/default/importtest/page3.html"));
siblings = cms.readSiblings("/importtest/linktest.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 "/othertest/index.html"
file = cms.readFile("/othertest/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());
System.out.println("Link: " + link.toString());
}
assertTrue(links.size() == 2);
assertTrue(links.contains("/sites/default/importtest/page2.html"));
assertTrue(links.contains("/sites/default/importtest/page3.html"));
// clean up for the next test
cms.getRequestContext().setSiteRoot("/");
cms.lockResource("/sites/default");
cms.lockResource("/system");
cms.deleteResource("/sites/default/importtest", CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.deleteResource("/system/galleries/pics", CmsResource.DELETE_PRESERVE_SIBLINGS);
cms.unlockResource("/sites/default");
cms.unlockResource("/system");
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// reset the translation rules
OpenCms.getResourceManager().setTranslators(
oldFolderTranslator,
OpenCms.getResourceManager().getFileTranslator());
}
/**
* Tests the resource translation during import with multiple sites.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportResourceTranslatorMultipleSite() throws Exception {
echo("Testing resource translator with multiple sites");
CmsObject cms = getCmsObject();
cms.getRequestContext().setSiteRoot("/");
// create a second site
cms.createResource("/sites/mysite", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
cms.unlockResource("/sites/mysite");
cms.createResource("/sites/othersite", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
cms.unlockResource("/sites/othersite");
CmsResourceTranslator oldFolderTranslator = OpenCms.getResourceManager().getFolderTranslator();
CmsResourceTranslator folderTranslator = new CmsResourceTranslator(new String[] {
"s#^/sites(.*)#/sites$1#",
"s#^/system(.*)#/system$1#",
"s#^/content/bodys(.*)#/system/bodies$1#",
"s#^/pics(.*)#/system/galleries/pics$1#",
"s#^/download(.*)#/system/galleries/download$1#",
"s#^/externallinks(.*)#/system/galleries/externallinks$1#",
"s#^/htmlgalleries(.*)#/system/galleries/htmlgalleries$1#",
"s#^/content(.*)#/system$1#",
"s#^/othertest(.*)#/sites/othersite$1#",
"s#^/(.*)#/sites/mysite/$1#"}, false);
// set modified folder translator
OpenCms.getResourceManager().setTranslators(folderTranslator, OpenCms.getResourceManager().getFileTranslator());
// update OpenCms context to ensure new translator is used
cms = getCmsObject();
// set root site
cms.getRequestContext().setSiteRoot("/");
// import the files
String importFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testimport01.zip");
OpenCms.getImportExportManager().importData(
cms,
importFile,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
// now switch to "mysite"
cms.getRequestContext().setSiteRoot("/sites/mysite/");
// 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/mysite/importtest/page2.html"));
assertTrue(links.contains("/sites/mysite/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/mysite/importtest/index.html"));
assertTrue(links.contains("/sites/mysite/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();
i = table.iterator();
while (i.hasNext()) {
CmsLink link = (CmsLink)i.next();
links.add(link.toString());
}
assertEquals(2, links.size());
assertTrue(links.contains("/system/galleries/pics/_anfang/bg_teaser_test2.jpg"));
assertTrue(links.contains("/sites/mysite/importtest/index.html"));
// test "/importtest/linktest.html" (sibling of "/importtest/index.html")
file = cms.readFile("/importtest/linktest.html");
assertEquals(CmsResourceTypeXmlPage.getStaticTypeId(), file.getTypeId());
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());
System.out.println("Link: " + link.toString());
}
assertEquals(2, links.size());
assertTrue(links.contains("/sites/mysite/importtest/page2.html"));
assertTrue(links.contains("/sites/mysite/importtest/page3.html"));
siblings = cms.readSiblings("/importtest/linktest.html", CmsResourceFilter.ALL);
i = siblings.iterator();
links = new ArrayList();
while (i.hasNext()) {
CmsResource sibling = (CmsResource)i.next();
links.add(sibling.getRootPath());
System.out.println("Sibling: " + sibling.toString());
}
assertEquals(2, links.size());
assertTrue(links.contains("/sites/mysite/importtest/index.html"));
assertTrue(links.contains("/sites/mysite/importtest/linktest.html"));
// now switch to "othersite"
cms.getRequestContext().setSiteRoot("/sites/othersite/");
// test "/othertest/index.html"
file = cms.readFile("/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());
System.out.println("Link: " + link.toString());
}
assertTrue(links.size() == 2);
assertTrue(links.contains("/sites/mysite/importtest/page2.html"));
assertTrue(links.contains("/sites/mysite/importtest/page3.html"));
// reset the translation rules
OpenCms.getResourceManager().setTranslators(
oldFolderTranslator,
OpenCms.getResourceManager().getFileTranslator());
}
/**
* Tests the import of a sibling.<p>
*
* @throws Exception if something goes wrong
*/
public void testImportSibling() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing the import of a sibling.");
String filename1 = "/newfile2.html";
String filename2 = "sibling.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));
try {
cms.readResource(filename1); // check resource by name
fail("should not be found");
} catch (Exception e) {
// ok
}
try {
cms.readResource(res1.getStructureId()); // check resource by id
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();
assertFilter(cms, cms.readResource(filename1), OpenCmsTestResourceFilter.FILTER_IMPORTEXPORT_SIBLING);
} finally {
try {
if (zipExportFilename != null) {
File file = new File(zipExportFilename);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -