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());
}
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());
}
}
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());
}
}
}
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);
}
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>"));
}
Aggregations