NIHVIVO-1564 Object dump with different exposure levels
This commit is contained in:
parent
1c1d48a971
commit
0cf1da00b8
2 changed files with 49 additions and 34 deletions
|
@ -7,6 +7,7 @@ import java.io.StringWriter;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -14,6 +15,8 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.SortedMap;
|
import java.util.SortedMap;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
|
@ -47,10 +50,12 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(BaseDumpDirective.class);
|
private static final Log log = LogFactory.getLog(BaseDumpDirective.class);
|
||||||
|
|
||||||
|
Pattern PROPERTY_NAME_PATTERN = Pattern.compile("^(get|is)\\w");
|
||||||
|
|
||||||
enum Key {
|
enum Key {
|
||||||
METHODS("methods"),
|
METHODS("methods"),
|
||||||
NAME("name"),
|
NAME("name"),
|
||||||
PROPERTIES(Key.METHODS.toString()),
|
PROPERTIES("properties"),
|
||||||
TYPE("type"),
|
TYPE("type"),
|
||||||
VALUE("value"),
|
VALUE("value"),
|
||||||
DATE_TYPE("dateType");
|
DATE_TYPE("dateType");
|
||||||
|
@ -289,7 +294,7 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
|
|
||||||
// Compile the sets of properties and methods available to template
|
// Compile the sets of properties and methods available to template
|
||||||
SortedMap<String, Object> properties = new TreeMap<String, Object>();
|
SortedMap<String, Object> properties = new TreeMap<String, Object>();
|
||||||
SortedMap<String, Object> methods = new TreeMap<String, Object>();
|
List<String> methods = new ArrayList<String>();
|
||||||
|
|
||||||
// keys() gets only values visible to template based on the BeansWrapper used.
|
// keys() gets only values visible to template based on the BeansWrapper used.
|
||||||
// Note: if the BeansWrapper exposure level > BeansWrapper.EXPOSE_PROPERTIES_ONLY,
|
// Note: if the BeansWrapper exposure level > BeansWrapper.EXPOSE_PROPERTIES_ONLY,
|
||||||
|
@ -329,23 +334,35 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
// Include only methods included in keys(). This factors in visibility
|
// Include only methods included in keys(). This factors in visibility
|
||||||
// defined by the model's BeansWrapper.
|
// defined by the model's BeansWrapper.
|
||||||
String methodName = method.getName();
|
String methodName = method.getName();
|
||||||
String propertyName = getPropertyName(methodName);
|
|
||||||
|
Matcher matcher = PROPERTY_NAME_PATTERN.matcher(methodName);
|
||||||
// If the method is available as a property, use that
|
// If the method name starts with "get" or "is", check if it's available
|
||||||
if (keySet.contains(propertyName)) {
|
// as a property
|
||||||
TemplateModel value = model.get(propertyName);
|
if (matcher.find()) {
|
||||||
properties.put(propertyName, getData(value));
|
String propertyName = getPropertyName(methodName);
|
||||||
// Else look for the entire methodName in the key set
|
|
||||||
} else if (keySet.contains(methodName)) {
|
// The method is available as a property
|
||||||
|
if (keySet.contains(propertyName)) {
|
||||||
|
TemplateModel value = model.get(propertyName);
|
||||||
|
properties.put(propertyName, getData(value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Else look for the entire methodName in the key set. Include those
|
||||||
|
// starting with "get" or "is" that were not found above.
|
||||||
|
if (keySet.contains(methodName)) {
|
||||||
String methodDisplayName = getMethodDisplayName(method);
|
String methodDisplayName = getMethodDisplayName(method);
|
||||||
methods.put(methodDisplayName, "");
|
methods.add(methodDisplayName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, Object> objectValue = new HashMap<String, Object>(2);
|
Map<String, Object> objectValue = new HashMap<String, Object>(2);
|
||||||
objectValue.put(Key.PROPERTIES.toString(), properties);
|
objectValue.put(Key.PROPERTIES.toString(), properties);
|
||||||
|
|
||||||
|
Collections.sort(methods);
|
||||||
objectValue.put(Key.METHODS.toString(), methods);
|
objectValue.put(Key.METHODS.toString(), methods);
|
||||||
|
|
||||||
map.put(Key.VALUE.toString(), objectValue);
|
map.put(Key.VALUE.toString(), objectValue);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
@ -363,9 +380,6 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
|
||||||
paramTypeList.add(typeName);
|
paramTypeList.add(typeName);
|
||||||
}
|
}
|
||||||
methodName += "(" + StringUtils.join(paramTypeList, ", ") + ")";
|
methodName += "(" + StringUtils.join(paramTypeList, ", ") + ")";
|
||||||
} else {
|
|
||||||
methodName = methodName.replaceAll("^(get|is)", "");
|
|
||||||
methodName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return methodName;
|
return methodName;
|
||||||
|
|
|
@ -12,6 +12,7 @@ import java.sql.Time;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
@ -645,12 +646,12 @@ public class DumpDirectiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dumpObjectWithExposeSafeWrapper() {
|
public void dumpObjectWithExposeSafeWrapper() {
|
||||||
//dumpObject(BeansWrapper.EXPOSE_SAFE);
|
dumpObject(BeansWrapper.EXPOSE_SAFE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void dumpObjectWithExposeAllWrapper() {
|
public void dumpObjectWithExposeAllWrapper() {
|
||||||
//dumpObject(BeansWrapper.EXPOSE_ALL);
|
dumpObject(BeansWrapper.EXPOSE_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -686,7 +687,7 @@ public class DumpDirectiveTest {
|
||||||
Map<String, Object> dump = getDump(varName, dataModel);
|
Map<String, Object> dump = getDump(varName, dataModel);
|
||||||
assertEquals(expectedDump, dump);
|
assertEquals(expectedDump, dump);
|
||||||
|
|
||||||
// Test the sorting of the properties and methods
|
// Test the sorting of the properties
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Map<String, Object> valueExpectedDump = (Map<String, Object>) expectedDump.get(Key.VALUE.toString());
|
Map<String, Object> valueExpectedDump = (Map<String, Object>) expectedDump.get(Key.VALUE.toString());
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
@ -700,15 +701,7 @@ public class DumpDirectiveTest {
|
||||||
Map<String, Object> propertiesActualDump = (Map<String, Object>) valueActualDump.get(Key.PROPERTIES.toString());
|
Map<String, Object> propertiesActualDump = (Map<String, Object>) valueActualDump.get(Key.PROPERTIES.toString());
|
||||||
List<String> propertyKeysActual = new ArrayList<String>(propertiesActualDump.keySet());
|
List<String> propertyKeysActual = new ArrayList<String>(propertiesActualDump.keySet());
|
||||||
assertEquals(propertyKeysExpected, propertyKeysActual);
|
assertEquals(propertyKeysExpected, propertyKeysActual);
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Map<String, Object> methodsExpectedDump = (Map<String, Object>) valueExpectedDump.get(Key.METHODS.toString());
|
|
||||||
List<String> methodKeysExpected = new ArrayList<String>(methodsExpectedDump.keySet());
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Map<String, Object> methodsActualDump = (Map<String, Object>) valueActualDump.get(Key.METHODS.toString());
|
|
||||||
List<String> methodKeysActual = new ArrayList<String>(methodsActualDump.keySet());
|
|
||||||
assertEquals(methodKeysExpected, methodKeysActual);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> getDump(String varName, Map<String, Object> dataModel) {
|
private Map<String, Object> getDump(String varName, Map<String, Object> dataModel) {
|
||||||
|
@ -721,8 +714,6 @@ public class DumpDirectiveTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private class HelplessMethod implements TemplateMethodModel {
|
private class HelplessMethod implements TemplateMethodModel {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -853,7 +844,7 @@ public class DumpDirectiveTest {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSupervisor(Employee supervisor) {
|
protected void setSupervisor(Employee supervisor) {
|
||||||
this.supervisor = supervisor;
|
this.supervisor = supervisor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1002,11 +993,22 @@ public class DumpDirectiveTest {
|
||||||
|
|
||||||
expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);
|
expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
|
expectedDump.put(Key.METHODS.toString(), getEmployeeMethodsExpectedDump(exposureLevel));
|
||||||
|
|
||||||
SortedMap<String, Object> methodsExpectedDump = new TreeMap<String, Object>();
|
return expectedDump;
|
||||||
expectedDump.put(Key.METHODS.toString(), methodsExpectedDump);
|
}
|
||||||
|
|
||||||
|
private List<String> getEmployeeMethodsExpectedDump(int exposureLevel) {
|
||||||
|
|
||||||
|
List<String> expectedDump = new ArrayList<String>();
|
||||||
|
if (exposureLevel == BeansWrapper.EXPOSE_SAFE || exposureLevel == BeansWrapper.EXPOSE_ALL) {
|
||||||
|
expectedDump.add("getEmployeeCount");
|
||||||
|
expectedDump.add("getName(String)");
|
||||||
|
expectedDump.add("setFavoriteColors(Strings)");
|
||||||
|
expectedDump.add("setNickname(String)");
|
||||||
|
}
|
||||||
|
Collections.sort(expectedDump);
|
||||||
return expectedDump;
|
return expectedDump;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1072,8 +1074,7 @@ public class DumpDirectiveTest {
|
||||||
expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);
|
expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);
|
||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
SortedMap<String, Object> methodsExpectedDump = new TreeMap<String, Object>();
|
expectedDump.put(Key.METHODS.toString(), getEmployeeMethodsExpectedDump(exposureLevel));
|
||||||
expectedDump.put(Key.METHODS.toString(), methodsExpectedDump);
|
|
||||||
|
|
||||||
return expectedDump;
|
return expectedDump;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue