Fix bug in dump methods that generated an error on individual page when getting dump value for individual.mostSpecificTypes

This commit is contained in:
ryounes 2011-08-02 15:47:50 +00:00
parent 4d212a4aec
commit 5dd5fe6c45
2 changed files with 104 additions and 32 deletions

View file

@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.ext.beans.CollectionModel;
import freemarker.ext.beans.StringModel; import freemarker.ext.beans.StringModel;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateBooleanModel; import freemarker.template.TemplateBooleanModel;
@ -169,7 +170,11 @@ public abstract class BaseDumpDirective implements TemplateDirectiveModel {
if (model != null) { if (model != null) {
if ( model instanceof TemplateSequenceModel ) { if ( model instanceof TemplateSequenceModel ) {
if (model instanceof CollectionModel && ! ((CollectionModel)model).getSupportsIndexedAccess()) {
map.putAll( getTemplateModelDump( ( TemplateCollectionModel)model ) );
} else {
map.putAll( getTemplateModelDump( ( TemplateSequenceModel)model ) ); map.putAll( getTemplateModelDump( ( TemplateSequenceModel)model ) );
}
} else if ( model instanceof TemplateNumberModel ) { } else if ( model instanceof TemplateNumberModel ) {
map.putAll( getTemplateModelDump( (TemplateNumberModel)model ) ); map.putAll( getTemplateModelDump( (TemplateNumberModel)model ) );

View file

@ -28,12 +28,14 @@ import org.apache.log4j.Logger;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import freemarker.core.CollectionAndSequence;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.ext.beans.BeansWrapper; import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.CollectionModel;
import freemarker.ext.dump.BaseDumpDirective.DateType; import freemarker.ext.dump.BaseDumpDirective.DateType;
import freemarker.ext.dump.BaseDumpDirective.Key; import freemarker.ext.dump.BaseDumpDirective.Key;
import freemarker.ext.dump.BaseDumpDirective.Value;
import freemarker.ext.dump.BaseDumpDirective.Type; import freemarker.ext.dump.BaseDumpDirective.Type;
import freemarker.ext.dump.BaseDumpDirective.Value;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.SimpleCollection; import freemarker.template.SimpleCollection;
import freemarker.template.Template; import freemarker.template.Template;
@ -445,14 +447,14 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE); expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE);
List<Map<String, Object>> myArrayexpectedDumpValue = new ArrayList<Map<String, Object>>(myArray.length); List<Map<String, Object>> myArrayExpectedDumpValue = new ArrayList<Map<String, Object>>(myArray.length);
for ( String str : myArray) { for ( String str : myArray) {
Map<String, Object> itemDump = new HashMap<String, Object>(); Map<String, Object> itemDump = new HashMap<String, Object>();
itemDump.put(Key.TYPE.toString(), Type.STRING); itemDump.put(Key.TYPE.toString(), Type.STRING);
itemDump.put(Key.VALUE.toString(), str); itemDump.put(Key.VALUE.toString(), str);
myArrayexpectedDumpValue.add(itemDump); myArrayExpectedDumpValue.add(itemDump);
} }
expectedDumpValue.put(Key.VALUE.toString(), myArrayexpectedDumpValue); expectedDumpValue.put(Key.VALUE.toString(), myArrayExpectedDumpValue);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -488,36 +490,36 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE); expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE);
List<Map<String, Object>> mixedListexpectedDumpValue = new ArrayList<Map<String, Object>>(mixedList.size()); List<Map<String, Object>> mixedListExpectedDumpValue = new ArrayList<Map<String, Object>>(mixedList.size());
Map<String, Object> myStringexpectedDumpValue = new HashMap<String, Object>(); Map<String, Object> myStringExpectedDumpValue = new HashMap<String, Object>();
myStringexpectedDumpValue.put(Key.TYPE.toString(), Type.STRING); myStringExpectedDumpValue.put(Key.TYPE.toString(), Type.STRING);
myStringexpectedDumpValue.put(Key.VALUE.toString(), myString); myStringExpectedDumpValue.put(Key.VALUE.toString(), myString);
mixedListexpectedDumpValue.add(myStringexpectedDumpValue); mixedListExpectedDumpValue.add(myStringExpectedDumpValue);
Map<String, Object> myIntexpectedDumpValue = new HashMap<String, Object>(); Map<String, Object> myIntExpectedDumpValue = new HashMap<String, Object>();
myIntexpectedDumpValue.put(Key.TYPE.toString(), Type.NUMBER); myIntExpectedDumpValue.put(Key.TYPE.toString(), Type.NUMBER);
myIntexpectedDumpValue.put(Key.VALUE.toString(), myInt); myIntExpectedDumpValue.put(Key.VALUE.toString(), myInt);
mixedListexpectedDumpValue.add(myIntexpectedDumpValue); mixedListExpectedDumpValue.add(myIntExpectedDumpValue);
Map<String, Object> myBoolexpectedDumpValue = new HashMap<String, Object>(); Map<String, Object> myBoolExpectedDumpValue = new HashMap<String, Object>();
myBoolexpectedDumpValue.put(Key.TYPE.toString(), Type.BOOLEAN); myBoolExpectedDumpValue.put(Key.TYPE.toString(), Type.BOOLEAN);
myBoolexpectedDumpValue.put(Key.VALUE.toString(), myBool); myBoolExpectedDumpValue.put(Key.VALUE.toString(), myBool);
mixedListexpectedDumpValue.add(myBoolexpectedDumpValue); mixedListExpectedDumpValue.add(myBoolExpectedDumpValue);
Map<String, Object> myListexpectedDumpValue = new HashMap<String, Object>(); Map<String, Object> myListExpectedDumpValue = new HashMap<String, Object>();
myListexpectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE); myListExpectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE);
List<Map<String, Object>> myListItemsexpectedDumpValue = new ArrayList<Map<String, Object>>(myList.size()); List<Map<String, Object>> myListItemsExpectedDumpValue = new ArrayList<Map<String, Object>>(myList.size());
for ( String animal : myList ) { for ( String animal : myList ) {
Map<String, Object> itemDump = new HashMap<String, Object>(); Map<String, Object> itemDump = new HashMap<String, Object>();
itemDump.put(Key.TYPE.toString(), Type.STRING); itemDump.put(Key.TYPE.toString(), Type.STRING);
itemDump.put(Key.VALUE.toString(), animal); itemDump.put(Key.VALUE.toString(), animal);
myListItemsexpectedDumpValue.add(itemDump); myListItemsExpectedDumpValue.add(itemDump);
} }
myListexpectedDumpValue.put(Key.VALUE.toString(), myListItemsexpectedDumpValue); myListExpectedDumpValue.put(Key.VALUE.toString(), myListItemsExpectedDumpValue);
mixedListexpectedDumpValue.add(myListexpectedDumpValue); mixedListExpectedDumpValue.add(myListExpectedDumpValue);
expectedDumpValue.put(Key.VALUE.toString(), mixedListexpectedDumpValue); expectedDumpValue.put(Key.VALUE.toString(), mixedListExpectedDumpValue);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -541,14 +543,14 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE); expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE);
List<Map<String, Object>> myIntSetexpectedDumpValue = new ArrayList<Map<String, Object>>(myIntSet.size()); List<Map<String, Object>> myIntSetExpectedDumpValue = new ArrayList<Map<String, Object>>(myIntSet.size());
for ( int i : myIntSet ) { for ( int i : myIntSet ) {
Map<String, Object> itemDump = new HashMap<String, Object>(); Map<String, Object> itemDump = new HashMap<String, Object>();
itemDump.put(Key.TYPE.toString(), Type.NUMBER); itemDump.put(Key.TYPE.toString(), Type.NUMBER);
itemDump.put(Key.VALUE.toString(), i); itemDump.put(Key.VALUE.toString(), i);
myIntSetexpectedDumpValue.add(itemDump); myIntSetExpectedDumpValue.add(itemDump);
} }
expectedDumpValue.put(Key.VALUE.toString(), myIntSetexpectedDumpValue); expectedDumpValue.put(Key.VALUE.toString(), myIntSetExpectedDumpValue);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);
@ -557,7 +559,7 @@ public class DumpDirectiveTest {
} }
@Test @Test
public void dumpNumberCollection() { public void dumpSimpleCollection() {
String varName = "oddNums"; String varName = "oddNums";
Map<String, Object> dataModel = new HashMap<String, Object>(); Map<String, Object> dataModel = new HashMap<String, Object>();
@ -573,14 +575,79 @@ public class DumpDirectiveTest {
Map<String, Object> expectedDumpValue = new HashMap<String, Object>(); Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.COLLECTION); expectedDumpValue.put(Key.TYPE.toString(), Type.COLLECTION);
List<Map<String, Object>> myCollectionexpectedDumpValue = new ArrayList<Map<String, Object>>(odds.size()); List<Map<String, Object>> myCollectionExpectedDumpValue = new ArrayList<Map<String, Object>>(odds.size());
for ( int i : odds ) { for ( int i : odds ) {
Map<String, Object> itemDump = new HashMap<String, Object>(); Map<String, Object> itemDump = new HashMap<String, Object>();
itemDump.put(Key.TYPE.toString(), Type.NUMBER); itemDump.put(Key.TYPE.toString(), Type.NUMBER);
itemDump.put(Key.VALUE.toString(), i); itemDump.put(Key.VALUE.toString(), i);
myCollectionexpectedDumpValue.add(itemDump); myCollectionExpectedDumpValue.add(itemDump);
} }
expectedDumpValue.put(Key.VALUE.toString(), myCollectionexpectedDumpValue); expectedDumpValue.put(Key.VALUE.toString(), myCollectionExpectedDumpValue);
Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue);
test(varName, dataModel, expectedDump);
}
@Test
public void dumpCollectionModel() {
String varName = "oddNums";
Map<String, Object> dataModel = new HashMap<String, Object>();
Set<Integer> odds = new HashSet<Integer>();
for (int i=0; i <= 10; i++) {
if (i % 2 == 1) {
odds.add(i);
}
}
TemplateCollectionModel myCollection = new CollectionModel(odds, new BeansWrapper());
dataModel.put(varName, myCollection);
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.COLLECTION);
List<Map<String, Object>> myCollectionExpectedDumpValue = new ArrayList<Map<String, Object>>(odds.size());
for ( int i : odds ) {
Map<String, Object> itemDump = new HashMap<String, Object>();
itemDump.put(Key.TYPE.toString(), Type.NUMBER);
itemDump.put(Key.VALUE.toString(), i);
myCollectionExpectedDumpValue.add(itemDump);
}
expectedDumpValue.put(Key.VALUE.toString(), myCollectionExpectedDumpValue);
Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue);
test(varName, dataModel, expectedDump);
}
@Test
public void dumpCollectionAndSequenceModel() {
String varName = "oddNums";
Map<String, Object> dataModel = new HashMap<String, Object>();
Set<Integer> odds = new HashSet<Integer>();
for (int i=0; i <= 10; i++) {
if (i % 2 == 1) {
odds.add(i);
}
}
TemplateCollectionModel coll = new CollectionModel(odds, new BeansWrapper());
TemplateCollectionModel myCollection = new CollectionAndSequence(coll);
dataModel.put(varName, myCollection);
Map<String, Object> expectedDumpValue = new HashMap<String, Object>();
expectedDumpValue.put(Key.TYPE.toString(), Type.SEQUENCE);
List<Map<String, Object>> myCollectionExpectedDumpValue = new ArrayList<Map<String, Object>>(odds.size());
for ( int i : odds ) {
Map<String, Object> itemDump = new HashMap<String, Object>();
itemDump.put(Key.TYPE.toString(), Type.NUMBER);
itemDump.put(Key.VALUE.toString(), i);
myCollectionExpectedDumpValue.add(itemDump);
}
expectedDumpValue.put(Key.VALUE.toString(), myCollectionExpectedDumpValue);
Map<String, Object> expectedDump = new HashMap<String, Object>(); Map<String, Object> expectedDump = new HashMap<String, Object>();
expectedDump.put(varName, expectedDumpValue); expectedDump.put(varName, expectedDumpValue);