📄 testlock.java
字号:
// all resources in the folder must be unlocked
resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
i = resources.iterator();
while (i.hasNext()) {
CmsResource res = (CmsResource)i.next();
assertLock(cms, cms.getSitePath(res), CmsLockType.UNLOCKED);
}
// all siblings outside of the folder must not locked
assertLock(cms, sibling1, CmsLockType.UNLOCKED);
assertLock(cms, sibling2, CmsLockType.UNLOCKED);
}
/**
* Tests lock status of a folder and its siblings.<p>
*
* In this test, the folder has some pre-locked siblings with exclusive locks in it
*
* @throws Throwable if something goes wrong
*/
public void testLockForFolderPrelockedExclusive() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing locking of folders with exclusive prelocks");
String folder = "/folder1/subfolder12/";
String source = "/folder1/subfolder12/page1.html";
String sibling1 = "/folder1/subfolder11/page1.html";
String sibling2 = "/folder2/subfolder22/page1.html";
storeResources(cms, folder);
// lock source
cms.lockResource(source);
// the source file must have an exclusive lock
// all siblings must have shared exclusive locks
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, sibling1, CmsLockType.SHARED_EXCLUSIVE);
assertLock(cms, sibling2, CmsLockType.SHARED_EXCLUSIVE);
// lock folder
cms.lockResource(folder);
// all resources in the folder must have an inherited lock
List resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
Iterator i = resources.iterator();
while (i.hasNext()) {
CmsResource res = (CmsResource)i.next();
assertLock(cms, cms.getSitePath(res), CmsLockType.INHERITED);
}
// The siblings outside the folder are unlocked
assertLock(cms, sibling1, CmsLockType.UNLOCKED);
assertLock(cms, sibling2, CmsLockType.UNLOCKED);
// now unlock the folder
cms.unlockResource(folder);
// the source folder must be unlocked
assertLock(cms, folder, CmsLockType.UNLOCKED);
// all resources in the folder must be unlocked
resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
i = resources.iterator();
while (i.hasNext()) {
CmsResource res = (CmsResource)i.next();
assertLock(cms, cms.getSitePath(res), CmsLockType.UNLOCKED);
}
// The siblings outside the folder kept their previous lock state
assertLock(cms, sibling1, CmsLockType.UNLOCKED);
assertLock(cms, sibling2, CmsLockType.UNLOCKED);
}
/**
* Tests lock status of a folder and its siblings.<p>
*
* In this test, the folder has some prelocked siblings with shared locks in it
*
* @throws Throwable if something goes wrong
*/
public void testLockForFolderPrelockedShared() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing locking of folders with shared prelocks");
String folder = "/folder1/subfolder12/";
String source = "/folder1/subfolder11/page1.html";
String sibling1 = "/folder1/subfolder12/page1.html";
String sibling2 = "/folder2/subfolder22/page1.html";
storeResources(cms, folder);
// lock source
cms.lockResource(source);
// the source file must have an exclusive lock
// all siblings must have shared exclusive locks
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, sibling1, CmsLockType.SHARED_EXCLUSIVE);
assertLock(cms, sibling2, CmsLockType.SHARED_EXCLUSIVE);
// lock folder
cms.lockResource(folder);
// all resources in the folder must have an inherited or shared inherited lock
List resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
Iterator i = resources.iterator();
while (i.hasNext()) {
CmsResource res = (CmsResource)i.next();
// the sibling to the resource "source" must have an shared inherited lock
// all other resources must have a inherited lock
if (cms.getSitePath(res).equals(sibling1)) {
assertLock(cms, cms.getSitePath(res), CmsLockType.SHARED_INHERITED);
} else {
assertLock(cms, cms.getSitePath(res), CmsLockType.INHERITED);
}
}
// The siblings outside the folder kept their previous lock state
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, sibling2, CmsLockType.SHARED_EXCLUSIVE);
// now unlock the folder
cms.unlockResource(folder);
// the source folder must be unlocked
assertLock(cms, folder, CmsLockType.UNLOCKED);
// all resources in the folder must be unlocked, except those siblings that had a
// shared inherited lock, they must get a shared exclusive lock
resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
i = resources.iterator();
while (i.hasNext()) {
CmsResource res = (CmsResource)i.next();
if (cms.getSitePath(res).equals(sibling1)) {
assertLock(cms, cms.getSitePath(res), CmsLockType.SHARED_EXCLUSIVE);
} else {
assertLock(cms, cms.getSitePath(res), CmsLockType.UNLOCKED);
}
}
// The siblings outside the folder kept their previous lock state
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, sibling2, CmsLockType.SHARED_EXCLUSIVE);
// now unlock the source
cms.unlockResource(source);
// the source file and all sibling are unlocked now
assertLock(cms, source, CmsLockType.UNLOCKED);
assertLock(cms, sibling1, CmsLockType.UNLOCKED);
assertLock(cms, sibling2, CmsLockType.UNLOCKED);
}
/**
* Tests lock status of a resource during sibling creation.<p>
*
* @throws Throwable if something goes wrong
*/
public void testLockForSiblings() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing lock state after sibling creation");
String source = "/folder2/index.html";
String destination1 = "/folder2/index_sib1.html";
String destination2 = "/folder2/index_sib2.html";
storeResources(cms, source);
// copy source
cms.copyResource(source, destination1, CmsResource.COPY_AS_SIBLING);
// since source was not locked, destination must be locked exclusive
// and source must be locked shared
assertLock(cms, source, CmsLockType.SHARED_EXCLUSIVE);
assertLock(cms, destination1, CmsLockType.EXCLUSIVE);
// copy source again
cms.copyResource(source, destination2, CmsResource.COPY_AS_SIBLING);
// since one sibling was already exclusive locked,
// new sibling must be shared locked
assertLock(cms, source, CmsLockType.SHARED_EXCLUSIVE);
assertLock(cms, destination1, CmsLockType.EXCLUSIVE);
assertLock(cms, destination2, CmsLockType.SHARED_EXCLUSIVE);
// same stuff but in a different order
source = "/folder2/page1.html";
destination1 = "/folder2/page1_sib1.html";
destination2 = "/folder2/page1_sib2.html";
// this time source is already locked
cms.lockResource(source);
cms.createSibling(source, destination1, null);
// since source was locked, destination must be shared exclusive
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, destination1, CmsLockType.SHARED_EXCLUSIVE);
// create another sibling
cms.createSibling(destination1, destination2, null);
// since one sibling was already exclusive locked,
// new sibling must be shared locked
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, destination1, CmsLockType.SHARED_EXCLUSIVE);
assertLock(cms, destination2, CmsLockType.SHARED_EXCLUSIVE);
// now create a sibling (from a locked resource) over a (locked) deleted resource
source = "/folder2/image1.gif";
String destination = "/folder2/image2.gif";
cms.lockResource(source);
cms.lockResource(destination);
cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
// both resources should be exclusive locked
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, destination, CmsLockType.EXCLUSIVE);
// now copy a sibling over the deleted
cms.copyResource(source, destination, CmsResource.COPY_AS_SIBLING);
// now the destination should have a shared lock
assertLock(cms, source, CmsLockType.EXCLUSIVE);
assertLock(cms, destination, CmsLockType.SHARED_EXCLUSIVE);
}
/**
* Tests an inherited lock in a resource delete scenario.<p>
*
* @throws Throwable if something goes wrong
*/
public void testLockInherit() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing inherited lock delete scenario");
String source = "/folder1/index.html";
String folder = "/folder1/";
storeResources(cms, source);
// first delete the resource
cms.lockResource(source);
cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS);
// now lock the folder
cms.lockResource(folder);
// make sure the deleted file has an inherited lock
assertLock(cms, source, CmsLockType.INHERITED);
}
/**
* Tests a lock set by other user.<p>
*
* @throws Throwable if something goes wrong
*/
public void testLockOtherUser() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing a lock set by other user");
String source = "/folder1/image1.gif";
// get the offline project
CmsProject offlineProject = cms.readProject("Offline");
// login as user "test1"
cms.loginUser("test1", "test1");
cms.getRequestContext().setCurrentProject(offlineProject);
// now lock the resource
cms.lockResource(source);
// make sure the file has an exclusive lock
assertLock(cms, source, CmsLockType.EXCLUSIVE);
// login back as admin
cms.loginUser("Admin", "admin");
cms.getRequestContext().setCurrentProject(offlineProject);
CmsUser test1 = cms.readUser("test1");
// make sure the file has still an exclusive lock on the test user
assertLock(cms, source, CmsLockType.EXCLUSIVE, test1);
}
/**
* Ensures that a lock is required for all write/control operations.<p>
*
* @throws Throwable if something goes wrong
*/
public void testLockRequired() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing if a lock is required for write/control operations");
String source = "/index.html";
storeResources(cms, source);
long timestamp = System.currentTimeMillis();
// make sure source is not locked
assertLock(cms, source, CmsLockType.UNLOCKED);
CmsFile file = cms.readFile(source);
boolean needLock;
needLock = false;
try {
cms.setDateLastModified(source, timestamp, false);
} catch (CmsLockException e) {
// must throw a security exception because resource is not locked
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -