Search in sources :

Example 91 with Configuration

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&lt;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}", "&lt;y&gt;");
    assertOutput("${m?esc}", "<p>M</p>");
}
Also used : Configuration(freemarker.template.Configuration) Test(org.junit.Test) TemplateTest(freemarker.test.TemplateTest)

Example 92 with Configuration

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

Example 93 with Configuration

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

Example 94 with Configuration

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

Example 95 with Configuration

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]");
}
Also used : Configuration(freemarker.template.Configuration) Time(java.sql.Time) Timestamp(java.sql.Timestamp) SimpleDate(freemarker.template.SimpleDate) Date(java.util.Date) Template(freemarker.template.Template) 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