private ToolContext getToolContext(String toolboxConfigLocation) throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(Config.class); context.refresh(); EmbeddedVelocityToolboxView view = context .getBean(EmbeddedVelocityToolboxView.class); view.setToolboxConfigLocation(toolboxConfigLocation); Map<String, Object> model = new LinkedHashMap<String, Object>(); HttpServletRequest request = new MockHttpServletRequest(); HttpServletResponse response = new MockHttpServletResponse(); ToolContext toolContext = (ToolContext) view.createVelocityContext(model, request, response); context.close(); return toolContext; }
/** * Creates and returns an initialized Velocity context */ @Override protected Context createVelocityContext(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) { ToolContext toolContext = toolManager.createContext(request, response); VelocityContext context = new VelocityContext(toolContext); if (model != null) { for(Map.Entry<String, Object> entry : (Set<Map.Entry<String, Object>>) model.entrySet()) { context.put(entry.getKey(), entry.getValue()); } } return context; }
@Test public void loadsContextFromClassPath() throws Exception { ToolContext context = getToolContext(PATH + "/toolbox.xml"); assertThat(context.getToolbox().keySet()).contains("math"); }
@Test public void loadsWithoutConfig() throws Exception { ToolContext context = getToolContext(null); assertThat(context).isNotNull(); }
@Test public void loadsContextFromClassPath() throws Exception { ToolContext context = getToolContext(PATH + "/toolbox.xml"); assertThat(context.getToolbox().keySet(), contains("math")); }
@Test public void loadsWithoutConfig() throws Exception { ToolContext context = getToolContext(null); assertThat(context, notNullValue()); }