Search in sources :

Example 86 with Configuration

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

the class TemplateCacheTest method testZeroUpdateDelay.

@Test
public void testZeroUpdateDelay() throws IOException {
    Configuration cfg = new Configuration();
    cfg.setLocale(Locale.US);
    cfg.setCacheStorage(new StrongCacheStorage());
    StringTemplateLoader loader = new StringTemplateLoader();
    cfg.setTemplateLoader(loader);
    cfg.setTemplateUpdateDelay(0);
    for (int i = 1; i <= 3; i++) {
        loader.putTemplate("t.ftl", "v" + i, i);
        assertEquals("v" + i, cfg.getTemplate("t.ftl").toString());
    }
    loader.putTemplate("t.ftl", "v10", 10);
    assertEquals("v10", cfg.getTemplate("t.ftl").toString());
    // same time stamp, different content
    loader.putTemplate("t.ftl", "v11", 10);
    // still v10
    assertEquals("v10", cfg.getTemplate("t.ftl").toString());
    // still v10
    assertEquals("v10", cfg.getTemplate("t.ftl").toString());
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test)

Example 87 with Configuration

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

the class TemplateCacheTest method testWrongEncodingReload.

@Test
public void testWrongEncodingReload() throws IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    cfg.setLocale(Locale.US);
    MonitoredTemplateLoader tl = new MonitoredTemplateLoader();
    tl.putTemplate("utf-8_en.ftl", "<#ftl encoding='utf-8'>Foo");
    tl.putTemplate("utf-8.ftl", "Bar");
    cfg.setTemplateLoader(tl);
    {
        Template t = cfg.getTemplate("utf-8.ftl", "Utf-8");
        assertEquals("utf-8.ftl", t.getName());
        assertEquals("utf-8_en.ftl", t.getSourceName());
        assertEquals("Utf-8", t.getEncoding());
        assertEquals("Foo", t.toString());
        assertEquals(ImmutableList.of(new FindTemplateSourceEvent("utf-8_en_US.ftl", false), new FindTemplateSourceEvent("utf-8_en.ftl", true), new GetLastModifiedEvent("utf-8_en.ftl"), // Attempt 1
        new GetReaderEvent("utf-8_en.ftl"), new CloseTemplateSourceEvent("utf-8_en.ftl")), tl.getEvents());
    }
    {
        tl.clearEvents();
        Template t = cfg.getTemplate("utf-8.ftl", "Utf-16");
        assertEquals("utf-8.ftl", t.getName());
        assertEquals("utf-8_en.ftl", t.getSourceName());
        assertEquals("utf-8", t.getEncoding());
        assertEquals("Foo", t.toString());
        assertEquals(ImmutableList.of(new FindTemplateSourceEvent("utf-8_en_US.ftl", false), new FindTemplateSourceEvent("utf-8_en.ftl", true), new GetLastModifiedEvent("utf-8_en.ftl"), // Attempt 1
        new GetReaderEvent("utf-8_en.ftl"), // Attempt 2
        new GetReaderEvent("utf-8_en.ftl"), new CloseTemplateSourceEvent("utf-8_en.ftl")), tl.getEvents());
    }
}
Also used : GetReaderEvent(freemarker.test.MonitoredTemplateLoader.GetReaderEvent) Configuration(freemarker.template.Configuration) CloseTemplateSourceEvent(freemarker.test.MonitoredTemplateLoader.CloseTemplateSourceEvent) FindTemplateSourceEvent(freemarker.test.MonitoredTemplateLoader.FindTemplateSourceEvent) MonitoredTemplateLoader(freemarker.test.MonitoredTemplateLoader) Template(freemarker.template.Template) GetLastModifiedEvent(freemarker.test.MonitoredTemplateLoader.GetLastModifiedEvent) Test(org.junit.Test)

Example 88 with Configuration

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

the class TemplateNameFormatTest method testBackslashNotSpecialWith23.

@Test
public void testBackslashNotSpecialWith23() throws MalformedTemplateNameException, ParseException, IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
    MonitoredTemplateLoader tl = new MonitoredTemplateLoader();
    tl.putTemplate("foo\\bar.ftl", "");
    cfg.setTemplateLoader(tl);
    {
        final String name = "foo\\bar.ftl";
        Template t = cfg.getTemplate(name, Locale.US);
        assertEquals(name, t.getName());
        assertEquals(name, t.getSourceName());
        assertEquals(ImmutableList.of("foo\\bar_en_US.ftl", "foo\\bar_en.ftl", name), tl.getNamesSearched());
        tl.clearEvents();
    }
    try {
        cfg.getTemplate("foo\\missing.ftl", Locale.US);
        fail();
    } catch (TemplateNotFoundException e) {
        assertEquals("foo\\missing.ftl", e.getTemplateName());
        assertEquals(ImmutableList.of("foo\\missing_en_US.ftl", "foo\\missing_en.ftl", "foo\\missing.ftl"), tl.getNamesSearched());
        tl.clearEvents();
        cfg.clearTemplateCache();
    }
    {
        final String name = "foo/bar\\..\\bar.ftl";
        try {
            cfg.getTemplate(name, Locale.US);
            fail();
        } catch (TemplateNotFoundException e) {
            assertEquals(name, e.getTemplateName());
        }
    }
}
Also used : Configuration(freemarker.template.Configuration) TemplateNotFoundException(freemarker.template.TemplateNotFoundException) MonitoredTemplateLoader(freemarker.test.MonitoredTemplateLoader) Template(freemarker.template.Template) Test(org.junit.Test)

Example 89 with Configuration

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

the class ASTPrinter method getASTAsString.

public static String getASTAsString(String templateName, String ftl, Options opts) throws IOException {
    Configuration cfg = new Configuration();
    Template t = new Template(templateName, ftl, cfg);
    return getASTAsString(t, opts);
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template)

Example 90 with Configuration

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

the class CoercionToTextualTest method setup.

@Before
public void setup() throws TemplateModelException {
    Configuration cfg = getConfiguration();
    cfg.setCustomNumberFormats(Collections.singletonMap("G", PrintfGTemplateNumberFormatFactory.INSTANCE));
    cfg.setCustomDateFormats(Collections.singletonMap("HI", HTMLISOTemplateDateFormatFactory.INSTANCE));
    cfg.setNumberFormat("@G 3");
    cfg.setDateTimeFormat("@HI");
    cfg.setBooleanFormat("y,n");
    addToDataModel("s", "abc");
    addToDataModel("n", 1500);
    addToDataModel("dt", TM);
    addToDataModel("b", Boolean.TRUE);
    addToDataModel("m", HTMLOutputFormat.INSTANCE.fromMarkup("<p>M</p>"));
}
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