📄 opencmstestcase.java
字号:
/**
* Compares two lists of CmsProperty objects and creates a list of all properties which are
* not included in a seperate exclude list.
* @param cms the CmsObject
* @param resourceName the name of the resource the properties belong to
* @param storedResource the stored resource corresponding to the resourcename
* @param excludeList the list of properies to exclude in the test or null
* @return string of non matching properties
* @throws CmsException if something goes wrong
*/
private static String compareProperties(
CmsObject cms,
String resourceName,
OpenCmsTestResourceStorageEntry storedResource,
List excludeList) throws CmsException {
String noMatches = "";
List storedProperties = storedResource.getProperties();
List properties = cms.readPropertyObjects(resourceName, false);
List unmatchedProperties;
unmatchedProperties = OpenCmsTestResourceFilter.compareProperties(storedProperties, properties, excludeList);
if (unmatchedProperties.size() > 0) {
noMatches += "[Properies missing " + unmatchedProperties.toString() + "]\n";
}
unmatchedProperties = OpenCmsTestResourceFilter.compareProperties(properties, storedProperties, excludeList);
if (unmatchedProperties.size() > 0) {
noMatches += "[Properies additional " + unmatchedProperties.toString() + "]\n";
}
return noMatches;
}
/**
* Copies the configuration files from the given folder to the "config" folder.
*
* @param newConfig the folder with the configuration files to copy
*/
private static void copyConfiguration(String newConfig) {
File configDir = new File(getTestDataPath("WEB-INF" + File.separatorChar + CmsSystemInfo.FOLDER_CONFIG));
File configOriDir = new File(newConfig);
if (configOriDir.exists()) {
File[] oriFiles = configOriDir.listFiles();
boolean initConfigDates = false;
if (m_dateConfigFiles == null) {
m_dateConfigFiles = new long[oriFiles.length];
initConfigDates = true;
}
for (int i = 0; i < oriFiles.length; i++) {
File source = oriFiles[i];
if (source.isFile()) {
// only copy files
String sourceName = source.getAbsolutePath();
File target = new File(configDir, source.getName());
if (initConfigDates) {
m_dateConfigFiles[i] = target.lastModified();
}
String targetName = target.getAbsolutePath();
try {
CmsFileUtil.copy(sourceName, targetName);
target.setLastModified(m_dateConfigFiles[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* Compares an access control entry of a resource with a given access control entry.<p>
*
* @param cms the CmsObject
* @param resourceName the name of the resource to compare
* @param ace the access control entry to compare or null if to compare with the stored values
*/
public void assertAce(CmsObject cms, String resourceName, CmsAccessControlEntry ace) {
try {
// create the exclude list
List excludeList = new ArrayList();
if (ace != null) {
excludeList.add(ace);
}
// get the stored resource
OpenCmsTestResourceStorageEntry storedResource = m_currentResourceStrorage.get(resourceName);
String noMatches = compareAccessEntries(cms, resourceName, storedResource, excludeList);
// now see if we have collected any no-matches
if (noMatches.length() > 0) {
fail("error comparing ace of resource " + resourceName + " with stored values: " + noMatches);
}
if (ace != null) {
List resAces = cms.getAccessControlEntries(resourceName);
boolean notFound = true;
Iterator i = resAces.iterator();
while (i.hasNext()) {
CmsAccessControlEntry resAce = (CmsAccessControlEntry)i.next();
if (resAce.getPrincipal().equals(ace.getPrincipal())
&& (resAce.getResource().equals(ace.getResource()))) {
notFound = false;
if (!resAce.equals(ace)) {
fail("[ACE " + ace + " != " + resAce + "]");
}
}
}
if (notFound) {
fail("[ACE not found" + ace + "]");
}
}
} catch (Exception e) {
e.printStackTrace();
fail("cannot read resource " + resourceName + " " + e.getMessage());
}
}
/**
* Compares an access control list of a resource with a given access control permission.<p>
*
* @param cms the CmsObject
* @param resourceName the name of the resource to compare
* @param principal the principal of the permission set or null if to compare with the stored values
* @param permission the permission set to compare
*/
public void assertAcl(CmsObject cms, String resourceName, CmsUUID principal, CmsPermissionSet permission) {
try {
// create the exclude list
List excludeList = new ArrayList();
if (permission != null) {
excludeList.add(principal);
}
// get the stored resource
OpenCmsTestResourceStorageEntry storedResource = m_currentResourceStrorage.get(resourceName);
String noMatches = compareAccessLists(cms, resourceName, storedResource, excludeList);
// now see if we have collected any no-matches
if (noMatches.length() > 0) {
fail("error comparing permission sets of resource "
+ resourceName
+ " with stored values: "
+ noMatches);
}
if (permission != null) {
CmsAccessControlList resAcls = cms.getAccessControlList(resourceName);
Map permissionMap = resAcls.getPermissionMap();
CmsPermissionSet resPermission = (CmsPermissionSet)permissionMap.get(principal);
if (resPermission != null) {
if (!resPermission.equals(permission)) {
fail("[Permission set not equal " + principal + ":" + permission + " != " + resPermission + "]");
}
} else {
fail("[Permission set not found " + principal + ":" + permission + "]");
}
}
} catch (Exception e) {
fail("cannot read resource " + resourceName + " " + e.getMessage());
}
}
/**
* Compares an access control list of a resource with a given access control permission.<p>
*
* @param cms the CmsObject
* @param modifiedResource the name of the which had its permissions changed
* @param resourceName the name of the resource to compare
* @param principal the principal of the permission set or null if to compare with the stored values
* @param permission the permission set to compare
*/
public void assertAcl(
CmsObject cms,
String modifiedResource,
String resourceName,
CmsUUID principal,
CmsPermissionSet permission) {
try {
// create the exclude list
List excludeList = new ArrayList();
if (permission != null) {
excludeList.add(principal);
}
// TODO: This is the code to recalculate the permission set if necessary. Its not completed yet!
Map parents = getParents(cms, resourceName);
List aceList = cms.getAccessControlEntries(resourceName);
Iterator i = aceList.iterator();
while (i.hasNext()) {
CmsAccessControlEntry ace = (CmsAccessControlEntry)i.next();
if (ace.getPrincipal().equals(principal)) {
String parent = (String)parents.get(ace.getResource());
if ((!parent.equals(modifiedResource)) && (parent.length() > modifiedResource.length())) {
permission = new CmsPermissionSet(ace.getAllowedPermissions(), ace.getDeniedPermissions());
}
}
}
// get the stored resource
OpenCmsTestResourceStorageEntry storedResource = m_currentResourceStrorage.get(resourceName);
String noMatches = compareAccessLists(cms, resourceName, storedResource, excludeList);
// now see if we have collected any no-matches
if (noMatches.length() > 0) {
fail("error comparing permission sets of resource "
+ resourceName
+ " with stored values: "
+ noMatches);
}
if (permission != null) {
CmsAccessControlList resAcls = cms.getAccessControlList(resourceName);
Map permissionMap = resAcls.getPermissionMap();
CmsPermissionSet resPermission = (CmsPermissionSet)permissionMap.get(principal);
if (resPermission != null) {
if (!resPermission.equals(permission)) {
fail("[Permission set not equal " + principal + ":" + permission + " != " + resPermission + "]");
}
} else {
fail("[Permission set not found " + principal + ":" + permission + "]");
}
}
} catch (Exception e) {
fail("cannot read resource " + resourceName + " " + e.getMessage());
}
}
/**
* Tests if a pattern can be found in a content string.<p>
* Fails if the pattern is not found.
*
* @param content the content string
* @param pattern the pattern to search for
*/
public void assertContains(String content, String pattern) {
if (content.toLowerCase().indexOf(pattern.toLowerCase()) == -1) {
fail("pattern '" + pattern + "' not found in content");
}
}
/**
* Tests if a pattern cannot be found in a content string.<p>
* Fails if the pattern is found.
*
* @param content the content string
* @param pattern the pattern to search for
*/
public void assertContainsNot(String content, String pattern) {
if (content.toLowerCase().indexOf(pattern.toLowerCase()) != -1) {
fail("pattern '" + pattern + "' found in content");
}
}
/**
* Compares the current content of a (file) resource with a given content.<p>
*
* @param cms the CmsObject
* @param resourceName the name of the resource to compare
* @param content the content to compare
*/
public void assertContent(CmsObject cms, String resourceName, byte[] content) {
try {
// get the actual resource from the vfs
CmsFile file = cms.readFile(resourceName, CmsResourceFilter.ALL);
byte[] fileContent = file.getContents();
if (fileContent.length != file.getLength()) {
fail("[Content length stored " + file.getContents().length + " != " + file.getLength() + "]");
}
if (fileContent.length != content.length) {
fail("[Content length compared " + file.getContents().length + " != " + content.length + "]");
}
for (int i = 0; i < content.length; i++) {
if (fileContent[i] != content[i]) {
fail("[Content compare failed at index " + i + "]");
}
}
} catch (CmsException e) {
fail("cannot read resource " + resourceName + " " + CmsException.getStackTraceAsString(e));
}
}
/**
* Tests if the current content date of a resource is equals to the given date.<p>
*
* @param cms the CmsObject
* @param resourceName the name of the resource to compare
* @param dateContent the content date
*/
public void assertDateContent(CmsObject cms, String resourceName, long dateContent) {
try {
// get the actual resource from the vfs
CmsResource res = cms.readResource(resourceName, CmsResourceFilter.ALL);
if (res.getDateContent() != dateContent) {
fail("[DateContent "
+ dateContent
+ " i.e. "
+ CmsDateUtil.getHeaderDate(dateContent)
+ " != "
+ res.getDateContent()
+ " i.e. "
+ CmsDateUtil.getHeaderDate(res.getDateContent())
+ "]");
}
} catch (CmsException e) {
fail("cannot read resource " + resourceName + " " + CmsException.getStackTraceAsString(e));
}
}
/**
* Tests if the the current date content of a resource is later than the given date.<p>
*
* @param cms the CmsObject
* @param resourceName the name of the resource to compare
* @param dateContent the content date
*/
public void assertDateContentAfter(CmsObject cms, String resourceName, long dateContent) {
try {
// get the actual resource from the vfs
CmsResource res = cms.readResource(resourceName, CmsResourceFilter.ALL);
if (res.getDateContent() < dateContent) {
fail("[DateContent "
+ dateContent
+ " i.e. "
+ CmsDateUtil.getHeaderDate(dateContent)
+ " > "
+ res.getDateContent()
+ " i.e. "
+ CmsDateUtil.getHeaderDate(res.getDateContent())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -