📄 testcreatewriteresource.java
字号:
// date created
assertDateCreated(cms, resourcename, timestamp);
// the user last modified must be the current user
assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
// the content
assertContent(cms, resourcename, content);
// publish the project
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
}
/**
* Test the import resource method.<p>
*
* @throws Throwable if something goes wrong
*/
public void testImportResourceAgain() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing to import an existing resource again");
String resourcename = "/folder1/test1.html";
storeResources(cms, resourcename);
long timestamp = System.currentTimeMillis() - 12345678;
CmsResource res = cms.readResource(resourcename);
String contentStr = "Hello this is my NEW AND CHANGED content";
byte[] content = contentStr.getBytes();
// create a new resource
CmsResource resource = new CmsResource(
res.getStructureId(),
res.getResourceId(),
resourcename,
CmsResourceTypePlain.getStaticTypeId(),
false,
0,
cms.getRequestContext().currentProject().getUuid(),
CmsResource.STATE_NEW,
timestamp,
cms.getRequestContext().currentUser().getId(),
timestamp,
cms.getRequestContext().currentUser().getId(),
CmsResource.DATE_RELEASED_DEFAULT,
CmsResource.DATE_EXPIRED_DEFAULT,
1,
content.length,
0,
0);
cms.importResource(resourcename, resource, content, null);
// ensure created resource type
assertResourceType(cms, resourcename, CmsResourceTypePlain.getStaticTypeId());
// project must be current project
assertProject(cms, resourcename, cms.getRequestContext().currentProject());
// state must be "new"
assertState(cms, resourcename, CmsResource.STATE_CHANGED);
// date last modified
assertDateLastModified(cms, resourcename, timestamp);
// the user last modified must be the current user
assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
// now evaluate the filter
assertFilter(cms, resourcename, OpenCmsTestResourceFilter.FILTER_CREATE_RESOURCE);
// publish the project
cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
}
/**
* Test the import of a sibling.<p>
*
* @throws Throwable if something goes wrong
*/
public void testImportSibling() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing to import an existing resource as sibling");
CmsProperty prop1 = new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "The title", null);
CmsProperty prop2 = new CmsProperty(CmsPropertyDefinition.PROPERTY_DESCRIPTION, "The description", null);
CmsProperty prop3 = new CmsProperty(CmsPropertyDefinition.PROPERTY_KEYWORDS, "The keywords", null);
List properties = new ArrayList();
properties.add(prop1);
String siblingname = "/folder1/test1.html";
// make sure some non-shared properties are attached to the sibling
cms.lockResource(siblingname);
cms.writePropertyObjects(siblingname, properties);
cms.unlockResource(siblingname);
long timestamp = System.currentTimeMillis() - 12345678;
String resourcename1 = "/folder2/test1_sib1.html";
String resourcename2 = "/folder1/subfolder11/test1_sib2.html";
// read the existing resource to create siblings for
CmsFile file = cms.readFile(siblingname);
byte[] content = file.getContents();
assertTrue(file.getLength() > 0);
assertTrue(content.length > 0);
System.err.println(OpenCms.getSiteManager().getCurrentSite(cms).getSiteRoot());
storeResources(cms, siblingname);
System.err.println(OpenCms.getSiteManager().getCurrentSite(cms).getSiteRoot());
// create a new resource
CmsResource resource;
// cw: Test changed: must now provide correct content size in resource
resource = new CmsResource(
CmsUUID.getNullUUID(),
file.getResourceId(),
resourcename2,
CmsResourceTypePlain.getStaticTypeId(),
false,
0,
cms.getRequestContext().currentProject().getUuid(),
CmsResource.STATE_NEW,
timestamp,
cms.getRequestContext().currentUser().getId(),
timestamp,
cms.getRequestContext().currentUser().getId(),
CmsResource.DATE_RELEASED_DEFAULT,
CmsResource.DATE_EXPIRED_DEFAULT,
1,
content.length,
0,
0);
properties.add(prop2);
// using null as content must create sibling of existing content
cms.importResource(resourcename2, resource, null, properties);
System.err.println(OpenCms.getSiteManager().getCurrentSite(cms).getSiteRoot());
// project must be current project
assertProject(cms, resourcename2, cms.getRequestContext().currentProject());
// resource type
assertResourceType(cms, resourcename2, CmsResourceTypePlain.getStaticTypeId());
assertResourceType(cms, siblingname, CmsResourceTypePlain.getStaticTypeId());
// state
assertState(cms, resourcename2, CmsResource.STATE_NEW);
assertState(cms, siblingname, CmsResource.STATE_CHANGED);
// date last modified
assertDateLastModified(cms, resourcename2, file.getDateLastModified());
assertDateLastModified(cms, siblingname, file.getDateLastModified());
// the user last modified
assertUserLastModified(cms, resourcename2, cms.getRequestContext().currentUser());
assertUserLastModified(cms, siblingname, cms.getRequestContext().currentUser());
// content must be identical to stored content of new resource
assertContent(cms, resourcename2, content);
assertContent(cms, siblingname, content);
// check the sibling count
assertSiblingCountIncremented(cms, siblingname, 1);
// now evaluate the filter
OpenCmsTestResourceConfigurableFilter filter = new OpenCmsTestResourceConfigurableFilter(
OpenCmsTestResourceFilter.FILTER_CREATE_RESOURCE);
filter.disableSiblingCountTest();
assertFilter(cms, siblingname, filter);
String contentStr = "Hello this is my NEW AND CHANGED sibling content";
content = contentStr.getBytes();
resource = new CmsResource(
new CmsUUID(),
file.getResourceId(),
resourcename1,
CmsResourceTypePlain.getStaticTypeId(),
false,
0,
cms.getRequestContext().currentProject().getUuid(),
CmsResource.STATE_NEW,
timestamp,
cms.getRequestContext().currentUser().getId(),
timestamp,
cms.getRequestContext().currentUser().getId(),
CmsResource.DATE_RELEASED_DEFAULT,
CmsResource.DATE_EXPIRED_DEFAULT,
1,
content.length,
timestamp,
0);
properties.add(prop3);
// using new content must replace existing content
cms.importResource(resourcename1, resource, content, properties);
// project must be current project
assertProject(cms, resourcename1, cms.getRequestContext().currentProject());
assertProject(cms, resourcename2, cms.getRequestContext().currentProject());
// resource type
assertResourceType(cms, resourcename1, CmsResourceTypePlain.getStaticTypeId());
assertResourceType(cms, resourcename2, CmsResourceTypePlain.getStaticTypeId());
assertResourceType(cms, siblingname, CmsResourceTypePlain.getStaticTypeId());
// state
assertState(cms, resourcename1, CmsResource.STATE_NEW);
assertState(cms, resourcename2, CmsResource.STATE_NEW);
assertState(cms, siblingname, CmsResource.STATE_CHANGED);
// date last modified
assertDateLastModified(cms, resourcename1, timestamp);
assertDateLastModified(cms, resourcename2, timestamp);
assertDateLastModified(cms, siblingname, timestamp);
// the user last modified
assertUserLastModified(cms, resourcename1, cms.getRequestContext().currentUser());
assertUserLastModified(cms, resourcename2, cms.getRequestContext().currentUser());
assertUserLastModified(cms, siblingname, cms.getRequestContext().currentUser());
// content must be identical to stored content of new resource
assertContent(cms, resourcename1, content);
assertContent(cms, resourcename2, content);
assertContent(cms, siblingname, content);
// check the sibling count
assertSiblingCountIncremented(cms, siblingname, 2);
// now evaluate the filter
assertFilter(cms, siblingname, filter);
// publish the project
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
assertState(cms, resourcename1, CmsResource.STATE_UNCHANGED);
assertState(cms, resourcename2, CmsResource.STATE_UNCHANGED);
}
/**
* Tests to overwrite invisible resource.<p>
*
* @throws Exception if the test fails
*/
public void testOverwriteInvisibleResource() throws Exception {
CmsObject cms = getCmsObject();
echo("Testing to overwrite invisible resource");
// Creating paths
String source = "index.html";
String target = "/test_index.html";
cms.createResource(target, CmsResourceTypePlain.getStaticTypeId());
// remove read permission for user test2
cms.chacc(target, I_CmsPrincipal.PRINCIPAL_USER, "test2", "-r+v+i");
cms.unlockResource(target);
storeResources(cms, source);
storeResources(cms, target);
// login as test2
cms.loginUser("test2", "test2");
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
// try to read the file
try {
cms.readResource(target, CmsResourceFilter.ALL);
fail("should fail to read the resource without permissions");
} catch (CmsPermissionViolationException e) {
// ok
}
// try to overwrite without locking
try {
cms.copyResource(source, target);
fail("should fail to overwrite a resource without a lock on the target");
} catch (CmsLockException e) {
// ok
}
// try to lock the target, this is not possible because of missing permissions
try {
cms.lockResource(target);
fail("should fail to overwrite the resource without read permissions");
} catch (CmsPermissionViolationException e) {
// ok
}
// now try to create a resource with the same name, must also fail
try {
cms.createResource(target, CmsResourceTypeXmlPage.getStaticTypeId());
fail("should fail to create a resource that already exists");
} catch (CmsLockException e) {
// ok
}
// login again as admin
cms.loginUser("Admin", "admin");
cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
// make sure nothing has been changed
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);
assertFilter(cms, target, OpenCmsTestResourceFilter.FILTER_EQUAL);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -