use of freemarker.template.Configuration in project freemarker by apache.
the class CoercionToTextualTest method testEscBuiltin.
@Test
public void testEscBuiltin() throws IOException, TemplateException {
Configuration cfg = getConfiguration();
cfg.setOutputFormat(HTMLOutputFormat.INSTANCE);
cfg.setAutoEscapingPolicy(Configuration.DISABLE_AUTO_ESCAPING_POLICY);
cfg.setBooleanFormat("<y>,<n>");
assertOutput("${'a<b'?esc}", "a<b");
assertOutput("${n?string?esc}", "1.50E+03");
assertOutput("${n?esc}", "1.50*10<sup>3</sup>");
assertOutput("${dt?string?esc}", "2015-09-06T12:00:00Z");
assertOutput("${dt?esc}", "2015-09-06<span class='T'>T</span>12:00:00Z");
assertOutput("${b?esc}", "<y>");
assertOutput("${m?esc}", "<p>M</p>");
}
use of freemarker.template.Configuration in project freemarker by apache.
the class DateFormatTest method testZeroArgDateBI.
/**
* ?date() and such are new in 2.3.24.
*/
@Test
public void testZeroArgDateBI() throws IOException, TemplateException {
Configuration cfg = getConfiguration();
cfg.setDateFormat("@epoch");
cfg.setDateTimeFormat("@epoch");
cfg.setTimeFormat("@epoch");
addToDataModel("t", String.valueOf(T));
assertOutput("${t?date?string.xs_u} ${t?date()?string.xs_u}", "2015-09-06Z 2015-09-06Z");
assertOutput("${t?time?string.xs_u} ${t?time()?string.xs_u}", "12:00:00Z 12:00:00Z");
assertOutput("${t?datetime?string.xs_u} ${t?datetime()?string.xs_u}", "2015-09-06T12:00:00Z 2015-09-06T12:00:00Z");
}
use of freemarker.template.Configuration in project freemarker by apache.
the class DateFormatTest method testIcIAndEscaping.
@Test
public void testIcIAndEscaping() throws Exception {
Configuration cfg = getConfiguration();
addToDataModel("d", new SimpleDate(new Date(12345678L), TemplateDateModel.DATETIME));
testIcIAndEscapingWhenCustFormsAreAccepted(cfg);
cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_23);
testIcIAndEscapingWhenCustFormsAreAccepted(cfg);
cfg.setCustomDateFormats(Collections.<String, TemplateDateFormatFactory>emptyMap());
cfg.setDateTimeFormat("@epoch");
assertErrorContains("${d}", "\"@epoch\"");
cfg.setDateTimeFormat("'@'yyyy");
assertOutput("${d}", "@1970");
cfg.setDateTimeFormat("@@yyyy");
assertOutput("${d}", "@@1970");
cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_24);
cfg.setDateTimeFormat("@epoch");
assertErrorContains("${d}", "custom", "\"epoch\"");
cfg.setDateTimeFormat("'@'yyyy");
assertOutput("${d}", "@1970");
cfg.setDateTimeFormat("@@yyyy");
assertOutput("${d}", "@@1970");
}
use of freemarker.template.Configuration in project freemarker by apache.
the class DateFormatTest method testAlieses2.
@Test
public void testAlieses2() throws Exception {
Configuration cfg = getConfiguration();
cfg.setCustomDateFormats(ImmutableMap.of("d", new AliasTemplateDateFormatFactory("yyyy-MMM", ImmutableMap.of(new Locale("en"), "yyyy-MMM'_en'", Locale.UK, "yyyy-MMM'_en_GB'", Locale.FRANCE, "yyyy-MMM'_fr_FR'"))));
cfg.setDateTimeFormat("@d");
addToDataModel("d", TM);
assertOutput("<#setting locale='en_US'>${d} " + "<#setting locale='en_GB'>${d} " + "<#setting locale='en_GB_Win'>${d} " + "<#setting locale='fr_FR'>${d} " + "<#setting locale='hu_HU'>${d}", "2015-Sep_en 2015-Sep_en_GB 2015-Sep_en_GB 2015-sept._fr_FR 2015-szept.");
}
use of freemarker.template.Configuration in project freemarker by apache.
the class DateFormatTest method testEnvironmentGetters.
@Test
public void testEnvironmentGetters() throws Exception {
Template t = new Template(null, "", getConfiguration());
Environment env = t.createProcessingEnvironment(null, null);
Configuration cfg = getConfiguration();
String dateFormatStr = "yyyy.MM.dd. (Z)";
String timeFormatStr = "HH:mm";
String dateTimeFormatStr = "yyyy.MM.dd. HH:mm";
cfg.setDateFormat(dateFormatStr);
cfg.setTimeFormat(timeFormatStr);
cfg.setDateTimeFormat(dateTimeFormatStr);
// Test that values are coming from the cache if possible
for (Class dateClass : new Class[] { Date.class, Timestamp.class, java.sql.Date.class, Time.class }) {
for (int dateType : new int[] { TemplateDateModel.DATE, TemplateDateModel.TIME, TemplateDateModel.DATETIME }) {
String formatString = dateType == TemplateDateModel.DATE ? cfg.getDateFormat() : (dateType == TemplateDateModel.TIME ? cfg.getTimeFormat() : cfg.getDateTimeFormat());
TemplateDateFormat expectedF = env.getTemplateDateFormat(formatString, dateType, dateClass);
// Note: Only reads the cache
assertSame(expectedF, env.getTemplateDateFormat(dateType, dateClass));
assertSame(expectedF, env.getTemplateDateFormat(formatString, dateType, dateClass));
assertSame(expectedF, env.getTemplateDateFormat(formatString, dateType, dateClass, cfg.getLocale()));
assertSame(expectedF, env.getTemplateDateFormat(formatString, dateType, dateClass, cfg.getLocale(), cfg.getTimeZone(), cfg.getSQLDateAndTimeTimeZone()));
}
}
String dateFormatStr2 = dateFormatStr + "'!'";
String timeFormatStr2 = timeFormatStr + "'!'";
String dateTimeFormatStr2 = dateTimeFormatStr + "'!'";
assertEquals("2015.09.06. 13:00", env.getTemplateDateFormat(TemplateDateModel.DATETIME, Date.class).formatToPlainText(TM));
assertEquals("2015.09.06. 13:00!", env.getTemplateDateFormat(dateTimeFormatStr2, TemplateDateModel.DATETIME, Date.class).formatToPlainText(TM));
assertEquals("2015.09.06. (+0100)", env.getTemplateDateFormat(TemplateDateModel.DATE, Date.class).formatToPlainText(TM));
assertEquals("2015.09.06. (+0100)!", env.getTemplateDateFormat(dateFormatStr2, TemplateDateModel.DATE, Date.class).formatToPlainText(TM));
assertEquals("13:00", env.getTemplateDateFormat(TemplateDateModel.TIME, Date.class).formatToPlainText(TM));
assertEquals("13:00!", env.getTemplateDateFormat(timeFormatStr2, TemplateDateModel.TIME, Date.class).formatToPlainText(TM));
assertEquals("2015.09.06. 13:00", env.getTemplateDateFormat(TemplateDateModel.DATETIME, Timestamp.class).formatToPlainText(TM));
assertEquals("2015.09.06. 13:00!", env.getTemplateDateFormat(dateTimeFormatStr2, TemplateDateModel.DATETIME, Timestamp.class).formatToPlainText(TM));
assertEquals("2015.09.06. (+0000)", env.getTemplateDateFormat(TemplateDateModel.DATE, java.sql.Date.class).formatToPlainText(TM));
assertEquals("2015.09.06. (+0000)!", env.getTemplateDateFormat(dateFormatStr2, TemplateDateModel.DATE, java.sql.Date.class).formatToPlainText(TM));
assertEquals("12:00", env.getTemplateDateFormat(TemplateDateModel.TIME, Time.class).formatToPlainText(TM));
assertEquals("12:00!", env.getTemplateDateFormat(timeFormatStr2, TemplateDateModel.TIME, Time.class).formatToPlainText(TM));
{
String dateTimeFormatStrLoc = dateTimeFormatStr + " EEEE";
// Gets into cache:
TemplateDateFormat format1 = env.getTemplateDateFormat(dateTimeFormatStrLoc, TemplateDateModel.DATETIME, Date.class);
assertEquals("2015.09.06. 13:00 Sunday", format1.formatToPlainText(TM));
// Different locale (not cached):
assertEquals("2015.09.06. 13:00 Sonntag", env.getTemplateDateFormat(dateTimeFormatStrLoc, TemplateDateModel.DATETIME, Date.class, Locale.GERMANY).formatToPlainText(TM));
// Different locale and zone (not cached):
assertEquals("2015.09.06. 14:00 Sonntag", env.getTemplateDateFormat(dateTimeFormatStrLoc, TemplateDateModel.DATETIME, Date.class, Locale.GERMANY, TimeZone.getTimeZone("GMT+02"), TimeZone.getTimeZone("GMT+03")).formatToPlainText(TM));
// Different locale and zone (not cached):
assertEquals("2015.09.06. 15:00 Sonntag", env.getTemplateDateFormat(dateTimeFormatStrLoc, TemplateDateModel.DATETIME, java.sql.Date.class, Locale.GERMANY, TimeZone.getTimeZone("GMT+02"), TimeZone.getTimeZone("GMT+03")).formatToPlainText(TM));
// Check for corrupted cache:
TemplateDateFormat format2 = env.getTemplateDateFormat(dateTimeFormatStrLoc, TemplateDateModel.DATETIME, Date.class);
assertEquals("2015.09.06. 13:00 Sunday", format2.formatToPlainText(TM));
assertSame(format1, format2);
}
addToDataModel("d", TM);
assertErrorContains("${d?string('[wrong]')}", "format string", "[wrong]");
cfg.setDateFormat("[wrong d]");
cfg.setDateTimeFormat("[wrong dt]");
cfg.setTimeFormat("[wrong t]");
assertErrorContains("${d?date}", "\"date_format\"", "[wrong d]");
assertErrorContains("${d?datetime}", "\"datetime_format\"", "[wrong dt]");
assertErrorContains("${d?time}", "\"time_format\"", "[wrong t]");
}
Aggregations