📄 testpublishing.java
字号:
String newFile = newFolder + "new_file";
String newSibling = newFolder + "new_sibling";
cms.createResource(newFolder, CmsResourceTypeFolder.getStaticTypeId());
cms.unlockResource(newFolder);
storeResources(cms, newFolder);
// change to test project
CmsProject project = getTestProject(cms);
cms.getRequestContext().setCurrentProject(project);
cms.copyResource(source, newFile, CmsResource.COPY_AS_NEW);
cms.unlockResource(newFile);
cms.copyResource(source, newSibling, CmsResource.COPY_AS_SIBLING);
cms.unlockResource(newSibling);
// direct publish of the new file will not publish the new file
echo ("Publishing the resource directly");
storeResources(cms, newFile);
boolean error = true;
try {
// this will generate an error in the log, ensure the test still continues
OpenCmsTestLogAppender.setBreakOnError(false);
cms.publishResource(newFile);
} catch (CmsMultiException e) {
CmsVfsException ex = (CmsVfsException)e.getExceptions().get(0);
if (ex.getMessageContainer().getKey() == org.opencms.db.Messages.ERR_DIRECT_PUBLISH_PARENT_NEW_2) {
error = false;
}
}
// reset log to stop test on error
OpenCmsTestLogAppender.setBreakOnError(true);
if (error) {
fail("A resource in a new folder could be published without generating an error!");
}
assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_EQUAL);
// direct publish of another sibling will not publish the new sibling
echo ("Publishing another sibling");
cms.lockResource(source);
cms.setDateLastModified(source, System.currentTimeMillis(), false);
cms.unlockResource(source);
storeResources(cms, newSibling);
cms.publishResource(source, true, new CmsShellReport(cms.getRequestContext().getLocale()));
assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_EQUAL);
// publishing the test project will not publish the new file or the new sibling
echo ("Publishing the test project");
cms.publishProject();
assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_EQUAL);
assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_EQUAL);
// publishing the offline project will publish the folder but not the resources
echo ("Publishing the offline project");
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
cms.publishProject();
assertFilter(cms, newFolder, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
assertState(cms, newFolder, CmsResource.STATE_UNCHANGED);
assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_EQUAL);
assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_EQUAL);
// publishing the test proejct again will now publish the resources
echo ("Publishing the test project again");
cms.getRequestContext().setCurrentProject(project);
cms.publishProject();
assertFilter(cms, newFile, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
assertState(cms, newFile, CmsResource.STATE_UNCHANGED);
assertFilter(cms, newSibling, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
assertState(cms, newSibling, CmsResource.STATE_UNCHANGED);
}
/**
* Tests publishing resources within a distinct project.<p>
*
* @throws Throwable if something goes wrong
*/
public void testPublishProjectLastmodified() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing publishing a distinct project");
String path = "/folder1";
String res1 = path + "/page1.html";
String res2 = path + "/page2.html";
long timestamp = System.currentTimeMillis();
cms.getRequestContext().setCurrentProject(cms.readProject("Online"));
storeResources(cms, res1);
// change first resource in the offline project
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
cms.lockResource(res1);
cms.setDateLastModified(res1, timestamp, false);
cms.unlockProject(cms.getRequestContext().currentProject().getId());
// create a new project
CmsProject project = getTestProject(cms);
cms.getRequestContext().setCurrentProject(project);
cms.copyResourceToProject(path);
// and change another resource in this project
cms.lockResource(res2);
cms.setDateLastModified(res2, timestamp, false);
cms.unlockProject(cms.getRequestContext().currentProject().getId());
storeResources(cms, res2);
// when the project is published, only the second resource will be published
cms.publishProject();
cms.getRequestContext().setCurrentProject(cms.readProject("Online"));
this.assertFilter(cms, res1, OpenCmsTestResourceFilter.FILTER_EQUAL);
this.assertFilter(cms, res2, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
this.assertDateLastModified(cms, res2, timestamp);
echo("Testing publishing in different projects");
// change back to the test project, create a new resource
String res3 = path + "/testPublishProjectLastmodified.txt";
cms.getRequestContext().setCurrentProject(project);
cms.createResource(res3, CmsResourceTypePlain.getStaticTypeId());
// change to offline project, copy resource as sibling
// this will also change the project in which the source was lastmodified (now "Offline")
String res4 = "/testPublishProjectLastmodified.txt";
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
cms.copyResource(res3, res4, CmsResource.COPY_AS_SIBLING);
// check the current state of the resources
assertState(cms, res3, CmsResource.STATE_NEW);
assertState(cms, res4, CmsResource.STATE_NEW);
// publish the test project
cms.getRequestContext().setCurrentProject(project);
cms.unlockProject(project.getId());
cms.publishProject();
// the resource inside the test project must not be published (not in test project)
assertState(cms, res3, CmsResource.STATE_NEW);
// the sibling outside the test project must not be published (not in test project)
assertState(cms, res4, CmsResource.STATE_NEW);
// publish the root folder of the test project within the test project
cms.publishResource(path, true, new CmsShellReport(cms.getRequestContext().getLocale()));
// the resource inside the test project must not be published (still not in test project)
assertState(cms, res3, CmsResource.STATE_NEW);
// the sibling outside the test project must not be published (still not in test project)
assertState(cms, res4, CmsResource.STATE_NEW);
}
/**
* Test publishing a temporary project.<p>
*
* @throws Throwable if something goes wrong
*/
public void testPublishTemporaryProject() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing publish temporary project");
String source = "/folder1/testfile.txt";
CmsProject onlineProject = cms.readProject("Online");
CmsProject offlineProject = cms.readProject("Offline");
//create a new temp project
CmsProject tempProject = cms.createProject("deleteme", "Temp project to be deleted after publish", "Users", "Projectmanagers", CmsProject.PROJECT_TYPE_TEMPORARY);
cms.copyResourceToProject("/");
cms.getRequestContext().setCurrentProject(tempProject);
// now create a new resource
cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
cms.unlockResource(source);
storeResources(cms, source);
cms.readResource(source);
//publish the project
cms.publishProject();
// now try to read the resource
try {
cms.readResource(source);
} catch (CmsException e) {
fail("Resource " + source + " not found in project " + cms.getRequestContext().currentProject().toString() + ":" +e);
}
// now try to read the resource in the online project
cms.getRequestContext().setCurrentProject(onlineProject);
try {
cms.readResource(source);
} catch (CmsException e) {
fail("Resource " + source + " not found in project " + cms.getRequestContext().currentProject().toString() + ":" +e);
}
// check if the state of the resource is ok
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_PUBLISHRESOURCE);
// check if the file in the offline project is unchancged now
cms.getRequestContext().setCurrentProject(offlineProject);
assertState(cms, source, CmsResource.STATE_UNCHANGED);
// check if the project is deleted
try {
cms.readProject("deleteme");
fail("Temporary project still existing");
} catch (CmsException e) {
// to nothing, this exception must be thrown
}
}
/**
* Returns a project for testing purposes.<p>
*
* @param cms the cms object
* @return a project for testing purposes
*/
CmsProject getTestProject(CmsObject cms) {
CmsProject project = null;
try {
project = cms.readProject("Test");
} catch (Exception e) {
try {
project = cms.createProject("Test", "", "Users", "Administrators");
} catch (Exception ee) {
// noop
}
}
return project;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -