下面列出了freemarker.template.TemplateNotFoundException#freemarker.template.MalformedTemplateNameException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void writeStatus(Writer writer, StatusDescription status, int indentSize) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
logger.debug("writeStatus - context: {}, status: {}, description: {}", currentContext, status.getStatus(), status.getDescription());
if(status.getStatus() != PASSED) {
createNode(writer, "Status", NodeType.STATUS, status.getStatus(), null, indentSize);
TemplateWrapper statusTableTemplate = templateWrapperFactory.createWrapper("status_table.ftlh");
statusTableTemplate.setData("status", status.getStatus());
statusTableTemplate.setData("description", status.getDescription());
statusTableTemplate.setData("exception", status.getCause());
statusTableTemplate.setData("id", ++nodeId * 1000);
statusTableTemplate.write(writer, indentSize + 2);
closeNode(writer, indentSize);
}
}
/**
* @param templateFilePath
* @param destFilePath
* @param configuration
* @param model
* @param override
* @param append
* @throws ParseException
* @throws MalformedTemplateNameException
* @throws IOException
* @throws TemplateException
*/
public static void generateFileByFile(String templateFilePath, String destFilePath, Configuration configuration, Object model, boolean override, boolean append)
throws MalformedTemplateNameException, ParseException, IOException, TemplateException {
Template t = configuration.getTemplate(templateFilePath);
File destFile = new File(destFilePath);
if (override || append || !destFile.exists()) {
File parent = destFile.getParentFile();
if (null != parent) {
parent.mkdirs();
}
try (FileOutputStream outputStream = new FileOutputStream(destFile, append); FileLock fileLock = outputStream.getChannel().tryLock();) {
Writer out = new OutputStreamWriter(outputStream, DEFAULT_CHARSET);
t.process(model, out);
}
log.info(destFilePath + " saved!");
} else {
log.error(destFilePath + " already exists!");
}
}
private void writeParametersTable(int id, String messageName, List<ActionParameter> parameters, boolean hasHeaders) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
createNode(testCaseWriter, "Input Parameters", NodeType.INPUT, null, null, 7);
TemplateWrapper testCaseActionParametersTemplate = templateWrapperFactory.createWrapper("test_case_parameters_table.ftlh");
testCaseActionParametersTemplate.setData("tableId", id);
testCaseActionParametersTemplate.setData("message_name", messageName);
testCaseActionParametersTemplate.setData("parameters", parameters);
testCaseActionParametersTemplate.setData("hasHeaders", hasHeaders);
testCaseActionParametersTemplate.write(testCaseWriter, 9);
closeNode(testCaseWriter, 7);
}
private String internalRender(String templateFile, Map<String, Object> data) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
if (!templateFile.endsWith(TEMPLATE_EXTENSION)) {
templateFile += TEMPLATE_EXTENSION;
}
Template template = config.getTemplate(templateFile);
Writer output = new StringWriter();
template.process(data, output);
return output.toString();
}
@Test
public void testGenerateWhenFreemarkerConfigGetTemplateThrowsMalformedTemplateNameExceptionThenCloudConnectorExceptionComesForAppCreationCommandTemplate()
throws IOException, TemplateException {
doThrow(new MalformedTemplateNameException(TEMPLATE_NAME, MALFORMED_TEMPLATE_NAMED_EXCEPTION_DESCRIPTION)).when(freemarkerConfiguration)
.getTemplate(APP_CREATION_COMMAND_TEMPLATE_PATH, ENCODING);
thrown.expect(CloudConnectorException.class);
thrown.expectMessage(format(GENERATE_EXCEPTION_MESSAGE_FORMAT, APP_CREATION_COMMAND_TEMPLATE_PATH));
underTest.generate(DEPLOYMENT_ADDRESS);
verify(template, times(0)).process(any(), any(StringWriter.class));
verify(freemarkerConfiguration, times(1)).getTemplate(anyString(), anyString());
verify(freemarkerConfiguration, times(1)).getTemplate(APP_CREATION_COMMAND_TEMPLATE_PATH, ENCODING);
}
@Test
public void testGenerateWhenFreemarkerConfigGetTemplateThrowsMalformedTemplateNameExceptionThenCloudConnectorExceptionComesForAppCreationJsonTemplate()
throws IOException, TemplateException {
doThrow(new MalformedTemplateNameException(TEMPLATE_NAME, MALFORMED_TEMPLATE_NAMED_EXCEPTION_DESCRIPTION)).when(freemarkerConfiguration)
.getTemplate(APP_CREATION_JSON_TEMPLATE_PATH, ENCODING);
thrown.expect(CloudConnectorException.class);
thrown.expectMessage(format(GENERATE_EXCEPTION_MESSAGE_FORMAT, APP_CREATION_JSON_TEMPLATE_PATH));
underTest.generateJSON(DEPLOYMENT_ADDRESS);
verify(template, times(0)).process(any(), any(StringWriter.class));
verify(freemarkerConfiguration, times(1)).getTemplate(anyString(), anyString());
verify(freemarkerConfiguration, times(1)).getTemplate(APP_CREATION_JSON_TEMPLATE_PATH, ENCODING);
}
public Template createTemplate(String templatePath)
throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
return freemarkerConfig.getTemplate(templatePath);
}
public TemplateWrapper createWrapper(String templateName) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException {
return new TemplateWrapper(configuration.getTemplate(templateName));
}
private void writeElements(Writer writer, List<Object> elements, int indentSize) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
List<Verification> verifications = new ArrayList<>();
for(Object element : elements) {
if(element instanceof Message) {
if(!verifications.isEmpty()) {
writeVerifications(writer, verifications, indentSize);
verifications.clear();
}
writeMessage(writer, (Message)element, indentSize);
} else if(element instanceof Verification) {
verifications.add((Verification)element);
} else if(element instanceof ReportTable) {
if(!verifications.isEmpty()) {
writeVerifications(writer, verifications, indentSize);
verifications.clear();
}
writeTable(writer, null, (ReportTable)element, indentSize);
} else if(element instanceof Action) {
writeAction((Action)element);
} else if (element instanceof ActionGroup) {
ActionGroup group = (ActionGroup) element;
createNode(testCaseWriter, group.getName(), group.getDescription(), NodeType.ACTION, group.getStatus(),
null, 5, null, null, Collections.emptyList(), null, true);
writeElements(writer, group.getElements(), indentSize);
String linkToReport = group.getLinkToReport();
if(StringUtils.isNotBlank(linkToReport)) {
createNode(testCaseWriter, "Report", NodeType.DESCRIPTION, null, null, 7);
writeLine(testCaseWriter, "<a href='" + linkToReport + "'>Link to report</a>", 9);
closeNode(testCaseWriter, 7);
}
closeNode(testCaseWriter, 7);
} else if(element instanceof Throwable) {
writeException(testCaseWriter, (Throwable)element);
} else if(element instanceof ParametersTable) {
ParametersTable table = (ParametersTable)element;
writeParametersTable(table.getId(), table.getMessageName(), table.getParameters(), table.isHasHeaders());
}
}
if(!verifications.isEmpty()) {
writeVerifications(writer, verifications, indentSize);
verifications.clear();
}
}
/**
* 解析电子邮件模板内容.
* @param templateLocation - 电子邮件模板相对路径
* @param model - 电子邮件的附加信息
* @return 解析后的电子邮件内容
* @throws TemplateException
* @throws IOException
* @throws ParseException
* @throws MalformedTemplateNameException
* @throws TemplateNotFoundException
*/
public String getMailContent(String templateLocation, Map<String, Object> model)
throws TemplateNotFoundException, MalformedTemplateNameException,
ParseException, IOException, TemplateException {
model.put("baseUrl", baseUrl);
return FreeMarkerTemplateUtils.processTemplateIntoString(
freeMarkerConfigurer.getConfiguration().getTemplate(templateLocation), model);
}
/**
* @param writer
* @param template
* @param configuration
* @param model
* @throws TemplateNotFoundException
* @throws MalformedTemplateNameException
* @throws ParseException
* @throws IOException
* @throws TemplateException
*/
public static void generateStringByPath(Writer writer, String template, Configuration configuration, Object model)
throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
Template tpl = configuration.getTemplate(template, null, null, null, true, true);
tpl.process(model, writer);
}