Engine _engine = engine;
if ("extends_default.httl".equals(templateName)) {
_engine = Engine.getEngine("httl-comment-extends.properties");
}
Template template = _engine.getTemplate("/templates/" + templateName, Locale.CHINA, encoding, data);
UnsafeByteArrayOutputStream actualStream = new UnsafeByteArrayOutputStream();
StringWriter actualWriter = new StringWriter();
if ("extends_var.httl".equals(templateName)) {
if (data instanceof Map) {
((Map<String, Object>) data).put("extends", "default.httl");
} else if (data instanceof Model) {
((Model) data).setExtends("default.httl");
}
}
try {
template.render(data, actualWriter);
template.render(data, actualStream);
} catch (Throwable e) {
System.out.println("\n================================\n" + config + ": " + template.getName() + "\n================================\n");
e.printStackTrace();
throw new IllegalStateException(e.getMessage() + "\n================================\n" + config + ": " + template.getName() + "\n================================\n", e);
}
if ("extends_var.httl".equals(templateName)) {
if (data instanceof Map) {
((Map<String, Object>) data).remove("extends");
} else if (data instanceof Model) {
((Model) data).setExtends(null);
}
}
if (data != null && ! (data instanceof String)) { // FIXME JSON数据的Map没有排序,导致断言失败,暂先跳过
URL url = this.getClass().getClassLoader().getResource(dir + "results/" + templateName + ".txt");
if (url == null) {
throw new FileNotFoundException("Not found file: " + dir + "results/" + templateName + ".txt");
}
File result = new File(url.getFile());
if (! result.exists()) {
throw new FileNotFoundException("Not found file: " + result.getAbsolutePath());
}
String expected = IOUtils.readToString(new InputStreamReader(new FileInputStream(result), encoding));
expected = expected.replace("\r", "");
if ("httl-comment-text.properties".equals(config)
&& ! template.getSource().contains("read(")) {
expected = expected.replace("<!--", "").replace("-->", "");
}
assertEquals(templateName, expected, actualWriter.getBuffer().toString().replace("\r", ""));
assertEquals(templateName, expected, new String(actualStream.toByteArray()).replace("\r", ""));
if ("set_parameters.httl".equals(templateName)) {
assertEquals(templateName, "abc", Context.getContext().get("title"));
}
}
}