下面列出了org.springframework.web.servlet.view.AbstractView#org.apache.velocity.tools.generic.MathTool 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public @Test void testMathTool() {
MathTool mathTool = (MathTool)toolbox.get("math");
assertNotNull(mathTool);
assertEquals(1,mathTool.abs(-1));
assertEquals(2,mathTool.add(1,1));
assertEquals(new Integer(3),mathTool.ceil(2.5));
assertEquals(4,mathTool.div(8,2));
assertEquals(new Integer(5),mathTool.floor(5.1));
assertEquals(6,mathTool.getAverage(new long[] {5,6,7}));
/* getTotal() watches the type of its first argument, so assertEquals needs a long */
assertEquals(7L,mathTool.getTotal(new long[]{2, 2, 3}));
assertEquals(new Integer(8), mathTool.idiv(130, 16));
assertEquals(9,mathTool.max(9,-10));
assertEquals(10, mathTool.min(10, 20));
assertEquals(new Integer(11),mathTool.mod(37,13));
assertEquals(12, mathTool.mul(3, 4));
assertEquals(new Integer(13),mathTool.round(12.8));
assertEquals(new Double(14.2),mathTool.roundTo(1,14.18));
assertEquals(new Double(-5.0),mathTool.roundTo(2,-4.999));
assertEquals(15,mathTool.sub(30,15));
assertEquals(16, mathTool.pow(4, 2));
assertEquals(new Integer(17),mathTool.toInteger("17"));
assertEquals(new Double(18.1),mathTool.toDouble("18.1"));
}
private void mergeTemplate(String name, String encoding, Map<String, Object> model, StringWriter writer)
throws ResourceNotFoundException, ParseErrorException, Exception {
VelocityContext velocityContext = new VelocityContext(model);
velocityContext.put("dateSymbol", new DateTool());
velocityContext.put("numberSymbol", new NumberTool());
velocityContext.put("mathSymbol", new MathTool());
Template template = engine().getTemplate(name, encoding);
template.merge(velocityContext, writer);
}
/**
* Initializes Velocity engine
*/
private void init() {
velocityEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "class");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
setLogFile();
DateTool dateTool = new DateTool();
dateTool.configure(this.configMap);
MathTool mathTool = new MathTool();
NumberTool numberTool = new NumberTool();
numberTool.configure(this.configMap);
SortTool sortTool = new SortTool();
defaultContext = new VelocityContext();
defaultContext.put("dateTool", dateTool);
defaultContext.put("dateComparisonTool", new ComparisonDateTool());
defaultContext.put("mathTool", mathTool);
defaultContext.put("numberTool", numberTool);
defaultContext.put("sortTool", sortTool);
// Following tools need VelocityTools version 2.0+
//defaultContext.put("displayTool", new DisplayTool());
//defaultContext.put("xmlTool", new XmlTool());
try {
velocityEngine.init();
} catch (Exception e) {
throw new VelocityException(e);
}
}
protected FactoryConfiguration getBaseConfig()
{
FactoryConfiguration base = new FactoryConfiguration();
Data datum = new Data();
datum.setKey("version");
datum.setType("number");
datum.setValue("2.0");
base.addData(datum);
ToolboxConfiguration toolbox = new ToolboxConfiguration();
toolbox.setScope(Scope.REQUEST);
toolbox.setProperty("locale", Locale.US);
ToolConfiguration tool = new ToolConfiguration();
tool.setClass(ResourceTool.class);
toolbox.addTool(tool);
base.addToolbox(toolbox);
toolbox = new ToolboxConfiguration();
toolbox.setScope(Scope.APPLICATION);
tool = new ToolConfiguration();
tool.setKey("calc");
tool.setClass(MathTool.class);
toolbox.addTool(tool);
tool = new ToolConfiguration();
tool.setClass(NumberTool.class);
tool.setProperty("locale", Locale.FRENCH);
toolbox.addTool(tool);
base.addToolbox(toolbox);
return base;
}
public @Test void testEasyConfig()
{
EasyFactoryConfiguration easy = new EasyFactoryConfiguration();
easy.number("version", 2.0);
easy.toolbox("request")
.property("locale", Locale.US)
.tool(ResourceTool.class);
easy.toolbox("application")
.tool("calc", MathTool.class)
.tool(NumberTool.class)
.property("locale", Locale.FRENCH);
assertValid(easy);
assertConfigEquals(getBaseConfig(), easy);
}
public static FactoryConfiguration getConfiguration()
{
EasyFactoryConfiguration easy = new EasyFactoryConfiguration();
easy.number("version", 2.0);
easy.toolbox("request")
.property("locale", Locale.US)
.tool(ResourceTool.class);
easy.toolbox("application")
.tool("calc", MathTool.class)
.tool(NumberTool.class)
.property("locale", Locale.FRENCH);
return easy;
}
private void generateHtmlfile(Map<String, Object> input) {
try{
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
ve.setProperty("classpath.resource.loader.class",ClasspathResourceLoader.class.getName());
ve.init();
Template template = ve.getTemplate("templates/acmeair-report.vtl");
VelocityContext context = new VelocityContext();
for(Map.Entry<String, Object> entry: input.entrySet()){
context.put(entry.getKey(), entry.getValue());
}
context.put("math", new MathTool());
context.put("number", new NumberTool());
context.put("date", new ComparisonDateTool());
Writer file = new FileWriter(new File(searchingLocation
+ System.getProperty("file.separator") + RESULTS_FILE));
template.merge( context, file );
file.flush();
file.close();
}catch(Exception e){
e.printStackTrace();
}
}
@Test
public void testExposeHelpers() throws Exception {
final String templateName = "test.vm";
WebApplicationContext wac = mock(WebApplicationContext.class);
given(wac.getServletContext()).willReturn(new MockServletContext());
final Template expectedTemplate = new Template();
VelocityConfig vc = new VelocityConfig() {
@Override
public VelocityEngine getVelocityEngine() {
return new TestVelocityEngine(templateName, expectedTemplate);
}
};
Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
configurers.put("velocityConfigurer", vc);
given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);
// let it ask for locale
HttpServletRequest req = mock(HttpServletRequest.class);
given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null);
given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver());
given(req.getLocale()).willReturn(Locale.CANADA);
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
VelocityView vv = new VelocityView() {
@Override
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
assertTrue(template == expectedTemplate);
assertTrue(response == expectedResponse);
assertEquals("myValue", context.get("myHelper"));
assertTrue(context.get("math") instanceof MathTool);
assertTrue(context.get("dateTool") instanceof DateTool);
DateTool dateTool = (DateTool) context.get("dateTool");
assertTrue(dateTool.getLocale().equals(Locale.CANADA));
assertTrue(context.get("numberTool") instanceof NumberTool);
NumberTool numberTool = (NumberTool) context.get("numberTool");
assertTrue(numberTool.getLocale().equals(Locale.CANADA));
}
@Override
protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
model.put("myHelper", "myValue");
}
};
vv.setUrl(templateName);
vv.setApplicationContext(wac);
Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>();
toolAttributes.put("math", MathTool.class);
vv.setToolAttributes(toolAttributes);
vv.setDateToolAttribute("dateTool");
vv.setNumberToolAttribute("numberTool");
vv.setExposeSpringMacroHelpers(false);
vv.render(new HashMap<String, Object>(), req, expectedResponse);
assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType());
}
@Test
public void testVelocityToolboxView() throws Exception {
final String templateName = "test.vm";
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
final Template expectedTemplate = new Template();
VelocityConfig vc = new VelocityConfig() {
@Override
public VelocityEngine getVelocityEngine() {
return new TestVelocityEngine(templateName, expectedTemplate);
}
};
wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
final HttpServletRequest expectedRequest = new MockHttpServletRequest();
final HttpServletResponse expectedResponse = new MockHttpServletResponse();
VelocityToolboxView vv = new VelocityToolboxView() {
@Override
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
assertTrue(template == expectedTemplate);
assertTrue(response == expectedResponse);
assertTrue(context instanceof ChainedContext);
assertEquals("this is foo.", context.get("foo"));
assertTrue(context.get("map") instanceof HashMap<?,?>);
assertTrue(context.get("date") instanceof DateTool);
assertTrue(context.get("math") instanceof MathTool);
assertTrue(context.get("link") instanceof LinkTool);
LinkTool linkTool = (LinkTool) context.get("link");
assertNotNull(linkTool.getContextURL());
assertTrue(context.get("link2") instanceof LinkTool);
LinkTool linkTool2 = (LinkTool) context.get("link2");
assertNotNull(linkTool2.getContextURL());
}
};
vv.setUrl(templateName);
vv.setApplicationContext(wac);
Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>();
toolAttributes.put("math", MathTool.class);
toolAttributes.put("link2", LinkTool.class);
vv.setToolAttributes(toolAttributes);
vv.setToolboxConfigLocation("org/springframework/web/servlet/view/velocity/toolbox.xml");
vv.setExposeSpringMacroHelpers(false);
vv.render(new HashMap<String,Object>(), expectedRequest, expectedResponse);
}