NIHVIVO-2378 Implement dumpAll directive in freemarker dump package
This commit is contained in:
parent
fbd5496514
commit
5152f971e7
3 changed files with 102 additions and 23 deletions
|
@ -8,6 +8,8 @@ import java.util.Collections;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
@ -45,32 +47,24 @@ public class DumpAllDirective extends BaseDumpDirective {
|
|||
"The dump directive doesn't allow nested content.");
|
||||
}
|
||||
|
||||
SortedMap<String, Object> dataModelDump = getDataModelDump(env);
|
||||
dump("dumpAll.ftl", dataModelDump, env);
|
||||
}
|
||||
|
||||
SortedMap<String, Object> getDataModelDump(Environment env) throws TemplateModelException {
|
||||
SortedMap<String, Object> dump = new TreeMap<String, Object>();
|
||||
TemplateHashModel dataModel = env.getDataModel();
|
||||
// Need to unwrap in order to iterate through the variables
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> unwrappedDataModel = (Map<String, Object>) DeepUnwrap.permissiveUnwrap(dataModel);
|
||||
List<String> varNames = new ArrayList<String>(unwrappedDataModel.keySet());
|
||||
Collections.sort(varNames);
|
||||
|
||||
// *** RY Change data structure so it's
|
||||
// "employee" => { type =>..., value => ...}
|
||||
// rather than { name => "employee", type => ..., value => ...}
|
||||
// Then this will be a SortedMap
|
||||
// Otherwise we need a Comparator to sort on the "name" key. Yuck!
|
||||
// The first data structure seems more natural
|
||||
List<Map<String, Object>> models = new ArrayList<Map<String, Object>>();
|
||||
for (String varName : varNames) {
|
||||
models.add(getTemplateVariableDump(varName, dataModel.get(varName)));
|
||||
}
|
||||
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
map.put("templateName", env.getTemplate().getName());
|
||||
|
||||
|
||||
map.put("models", models);
|
||||
|
||||
dump("dumpAll.ftl", map, env);
|
||||
dump.putAll(getTemplateVariableDump(varName, dataModel.get(varName)));
|
||||
}
|
||||
|
||||
return dump;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,13 @@ import static junit.framework.Assert.fail;
|
|||
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
|
@ -15,6 +21,9 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
|
||||
import freemarker.core.Environment;
|
||||
import freemarker.ext.dump.BaseDumpDirective.DateType;
|
||||
import freemarker.ext.dump.BaseDumpDirective.Key;
|
||||
import freemarker.ext.dump.BaseDumpDirective.Type;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
|
||||
|
@ -38,11 +47,87 @@ public class DumpAllDirectiveTest {
|
|||
@Test
|
||||
public void dumpDataModel() {
|
||||
|
||||
Map<String, Object> dataModel = new HashMap<String, Object>();
|
||||
|
||||
String stringName = "dog";
|
||||
String stringVal = "Rover";
|
||||
dataModel.put(stringName, stringVal);
|
||||
|
||||
String boolName = "isLoggedIn";
|
||||
boolean boolVal = true;
|
||||
dataModel.put(boolName, boolVal);
|
||||
|
||||
String intName = "tabCount";
|
||||
int intVal = 7;
|
||||
dataModel.put(intName, intVal);
|
||||
|
||||
String dateName = "now";
|
||||
Date dateVal = new Date();
|
||||
dataModel.put(dateName, dateVal);
|
||||
|
||||
String listName = "fruit";
|
||||
List<String> listVal = new ArrayList<String>();
|
||||
listVal.add("apples");
|
||||
listVal.add("bananas");
|
||||
listVal.add("oranges");
|
||||
dataModel.put(listName, listVal);
|
||||
|
||||
SortedMap<String, Object> expectedDump = new TreeMap<String, Object>();
|
||||
|
||||
Map<String, Object> expectedStringDump = new HashMap<String, Object>();
|
||||
expectedStringDump.put(Key.TYPE.toString(), Type.STRING);
|
||||
expectedStringDump.put(Key.VALUE.toString(), stringVal);
|
||||
expectedDump.put(stringName, expectedStringDump);
|
||||
|
||||
Map<String, Object> expectedBoolDump = new HashMap<String, Object>();
|
||||
expectedBoolDump.put(Key.TYPE.toString(), Type.BOOLEAN);
|
||||
expectedBoolDump.put(Key.VALUE.toString(), boolVal);
|
||||
expectedDump.put(boolName, expectedBoolDump);
|
||||
|
||||
Map<String, Object> expectedIntDump = new HashMap<String, Object>();
|
||||
expectedIntDump.put(Key.TYPE.toString(), Type.NUMBER);
|
||||
expectedIntDump.put(Key.VALUE.toString(), intVal);
|
||||
expectedDump.put(intName, expectedIntDump);
|
||||
|
||||
Map<String, Object> expectedDateDump = new HashMap<String, Object>();
|
||||
expectedDateDump.put(Key.TYPE.toString(), Type.DATE);
|
||||
expectedDateDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN);
|
||||
expectedDateDump.put(Key.VALUE.toString(), dateVal);
|
||||
expectedDump.put(dateName, expectedDateDump);
|
||||
|
||||
Map<String, Object> expectedListDump = new HashMap<String, Object>();
|
||||
expectedListDump.put(Key.TYPE.toString(), Type.SEQUENCE);
|
||||
List<Map<String, Object>> listItemsExpectedDump = new ArrayList<Map<String, Object>>(listVal.size());
|
||||
for ( String str : listVal ) {
|
||||
Map<String, Object> itemDump = new HashMap<String, Object>();
|
||||
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
||||
itemDump.put(Key.VALUE.toString(), str);
|
||||
listItemsExpectedDump.add(itemDump);
|
||||
}
|
||||
expectedListDump.put(Key.VALUE.toString(), listItemsExpectedDump);
|
||||
expectedDump.put(listName, expectedListDump);
|
||||
|
||||
SortedMap<String, Object> dump = getDump(dataModel);
|
||||
assertEquals(expectedDump, dump);
|
||||
|
||||
// Test sorting of the data model
|
||||
List<String> expectedKeys = new ArrayList<String>(expectedDump.keySet());
|
||||
List<String> actualKeys = new ArrayList<String>(dump.keySet());
|
||||
assertEquals(expectedKeys, actualKeys);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////// Private stub classes and helper methods ///////////////////////////
|
||||
|
||||
/////////////////////////// Private helper methods ///////////////////////////
|
||||
|
||||
private SortedMap<String, Object> getDump(Map<String, Object> dataModel) {
|
||||
try {
|
||||
Environment env = template.createProcessingEnvironment(dataModel, new StringWriter());
|
||||
return new DumpAllDirective().getDataModelDump(env);
|
||||
} catch (Exception e) {
|
||||
fail(e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -358,14 +358,14 @@ public class DumpDirectiveTest {
|
|||
|
||||
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
|
||||
expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE);
|
||||
List<Map<String, Object>> myListexpectedDumpValue = new ArrayList<Map<String, Object>>(myList.size());
|
||||
List<Map<String, Object>> myListItemsExpectedDump = new ArrayList<Map<String, Object>>(myList.size());
|
||||
for ( String str : myList) {
|
||||
Map<String, Object> itemDump = new HashMap<String, Object>();
|
||||
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
||||
itemDump.put(Key.VALUE.toString(), str);
|
||||
myListexpectedDumpValue.add(itemDump);
|
||||
myListItemsExpectedDump.add(itemDump);
|
||||
}
|
||||
expectedDumpValue.put(Key.VALUE.toString(), myListexpectedDumpValue);
|
||||
expectedDumpValue.put(Key.VALUE.toString(), myListItemsExpectedDump);
|
||||
|
||||
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
||||
expectedDump.put(varName, expectedDumpValue);
|
||||
|
@ -707,7 +707,7 @@ public class DumpDirectiveTest {
|
|||
}
|
||||
|
||||
|
||||
/////////////////////////// Private stub classes and helper methods ///////////////////////////
|
||||
/////////////////////////// Private test classes and helper methods ///////////////////////////
|
||||
|
||||
private void test(String varName, Map<String, Object> dataModel, Map<String, Object> expectedDump) {
|
||||
Map<String, Object> dump = getDump(varName, dataModel);
|
||||
|
|
Loading…
Add table
Reference in a new issue