fix mustache render failure test

This commit is contained in:
Sebastian Sdorra
2012-08-18 13:40:21 +02:00
parent d619bd73da
commit 65dde22676
4 changed files with 63 additions and 4 deletions

View File

@@ -30,17 +30,21 @@
*/ */
package sonia.scm.template; package sonia.scm.template;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.github.mustachejava.Mustache; import com.github.mustachejava.Mustache;
import com.google.common.base.Throwables;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.io.IOException;
import java.io.Writer; import java.io.Writer;
/** /**
@@ -81,16 +85,30 @@ public class MustacheTemplate implements Template
* @param writer * @param writer
* @param environment * @param environment
* @param model * @param model
*
* @throws IOException
*/ */
@Override @Override
public void execute(Writer writer, Object model) public void execute(Writer writer, Object model) throws IOException
{ {
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
logger.debug("render mustache template at {}", templatePath); logger.debug("render mustache template at {}", templatePath);
} }
mustache.execute(writer, model); try
{
mustache.execute(writer, model);
}
catch (Exception ex)
{
Throwables.propagateIfInstanceOf(ex, IOException.class);
throw new TemplateRenderException(
"could not render template ".concat(templatePath), ex);
}
} }
//~--- fields --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------

View File

@@ -36,10 +36,14 @@ package sonia.scm.template;
import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache; import com.github.mustachejava.Mustache;
import com.google.common.base.Function;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
import java.io.IOException; import java.io.IOException;
import java.util.Map;
/** /**
* *
* @author Sebastian Sdorra * @author Sebastian Sdorra
@@ -73,6 +77,31 @@ public class MustacheTemplateTest extends TemplateTestBase
return getTemplate("sonia/scm/template/001.mustache"); return getTemplate("sonia/scm/template/001.mustache");
} }
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @param env
*/
@Override
protected void prepareEnv(Map<String, Object> env)
{
env.put("test", new Function<String, String>()
{
@Override
public String apply(String input)
{
throw new UnsupportedOperationException("Not supported yet.");
}
});
}
//~--- get methods ----------------------------------------------------------
/** /**
* Method description * Method description
* *

View File

@@ -58,6 +58,8 @@ public abstract class TemplateTestBase
* *
* *
* @return * @return
*
* @throws IOException
*/ */
public abstract Template getFailureTemplate() throws IOException; public abstract Template getFailureTemplate() throws IOException;
@@ -101,6 +103,14 @@ public abstract class TemplateTestBase
execute(template); execute(template);
} }
/**
* Method description
*
*
* @param env
*/
protected void prepareEnv(Map<String, Object> env) {}
/** /**
* Method description * Method description
* *
@@ -113,10 +123,12 @@ public abstract class TemplateTestBase
*/ */
private String execute(Template template) throws IOException private String execute(Template template) throws IOException
{ {
Map<String, String> env = Maps.newHashMap(); Map<String, Object> env = Maps.newHashMap();
env.put("name", "marvin"); env.put("name", "marvin");
prepareEnv(env);
StringWriter writer = new StringWriter(); StringWriter writer = new StringWriter();
template.execute(writer, env); template.execute(writer, env);

View File

@@ -1 +1 @@
Hello {{/name}}! Hello {{test}}!