2011-04-07 16:15:49 +00:00
|
|
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
|
|
|
|
|
|
package freemarker.ext.dump;
|
|
|
|
|
2011-04-08 17:23:16 +00:00
|
|
|
import static junit.framework.Assert.assertEquals;
|
|
|
|
import static junit.framework.Assert.fail;
|
|
|
|
|
2011-04-07 16:15:49 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.io.StringWriter;
|
2011-04-08 21:23:32 +00:00
|
|
|
import java.sql.Time;
|
|
|
|
import java.sql.Timestamp;
|
2011-04-08 16:50:33 +00:00
|
|
|
import java.util.ArrayList;
|
2011-04-11 22:05:36 +00:00
|
|
|
import java.util.Calendar;
|
2011-04-08 21:23:32 +00:00
|
|
|
import java.util.Date;
|
2011-04-07 16:15:49 +00:00
|
|
|
import java.util.HashMap;
|
2011-04-11 15:32:12 +00:00
|
|
|
import java.util.HashSet;
|
2011-04-07 16:15:49 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2011-04-11 15:32:12 +00:00
|
|
|
import java.util.Set;
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-08 17:23:16 +00:00
|
|
|
import org.apache.log4j.Level;
|
|
|
|
import org.apache.log4j.Logger;
|
2011-04-07 16:15:49 +00:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import freemarker.core.Environment;
|
2011-04-11 22:05:36 +00:00
|
|
|
import freemarker.ext.dump.BaseDumpDirective.Type;
|
|
|
|
import freemarker.ext.dump.BaseDumpDirective.DateType;
|
|
|
|
import freemarker.ext.dump.BaseDumpDirective.Key;
|
2011-04-07 16:15:49 +00:00
|
|
|
import freemarker.template.Configuration;
|
2011-04-11 15:32:12 +00:00
|
|
|
import freemarker.template.SimpleCollection;
|
2011-04-07 16:15:49 +00:00
|
|
|
import freemarker.template.Template;
|
2011-04-11 15:32:12 +00:00
|
|
|
import freemarker.template.TemplateCollectionModel;
|
2011-04-07 16:15:49 +00:00
|
|
|
import freemarker.template.TemplateDirectiveBody;
|
|
|
|
import freemarker.template.TemplateDirectiveModel;
|
|
|
|
import freemarker.template.TemplateException;
|
|
|
|
import freemarker.template.TemplateMethodModel;
|
|
|
|
import freemarker.template.TemplateModel;
|
|
|
|
import freemarker.template.TemplateModelException;
|
|
|
|
|
|
|
|
public class DumpDirectiveTest {
|
|
|
|
|
|
|
|
private Template template;
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
|
|
|
|
|
2011-04-07 16:15:49 +00:00
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
Configuration config = new Configuration();
|
|
|
|
String templateStr = "";
|
|
|
|
try {
|
|
|
|
template = new Template("template", new StringReader(templateStr), config);
|
|
|
|
} catch (Exception e) {
|
2011-04-08 16:50:33 +00:00
|
|
|
fail(e.getMessage());
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
2011-04-08 17:23:16 +00:00
|
|
|
// Turn off log messages to console
|
|
|
|
Logger.getLogger(BaseDumpDirective.class).setLevel(Level.OFF);
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpString() {
|
|
|
|
|
|
|
|
String varName = "dog";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
String value = "Rover";
|
2011-04-07 16:15:49 +00:00
|
|
|
dataModel.put(varName, value);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
expectedDump.put(Key.VALUE.toString(), value);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpBoolean() {
|
|
|
|
|
2011-04-11 15:32:12 +00:00
|
|
|
String varName = "isLoggedIn";
|
2011-04-07 16:15:49 +00:00
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
boolean value = true;
|
2011-04-07 16:15:49 +00:00
|
|
|
dataModel.put(varName, value);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.BOOLEAN);
|
|
|
|
expectedDump.put(Key.VALUE.toString(), value);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpNumber() {
|
|
|
|
|
|
|
|
String varName = "tabCount";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
int value = 7;
|
2011-04-07 16:15:49 +00:00
|
|
|
dataModel.put(varName, value);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.NUMBER);
|
|
|
|
expectedDump.put(Key.VALUE.toString(), value);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2011-04-08 21:23:32 +00:00
|
|
|
public void dumpSimpleDate() {
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-08 21:23:32 +00:00
|
|
|
String varName = "now";
|
2011-04-07 16:15:49 +00:00
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
Date now = new Date();
|
2011-04-08 21:23:32 +00:00
|
|
|
dataModel.put(varName, now);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.DATE);
|
|
|
|
expectedDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN);
|
|
|
|
expectedDump.put(Key.VALUE.toString(), now);
|
2011-04-08 21:23:32 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 21:23:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpDateTime() {
|
|
|
|
|
|
|
|
String varName = "timestamp";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
Timestamp ts = new Timestamp(1302297332043L);
|
2011-04-08 21:23:32 +00:00
|
|
|
dataModel.put(varName, ts);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.DATE);
|
|
|
|
expectedDump.put(Key.DATE_TYPE.toString(), DateType.DATETIME);
|
|
|
|
expectedDump.put(Key.VALUE.toString(), ts);
|
2011-04-08 21:23:32 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 21:23:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpSqlDate() {
|
|
|
|
|
|
|
|
String varName = "date";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
java.sql.Date date = new java.sql.Date(1302297332043L);
|
2011-04-08 21:23:32 +00:00
|
|
|
dataModel.put(varName, date);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.DATE);
|
|
|
|
expectedDump.put(Key.DATE_TYPE.toString(), DateType.DATE);
|
|
|
|
expectedDump.put(Key.VALUE.toString(), date);
|
2011-04-08 21:23:32 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 21:23:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpTime() {
|
|
|
|
|
|
|
|
String varName = "time";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
Time time = new Time(1302297332043L);
|
2011-04-08 21:23:32 +00:00
|
|
|
dataModel.put(varName, time);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.DATE);
|
|
|
|
expectedDump.put(Key.DATE_TYPE.toString(), DateType.TIME);
|
|
|
|
expectedDump.put(Key.VALUE.toString(), time);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
2011-04-08 21:23:32 +00:00
|
|
|
|
2011-04-07 16:15:49 +00:00
|
|
|
// RY test method and directive types with and without help methods
|
|
|
|
|
|
|
|
@Test
|
2011-04-08 16:50:33 +00:00
|
|
|
public void dumpHelplessMethod() {
|
|
|
|
|
|
|
|
String varName = "square";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
TemplateMethodModel methodModel = new HelplessMethod();
|
|
|
|
dataModel.put(varName, methodModel);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.METHOD);
|
|
|
|
expectedDump.put("help", null);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 16:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpHelpfulMethod() {
|
|
|
|
|
|
|
|
String varName = "square";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
TemplateMethodModel methodModel = new HelpfulMethod();
|
|
|
|
dataModel.put(varName, methodModel);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.METHOD);
|
|
|
|
expectedDump.put("help", getMethodHelp(varName));
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 16:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpMethodWithBadHelp() {
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-08 16:50:33 +00:00
|
|
|
String varName = "square";
|
2011-04-07 16:15:49 +00:00
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
TemplateMethodModel methodModel = new MethodWithBadHelp();
|
|
|
|
dataModel.put(varName, methodModel);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.METHOD);
|
|
|
|
expectedDump.put("help", null);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 16:50:33 +00:00
|
|
|
}
|
2011-04-08 17:23:16 +00:00
|
|
|
|
2011-04-08 16:50:33 +00:00
|
|
|
@Test
|
|
|
|
public void dumpHelplessDirective() {
|
|
|
|
|
|
|
|
String varName = "dump";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
TemplateDirectiveModel directiveModel = new HelplessDirective();
|
|
|
|
dataModel.put(varName, directiveModel);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.DIRECTIVE);
|
|
|
|
expectedDump.put("help", null);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2011-04-08 16:50:33 +00:00
|
|
|
public void dumpHelpfulDirective() {
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 15:32:12 +00:00
|
|
|
String varName = "dump";
|
2011-04-07 16:15:49 +00:00
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
TemplateDirectiveModel directiveModel = new HelpfulDirective();
|
|
|
|
dataModel.put(varName, directiveModel);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.DIRECTIVE);
|
|
|
|
expectedDump.put("help", getDirectiveHelp(varName));
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
2011-04-08 16:50:33 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpDirectiveWithBadHelp() {
|
|
|
|
|
|
|
|
String varName = "dump";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
TemplateDirectiveModel directiveModel = new DirectiveWithBadHelp();
|
|
|
|
dataModel.put(varName, directiveModel);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.DIRECTIVE);
|
|
|
|
expectedDump.put("help", null);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 16:50:33 +00:00
|
|
|
}
|
2011-04-08 20:15:13 +00:00
|
|
|
|
2011-04-07 16:15:49 +00:00
|
|
|
@Test
|
2011-04-08 21:23:32 +00:00
|
|
|
public void dumpStringList() {
|
2011-04-08 17:23:16 +00:00
|
|
|
|
2011-04-11 15:32:12 +00:00
|
|
|
String varName = "fruit";
|
2011-04-08 17:23:16 +00:00
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-08 17:23:16 +00:00
|
|
|
List<String> list = new ArrayList<String>();
|
|
|
|
list.add("apples");
|
|
|
|
list.add("bananas");
|
|
|
|
list.add("oranges");
|
|
|
|
dataModel.put(varName, list);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.SEQUENCE);
|
|
|
|
List<Map<String, Object>> listDump = new ArrayList<Map<String, Object>>(list.size());
|
2011-04-08 17:23:16 +00:00
|
|
|
for ( String str : list) {
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
itemDump.put(Key.VALUE.toString(), str);
|
|
|
|
listDump.add(itemDump);
|
2011-04-08 17:23:16 +00:00
|
|
|
}
|
2011-04-11 22:05:36 +00:00
|
|
|
expectedDump.put(Key.VALUE.toString(), listDump);
|
2011-04-08 17:23:16 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 17:23:16 +00:00
|
|
|
}
|
2011-04-08 20:15:13 +00:00
|
|
|
|
2011-04-08 17:23:16 +00:00
|
|
|
@Test
|
2011-04-08 21:23:32 +00:00
|
|
|
public void dumpStringArray() {
|
2011-04-11 15:32:12 +00:00
|
|
|
|
|
|
|
String varName = "fruit";
|
2011-04-08 20:15:13 +00:00
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
String[] arr = { "apples", "bananas", "oranges" };
|
|
|
|
dataModel.put(varName, arr);
|
|
|
|
|
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.SEQUENCE);
|
|
|
|
List<Map<String, Object>> arrDump = new ArrayList<Map<String, Object>>(arr.length);
|
|
|
|
for ( String str : arr) {
|
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
itemDump.put(Key.VALUE.toString(), str);
|
|
|
|
arrDump.add(itemDump);
|
2011-04-08 20:15:13 +00:00
|
|
|
}
|
2011-04-11 22:05:36 +00:00
|
|
|
expectedDump.put(Key.VALUE.toString(), arrDump);
|
2011-04-08 17:23:16 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 20:15:13 +00:00
|
|
|
}
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-08 20:15:13 +00:00
|
|
|
@Test
|
|
|
|
public void dumpMixedList() {
|
|
|
|
|
2011-04-11 15:32:12 +00:00
|
|
|
String varName = "stuff";
|
2011-04-08 21:35:10 +00:00
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
List<Object> mixedList = new ArrayList<Object>();
|
|
|
|
|
|
|
|
String myString = "apples";
|
|
|
|
mixedList.add(myString);
|
2011-04-08 21:35:10 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
int myInt = 4;
|
|
|
|
mixedList.add(myInt);
|
2011-04-08 21:35:10 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
boolean myBool = true;
|
|
|
|
mixedList.add(myBool);
|
2011-04-08 21:35:10 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
List<String> myList = new ArrayList<String>();
|
|
|
|
myList.add("dog");
|
|
|
|
myList.add("cat");
|
|
|
|
myList.add("elephant");
|
|
|
|
mixedList.add(myList);
|
|
|
|
|
|
|
|
dataModel.put(varName, mixedList);
|
|
|
|
|
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.SEQUENCE);
|
|
|
|
|
|
|
|
List<Map<String, Object>> mixedListDump = new ArrayList<Map<String, Object>>(mixedList.size());
|
|
|
|
|
|
|
|
Map<String, Object> stringDump = new HashMap<String, Object>();
|
|
|
|
stringDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
stringDump.put(Key.VALUE.toString(), myString);
|
|
|
|
mixedListDump.add(stringDump);
|
2011-04-08 21:35:10 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> numberDump = new HashMap<String, Object>();
|
|
|
|
numberDump.put(Key.TYPE.toString(), Type.NUMBER);
|
|
|
|
numberDump.put(Key.VALUE.toString(), myInt);
|
|
|
|
mixedListDump.add(numberDump);
|
|
|
|
|
|
|
|
Map<String, Object> booleanDump = new HashMap<String, Object>();
|
|
|
|
booleanDump.put(Key.TYPE.toString(), Type.BOOLEAN);
|
|
|
|
booleanDump.put(Key.VALUE.toString(), myBool);
|
|
|
|
mixedListDump.add(booleanDump);
|
|
|
|
|
|
|
|
Map<String, Object> myListDump = new HashMap<String, Object>();
|
|
|
|
myListDump.put(Key.TYPE.toString(), Type.SEQUENCE);
|
|
|
|
List<Map<String, Object>> itemsDump = new ArrayList<Map<String, Object>>(myList.size());
|
|
|
|
for ( String animal : myList ) {
|
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
itemDump.put(Key.VALUE.toString(), animal);
|
|
|
|
itemsDump.add(itemDump);
|
2011-04-08 21:35:10 +00:00
|
|
|
}
|
2011-04-11 22:05:36 +00:00
|
|
|
myListDump.put(Key.VALUE.toString(), itemsDump);
|
|
|
|
mixedListDump.add(myListDump);
|
2011-04-08 21:35:10 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
expectedDump.put(Key.VALUE.toString(), mixedListDump);
|
2011-04-08 21:35:10 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-08 20:15:13 +00:00
|
|
|
}
|
|
|
|
|
2011-04-11 15:32:12 +00:00
|
|
|
@Test
|
|
|
|
public void dumpNumberSet() {
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dataModel.put(varName, odds);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.SEQUENCE);
|
|
|
|
List<Map<String, Object>> sequenceDump = new ArrayList<Map<String, Object>>(odds.size());
|
2011-04-11 15:32:12 +00:00
|
|
|
for ( int i : odds ) {
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.NUMBER);
|
|
|
|
itemDump.put(Key.VALUE.toString(), i);
|
|
|
|
sequenceDump.add(itemDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
}
|
2011-04-11 22:05:36 +00:00
|
|
|
expectedDump.put(Key.VALUE.toString(), sequenceDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpNumberCollection() {
|
|
|
|
|
|
|
|
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 collection = new SimpleCollection(odds);
|
|
|
|
dataModel.put(varName, collection);
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.COLLECTION);
|
|
|
|
List<Map<String, Object>> collectionDump = new ArrayList<Map<String, Object>>(odds.size());
|
2011-04-11 15:32:12 +00:00
|
|
|
for ( int i : odds ) {
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.NUMBER);
|
|
|
|
itemDump.put(Key.VALUE.toString(), i);
|
|
|
|
collectionDump.add(itemDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
}
|
2011-04-11 22:05:36 +00:00
|
|
|
expectedDump.put(Key.VALUE.toString(), collectionDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
}
|
|
|
|
|
2011-04-07 16:15:49 +00:00
|
|
|
@Test
|
|
|
|
public void dumpHash() {
|
|
|
|
|
|
|
|
}
|
2011-04-08 20:15:13 +00:00
|
|
|
|
|
|
|
// RY Do these with different BeansWrappers
|
2011-04-11 22:05:36 +00:00
|
|
|
|
2011-04-07 16:15:49 +00:00
|
|
|
@Test
|
2011-04-11 15:32:12 +00:00
|
|
|
public void dumpStringToStringMap() {
|
|
|
|
|
|
|
|
String varName = "capitals";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
Map<String, String> myMap = new HashMap<String, String>();
|
|
|
|
myMap.put("Albany", "New York");
|
|
|
|
myMap.put("St. Paul", "Minnesota");
|
|
|
|
myMap.put("Austin", "Texas");
|
|
|
|
myMap.put("Sacramento", "California");
|
|
|
|
myMap.put("Richmond", "Virginia");
|
|
|
|
dataModel.put(varName, myMap);
|
|
|
|
|
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.HASH_EX);
|
|
|
|
Map<String, Object> myMapDump = new HashMap<String, Object>(myMap.size());
|
|
|
|
for ( String key : myMap.keySet() ) {
|
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
itemDump.put(Key.VALUE.toString(), myMap.get(key));
|
|
|
|
myMapDump.put(key, itemDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
}
|
2011-04-11 22:05:36 +00:00
|
|
|
expectedDump.put(Key.VALUE.toString(), myMapDump);
|
2011-04-11 15:32:12 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
test(varName, dataModel, expectedDump);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpStringToObjectMap() {
|
|
|
|
|
|
|
|
String varName = "stuff";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
|
|
|
|
|
|
|
Map<String, Object> mixedMap = new HashMap<String, Object>();
|
|
|
|
|
|
|
|
String myString = "apples";
|
|
|
|
mixedMap.put("myString", myString);
|
|
|
|
|
|
|
|
boolean myBool = true;
|
|
|
|
mixedMap.put("myBoolean", myBool);
|
|
|
|
|
|
|
|
int myInt = 4;
|
|
|
|
mixedMap.put("myNumber", myInt);
|
|
|
|
|
|
|
|
Date now = new Date();
|
|
|
|
mixedMap.put("myDate", now);
|
|
|
|
|
|
|
|
List<String> myList = new ArrayList<String>();
|
|
|
|
myList.add("apples");
|
|
|
|
myList.add("bananas");
|
|
|
|
myList.add("oranges");
|
|
|
|
mixedMap.put("myList", myList);
|
|
|
|
|
|
|
|
Map<String, String> myMap = new HashMap<String, String>();
|
|
|
|
myMap.put("Great Expectations", "Charles Dickens");
|
|
|
|
myMap.put("Pride and Prejudice", "Jane Austen");
|
|
|
|
myMap.put("Middlemarch", "George Eliot");
|
|
|
|
myMap.put("Jude the Obscure", "Thomas Hardy");
|
|
|
|
mixedMap.put("myMap", myMap);
|
|
|
|
|
|
|
|
dataModel.put(varName, mixedMap);
|
|
|
|
|
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
expectedDump.put(Key.NAME.toString(), varName);
|
|
|
|
expectedDump.put(Key.TYPE.toString(), Type.HASH_EX);
|
|
|
|
Map<String, Object> mixedMapDump = new HashMap<String, Object>(mixedMap.size());
|
|
|
|
|
|
|
|
Map<String, Object> myStringDump = new HashMap<String, Object>();
|
|
|
|
myStringDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
myStringDump.put(Key.VALUE.toString(), myString);
|
|
|
|
mixedMapDump.put("myString", myStringDump);
|
|
|
|
|
|
|
|
Map<String, Object> myBooleanDump = new HashMap<String, Object>();
|
|
|
|
myBooleanDump.put(Key.TYPE.toString(), Type.BOOLEAN);
|
|
|
|
myBooleanDump.put(Key.VALUE.toString(), myBool);
|
|
|
|
mixedMapDump.put("myBoolean", myBooleanDump);
|
|
|
|
|
|
|
|
Map<String, Object> myNumberDump = new HashMap<String, Object>();
|
|
|
|
myNumberDump.put(Key.TYPE.toString(), Type.NUMBER);
|
|
|
|
myNumberDump.put(Key.VALUE.toString(), myInt);
|
|
|
|
mixedMapDump.put("myNumber", myNumberDump);
|
|
|
|
|
|
|
|
Map<String, Object> myDateDump = new HashMap<String, Object>();
|
|
|
|
myDateDump.put(Key.TYPE.toString(), Type.DATE);
|
|
|
|
myDateDump.put(Key.DATE_TYPE.toString(), DateType.UNKNOWN);
|
|
|
|
myDateDump.put(Key.VALUE.toString(), now);
|
|
|
|
mixedMapDump.put("myDate", myDateDump);
|
|
|
|
|
|
|
|
Map<String, Object> myListDump = new HashMap<String, Object>();
|
|
|
|
myListDump.put(Key.TYPE.toString(), Type.SEQUENCE);
|
|
|
|
List<Map<String, Object>> listItemsDump = new ArrayList<Map<String, Object>>(myList.size());
|
|
|
|
for ( String item : myList ) {
|
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
itemDump.put(Key.VALUE.toString(), item);
|
|
|
|
listItemsDump.add(itemDump);
|
|
|
|
}
|
|
|
|
myListDump.put(Key.VALUE.toString(), listItemsDump);
|
|
|
|
mixedMapDump.put("myList", myListDump);
|
|
|
|
|
|
|
|
Map<String, Object> myMapDump = new HashMap<String, Object>();
|
|
|
|
myMapDump.put(Key.TYPE.toString(), Type.HASH_EX);
|
|
|
|
Map<String, Object> mapItemsDump = new HashMap<String, Object>(myMap.size());
|
|
|
|
for ( String key : myMap.keySet() ) {
|
|
|
|
Map<String, Object> itemDump = new HashMap<String, Object>();
|
|
|
|
itemDump.put(Key.TYPE.toString(), Type.STRING);
|
|
|
|
itemDump.put(Key.VALUE.toString(), myMap.get(key));
|
|
|
|
mapItemsDump.put(key, itemDump);
|
|
|
|
}
|
|
|
|
myMapDump.put(Key.VALUE.toString(), mapItemsDump);
|
|
|
|
mixedMapDump.put("myMap", myMapDump);
|
|
|
|
|
|
|
|
expectedDump.put(Key.VALUE.toString(), mixedMapDump);
|
|
|
|
|
|
|
|
test(varName, dataModel, expectedDump);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dumpObject() {
|
|
|
|
|
|
|
|
String varName = "employee";
|
|
|
|
Map<String, Object> dataModel = new HashMap<String, Object>();
|
|
|
|
|
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
|
c.set(75, Calendar.MAY, 5);
|
|
|
|
Employee jdoe = new Employee("John Doe", c.getTime(), 34523);
|
|
|
|
|
|
|
|
c.set(65, Calendar.AUGUST, 10);
|
|
|
|
Employee jsmith = new Employee("Jane Smith", c.getTime(), 11111);
|
|
|
|
|
|
|
|
c.set(80, Calendar.JUNE, 20);
|
|
|
|
Employee mjones = new Employee("Michael Jones", c.getTime(), 22222);
|
|
|
|
|
|
|
|
c.set(81, Calendar.NOVEMBER, 30);
|
|
|
|
Employee mturner = new Employee("Mary Turner", c.getTime(), 33333);
|
|
|
|
|
|
|
|
List<Employee> supervisees = new ArrayList<Employee>();
|
|
|
|
supervisees.add(mjones);
|
|
|
|
supervisees.add(mturner);
|
|
|
|
jdoe.setSupervisor(jsmith);
|
|
|
|
jdoe.setSupervisees(supervisees);
|
|
|
|
jdoe.setSalary(65000);
|
|
|
|
|
|
|
|
dataModel.put("employee", jdoe);
|
|
|
|
|
|
|
|
Map<String, Object> expectedDump = new HashMap<String, Object>();
|
|
|
|
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
|
|
|
|
2011-04-11 15:32:12 +00:00
|
|
|
/////////////////////////// Private stub classes and helper methods ///////////////////////////
|
2011-04-08 17:23:16 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
private void test(String varName, Map<String, Object> dataModel, Map<String, Object> expectedDump) {
|
2011-04-08 17:23:16 +00:00
|
|
|
try {
|
|
|
|
Environment env = template.createProcessingEnvironment(dataModel, new StringWriter());
|
|
|
|
Map<String, Object> dumpData = new DumpDirective().getTemplateVariableData(varName, env);
|
2011-04-11 22:05:36 +00:00
|
|
|
assertEquals(expectedDump, dumpData);
|
2011-04-08 17:23:16 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
fail(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-08 16:50:33 +00:00
|
|
|
private class HelplessMethod implements TemplateMethodModel {
|
2011-04-07 16:15:49 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object exec(List arg0) throws TemplateModelException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2011-04-08 16:50:33 +00:00
|
|
|
|
|
|
|
private class HelpfulMethod implements TemplateMethodModel {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object exec(List arg0) throws TemplateModelException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> help(String name) {
|
|
|
|
return getMethodHelp(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class MethodWithBadHelp implements TemplateMethodModel {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object exec(List arg0) throws TemplateModelException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> help() {
|
|
|
|
return new HashMap<String, Object>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class HelplessDirective implements TemplateDirectiveModel {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void execute(Environment arg0, Map arg1, TemplateModel[] arg2,
|
|
|
|
TemplateDirectiveBody arg3) throws TemplateException,
|
|
|
|
IOException {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class HelpfulDirective implements TemplateDirectiveModel {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void execute(Environment arg0, Map arg1, TemplateModel[] arg2,
|
|
|
|
TemplateDirectiveBody arg3) throws TemplateException,
|
|
|
|
IOException {
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> help(String name) {
|
|
|
|
return getDirectiveHelp(name);
|
|
|
|
}
|
|
|
|
}
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-08 16:50:33 +00:00
|
|
|
private class DirectiveWithBadHelp implements TemplateDirectiveModel {
|
2011-04-07 16:15:49 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void execute(Environment arg0, Map arg1, TemplateModel[] arg2,
|
|
|
|
TemplateDirectiveBody arg3) throws TemplateException,
|
2011-04-08 16:50:33 +00:00
|
|
|
IOException {
|
|
|
|
}
|
|
|
|
|
|
|
|
public String help(String name) {
|
|
|
|
return "help";
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
2011-04-08 16:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, Object> getDirectiveHelp(String name) {
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
|
|
|
|
|
map.put("effect", "Dump the contents of a template variable.");
|
|
|
|
|
|
|
|
map.put("comments", "Sequences (lists and arrays) are enclosed in square brackets. Hashes are enclosed in curly braces.");
|
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("var", "name of variable to dump");
|
|
|
|
map.put("params", params);
|
|
|
|
|
|
|
|
List<String> examples = new ArrayList<String>();
|
|
|
|
examples.add("<@" + name + " var=\"urls\" />");
|
|
|
|
map.put("examples", examples);
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, Object> getMethodHelp(String name) {
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
map.put(Key.NAME.toString(), name);
|
2011-04-08 16:50:33 +00:00
|
|
|
|
|
|
|
map.put("returns", "The square of the argument");
|
|
|
|
|
|
|
|
List<String>params = new ArrayList<String>();
|
|
|
|
params.add("Integer to square");
|
|
|
|
map.put("params", params);
|
2011-04-07 16:15:49 +00:00
|
|
|
|
2011-04-08 16:50:33 +00:00
|
|
|
List<String> examples = new ArrayList<String>();
|
|
|
|
examples.add(name + "(4)");
|
|
|
|
map.put("examples", examples);
|
|
|
|
|
|
|
|
return map;
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
private class Employee {
|
|
|
|
|
|
|
|
private String name;
|
|
|
|
private Date birthdate;
|
|
|
|
private int id;
|
|
|
|
private Employee supervisor;
|
|
|
|
private List<Employee> supervisees;
|
|
|
|
private float salary;
|
|
|
|
|
|
|
|
Employee(String name, Date birthdate, int id) {
|
|
|
|
this.name = name;
|
|
|
|
this.birthdate = birthdate;
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Date getBirthdate() {
|
|
|
|
return birthdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
int getId() {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSupervisor(Employee supervisor) {
|
|
|
|
this.supervisor = supervisor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Employee getSupervisor() {
|
|
|
|
return supervisor;
|
|
|
|
}
|
2011-04-08 16:50:33 +00:00
|
|
|
|
2011-04-11 22:05:36 +00:00
|
|
|
void setSupervisees(List<Employee> supervisees) {
|
|
|
|
this.supervisees = supervisees;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Employee> getSupervisees() {
|
|
|
|
return supervisees;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSalary(float salary) {
|
|
|
|
this.salary = salary;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getSalary() {
|
|
|
|
return salary;
|
|
|
|
}
|
|
|
|
}
|
2011-04-07 16:15:49 +00:00
|
|
|
}
|