Search in sources :

Example 1 with DummyMacroRequestContext

use of org.springframework.web.servlet.view.DummyMacroRequestContext in project spring-framework by spring-projects.

the class FreeMarkerMacroTests method getMacroOutput.

private String getMacroOutput(String name) throws Exception {
    String macro = fetchMacro(name);
    assertThat(macro).isNotNull();
    FileSystemResource resource = new FileSystemResource(System.getProperty("java.io.tmpdir") + "/tmp.ftl");
    FileCopyUtils.copy("<#import \"spring.ftl\" as spring />\n" + macro, new FileWriter(resource.getPath()));
    DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
    Map<String, String> msgMap = new HashMap<>();
    msgMap.put("hello", "Howdy");
    msgMap.put("world", "Mundo");
    rc.setMessageMap(msgMap);
    Map<String, String> themeMsgMap = new HashMap<>();
    themeMsgMap.put("hello", "Howdy!");
    themeMsgMap.put("world", "Mundo!");
    rc.setThemeMessageMap(themeMsgMap);
    rc.setContextPath("/springtest");
    TestBean darren = new TestBean("Darren", 99);
    TestBean fred = new TestBean("Fred");
    fred.setJedi(true);
    darren.setSpouse(fred);
    darren.setJedi(true);
    darren.setStringArray(new String[] { "John", "Fred" });
    request.setAttribute("command", darren);
    Map<String, String> names = new HashMap<>();
    names.put("Darren", "Darren Davison");
    names.put("John", "John Doe");
    names.put("Fred", "Fred Bloggs");
    names.put("Rob&Harrop", "Rob Harrop");
    Configuration config = fc.getConfiguration();
    Map<String, Object> model = new HashMap<>();
    model.put("command", darren);
    model.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, rc);
    model.put("msgArgs", new Object[] { "World" });
    model.put("nameOptionMap", names);
    model.put("options", names.values());
    FreeMarkerView view = new FreeMarkerView();
    view.setBeanName("myView");
    view.setUrl("tmp.ftl");
    view.setExposeSpringMacroHelpers(false);
    view.setConfiguration(config);
    view.setServletContext(new MockServletContext());
    view.render(model, request, response);
    // tokenize output and ignore whitespace
    String output = response.getContentAsString();
    output = output.replace("\r\n", "\n");
    return output.trim();
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) FileWriter(java.io.FileWriter) DummyMacroRequestContext(org.springframework.web.servlet.view.DummyMacroRequestContext) FileSystemResource(org.springframework.core.io.FileSystemResource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext)

Aggregations

Configuration (freemarker.template.Configuration)1 FileWriter (java.io.FileWriter)1 HashMap (java.util.HashMap)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 DummyMacroRequestContext (org.springframework.web.servlet.view.DummyMacroRequestContext)1 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)1