From 5152f971e7b757ee4b6ea3bdf184bfebd9fe051c Mon Sep 17 00:00:00 2001 From: ryounes Date: Wed, 20 Apr 2011 14:42:32 +0000 Subject: [PATCH] NIHVIVO-2378 Implement dumpAll directive in freemarker dump package --- .../freemarker/ext/dump/DumpAllDirective.java | 28 +++--- .../ext/dump/DumpAllDirectiveTest.java | 89 ++++++++++++++++++- .../ext/dump/DumpDirectiveTest.java | 8 +- 3 files changed, 102 insertions(+), 23 deletions(-) diff --git a/webapp/src/freemarker/ext/dump/DumpAllDirective.java b/webapp/src/freemarker/ext/dump/DumpAllDirective.java index beda35f49..7f0f7ae8b 100644 --- a/webapp/src/freemarker/ext/dump/DumpAllDirective.java +++ b/webapp/src/freemarker/ext/dump/DumpAllDirective.java @@ -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 dataModelDump = getDataModelDump(env); + dump("dumpAll.ftl", dataModelDump, env); + } + + SortedMap getDataModelDump(Environment env) throws TemplateModelException { + SortedMap dump = new TreeMap(); TemplateHashModel dataModel = env.getDataModel(); // Need to unwrap in order to iterate through the variables @SuppressWarnings("unchecked") Map unwrappedDataModel = (Map) DeepUnwrap.permissiveUnwrap(dataModel); - List varNames = new ArrayList(unwrappedDataModel.keySet()); - Collections.sort(varNames); + List varNames = new ArrayList(unwrappedDataModel.keySet()); - // *** 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> models = new ArrayList>(); for (String varName : varNames) { - models.add(getTemplateVariableDump(varName, dataModel.get(varName))); + dump.putAll(getTemplateVariableDump(varName, dataModel.get(varName))); } - Map map = new HashMap(); - map.put("templateName", env.getTemplate().getName()); + return dump; - - map.put("models", models); - - dump("dumpAll.ftl", map, env); } - } diff --git a/webapp/test/freemarker/ext/dump/DumpAllDirectiveTest.java b/webapp/test/freemarker/ext/dump/DumpAllDirectiveTest.java index 62554d916..336c5c1f7 100644 --- a/webapp/test/freemarker/ext/dump/DumpAllDirectiveTest.java +++ b/webapp/test/freemarker/ext/dump/DumpAllDirectiveTest.java @@ -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; @@ -37,12 +46,88 @@ public class DumpAllDirectiveTest { @Test public void dumpDataModel() { + + Map dataModel = new HashMap(); + + 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 listVal = new ArrayList(); + listVal.add("apples"); + listVal.add("bananas"); + listVal.add("oranges"); + dataModel.put(listName, listVal); + + SortedMap expectedDump = new TreeMap(); + + Map expectedStringDump = new HashMap(); + expectedStringDump.put(Key.TYPE.toString(), Type.STRING); + expectedStringDump.put(Key.VALUE.toString(), stringVal); + expectedDump.put(stringName, expectedStringDump); + + Map expectedBoolDump = new HashMap(); + expectedBoolDump.put(Key.TYPE.toString(), Type.BOOLEAN); + expectedBoolDump.put(Key.VALUE.toString(), boolVal); + expectedDump.put(boolName, expectedBoolDump); + + Map expectedIntDump = new HashMap(); + expectedIntDump.put(Key.TYPE.toString(), Type.NUMBER); + expectedIntDump.put(Key.VALUE.toString(), intVal); + expectedDump.put(intName, expectedIntDump); + + Map expectedDateDump = new HashMap(); + 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 expectedListDump = new HashMap(); + expectedListDump.put(Key.TYPE.toString(), Type.SEQUENCE); + List> listItemsExpectedDump = new ArrayList>(listVal.size()); + for ( String str : listVal ) { + Map itemDump = new HashMap(); + 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 dump = getDump(dataModel); + assertEquals(expectedDump, dump); + + // Test sorting of the data model + List expectedKeys = new ArrayList(expectedDump.keySet()); + List actualKeys = new ArrayList(dump.keySet()); + assertEquals(expectedKeys, actualKeys); } - /////////////////////////// Private stub classes and helper methods /////////////////////////// + /////////////////////////// Private helper methods /////////////////////////// - + private SortedMap getDump(Map dataModel) { + try { + Environment env = template.createProcessingEnvironment(dataModel, new StringWriter()); + return new DumpAllDirective().getDataModelDump(env); + } catch (Exception e) { + fail(e.getMessage()); + return null; + } + } } diff --git a/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java b/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java index 1adb4b124..e5fb658c0 100644 --- a/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java +++ b/webapp/test/freemarker/ext/dump/DumpDirectiveTest.java @@ -358,14 +358,14 @@ public class DumpDirectiveTest { Map expectedDumpValue = new HashMap(); expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE); - List> myListexpectedDumpValue = new ArrayList>(myList.size()); + List> myListItemsExpectedDump = new ArrayList>(myList.size()); for ( String str : myList) { Map itemDump = new HashMap(); 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 expectedDump = new HashMap(); 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 dataModel, Map expectedDump) { Map dump = getDump(varName, dataModel);