Search in sources :

Example 51 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateConfigurationTest method testConfigureParserTooLowIcI.

@Test
public void testConfigureParserTooLowIcI() throws Exception {
    Configuration cfgWithTooLowIcI = new Configuration(Configuration.VERSION_2_3_21);
    for (PropertyDescriptor propDesc : getTemplateConfigurationSettingPropDescs(true, false)) {
        TemplateConfiguration tc = new TemplateConfiguration();
        String propName = propDesc.getName();
        Object value = SETTING_ASSIGNMENTS.get(propName);
        propDesc.getWriteMethod().invoke(tc, value);
        boolean shouldFail;
        if (CONFIGURABLE_PROP_NAMES.contains(propName)) {
            shouldFail = true;
        } else if (PARSER_PROP_NAMES.contains(propName)) {
            shouldFail = false;
        } else {
            fail("Uncategorized property: " + propName);
            return;
        }
        try {
            tc.setParentConfiguration(cfgWithTooLowIcI);
            if (shouldFail) {
                fail("Should fail with property: " + propName);
            }
        } catch (IllegalStateException e) {
            if (!shouldFail) {
                throw e;
            }
            assertThat(e.getMessage(), containsString("2.3.22"));
        }
    }
}
Also used : Configuration(freemarker.template.Configuration) PropertyDescriptor(java.beans.PropertyDescriptor) Test(org.junit.Test)

Example 52 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateConfigurationTest method testSetParentConfiguration.

@Test
public void testSetParentConfiguration() throws IOException {
    TemplateConfiguration tc = new TemplateConfiguration();
    Template t = new Template(null, "", DEFAULT_CFG);
    try {
        tc.apply(t);
        fail();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Configuration"));
    }
    tc.setParent(DEFAULT_CFG);
    try {
        tc.setParentConfiguration(new Configuration());
        fail();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Configuration"));
    }
    try {
        // Same as setParentConfiguration
        tc.setParent(new Configuration());
        fail();
    } catch (IllegalStateException e) {
        assertThat(e.getMessage(), containsString("Configuration"));
    }
    try {
        tc.setParentConfiguration(null);
        fail();
    } catch (NullArgumentException e) {
    // exected
    }
    tc.setParent(DEFAULT_CFG);
    tc.apply(t);
}
Also used : Configuration(freemarker.template.Configuration) NullArgumentException(freemarker.template.utility.NullArgumentException) Template(freemarker.template.Template) Test(org.junit.Test)

Example 53 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateConfigurationWithTemplateCacheTest method testPlainText.

@Test
public void testPlainText() throws Exception {
    Configuration cfg = createCommonEncodingTesterConfig();
    cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_22);
    TemplateConfiguration tcDE = new TemplateConfiguration();
    tcDE.setLocale(Locale.GERMANY);
    TemplateConfiguration tcYN = new TemplateConfiguration();
    tcYN.setBooleanFormat("Y,N");
    cfg.setTemplateConfigurations(new MergingTemplateConfigurationFactory(cfg.getTemplateConfigurations(), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcDE), new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcYN)));
    {
        Template t = cfg.getTemplate("utf8.ftl", null, null, false);
        assertEquals("utf-8", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
        assertEquals(Locale.US, t.getLocale());
        assertEquals("true,false", t.getBooleanFormat());
    }
    {
        Template t = cfg.getTemplate("utf8.ftl", null, "iso-8859-1", false);
        assertEquals("utf-8", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
    }
    {
        Template t = cfg.getTemplate("utf16.ftl", null, null, false);
        assertEquals("utf-16", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
        assertEquals(Locale.GERMANY, t.getLocale());
        assertEquals("Y,N", t.getBooleanFormat());
    }
    {
        Template t = cfg.getTemplate("default.ftl", null, null, false);
        assertEquals("iso-8859-1", t.getEncoding());
        assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t));
    }
}
Also used : MergingTemplateConfigurationFactory(freemarker.cache.MergingTemplateConfigurationFactory) Configuration(freemarker.template.Configuration) FileNameGlobMatcher(freemarker.cache.FileNameGlobMatcher) ConditionalTemplateConfigurationFactory(freemarker.cache.ConditionalTemplateConfigurationFactory) Template(freemarker.template.Template) Test(org.junit.Test)

Example 54 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateLevelSettings method renderWith.

private String renderWith(Version version, String mainBoolFmt, String incBoolFmt, String impBoolFtm) throws IOException, TemplateException {
    Configuration cfg = new Configuration(version);
    cfg.setTemplateLoader(TEMPLATES);
    cfg.setCacheStorage(new StrongCacheStorage());
    cfg.setBooleanFormat("C,c");
    if (incBoolFmt != null) {
        cfg.getTemplate(INCLUDED_FTL).setBooleanFormat(incBoolFmt);
    }
    if (impBoolFtm != null) {
        cfg.getTemplate(IMPORTED_FTL).setBooleanFormat(impBoolFtm);
    }
    Template t = cfg.getTemplate(MAIN_FTL);
    if (mainBoolFmt != null) {
        t.setBooleanFormat(mainBoolFmt);
    }
    StringWriter sw = new StringWriter();
    t.process(null, sw);
    return sw.toString();
}
Also used : Configuration(freemarker.template.Configuration) StringWriter(java.io.StringWriter) StrongCacheStorage(freemarker.cache.StrongCacheStorage) Template(freemarker.template.Template)

Example 55 with Configuration

use of freemarker.template.Configuration in project freemarker by apache.

the class TemplateNameSpecialVariablesTest method setup.

@Before
public void setup() {
    Configuration cfg = getConfiguration();
    cfg.setWhitespaceStripping(false);
}
Also used : Configuration(freemarker.template.Configuration) Before(org.junit.Before)

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