org.apache.velocity.exception.ParseErrorException Java Examples

The following examples show how to use org.apache.velocity.exception.ParseErrorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VelocityEngine.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 *  merges a template and puts the rendered stream into the writer
 *
 *  @param templateName name of template to be used in merge
 *  @param encoding encoding used in template
 *  @param context  filled context to be used in merge
 *  @param  writer  writer to write template into
 *
 *  @return true if successful, false otherwise.  Errors
 *           logged to velocity log
 * @throws ResourceNotFoundException
 * @throws ParseErrorException
 * @throws MethodInvocationException
 *  @since Velocity v1.1
 */
public boolean mergeTemplate( String templateName, String encoding,
                                  Context context, Writer writer )
    throws ResourceNotFoundException, ParseErrorException, MethodInvocationException
{
    Template template = ri.getTemplate(templateName, encoding);

    if ( template == null )
    {
        String msg = "VelocityEngine.mergeTemplate() was unable to load template '"
                       + templateName + "'";
        getLog().error(msg);
        throw new ResourceNotFoundException(msg);
    }
    else
    {
        template.merge(context, writer);
        return true;
     }
}
 
Example #2
Source File: Split.java    From CodeGen with MIT License 6 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
    String value = (String) node.jjtGetChild(0).value(context);
    String character = (String) node.jjtGetChild(1).value(context);
    int len = value.length();
    StringBuilder sb = new StringBuilder(len);
    sb.append(value.charAt(0));
    for (int i = 1; i < len; i++) {
        char c = value.charAt(i);
        if (Character.isUpperCase(c)) {
            sb.append(character).append(Character.toLowerCase(c));
        } else {
            sb.append(c);
        }
    }
    writer.write(sb.toString());
    return true;
}
 
Example #3
Source File: MarkdownRenderer.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a markdown rendering of rule documentation for the given rule information object with
 * the given rule name.
 */
public String render(String ruleName, RuleInfo ruleInfo) throws IOException {
  VelocityContext context = new VelocityContext();
  context.put("util", new MarkdownUtil());
  context.put("ruleName", ruleName);
  context.put("ruleInfo", ruleInfo);

  StringWriter stringWriter = new StringWriter();
  Reader reader = readerFromPath(ruleTemplateFilename);
  try {
    velocityEngine.evaluate(context, stringWriter, ruleTemplateFilename, reader);
  } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
    throw new IOException(e);
  }
  return stringWriter.toString();
}
 
Example #4
Source File: VelocityTemplateParser.java    From eagle with Apache License 2.0 6 votes vote down vote up
public VelocityTemplateParser(String templateString) throws ParseErrorException {
    VelocityEngine engine = new VelocityEngine();
    engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
    engine.setProperty("runtime.log.logsystem.log4j.logger", LOG.getName());
    engine.setProperty(Velocity.RESOURCE_LOADER, "string");
    engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
    engine.addProperty("string.resource.loader.repository.static", "false");
    engine.addProperty("runtime.references.strict", "true");
    engine.init();
    StringResourceRepository resourceRepository = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
    resourceRepository.putStringResource(TEMPLATE_NAME, templateString);
    template = engine.getTemplate(TEMPLATE_NAME);
    ASTprocess data = (ASTprocess) template.getData();
    visitor = new ParserNodeVisitor();
    data.jjtAccept(visitor, null);
}
 
Example #5
Source File: VelocityGenerator.java    From celerio with Apache License 2.0 6 votes vote down vote up
public String evaluate(Map<String, Object> context, TemplatePack templatePack, Template template) throws IOException {
    StringWriter sw = new StringWriter();
    try {
        engine.evaluate(new VelocityContext(context), sw, template.getName(), template.getTemplate());
        return sw.toString();
    } catch (ParseErrorException parseException) {
        handleStopFileGeneration(parseException);
        log.error("In " + templatePack.getName() + ":" + template.getName() + " template, parse exception " + parseException.getMessage(),
                parseException.getCause());
        displayLinesInError(parseException, templatePack, template);
        throw new IllegalStateException();
    } catch (MethodInvocationException mie) {
        handleStopFileGeneration(mie);
        log.error("In " + templatePack.getName() + ":" + mie.getTemplateName() + " method [" + mie.getMethodName() + "] has not been set", mie.getCause());
        displayLinesInError(mie, templatePack, template);
        throw mie;
    } finally {
        closeQuietly(sw);
    }
}
 
Example #6
Source File: HtmlCompressorDirective.java    From htmlcompressor with Apache License 2.0 6 votes vote down vote up
public boolean render(InternalContextAdapter context, Writer writer, Node node) 
  		throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
  	
  	//render content
  	StringWriter content = new StringWriter();
node.jjtGetChild(0).render(context, content);

//compress
try {
	writer.write(htmlCompressor.compress(content.toString()));
} catch (Exception e) {
	writer.write(content.toString());
	String msg = "Failed to compress content: "+content.toString();
          log.error(msg, e);
          throw new RuntimeException(msg, e);
          
}
return true;
  	
  }
 
Example #7
Source File: XmlCompressorDirective.java    From htmlcompressor with Apache License 2.0 6 votes vote down vote up
public boolean render(InternalContextAdapter context, Writer writer, Node node) 
  		throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
  	
  	//render content
  	StringWriter content = new StringWriter();
node.jjtGetChild(0).render(context, content);

//compress
try {
	writer.write(xmlCompressor.compress(content.toString()));
} catch (Exception e) {
	writer.write(content.toString());
	String msg = "Failed to compress content: "+content.toString();
          log.error(msg, e);
          throw new RuntimeException(msg, e);
          
}
return true;
  	
  }
 
Example #8
Source File: MarkdownRenderer.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a markdown rendering of provider documentation for the given provider information
 * object with the given name.
 */
public String render(String providerName, ProviderInfo providerInfo) throws IOException {
  VelocityContext context = new VelocityContext();
  context.put("util", new MarkdownUtil());
  context.put("providerName", providerName);
  context.put("providerInfo", providerInfo);

  StringWriter stringWriter = new StringWriter();
  Reader reader = readerFromPath(providerTemplateFilename);
  try {
    velocityEngine.evaluate(context, stringWriter, providerTemplateFilename, reader);
  } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
    throw new IOException(e);
  }
  return stringWriter.toString();
}
 
Example #9
Source File: MarkdownRenderer.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a markdown rendering of aspect documentation for the given aspect information object
 * with the given aspect name.
 */
public String render(String aspectName, AspectInfo aspectInfo) throws IOException {
  VelocityContext context = new VelocityContext();
  context.put("util", new MarkdownUtil());
  context.put("aspectName", aspectName);
  context.put("aspectInfo", aspectInfo);

  StringWriter stringWriter = new StringWriter();
  Reader reader = readerFromPath(aspectTemplateFilename);
  try {
    velocityEngine.evaluate(context, stringWriter, aspectTemplateFilename, reader);
  } catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
    throw new IOException(e);
  }
  return stringWriter.toString();
}
 
Example #10
Source File: Page.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Renders the template and writes the output to the given file.
 *
 * Strips all trailing whitespace before writing to file.
 */
public void write(File outputFile) throws IOException {
  StringWriter stringWriter = new StringWriter();
  try {
    engine.mergeTemplate(template, "UTF-8", context, stringWriter);
  } catch (ResourceNotFoundException|ParseErrorException|MethodInvocationException e) {
    throw new IOException(e);
  }
  stringWriter.close();

  String[] lines = stringWriter.toString().split(System.getProperty("line.separator"));
  try (Writer fileWriter = Files.newBufferedWriter(outputFile.toPath(), UTF_8)) {
    for (String line : lines) {
      // Strip trailing whitespace then append newline before writing to file.
      fileWriter.write(line.replaceFirst("\\s+$", "") + "\n");
    }
  }
}
 
Example #11
Source File: JspUtilsTest.java    From velocity-tools with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.JspUtils#executeTag(org.apache.velocity.context.InternalContextAdapter, org.apache.velocity.runtime.parser.node.Node, javax.servlet.jsp.PageContext, javax.servlet.jsp.tagext.Tag)}.
 * @throws JspException If something goes wrong.
 * @throws IOException If something goes wrong.
 * @throws ParseErrorException If something goes wrong.
 * @throws ResourceNotFoundException If something goes wrong.
 * @throws MethodInvocationException If something goes wrong.
 */
@Test
public void testExecuteTag() throws JspException, MethodInvocationException, ResourceNotFoundException, ParseErrorException, IOException
{
    InternalContextAdapter context = createMock(InternalContextAdapter.class);
    Node node = createMock(Node.class);
    PageContext pageContext = createMock(PageContext.class);
    BodyTag tag = createMock(BodyTag.class);
    ASTBlock block = createMock(ASTBlock.class);
    JspWriter writer = createMock(JspWriter.class);

    expect(tag.doStartTag()).andReturn(BodyTag.EVAL_BODY_BUFFERED);
    tag.setBodyContent(isA(VelocityBodyContent.class));
    tag.doInitBody();
    expect(node.jjtGetChild(1)).andReturn(block);
    expect(tag.doAfterBody()).andReturn(BodyTag.SKIP_BODY);
    expect(tag.doEndTag()).andReturn(BodyTag.EVAL_PAGE);
    expect(pageContext.getOut()).andReturn(writer);
    expect(block.render(eq(context), isA(StringWriter.class))).andReturn(true);

    replay(context, node, pageContext, block, tag, writer);
    JspUtils.executeTag(context, node, pageContext, tag);
    verify(context, node, pageContext, block, tag, writer);
}
 
Example #12
Source File: Velocity.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 *  Merges a template and puts the rendered stream into the writer
 *
 *  @param templateName name of template to be used in merge
 *  @param encoding encoding used in template
 *  @param context  filled context to be used in merge
 *  @param  writer  writer to write template into
 *
 *  @return true if successful, false otherwise.  Errors
 *           logged to velocity log
 *
 * @throws ParseErrorException The template could not be parsed.
 * @throws MethodInvocationException A method on a context object could not be invoked.
 * @throws ResourceNotFoundException A referenced resource could not be loaded.
 *
 * @since Velocity v1.1
 */
public static boolean mergeTemplate( String templateName, String encoding,
                                  Context context, Writer writer )
    throws ResourceNotFoundException, ParseErrorException, MethodInvocationException
{
    Template template = RuntimeSingleton.getTemplate(templateName, encoding);

    if ( template == null )
    {
        String msg = "Velocity.mergeTemplate() was unable to load template '"
                       + templateName + "'";
        getLog().error(msg);
        throw new ResourceNotFoundException(msg);
    }
    else
    {
        template.merge(context, writer);
        return true;
    }
}
 
Example #13
Source File: ASTBlock.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
 */
public boolean render( InternalContextAdapter context, Writer writer)
    throws IOException, MethodInvocationException,
    	ResourceNotFoundException, ParseErrorException
{
    SpaceGobbling spaceGobbling = rsvc.getSpaceGobbling();

    if (spaceGobbling == SpaceGobbling.NONE)
    {
        writer.write(prefix);
    }

    int i, k = jjtGetNumChildren();

    for (i = 0; i < k; i++)
        jjtGetChild(i).render(context, writer);

    if (morePostfix.length() > 0 || spaceGobbling.compareTo(SpaceGobbling.LINES) < 0)
    {
        writer.write(postfix);
    }

    writer.write(morePostfix);

    return true;
}
 
Example #14
Source File: ConfigurableLDAPResolver.java    From XACML with MIT License 5 votes vote down vote up
private String evaluateVelocityTemplate(String template,
		final Map<String,PIPRequest> templateParameters,
		final PIPEngine pipEngine,
		final PIPFinder pipFinder) 
				throws PIPException {
	StringWriter out = new StringWriter();
	VelocityContext vctx = new VelocityContext();
	EventCartridge vec = new EventCartridge();
	VelocityParameterWriter writer = new VelocityParameterWriter(
			pipEngine, pipFinder, templateParameters);
	vec.addEventHandler(writer);
	vec.attachToContext(vctx);

	try {
		Velocity.evaluate(vctx, out,
				"LdapResolver", template);
	}
	catch (ParseErrorException pex) {
		throw new PIPException(
				"Velocity template evaluation failed",pex);
	}
	catch (MethodInvocationException mix) {
		throw new PIPException(
				"Velocity template evaluation failed",mix);
	}
	catch (ResourceNotFoundException rnfx) {
		throw new PIPException(
				"Velocity template evaluation failed",rnfx);
	}

	this.logger.warn("(" + id + ") " + " template yields " + out.toString());

	return out.toString();
}
 
Example #15
Source File: LowerCase.java    From CodeGen with MIT License 5 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
    String value = (String) node.jjtGetChild(0).value(context);
    if (StringUtils.isBlank(value)) {
        writer.write(value);
        return true;
    }
    String result = value.replaceFirst(value.substring(0, 1), value.substring(0, 1).toLowerCase());
    writer.write(result);
    return true;
}
 
Example #16
Source File: ParseExceptionTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that parseException has useful info when called by template.marge()
 * @throws Exception
 */
public void testParseExceptionFromTemplate ()
        throws Exception
{

    VelocityEngine ve = new VelocityEngine();

    ve.setProperty("file.resource.loader.cache", "true");
    ve.setProperty("file.resource.loader.path", TemplateTestBase.TEST_COMPARE_DIR + "/" + FILE_RESOURCE_LOADER_PATH);
    ve.init();


    Writer writer = new StringWriter();

    VelocityContext context = new VelocityContext();

    try
    {
        Template template = ve.getTemplate("badtemplate.vm");
        template.merge(context, writer);
        fail("Should have thown a ParseErrorException");
    }
    catch (ParseErrorException e)
    {
        assertEquals("badtemplate.vm",e.getTemplateName());
        assertEquals(5,e.getLineNumber());
        assertEquals(9,e.getColumnNumber());
    }
    finally
    {
        if (writer != null)
        {
            writer.close();
        }
    }
}
 
Example #17
Source File: ASTElseIfStatement.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
 */
public boolean render( InternalContextAdapter context, Writer writer)
    throws IOException,MethodInvocationException,
    	ResourceNotFoundException, ParseErrorException
{
    return jjtGetChild(1).render( context, writer );
}
 
Example #18
Source File: ASTComment.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.velocity.runtime.parser.node.SimpleNode#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
 */
public boolean render( InternalContextAdapter context, Writer writer)
    throws IOException, MethodInvocationException, ParseErrorException, ResourceNotFoundException
{
    writer.write(carr);
    return true;
}
 
Example #19
Source File: ReportInvalidReferences.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Check for an invalid reference and collect the error or throw an exception
 * (depending on configuration).
 *
 * @param reference the invalid reference
 * @param info line, column, template name
 */
private void reportInvalidReference(String reference, Info info)
{
    InvalidReferenceInfo invalidReferenceInfo = new InvalidReferenceInfo(reference, info);
    invalidReferences.add(invalidReferenceInfo);

    if (stopOnFirstInvalidReference)
    {
        throw new ParseErrorException(
                "Error in page - invalid reference.  ",
                info,
                invalidReferenceInfo.getInvalidReference());
    }
}
 
Example #20
Source File: GetPackage.java    From CodeGen with MIT License 5 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
    String clazz = (String) node.jjtGetChild(0).value(context);
    if (context.containsKey(clazz)) {
        String packagePath = context.get(clazz).toString();
        packagePath = new StringBuilder("").append(packagePath).toString();
        writer.write(packagePath);
        return true;
    }
    return false;
}
 
Example #21
Source File: MicroSqlReplace.java    From nh-micro with Apache License 2.0 5 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node)
		throws IOException, ResourceNotFoundException, ParseErrorException,
		MethodInvocationException {
	List placeList=(List) context.get("placeList");
	if(placeList==null){
		return true;
	}
	Object value=node.jjtGetChild(0).value(context);
	placeList.add(value);
	writer.write("?");
	return true;
}
 
Example #22
Source File: SimpleNode.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.apache.velocity.runtime.parser.node.Node#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
 */
public boolean render( InternalContextAdapter context, Writer writer)
    throws IOException, MethodInvocationException, ParseErrorException, ResourceNotFoundException
{
    int i, k = jjtGetNumChildren();

    for (i = 0; i < k; i++)
        jjtGetChild(i).render(context, writer);

    return true;
}
 
Example #23
Source File: VelocityBodyContentTest.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.VelocityBodyContent#writeOut(java.io.Writer)}.
 * @throws IOException If something goes wrong.
 * @throws ParseErrorException If something goes wrong.
 * @throws ResourceNotFoundException If something goes wrong.
 * @throws MethodInvocationException If something goes wrong.
 */
@Test
public void testWriteOutWriter() throws MethodInvocationException, ResourceNotFoundException, ParseErrorException, IOException
{
    Writer writer = createMock(Writer.class);
    expect(block.render(context, writer)).andReturn(true);
    replay(jspWriter, block, context, writer);
    content.writeOut(writer);
    verify(jspWriter, block, context, writer);
}
 
Example #24
Source File: Velocity614TestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testDirectivesWithBadArg()
{
    // make sure these all still bomb, but don't spam sysout
    log.off();
    assertEvalException("#foreach(Stuff in That)foo#end");
    assertEvalException("#include(Stuff)");
    assertEvalException("#parse(Stuff)");
    assertEvalException("#define(Stuff)foo#end");
    assertEvalException("#macro( name Stuff)foo#end");
    assertEvalException("#foreach($i in [1..3])#break(Stuff)#end");
    assertEvalException("#literal(Stuff)foo#end");
    assertEvalException("#evaluate(Stuff)", ParseErrorException.class);
    log.on();
}
 
Example #25
Source File: VelocityBodyContentTest.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.VelocityBodyContent#getReader()}.
 * @throws IOException If something goes wrong.
 * @throws ParseErrorException If something goes wrong.
 * @throws ResourceNotFoundException If something goes wrong.
 * @throws MethodInvocationException If something goes wrong.
 */
@Test
public void testGetReader() throws MethodInvocationException, ResourceNotFoundException, ParseErrorException, IOException
{
    expect(block.render(eq(context), isA(StringWriter.class))).andReturn(true);
    replay(jspWriter, block, context);
    assertNotNull(content.getReader());
    verify(jspWriter, block, context);
}
 
Example #26
Source File: VelocityJspFragmentTest.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link org.apache.velocity.tools.view.jsp.jspimpl.VelocityJspFragment#invoke(java.io.Writer)}.
 * @throws IOException If something goes wrong.
 * @throws ParseErrorException If something goes wrong.
 * @throws ResourceNotFoundException If something goes wrong.
 * @throws MethodInvocationException If something goes wrong.
 * @throws JspException If something goes wrong.
 */
@Test
public void testInvokeWriter() throws MethodInvocationException, ResourceNotFoundException, ParseErrorException, IOException, JspException
{
    PageContext pageContext = createMock(PageContext.class);
    ASTBlock block = createMock(ASTBlock.class);
    InternalContextAdapter context = createMock(InternalContextAdapter.class);
    Writer writer = createMock(Writer.class);
    expect(block.render(context, writer)).andReturn(true);

    replay(pageContext, block, context, writer);
    VelocityJspFragment fragment = new VelocityJspFragment(pageContext, block, context);
    fragment.invoke(writer);
    verify(pageContext, block, context, writer);
}
 
Example #27
Source File: ImportPackage.java    From CodeGen with MIT License 5 votes vote down vote up
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
    String clazz = (String) node.jjtGetChild(0).value(context);
    if (context.containsKey(clazz)) {
        String packagePath = context.get(clazz).toString();
        packagePath = new StringBuilder("import ").append(packagePath).append(";\n").toString();
        writer.write(packagePath);
        return true;
    }
    return false;
}
 
Example #28
Source File: VelocityWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
static boolean evaluate(@Nullable Project project, Context context, Writer writer, String templateContent)
        throws ParseErrorException, MethodInvocationException, ResourceNotFoundException {
  try {
    ourTemplateManager.set(project == null ? FileTemplateManager.getDefaultInstance() : FileTemplateManager.getInstance(project));
    return Velocity.evaluate(context, writer, "", templateContent);
  }
  finally {
    ourTemplateManager.set(null);
  }
}
 
Example #29
Source File: VelocityTemplate.java    From XBatis-Code-Generator with Apache License 2.0 5 votes vote down vote up
/**
 * 获取并解析模版
 * @param templateFile
 * @return template
 * @throws Exception
 */
private static Template buildTemplate(String templateFile) {
	Template template = null;
	try {
		template = Velocity.getTemplate(templateFile);
	} catch (ResourceNotFoundException rnfe) {
		logger.error("buildTemplate error : cannot find template " + templateFile);
	} catch (ParseErrorException pee) {
		logger.error("buildTemplate error in template " + templateFile + ":" + pee);
	} catch (Exception e) {
		logger.error("buildTemplate error in template " + templateFile + ":" + e);
	}
	return template;
}
 
Example #30
Source File: VelocityViewTest.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for {@link org.apache.velocity.tools.view.VelocityView#getTemplate(javax.servlet.http.HttpServletRequest)}.
 * Tests VELTOOLS-119
 * @throws IOException If something goes wrong.
 * @throws MethodInvocationException If something goes wrong.
 * @throws ParseErrorException If something goes wrong.
 * @throws ResourceNotFoundException If something goes wrong.
 */
@Test
public void testGetTemplateHttpServletRequestHttpServletResponse() throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException
{
    JeeConfig config = createMock(JeeConfig.class);
    ServletContext servletContext = createMock(ServletContext.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpServletResponse response = createMock(HttpServletResponse.class);
    Context context = createMock(Context.class);

    expect(config.getServletContext()).andAnswer(eval(servletContext));
    expect(config.findInitParameter(VelocityView.USER_OVERWRITE_KEY)).andAnswer(eval(null));
    expect(config.findInitParameter(VelocityView.LOAD_DEFAULTS_KEY)).andAnswer(eval("false"));
    expect(servletContext.getInitParameter(VelocityView.PROPERTIES_KEY)).andAnswer(eval(null));
    expect(servletContext.getResourceAsStream(VelocityView.USER_PROPERTIES_PATH))
        .andAnswer(eval(getClass().getResourceAsStream("/WEB-INF/velocity.properties")));
    String root = new File(getClass().getResource("/").getFile()).getAbsolutePath();
    expect(config.getInitParameter(VelocityView.PROPERTIES_KEY)).andAnswer(eval(null));
    expect(config.findInitParameter(VelocityView.CLEAN_CONFIGURATION_KEY)).andAnswer(eval(null));
    expect(servletContext.getInitParameter(VelocityView.TOOLS_KEY)).andAnswer(eval(null));
    expect(config.getInitParameter(VelocityView.TOOLS_KEY)).andAnswer(eval(null));
    expect(servletContext.getAttribute(ServletUtils.CONFIGURATION_KEY)).andAnswer(eval((String)null));
    expect(servletContext.getResource(VelocityView.USER_TOOLS_PATH)).andAnswer(eval(null));
    expect(request.getAttribute("javax.servlet.include.servlet_path")).andAnswer(eval("/charset-test.vm"));
    expect(request.getAttribute("javax.servlet.include.path_info")).andAnswer(eval((String)null));

    // This was necessary to verify the bug, now it is not called at all.
    // expect(response.getCharacterEncoding()).andReturn("ISO-8859-1");

    replay(config, servletContext, request, response, context);
    VelocityView view = new VelocityView(config);
    Template template = view.getTemplate(request);
    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    writer.close();
    assertTrue(writer.toString().startsWith("Questo è il momento della verità"));

    verify(config, servletContext, request, response, context);
}