📄 testlinkparseableresourcetypes.java
字号:
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
sources = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size();
cms.getRequestContext().setCurrentProject(project);
cms.unlockResource(siblingName);
OpenCms.getPublishManager().publishResource(cms, siblingName);
OpenCms.getPublishManager().waitWhileRunning();
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
assertRelationOperation(cms, source, target, sources + 1, 1);
assertRelationOperation(cms, sibling, target, sources + 1, 1);
}
/**
* Test deleteResource method for a folder.<p>
*
* @throws Throwable if something goes wrong
*/
public void testDeleteFolder() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing 'deleteResource' method for a folder");
String folderName = "/testFolder";
cms.createResource(folderName, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
String targetName = "/folder2/image2.gif";
CmsResource target = cms.readResource(targetName);
String sourceName = folderName + "/index.html";
CmsResource source = cms.createResource(sourceName, CmsResourceTypeXmlPage.getStaticTypeId());
TestLinkValidation.setContent(cms, sourceName, "<img src='" + targetName + "'>");
List relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
CmsRelation expected = new CmsRelation(source, target, CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.SOURCES);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES);
assertEquals(1, relations.size());
cms.lockResource(folderName);
cms.deleteResource(folderName, CmsResource.DELETE_PRESERVE_SIBLINGS);
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES);
assertTrue(relations.isEmpty());
}
/**
* Test deleteResource method.<p>
*
* @throws Throwable if something goes wrong
*/
public void testDeleteResource() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing 'deleteResource' method");
String sourceName = "/index_copy.html";
CmsResource source = cms.readResource(sourceName);
String targetName = "/folder1/image2.gif";
CmsResource target = cms.readResource(targetName);
List relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertEquals(relations.size(), 1);
CmsRelation expected = new CmsRelation(source, target, CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.SOURCES);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
int sources = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size();
cms.lockResource(sourceName);
cms.deleteResource(sourceName, CmsResource.DELETE_PRESERVE_SIBLINGS);
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES);
assertEquals(sources, relations.size());
// there is no direct way to check the relations of deleted resources
/*
String query = "select * from cms_offline_resource_relations where relation_source_id = '"
+ source.getStructureId()
+ "' or relation_source_path like '"
+ source.getRootPath()
+ "%';";
*/
// now test deleting a target
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size();
cms.lockResource(targetName);
cms.deleteResource(targetName, CmsResource.DELETE_PRESERVE_SIBLINGS);
sourceName = "/index.html";
source = cms.readResource(sourceName);
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
expected = new CmsRelation(source, target, CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.SOURCES);
assertTrue(relations.isEmpty());
// there is no direct way to check the relations of deleted resources
/*
query = "select * from cms_offline_resource_relations where relation_target_id = '"
+ target.getStructureId()
+ "' or relation_target_path like '"
+ target.getRootPath()
+ "%';";
*/
}
/**
* Test importResource method for link parseable resources.<p>
*
* @throws Throwable if something goes wrong
*/
public void testImportResourceLinkParseable() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing 'importResource' method for link parseable resources");
String sourceName = "/index.html";
CmsResource source = cms.readResource(sourceName);
String targetName = "folder1/subfolder11/subsubfolder111/jsp.jsp";
String zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
"packages/testImportChangeType.zip");
try {
List relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
// replace by link parseable
cms.lockResource(targetName);
cms.replaceResource(targetName, source.getTypeId(), cms.readFile(source).getContents(), null);
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertEquals(relations.size(), 1);
// export the file
CmsVfsImportExportHandler vfsExportHandler = new CmsVfsImportExportHandler();
vfsExportHandler.setFileName(zipExportFilename);
List exportPaths = new ArrayList(1);
exportPaths.add(targetName);
vfsExportHandler.setExportPaths(exportPaths);
vfsExportHandler.setIncludeSystem(false);
vfsExportHandler.setIncludeUnchanged(true);
vfsExportHandler.setExportUserdata(false);
OpenCms.getImportExportManager().exportData(
cms,
vfsExportHandler,
new CmsShellReport(cms.getRequestContext().getLocale()));
// change the type back
cms.undoChanges(targetName, CmsResource.UNDO_CONTENT);
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
// re-import the exported file
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertEquals(relations.size(), 1);
} finally {
try {
if (zipExportFilename != null) {
File file = new File(zipExportFilename);
if (file.exists()) {
file.delete();
}
}
} catch (Throwable t) {
// intentionally left blank
}
}
}
/**
* Test importResource method for non link parseable resources.<p>
*
* @throws Throwable if something goes wrong
*/
public void testImportResourceNonLinkParseable() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing 'importResource' method for non link parseable resources");
String sourceName = "/index_created.html";
String zipExportFilename = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
"packages/testImportChangeType.zip");
try {
List relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertEquals(relations.size(), 1);
// change the type of the resource
cms.lockResource(sourceName);
cms.chtype(sourceName, CmsResourceTypeBinary.getStaticTypeId());
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
// export the file
CmsVfsImportExportHandler vfsExportHandler = new CmsVfsImportExportHandler();
vfsExportHandler.setFileName(zipExportFilename);
List exportPaths = new ArrayList(1);
exportPaths.add(sourceName);
vfsExportHandler.setExportPaths(exportPaths);
vfsExportHandler.setIncludeSystem(false);
vfsExportHandler.setIncludeUnchanged(true);
vfsExportHandler.setExportUserdata(false);
OpenCms.getImportExportManager().exportData(
cms,
vfsExportHandler,
new CmsShellReport(cms.getRequestContext().getLocale()));
// change the type back
cms.undoChanges(sourceName, CmsResource.UNDO_CONTENT);
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
// re-import the exported file
OpenCms.getImportExportManager().importData(
cms,
zipExportFilename,
"/",
new CmsShellReport(cms.getRequestContext().getLocale()));
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
} finally {
deleteFile(zipExportFilename);
}
}
/**
* Test the links after the setup.<p>
*
* @throws Throwable if something goes wrong
*/
public void testInitialSetup() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing the links after the setup");
String sourceName = "/index.html";
CmsResource source = cms.readResource(sourceName);
String targetName = "/folder1/image2.gif";
CmsResource target = cms.readResource(targetName);
List relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.TARGETS);
assertEquals(1, relations.size());
CmsRelation expected = new CmsRelation(source, target, CmsRelationType.EMBEDDED_IMAGE);
assertRelation(expected, (CmsRelation)relations.get(0));
relations = cms.getRelationsForResource(sourceName, CmsRelationFilter.SOURCES);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES);
assertEquals(20, relations.size());
}
/**
* Test moveResource method.<p>
*
* @throws Throwable if something goes wrong
*/
public void testMoveResource() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing 'moveResource' method");
String sourceName = "/index_sibling.html";
String targetName = "/folder1/image2.gif";
CmsResource target = cms.readResource(targetName);
List relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
int sources = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size();
String movedName = "/index_moved.html";
cms.lockResource(sourceName);
cms.moveResource(sourceName, movedName);
CmsResource moved = cms.readResource(movedName);
assertRelationOperation(cms, moved, target, sources, 1);
// check the online project
CmsProject project = cms.getRequestContext().currentProject();
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
relations = cms.getRelationsForResource(targetName, CmsRelationFilter.TARGETS);
assertTrue(relations.isEmpty());
sources = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size();
cms.getRequestContext().setCurrentProject(project);
cms.unlockResource(movedName);
OpenCms.getPublishManager().publishResource(cms, movedName);
OpenCms.getPublishManager().waitWhileRunning();
cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
assertRelationOperation(cms, moved, target, sources, 1);
}
/**
* Test replaceResource method.<p>
*
* @throws Throwable if something goes wrong
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -