use of freemarker.template.Configuration in project freemarker by apache.
the class StringLiteralInterpolationTest method markup.
@Test
public void markup() throws IOException, TemplateException {
Configuration cfg = getConfiguration();
cfg.setCustomNumberFormats(Collections.singletonMap("G", PrintfGTemplateNumberFormatFactory.INSTANCE));
cfg.setNumberFormat("@G 3");
assertOutput("${\"${1000}\"}", "1.00*10<sup>3</sup>");
assertOutput("${\"&_${1000}\"}", "&_1.00*10<sup>3</sup>");
assertOutput("${\"${1000}_&\"}", "1.00*10<sup>3</sup>_&");
assertOutput("${\"${1000}, ${2000}\"}", "1.00*10<sup>3</sup>, 2.00*10<sup>3</sup>");
assertOutput("${\"& ${'x'}, ${2000}\"}", "& x, 2.00*10<sup>3</sup>");
assertOutput("${\"& ${'x'}, #{2000}\"}", "& x, 2000");
assertOutput("${\"${2000}\"?isMarkupOutput?c}", "true");
assertOutput("${\"x ${2000}\"?isMarkupOutput?c}", "true");
assertOutput("${\"${2000} x\"?isMarkupOutput?c}", "true");
assertOutput("${\"#{2000}\"?isMarkupOutput?c}", "false");
assertOutput("${\"${'x'}\"?isMarkupOutput?c}", "false");
assertOutput("${\"x ${'x'}\"?isMarkupOutput?c}", "false");
assertOutput("${\"${'x'} x\"?isMarkupOutput?c}", "false");
addToDataModel("rtf", RTFOutputFormat.INSTANCE.fromMarkup("\\p"));
assertOutput("${\"${rtf}\"?isMarkupOutput?c}", "true");
assertErrorContains("${\"${1000}${rtf}\"}", TemplateException.class, "HTML", "RTF", "onversion");
assertErrorContains("x${\"${1000}${rtf}\"}", TemplateException.class, "HTML", "RTF", "onversion");
}
use of freemarker.template.Configuration in project freemarker by apache.
the class TabSizeTest method createConfiguration.
@Override
protected Configuration createConfiguration() throws Exception {
Configuration cfg = super.createConfiguration();
cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_22);
return cfg;
}
use of freemarker.template.Configuration in project freemarker by apache.
the class TagSyntaxVariationsTest method test.
public final void test() throws TemplateException, IOException {
Configuration cfgBuggy = new Configuration();
// Default on 2.3.x: cfgBuggy.setEmulate23ParserBugs(true);
// Default on 2.3.x: cfgBuggy.setTagSyntax(Configuration.ANGLE_BRACKET_TAG_SYNTAX);
Configuration cfgFixed = new Configuration();
cfgFixed.setIncompatibleImprovements(Configuration.VERSION_2_3_19);
// Permutations
for (int ifOrAssign = 0; ifOrAssign < 2; ifOrAssign++) {
String dir_ang = ifOrAssign == 0 ? IF_ANG : ASSIGN_ANG;
String dir_squ = ifOrAssign == 0 ? IF_SQU : ASSIGN_SQU;
String dir_out = ifOrAssign == 0 ? IF_OUT : ASSIGN_OUT;
// Permutations
for (int angOrSqu = 0; angOrSqu < 2; angOrSqu++) {
cfgBuggy.setTagSyntax(angOrSqu == 0 ? Configuration.ANGLE_BRACKET_TAG_SYNTAX : Configuration.SQUARE_BRACKET_TAG_SYNTAX);
cfgFixed.setTagSyntax(angOrSqu == 0 ? Configuration.ANGLE_BRACKET_TAG_SYNTAX : Configuration.SQUARE_BRACKET_TAG_SYNTAX);
String dir_xxx = angOrSqu == 0 ? dir_ang : dir_squ;
String cust_xxx = angOrSqu == 0 ? CUST_ANG : CUST_SQU;
String hdr_xxx = angOrSqu == 0 ? HDR_ANG : HDR_SQU;
String wrong_xxx = angOrSqu == 0 ? WRONG_ANG : WRONG_SQU;
String wrongc_xxx = angOrSqu == 0 ? WRONGC_ANG : WRONGC_SQU;
test(cfgBuggy, dir_xxx + cust_xxx, dir_out + CUST_OUT);
test(cfgFixed, dir_xxx + cust_xxx, dir_out + CUST_OUT);
// Permutations
for (int wrongOrWrongc = 0; wrongOrWrongc < 2; wrongOrWrongc++) {
String wrongx_xxx = wrongOrWrongc == 0 ? wrong_xxx : wrongc_xxx;
// Bug: initial unknown # tags are treated as static text
test(cfgBuggy, wrongx_xxx + dir_xxx, wrongx_xxx + dir_out);
test(cfgFixed, wrongx_xxx + dir_xxx, null);
// Bug: same as above
test(cfgBuggy, wrongx_xxx + wrongx_xxx + dir_xxx, wrongx_xxx + wrongx_xxx + dir_out);
test(cfgBuggy, dir_xxx + wrongx_xxx, null);
test(cfgFixed, dir_xxx + wrongx_xxx, null);
test(cfgBuggy, hdr_xxx + wrongx_xxx, null);
test(cfgFixed, hdr_xxx + wrongx_xxx, null);
test(cfgBuggy, cust_xxx + wrongx_xxx + dir_xxx, null);
test(cfgFixed, cust_xxx + wrongx_xxx + dir_xxx, null);
}
// for wrongc
}
// for squ
cfgBuggy.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
cfgFixed.setTagSyntax(Configuration.AUTO_DETECT_TAG_SYNTAX);
for (int perm = 0; perm < 4; perm++) {
// All 4 permutations
String wrong_xxx = (perm & 1) == 0 ? WRONG_ANG : WRONG_SQU;
String dir_xxx = (perm & 2) == 0 ? dir_ang : dir_squ;
// Bug: Auto-detection ignores unknown # tags
test(cfgBuggy, wrong_xxx + dir_xxx, wrong_xxx + dir_out);
test(cfgFixed, wrong_xxx + dir_xxx, null);
// Bug: same as above
test(cfgBuggy, wrong_xxx + wrong_xxx + dir_xxx, wrong_xxx + wrong_xxx + dir_out);
}
// Permutations
for (int angOrSquStart = 0; angOrSquStart < 2; angOrSquStart++) {
String hdr_xxx = angOrSquStart == 0 ? HDR_ANG : HDR_SQU;
String cust_xxx = angOrSquStart == 0 ? CUST_ANG : CUST_SQU;
String wrong_yyy = angOrSquStart != 0 ? WRONG_ANG : WRONG_SQU;
String dir_xxx = angOrSquStart == 0 ? dir_ang : dir_squ;
String dir_yyy = angOrSquStart != 0 ? dir_ang : dir_squ;
test(cfgBuggy, cust_xxx + wrong_yyy + dir_xxx, CUST_OUT + wrong_yyy + dir_out);
test(cfgFixed, cust_xxx + wrong_yyy + dir_xxx, CUST_OUT + wrong_yyy + dir_out);
test(cfgBuggy, hdr_xxx + wrong_yyy + dir_xxx, wrong_yyy + dir_out);
test(cfgFixed, hdr_xxx + wrong_yyy + dir_xxx, wrong_yyy + dir_out);
test(cfgBuggy, cust_xxx + wrong_yyy + dir_yyy, CUST_OUT + wrong_yyy + dir_yyy);
test(cfgFixed, cust_xxx + wrong_yyy + dir_yyy, CUST_OUT + wrong_yyy + dir_yyy);
test(cfgBuggy, hdr_xxx + wrong_yyy + dir_yyy, wrong_yyy + dir_yyy);
test(cfgFixed, hdr_xxx + wrong_yyy + dir_yyy, wrong_yyy + dir_yyy);
test(cfgBuggy, dir_xxx + wrong_yyy + dir_yyy, dir_out + wrong_yyy + dir_yyy);
test(cfgFixed, dir_xxx + wrong_yyy + dir_yyy, dir_out + wrong_yyy + dir_yyy);
}
// for squStart
}
// for assign
}
use of freemarker.template.Configuration in project freemarker by apache.
the class UncheckedExceptionHandlingTest method testNoBackwardCompatible.
@Test
public void testNoBackwardCompatible() {
Configuration cfg = getConfiguration();
cfg.setWrapUncheckedExceptions(true);
for (Version ici : new Version[] { Configuration.VERSION_2_3_26, Configuration.VERSION_2_3_27 }) {
cfg.setIncompatibleImprovements(ici);
assertThat(assertErrorContains("${f()}", TemplateException.class, "thrown an unchecked").getCause(), instanceOf(MyUncheckedException.class));
assertThat(assertErrorContains("<@d />", TemplateException.class, "thrown an unchecked").getCause(), instanceOf(MyUncheckedException.class));
assertThat(assertErrorContains("${f('NPE')}", TemplateException.class, "thrown an unchecked").getCause(), instanceOf(NullPointerException.class));
assertThat(assertErrorContains("<@d type='NPE' />", TemplateException.class, "thrown an unchecked").getCause(), instanceOf(NullPointerException.class));
}
}
use of freemarker.template.Configuration in project freemarker by apache.
the class UncheckedExceptionHandlingTest method testFlowControlWorks.
@Test
public void testFlowControlWorks() throws IOException, TemplateException {
Configuration cfg = getConfiguration();
for (boolean wrapUnchecked : new boolean[] { false, true }) {
cfg.setWrapUncheckedExceptions(wrapUnchecked);
assertOutput("<#list 1..2 as i>a<@fd>b<#break>c</@>d</#list>.", "ab.");
assertOutput("<#list 1..2 as i>a<@fd>b<#continue>c</@>d</#list>.", "abab.");
assertOutput("<#function f()><@fd><#return 1></@></#function>${f()}.", "1.");
}
}
Aggregations