Search in sources :

Example 71 with Configuration

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}\"}", "&amp;_1.00*10<sup>3</sup>");
    assertOutput("${\"${1000}_&\"}", "1.00*10<sup>3</sup>_&amp;");
    assertOutput("${\"${1000}, ${2000}\"}", "1.00*10<sup>3</sup>, 2.00*10<sup>3</sup>");
    assertOutput("${\"& ${'x'}, ${2000}\"}", "&amp; 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");
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 72 with Configuration

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;
}
Also used : Configuration(freemarker.template.Configuration)

Example 73 with Configuration

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
}
Also used : Configuration(freemarker.template.Configuration)

Example 74 with Configuration

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));
    }
}
Also used : Configuration(freemarker.template.Configuration) Version(freemarker.template.Version) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 75 with Configuration

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.");
    }
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Aggregations

Configuration (freemarker.template.Configuration)282 Template (freemarker.template.Template)106 Test (org.junit.Test)86 TemplateTest (freemarker.test.TemplateTest)52 HashMap (java.util.HashMap)51 IOException (java.io.IOException)41 StringWriter (java.io.StringWriter)39 File (java.io.File)33 TemplateException (freemarker.template.TemplateException)29 Writer (java.io.Writer)27 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)19 ConditionalTemplateConfigurationFactory (freemarker.cache.ConditionalTemplateConfigurationFactory)19 StringTemplateLoader (freemarker.cache.StringTemplateLoader)18 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)18 FileNameGlobMatcher (freemarker.cache.FileNameGlobMatcher)17 TemplateLoader (freemarker.cache.TemplateLoader)16 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)15 FileWriter (java.io.FileWriter)13 ArrayList (java.util.ArrayList)13 User (org.vcell.util.document.User)13