NIHVIVO-1564 Separate properties and methods in object dump. Reorganize unit tests to share more code.

This commit is contained in:
ryounes 2011-04-18 20:02:08 +00:00
parent 0eb6f25e89
commit 6ebb72a09d
2 changed files with 187 additions and 198 deletions

View file

@ -48,7 +48,9 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
private static final Log log = LogFactory.getLog(BaseDumpDirective.class); private static final Log log = LogFactory.getLog(BaseDumpDirective.class);
enum Key { enum Key {
METHODS("methods"),
NAME("name"), NAME("name"),
PROPERTIES(Key.METHODS.toString()),
TYPE("type"), TYPE("type"),
VALUE("value"), VALUE("value"),
DATE_TYPE("dateType"); DATE_TYPE("dateType");
@ -285,8 +287,9 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put(Key.TYPE.toString(), object.getClass().getName()); map.put(Key.TYPE.toString(), object.getClass().getName());
// Compile the set of properties and methods available to model // Compile the sets of properties and methods available to template
SortedMap<String, Object> availableMethods = new TreeMap<String, Object>(); SortedMap<String, Object> properties = new TreeMap<String, Object>();
SortedMap<String, Object> methods = new TreeMap<String, Object>();
// 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,
@ -305,12 +308,12 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
if (keySet.size() > 0) { if (keySet.size() > 0) {
Class<?> cls = object.getClass(); Class<?> cls = object.getClass();
Method[] methods = cls.getMethods(); Method[] classMethods = cls.getMethods();
// Iterate through the methods rather than the keys, so that we can remove // Iterate through the methods rather than the keys, so that we can remove
// some keys based on reflection on the methods. We also want to remove duplicates // some keys based on reflection on the methods. We also want to remove duplicates
// like name/getName - we'll keep only the first form. // like name/getName - we'll keep only the first form.
for ( Method method : methods ) { for ( Method method : classMethods ) {
// Eliminate methods declared on Object // Eliminate methods declared on Object
Class<?> c = method.getDeclaringClass(); Class<?> c = method.getDeclaringClass();
@ -331,16 +334,19 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
// If the method is available as a property, use that // If the method is available as a property, use that
if (keySet.contains(propertyName)) { if (keySet.contains(propertyName)) {
TemplateModel value = model.get(propertyName); TemplateModel value = model.get(propertyName);
availableMethods.put(propertyName, getData(value)); properties.put(propertyName, getData(value));
// Else look for the entire methodName in the key set // Else look for the entire methodName in the key set
} else if (keySet.contains(methodName)) { } else if (keySet.contains(methodName)) {
String methodDisplayName = getMethodDisplayName(method); String methodDisplayName = getMethodDisplayName(method);
availableMethods.put(methodDisplayName, ""); methods.put(methodDisplayName, "");
} }
} }
} }
map.put(Key.VALUE.toString(), availableMethods); Map<String, Object> objectValue = new HashMap<String, Object>(2);
objectValue.put(Key.PROPERTIES.toString(), properties);
objectValue.put(Key.METHODS.toString(), methods);
map.put(Key.VALUE.toString(), objectValue);
return map; return map;
} }

View file

@ -635,118 +635,22 @@ public class DumpDirectiveTest {
@Test @Test
public void dumpObjectWithExposeNothingWrapper() { public void dumpObjectWithExposeNothingWrapper() {
dumpObject(BeansWrapper.EXPOSE_NOTHING);
String varName = "employee";
Map<String, Object> dataModel = new HashMap<String, Object>();
BeansWrapper wrapper = new BeansWrapper();
wrapper.setExposureLevel(BeansWrapper.EXPOSE_NOTHING);
try {
dataModel.put("employee", wrapper.wrap(getEmployee()));
} catch (TemplateModelException e) {
// logging is suppressed, so what do we do here?
}
Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(Key.NAME.toString(), varName);
expectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee");
expectedDump.put(Key.VALUE.toString(), new TreeMap<String, Object>());
test(varName, dataModel, expectedDump);
} }
@Test @Test
public void dumpObjectWithExposePropertiesOnlyWrapper() { public void dumpObjectWithExposePropertiesOnlyWrapper() {
dumpObject(BeansWrapper.EXPOSE_PROPERTIES_ONLY);
String varName = "employee";
Map<String, Object> dataModel = new HashMap<String, Object>();
BeansWrapper wrapper = new BeansWrapper();
wrapper.setExposureLevel(BeansWrapper.EXPOSE_PROPERTIES_ONLY);
try {
dataModel.put("employee", wrapper.wrap(getEmployee()));
} catch (TemplateModelException e) {
// logging is suppressed, so what do we do here?
}
Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(Key.NAME.toString(), varName);
expectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee");
SortedMap<String, Object> propertiesExpectedDump = getJohnDoePropertiesExpectedDump();
expectedDump.put(Key.VALUE.toString(), propertiesExpectedDump);
Map<String, Object> dump = getDump(varName, dataModel);
assertEquals(expectedDump, dump);
// Test the sorting of the methods
List<String> expectedKeys = new ArrayList<String>(propertiesExpectedDump.keySet());
@SuppressWarnings("unchecked")
Map<String, Object> methodsActualDump = (Map<String, Object>) dump.get(Key.VALUE.toString());
List<String> actualKeys = new ArrayList<String>(methodsActualDump.keySet());
assertEquals(expectedKeys, actualKeys);
} }
@Test @Test
public void dumpObjectWithExposeSafeWrapper() { public void dumpObjectWithExposeSafeWrapper() {
//dumpObject(BeansWrapper.EXPOSE_SAFE);
String varName = "employee";
Map<String, Object> dataModel = new HashMap<String, Object>();
BeansWrapper wrapper = new BeansWrapper();
wrapper.setExposureLevel(BeansWrapper.EXPOSE_SAFE);
try {
dataModel.put("employee", wrapper.wrap(getEmployee()));
} catch (TemplateModelException e) {
// logging is suppressed, so what do we do here?
}
Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(Key.NAME.toString(), varName);
expectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee");
SortedMap<String, Object> methodsExpectedDump = getJohnDoePropertiesExpectedDump();
// methodsExpectedDump.putAll(...);
expectedDump.put(Key.VALUE.toString(), methodsExpectedDump);
//Map<String, Object> dump = getDump(varName, dataModel);
//assertEquals(expectedDump, dump);
// Test the sorting of the methods
// List<String> expectedKeys = new ArrayList<String>(properties.keySet());
// @SuppressWarnings("unchecked")
// Map<String, Object> methodsActualDump = (Map<String, Object>) dump.get(Key.VALUE.toString());
// List<String> actualKeys = new ArrayList<String>(methodsActualDump.keySet());
//assertEquals(expectedKeys, actualKeys);
} }
@Test @Test
public void dumpObjectWithExposeAllWrapper() { public void dumpObjectWithExposeAllWrapper() {
//dumpObject(BeansWrapper.EXPOSE_ALL);
String varName = "employee";
Map<String, Object> dataModel = new HashMap<String, Object>();
BeansWrapper wrapper = new BeansWrapper();
wrapper.setExposureLevel(BeansWrapper.EXPOSE_ALL);
try {
dataModel.put("employee", wrapper.wrap(getEmployee()));
} catch (TemplateModelException e) {
// logging is suppressed, so what do we do here?
}
Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(Key.NAME.toString(), varName);
expectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee");
SortedMap<String, Object> methodsExpectedDump = getJohnDoePropertiesExpectedDump();
// methodsExpectedDump.putAll(...);
expectedDump.put(Key.VALUE.toString(), methodsExpectedDump);
//Map<String, Object> dump = getDump(varName, dataModel);
//assertEquals(expectedDump, dump);
// Test the sorting of the methods
// List<String> expectedKeys = new ArrayList<String>(properties.keySet());
// @SuppressWarnings("unchecked")
// Map<String, Object> methodsActualDump = (Map<String, Object>) dump.get(Key.VALUE.toString());
// List<String> actualKeys = new ArrayList<String>(methodsActualDump.keySet());
//assertEquals(expectedKeys, actualKeys);
} }
@ -757,6 +661,56 @@ public class DumpDirectiveTest {
assertEquals(expectedDump, dump); assertEquals(expectedDump, dump);
} }
private void dumpObject(int exposureLevel) {
String varName = "employee";
Map<String, Object> dataModel = new HashMap<String, Object>();
BeansWrapper wrapper = new BeansWrapper();
wrapper.setExposureLevel(exposureLevel);
try {
dataModel.put("employee", wrapper.wrap(getEmployee()));
} catch (TemplateModelException e) {
// logging is suppressed, so what do we do here?
}
Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(Key.NAME.toString(), varName);
expectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee");
expectedDump.put(Key.VALUE.toString(), getJohnDoeExpectedDump(exposureLevel));
testObjectDump(varName, dataModel, expectedDump);
}
private void testObjectDump(String varName, Map<String, Object> dataModel, Map<String, Object> expectedDump) {
Map<String, Object> dump = getDump(varName, dataModel);
assertEquals(expectedDump, dump);
// Test the sorting of the properties and methods
@SuppressWarnings("unchecked")
Map<String, Object> valueExpectedDump = (Map<String, Object>) expectedDump.get(Key.VALUE.toString());
@SuppressWarnings("unchecked")
Map<String, Object> valueActualDump = (Map<String, Object>) dump.get(Key.VALUE.toString());
@SuppressWarnings("unchecked")
Map<String, Object> propertiesExpectedDump = (Map<String, Object>) valueExpectedDump.get(Key.PROPERTIES.toString());
List<String> propertyKeysExpected = new ArrayList<String>(propertiesExpectedDump.keySet());
@SuppressWarnings("unchecked")
Map<String, Object> propertiesActualDump = (Map<String, Object>) valueActualDump.get(Key.PROPERTIES.toString());
List<String> propertyKeysActual = new ArrayList<String>(propertiesActualDump.keySet());
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) {
try { try {
Environment env = template.createProcessingEnvironment(dataModel, new StringWriter()); Environment env = template.createProcessingEnvironment(dataModel, new StringWriter());
@ -767,6 +721,8 @@ public class DumpDirectiveTest {
} }
} }
private class HelplessMethod implements TemplateMethodModel { private class HelplessMethod implements TemplateMethodModel {
@Override @Override
@ -982,9 +938,15 @@ public class DumpDirectiveTest {
return jdoe; return jdoe;
} }
private SortedMap<String, Object> getJohnDoePropertiesExpectedDump() { private Map<String, Object> getJohnDoeExpectedDump(int exposureLevel) {
Map<String, Object> expectedDump = new HashMap<String, Object>();
// Properties
SortedMap<String, Object> propertiesExpectedDump = new TreeMap<String, Object>(); SortedMap<String, Object> propertiesExpectedDump = new TreeMap<String, Object>();
if (exposureLevel != BeansWrapper.EXPOSE_NOTHING) {
// Map<String, Object> birthdateExpectedDump = new HashMap<String, Object>(); // Map<String, Object> birthdateExpectedDump = new HashMap<String, Object>();
// birthdateExpectedDump.put(Key.TYPE.toString(), Type.DATE); // birthdateExpectedDump.put(Key.TYPE.toString(), Type.DATE);
// birthdateExpectedDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN); // birthdateExpectedDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN);
@ -1019,7 +981,8 @@ public class DumpDirectiveTest {
Map<String, Object> supervisorExpectedDump = new HashMap<String, Object>(); Map<String, Object> supervisorExpectedDump = new HashMap<String, Object>();
supervisorExpectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee"); supervisorExpectedDump.put(Key.TYPE.toString(), "freemarker.ext.dump.DumpDirectiveTest$Employee");
supervisorExpectedDump.put(Key.VALUE.toString(), getJaneSmithPropertiesExpectedDump());
supervisorExpectedDump.put(Key.VALUE.toString(), getJaneSmithExpectedDump(exposureLevel));
propertiesExpectedDump.put("supervisor", supervisorExpectedDump); propertiesExpectedDump.put("supervisor", supervisorExpectedDump);
Map<String, Object> favoriteColorsExpectedDump = new HashMap<String, Object>(); Map<String, Object> favoriteColorsExpectedDump = new HashMap<String, Object>();
@ -1035,14 +998,27 @@ public class DumpDirectiveTest {
favoriteColorListExpectedDump.add(color2ExpectedDump); favoriteColorListExpectedDump.add(color2ExpectedDump);
favoriteColorsExpectedDump.put(Key.VALUE.toString(), favoriteColorListExpectedDump); favoriteColorsExpectedDump.put(Key.VALUE.toString(), favoriteColorListExpectedDump);
propertiesExpectedDump.put("favoriteColors", favoriteColorsExpectedDump); propertiesExpectedDump.put("favoriteColors", favoriteColorsExpectedDump);
return propertiesExpectedDump;
} }
private SortedMap<String, Object> getJaneSmithPropertiesExpectedDump() { expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);
// Methods
SortedMap<String, Object> methodsExpectedDump = new TreeMap<String, Object>();
expectedDump.put(Key.METHODS.toString(), methodsExpectedDump);
return expectedDump;
}
private Map<String, Object> getJaneSmithExpectedDump(int exposureLevel) {
Map<String, Object> expectedDump = new HashMap<String, Object>();
SortedMap<String, Object> propertiesExpectedDump = new TreeMap<String, Object>(); SortedMap<String, Object> propertiesExpectedDump = new TreeMap<String, Object>();
// Properties
if (exposureLevel != BeansWrapper.EXPOSE_NOTHING) {
// Map<String, Object> birthdateExpectedDump = new HashMap<String, Object>(); // Map<String, Object> birthdateExpectedDump = new HashMap<String, Object>();
// birthdateExpectedDump.put(Key.TYPE.toString(), Type.DATE); // birthdateExpectedDump.put(Key.TYPE.toString(), Type.DATE);
// birthdateExpectedDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN); // birthdateExpectedDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN);
@ -1092,7 +1068,14 @@ public class DumpDirectiveTest {
favoriteColorListExpectedDump.add(color2ExpectedDump); favoriteColorListExpectedDump.add(color2ExpectedDump);
favoriteColorsExpectedDump.put(Key.VALUE.toString(), favoriteColorListExpectedDump); favoriteColorsExpectedDump.put(Key.VALUE.toString(), favoriteColorListExpectedDump);
propertiesExpectedDump.put("favoriteColors", favoriteColorsExpectedDump); propertiesExpectedDump.put("favoriteColors", favoriteColorsExpectedDump);
}
expectedDump.put(Key.PROPERTIES.toString(), propertiesExpectedDump);
return propertiesExpectedDump; // Methods
SortedMap<String, Object> methodsExpectedDump = new TreeMap<String, Object>();
expectedDump.put(Key.METHODS.toString(), methodsExpectedDump);
return expectedDump;
} }
} }