Repository: incubator-freemarker Updated Branches: refs/heads/3 5062b4ebf -> 27a62ce01
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/ConfigurationTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/ConfigurationTest.java b/src/test/java/org/apache/freemarker/core/ConfigurationTest.java index c62b078..55a40d0 100644 --- a/src/test/java/org/apache/freemarker/core/ConfigurationTest.java +++ b/src/test/java/org/apache/freemarker/core/ConfigurationTest.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.Serializable; import java.io.StringWriter; import java.lang.reflect.Field; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; @@ -88,6 +89,8 @@ import junit.framework.TestCase; public class ConfigurationTest extends TestCase { + private static final Charset ISO_8859_2 = Charset.forName("ISO-8859-2"); + public ConfigurationTest(String name) { super(name); } @@ -248,21 +251,18 @@ public class ConfigurationTest extends TestCase { @SuppressWarnings("boxing") public void testGetTemplateOverloads() throws Exception { final Locale hu = new Locale("hu", "HU"); - final String latin1 = "ISO-8859-1"; - final String latin2 = "ISO-8859-2"; - final String utf8 = "utf-8"; final String tFtl = "t.ftl"; final String tHuFtl = "t_hu.ftl"; final String tEnFtl = "t_en.ftl"; - final String tUtf8Ftl = "t-utf8.ftl"; + final String tUtf8Ftl = "utf8.ftl"; final Serializable custLookupCond = new Serializable() { }; Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); cfg.setLocale(Locale.GERMAN); - cfg.setEncoding(latin1); + cfg.setSourceEncoding(StandardCharsets.ISO_8859_1); TemplateConfiguration huTC = new TemplateConfiguration(); - huTC.setSourceEncoding(latin2); + huTC.setSourceEncoding(ISO_8859_2); cfg.setTemplateConfigurations( new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*_hu.*"), huTC)); @@ -281,7 +281,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tFtl, t.getSourceName()); assertEquals(Locale.GERMAN, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(latin1, t.getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); } { Template t = cfg.getTemplate(tUtf8Ftl); @@ -289,7 +289,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tUtf8Ftl, t.getSourceName()); assertEquals(Locale.GERMAN, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(utf8, t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); } // 2 args: @@ -299,7 +299,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tFtl, t.getSourceName()); assertEquals(Locale.GERMAN, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(latin1, t.getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); } { Template t = cfg.getTemplate(tFtl, (Locale) null); @@ -307,7 +307,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tFtl, t.getSourceName()); assertEquals(Locale.GERMAN, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(latin1, t.getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); } { Template t = cfg.getTemplate(tFtl, Locale.US); @@ -315,7 +315,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tEnFtl, t.getSourceName()); assertEquals(Locale.US, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(latin1, t.getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); } { Template t = cfg.getTemplate(tUtf8Ftl, Locale.US); @@ -323,7 +323,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tUtf8Ftl, t.getSourceName()); assertEquals(Locale.US, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(utf8, t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); } { Template t = cfg.getTemplate(tFtl, hu); @@ -331,7 +331,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tHuFtl, t.getSourceName()); assertEquals(hu, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(latin2, t.getSourceEncoding()); + assertEquals(ISO_8859_2, t.getSourceEncoding()); } { Template t = cfg.getTemplate(tUtf8Ftl, hu); @@ -339,7 +339,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tUtf8Ftl, t.getSourceName()); assertEquals(hu, t.getLocale()); assertNull(t.getCustomLookupCondition()); - assertEquals(utf8, t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); } // 4 args: @@ -356,7 +356,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tHuFtl, t.getSourceName()); assertEquals(hu, t.getLocale()); assertEquals(custLookupCond, t.getCustomLookupCondition()); - assertEquals(latin2, t.getSourceEncoding()); + assertEquals(ISO_8859_2, t.getSourceEncoding()); assertOutputEquals("1", t); } { @@ -365,7 +365,7 @@ public class ConfigurationTest extends TestCase { assertEquals(tFtl, t.getSourceName()); assertEquals(Locale.GERMAN, t.getLocale()); assertEquals(custLookupCond, t.getCustomLookupCondition()); - assertEquals(latin1, t.getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); assertOutputEquals("1", t); } } @@ -1336,24 +1336,22 @@ public class ConfigurationTest extends TestCase { public void testDefaultEncodingSetting() throws TemplateException, ConfigurationException { Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - String defaultFileEncoding = System.getProperty("file.encoding"); - assertNotNull(defaultFileEncoding); - - assertEquals(defaultFileEncoding, cfg.getSourceEncoding()); - assertFalse(cfg.isDefaultEncodingExplicitlySet()); + assertEquals(Charset.defaultCharset(), cfg.getSourceEncoding()); + assertFalse(cfg.isSourceEncodingExplicitlySet()); - String nonDefault = defaultFileEncoding.equalsIgnoreCase("UTF-8") ? "ISO-8859-1" : "UTF-8"; - cfg.setEncoding(nonDefault); - assertTrue(cfg.isDefaultEncodingExplicitlySet()); + Charset nonDefault = Charset.defaultCharset().equals(StandardCharsets.UTF_8) ? StandardCharsets.ISO_8859_1 + : StandardCharsets.UTF_8; + cfg.setSourceEncoding(nonDefault); + assertTrue(cfg.isSourceEncodingExplicitlySet()); assertEquals(nonDefault, cfg.getSourceEncoding()); - cfg.unsetDefaultEncoding(); - assertEquals(defaultFileEncoding, cfg.getSourceEncoding()); - assertFalse(cfg.isDefaultEncodingExplicitlySet()); + cfg.unsetSourceEncoding(); + assertEquals(Charset.defaultCharset(), cfg.getSourceEncoding()); + assertFalse(cfg.isSourceEncodingExplicitlySet()); cfg.setSetting(Configuration.SOURCE_ENCODING_KEY, "JVM default"); - assertEquals(defaultFileEncoding, cfg.getSourceEncoding()); - assertTrue(cfg.isDefaultEncodingExplicitlySet()); + assertEquals(Charset.defaultCharset(), cfg.getSourceEncoding()); + assertTrue(cfg.isSourceEncodingExplicitlySet()); } public void testTimeZoneSetting() throws TemplateException, ConfigurationException { @@ -1453,10 +1451,10 @@ public class ConfigurationTest extends TestCase { public void testSetSettingSupportsBothNamingConventions() throws Exception { Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setSetting(Configuration.SOURCE_ENCODING_KEY_CAMEL_CASE, "UTF-16LE"); - assertEquals("UTF-16LE", cfg.getSourceEncoding()); - cfg.setSetting(Configuration.SOURCE_ENCODING_KEY_SNAKE_CASE, "UTF-8"); - assertEquals("UTF-8", cfg.getSourceEncoding()); + cfg.setSetting(Configuration.SOURCE_ENCODING_KEY_CAMEL_CASE, StandardCharsets.UTF_16LE.name()); + assertEquals(StandardCharsets.UTF_16LE, cfg.getSourceEncoding()); + cfg.setSetting(Configuration.SOURCE_ENCODING_KEY_SNAKE_CASE, StandardCharsets.UTF_8.name()); + assertEquals(StandardCharsets.UTF_8, cfg.getSourceEncoding()); for (String nameCC : cfg.getSettingNames(true)) { for (String value : new String[] { "1", "default", "true" }) { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/EncodingOverrideTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/EncodingOverrideTest.java b/src/test/java/org/apache/freemarker/core/EncodingOverrideTest.java index d1b70eb..7648624 100644 --- a/src/test/java/org/apache/freemarker/core/EncodingOverrideTest.java +++ b/src/test/java/org/apache/freemarker/core/EncodingOverrideTest.java @@ -23,6 +23,8 @@ import static org.junit.Assert.*; import java.io.IOException; import java.io.StringWriter; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collections; import org.junit.Test; @@ -30,36 +32,29 @@ import org.junit.Test; public class EncodingOverrideTest { @Test - public void testExactMarchingCharset() throws Exception { - Template t = createConfig("UTF-8").getTemplate("encodingOverride-UTF-8.ftl"); - assertEquals("UTF-8", t.getSourceEncoding()); - checkTempateOutput(t); + public void testMarchingCharset() throws Exception { + Template t = createConfig(StandardCharsets.UTF_8).getTemplate("encodingOverride-UTF-8.ftl"); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); + checkTemplateOutput(t); } @Test - public void testCaseDiffCharset() throws Exception { - Template t = createConfig("utf-8").getTemplate("encodingOverride-UTF-8.ftl"); - assertEquals("utf-8", t.getSourceEncoding()); - checkTempateOutput(t); + public void testDifferentCharset() throws Exception { + Template t = createConfig(StandardCharsets.UTF_8).getTemplate("encodingOverride-ISO-8859-1.ftl"); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); + checkTemplateOutput(t); } - @Test - public void testReallyDiffCharset() throws Exception { - Template t = createConfig("utf-8").getTemplate("encodingOverride-ISO-8859-1.ftl"); - assertEquals("ISO-8859-1", t.getSourceEncoding()); - checkTempateOutput(t); - } - - private void checkTempateOutput(Template t) throws TemplateException, IOException { + private void checkTemplateOutput(Template t) throws TemplateException, IOException { StringWriter out = new StringWriter(); t.process(Collections.emptyMap(), out); assertEquals("Béka", out.toString()); } - private Configuration createConfig(String charset) { + private Configuration createConfig(Charset charset) { Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); cfg.setClassForTemplateLoading(EncodingOverrideTest.class, ""); - cfg.setEncoding(charset); + cfg.setSourceEncoding(charset); return cfg; } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/ObjectBuilderSettingsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/ObjectBuilderSettingsTest.java b/src/test/java/org/apache/freemarker/core/ObjectBuilderSettingsTest.java index 10e4dfd..73e03f6 100644 --- a/src/test/java/org/apache/freemarker/core/ObjectBuilderSettingsTest.java +++ b/src/test/java/org/apache/freemarker/core/ObjectBuilderSettingsTest.java @@ -28,6 +28,9 @@ import java.io.Serializable; import java.io.Writer; import java.math.BigDecimal; import java.math.BigInteger; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -379,7 +382,7 @@ public class ObjectBuilderSettingsTest { assertEquals(DummyCacheStorage.class, cfg.getCacheStorage().getClass()); assertEquals(DummyNewBuiltinClassResolver.class, cfg.getNewBuiltinClassResolver().getClass()); assertEquals(DummyTemplateLoader.class, cfg.getTemplateLoader().getClass()); - assertEquals("utf-8", cfg.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, cfg.getSourceEncoding()); } { @@ -401,7 +404,7 @@ public class ObjectBuilderSettingsTest { ((DefaultObjectWrapper) cfg.getObjectWrapper()).getIncompatibleImprovements()); assertEquals(500, ((MruCacheStorage) cfg.getCacheStorage()).getSoftSizeLimit()); assertEquals(TemplateClassResolver.ALLOWS_NOTHING_RESOLVER, cfg.getNewBuiltinClassResolver()); - assertEquals("utf-8", cfg.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, cfg.getSourceEncoding()); } { @@ -449,7 +452,27 @@ public class ObjectBuilderSettingsTest { allOf(containsStringIgnoringCase("unrecognized"), containsString("foobar"))); } } - + + @Test + public void charsetTest() throws _ObjectBuilderSettingEvaluationException, ClassNotFoundException, + InstantiationException, IllegalAccessException { + for (String timeZoneId : new String[] { "uTf-8", "GMT", "UTC" }) { + TestBean8 result = (TestBean8) _ObjectBuilderSettingEvaluator.eval( + "org.apache.freemarker.core.ObjectBuilderSettingsTest$TestBean8(charset=Charset('iso-8859-1'))", + TestBean8.class, false, new _SettingEvaluationEnvironment()); + assertEquals(StandardCharsets.ISO_8859_1, result.getCharset()); + } + + try { + _ObjectBuilderSettingEvaluator.eval( + "org.apache.freemarker.core.ObjectBuilderSettingsTest$TestBean8(charset=Charset('noSuchCS'))", + TestBean8.class, false, new _SettingEvaluationEnvironment()); + fail(); + } catch (_ObjectBuilderSettingEvaluationException e) { + assertThat(e.getCause(), instanceOf(UnsupportedCharsetException.class)); + } + } + @Test public void configureBeanTest() throws Exception { final TestBean7 bean = new TestBean7(); @@ -1263,6 +1286,7 @@ public class ObjectBuilderSettingsTest { public static class TestBean8 { private TimeZone timeZone; + private Charset charset; private Object anyObject; private List<?> list; @@ -1273,7 +1297,15 @@ public class ObjectBuilderSettingsTest { public void setTimeZone(TimeZone timeZone) { this.timeZone = timeZone; } - + + public Charset getCharset() { + return charset; + } + + public void setCharset(Charset charset) { + this.charset = charset; + } + public Object getAnyObject() { return anyObject; } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java b/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java index 3d851cf..7cbb0f6 100644 --- a/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java +++ b/src/test/java/org/apache/freemarker/core/TemplateConfigurationTest.java @@ -30,6 +30,7 @@ import java.io.StringReader; import java.io.StringWriter; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; @@ -71,6 +72,8 @@ import com.google.common.collect.ImmutableMap; @SuppressWarnings("boxing") public class TemplateConfigurationTest { + private static final Charset ISO_8859_2 = Charset.forName("ISO-8859-2"); + private final class DummyArithmeticEngine extends ArithmeticEngine { @Override @@ -133,33 +136,13 @@ public class TemplateConfigurationTest { NON_DEFAULT_TZ = tz; } - private static final Locale NON_DEFAULT_LOCALE; - static { - Locale defaultLocale = DEFAULT_CFG.getLocale(); - Locale locale = Locale.GERMAN; - if (locale.equals(defaultLocale)) { - locale = Locale.US; - if (locale.equals(defaultLocale)) { - throw new AssertionError("Couldn't chose a non-default locale"); - } - } - NON_DEFAULT_LOCALE = locale; - } + private static final Locale NON_DEFAULT_LOCALE = + DEFAULT_CFG.getLocale().equals(Locale.US) ? Locale.GERMAN : Locale.US; - private static final String NON_DEFAULT_ENCODING; + private static final Charset NON_DEFAULT_ENCODING = + DEFAULT_CFG.getSourceEncoding().equals(StandardCharsets.UTF_8) ? StandardCharsets.UTF_16LE + : StandardCharsets.UTF_8; - static { - String defaultEncoding = DEFAULT_CFG.getSourceEncoding(); - String encoding = "UTF-16"; - if (encoding.equals(defaultEncoding)) { - encoding = "UTF-8"; - if (encoding.equals(defaultEncoding)) { - throw new AssertionError("Couldn't chose a non-default locale"); - } - } - NON_DEFAULT_ENCODING = encoding; - } - private static final Map<String, Object> SETTING_ASSIGNMENTS; static { @@ -168,7 +151,7 @@ public class TemplateConfigurationTest { // "MutableProcessingConfiguration" settings: SETTING_ASSIGNMENTS.put("APIBuiltinEnabled", true); SETTING_ASSIGNMENTS.put("SQLDateAndTimeTimeZone", NON_DEFAULT_TZ); - SETTING_ASSIGNMENTS.put("URLEscapingCharset", "utf-16"); + SETTING_ASSIGNMENTS.put("URLEscapingCharset", StandardCharsets.UTF_16); SETTING_ASSIGNMENTS.put("autoFlush", false); SETTING_ASSIGNMENTS.put("booleanFormat", "J,N"); SETTING_ASSIGNMENTS.put("dateFormat", "yyyy-#DDD"); @@ -178,7 +161,7 @@ public class TemplateConfigurationTest { SETTING_ASSIGNMENTS.put("newBuiltinClassResolver", TemplateClassResolver.ALLOWS_NOTHING_RESOLVER); SETTING_ASSIGNMENTS.put("numberFormat", "0.0000"); SETTING_ASSIGNMENTS.put("objectWrapper", new RestrictedObjectWrapper.Builder(ICI).build()); - SETTING_ASSIGNMENTS.put("outputEncoding", "utf-16"); + SETTING_ASSIGNMENTS.put("outputEncoding", StandardCharsets.UTF_16); SETTING_ASSIGNMENTS.put("showErrorTips", false); SETTING_ASSIGNMENTS.put("templateExceptionHandler", TemplateExceptionHandler.IGNORE_HANDLER); SETTING_ASSIGNMENTS.put("timeFormat", "@HH:mm"); @@ -710,10 +693,10 @@ public class TemplateConfigurationTest { // assertOutput here, as that hard-coded to create an FTL Template. TemplateConfiguration tc = new TemplateConfiguration(); - tc.setSourceEncoding("ISO-8859-1"); + tc.setSourceEncoding(StandardCharsets.ISO_8859_1); Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setEncoding("utf-8"); + cfg.setSourceEncoding(StandardCharsets.UTF_8); cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher ("latin1.ftl"), tc)); @@ -819,24 +802,24 @@ public class TemplateConfigurationTest { { TemplateConfiguration tc = new TemplateConfiguration(); tc.setParentConfiguration(DEFAULT_CFG); - String outputEncoding = "ISO-8859-2"; + Charset outputEncoding = ISO_8859_2; tc.setOutputEncoding(outputEncoding); String legacyNCFtl = "${r'.output_encoding!\"null\"'?eval}"; String camelCaseNCFtl = "${r'.outputEncoding!\"null\"'?eval}"; // Default is re-auto-detecting in ?eval: - assertOutputWithoutAndWithTC(tc, legacyNCFtl, "null", outputEncoding); - assertOutputWithoutAndWithTC(tc, camelCaseNCFtl, "null", outputEncoding); + assertOutputWithoutAndWithTC(tc, legacyNCFtl, "null", outputEncoding.name()); + assertOutputWithoutAndWithTC(tc, camelCaseNCFtl, "null", outputEncoding.name()); // Force camelCase: tc.setNamingConvention(Configuration.CAMEL_CASE_NAMING_CONVENTION); assertOutputWithoutAndWithTC(tc, legacyNCFtl, "null", null); - assertOutputWithoutAndWithTC(tc, camelCaseNCFtl, "null", outputEncoding); + assertOutputWithoutAndWithTC(tc, camelCaseNCFtl, "null", outputEncoding.name()); // Force legacy: tc.setNamingConvention(Configuration.LEGACY_NAMING_CONVENTION); - assertOutputWithoutAndWithTC(tc, legacyNCFtl, "null", outputEncoding); + assertOutputWithoutAndWithTC(tc, legacyNCFtl, "null", outputEncoding.name()); assertOutputWithoutAndWithTC(tc, camelCaseNCFtl, "null", null); } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/TemplateConfigurationWithDefaltTemplateResolverTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TemplateConfigurationWithDefaltTemplateResolverTest.java b/src/test/java/org/apache/freemarker/core/TemplateConfigurationWithDefaltTemplateResolverTest.java index af3ec9c..f26ce63 100644 --- a/src/test/java/org/apache/freemarker/core/TemplateConfigurationWithDefaltTemplateResolverTest.java +++ b/src/test/java/org/apache/freemarker/core/TemplateConfigurationWithDefaltTemplateResolverTest.java @@ -23,6 +23,8 @@ import static org.junit.Assert.*; import java.io.IOException; import java.io.StringWriter; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Locale; import org.apache.freemarker.core.templateresolver.ConditionalTemplateConfigurationFactory; @@ -40,33 +42,35 @@ public class TemplateConfigurationWithDefaltTemplateResolverTest { private static final Object CUST_ATT_1 = new Object(); private static final Object CUST_ATT_2 = new Object(); + private static final Charset ISO_8859_2 = Charset.forName("ISO-8859-2"); + @Test public void testEncoding() throws Exception { Configuration cfg = createCommonEncodingTesterConfig(); { Template t = cfg.getTemplate("utf8.ftl"); - assertEquals("utf-8", t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); } { Template t = cfg.getTemplate("utf16.ftl"); - assertEquals("utf-16", t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_16LE, t.getSourceEncoding()); assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); } { Template t = cfg.getTemplate("default.ftl"); - assertEquals("iso-8859-1", t.getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); } { Template t = cfg.getTemplate("utf8-latin2.ftl"); - assertEquals("iso-8859-2", t.getSourceEncoding()); + assertEquals(ISO_8859_2, t.getSourceEncoding()); assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); } { Template t = cfg.getTemplate("default-latin2.ftl"); - assertEquals("iso-8859-2", t.getSourceEncoding()); + assertEquals(ISO_8859_2, t.getSourceEncoding()); assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); } } @@ -80,7 +84,7 @@ public class TemplateConfigurationWithDefaltTemplateResolverTest { + "<#include 'utf16.ftl'>" + "<#include 'default.ftl'>" + "<#include 'utf8-latin2.ftl'>" - ).getBytes("iso-8859-1")); + ).getBytes(StandardCharsets.ISO_8859_1)); assertEquals( TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS, getTemplateOutput(cfg.getTemplate("main.ftl"))); @@ -232,23 +236,23 @@ public class TemplateConfigurationWithDefaltTemplateResolverTest { private Configuration createCommonEncodingTesterConfig() throws UnsupportedEncodingException { Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setEncoding("iso-8859-1"); + cfg.setSourceEncoding(StandardCharsets.ISO_8859_1); cfg.setLocale(Locale.US); ByteArrayTemplateLoader tl = new ByteArrayTemplateLoader(); - tl.putTemplate("utf8.ftl", TEXT_WITH_ACCENTS.getBytes("utf-8")); - tl.putTemplate("utf16.ftl", TEXT_WITH_ACCENTS.getBytes("utf-16")); - tl.putTemplate("default.ftl", TEXT_WITH_ACCENTS.getBytes("iso-8859-2")); + tl.putTemplate("utf8.ftl", TEXT_WITH_ACCENTS.getBytes(StandardCharsets.UTF_8)); + tl.putTemplate("utf16.ftl", TEXT_WITH_ACCENTS.getBytes(StandardCharsets.UTF_16LE)); + tl.putTemplate("default.ftl", TEXT_WITH_ACCENTS.getBytes(ISO_8859_2)); tl.putTemplate("utf8-latin2.ftl", - ("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes("iso-8859-2")); + ("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes(ISO_8859_2)); tl.putTemplate("default-latin2.ftl", - ("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes("iso-8859-2")); + ("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes(ISO_8859_2)); cfg.setTemplateLoader(tl); TemplateConfiguration tcUtf8 = new TemplateConfiguration(); - tcUtf8.setSourceEncoding("utf-8"); + tcUtf8.setSourceEncoding(StandardCharsets.UTF_8); TemplateConfiguration tcUtf16 = new TemplateConfiguration(); - tcUtf16.setSourceEncoding("utf-16"); + tcUtf16.setSourceEncoding(StandardCharsets.UTF_16LE); cfg.setTemplateConfigurations( new FirstMatchTemplateConfigurationFactory( new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*utf8*"), tcUtf8), http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/TemplateConstructorsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TemplateConstructorsTest.java b/src/test/java/org/apache/freemarker/core/TemplateConstructorsTest.java index aa011a7..dd73d79 100644 --- a/src/test/java/org/apache/freemarker/core/TemplateConstructorsTest.java +++ b/src/test/java/org/apache/freemarker/core/TemplateConstructorsTest.java @@ -25,81 +25,89 @@ import static org.junit.Assert.*; import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.junit.Test; public class TemplateConstructorsTest { - private static final String READER_CONTENT = "From a reader..."; - private static final String READER_CONTENT_FORCE_UTF8 = "<#ftl encoding='utf-8'>From a reader..."; + private static final String CONTENT = "From a reader..."; + private static final String CONTENT_FORCE_UTF8 = "<#ftl encoding='utf-8'>From a reader..."; @Test public void test() throws IOException { final Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - //cfg.setSourceEncoding("ISO-8859-1"); + cfg.setSourceEncoding(StandardCharsets.ISO_8859_1); final String name = "foo/bar.ftl"; final String sourceName = "foo/bar_de.ftl"; - final String content = "From a String..."; - final String encoding = "UTF-16LE"; + final Charset sourceEncoding = StandardCharsets.UTF_16LE; { Template t = new Template(name, createReader(), cfg); assertEquals(name, t.getName()); assertEquals(name, t.getSourceName()); - assertEquals(READER_CONTENT, t.toString()); + assertEquals(CONTENT, t.toString()); assertNull(t.getSourceEncoding()); } { - Template t = new Template(name, content, cfg); + Template t = new Template(name, CONTENT, cfg); assertEquals(name, t.getName()); assertEquals(name, t.getSourceName()); - assertEquals(content, t.toString()); + assertEquals(CONTENT, t.toString()); assertNull(t.getSourceEncoding()); } { - Template t = new Template(name, createReader(), cfg, encoding); + Template t = new Template(name, CONTENT_FORCE_UTF8, cfg); assertEquals(name, t.getName()); assertEquals(name, t.getSourceName()); - assertEquals(READER_CONTENT, t.toString()); - assertEquals("UTF-16LE", t.getSourceEncoding()); + // assertEquals(CONTENT_FORCE_UTF8, t.toString()); // FIXME the #ftl header is missing from the dump, why? + assertNull(t.getSourceEncoding()); // Because it was created from a String + } + { + Template t = new Template(name, createReader(), cfg, sourceEncoding); + assertEquals(name, t.getName()); + assertEquals(name, t.getSourceName()); + assertEquals(CONTENT, t.toString()); + assertEquals(StandardCharsets.UTF_16LE, t.getSourceEncoding()); } { Template t = new Template(name, sourceName, createReader(), cfg); assertEquals(name, t.getName()); assertEquals(sourceName, t.getSourceName()); - assertEquals(READER_CONTENT, t.toString()); + assertEquals(CONTENT, t.toString()); assertNull(t.getSourceEncoding()); } { - Template t = new Template(name, sourceName, createReader(), cfg, encoding); + Template t = new Template(name, sourceName, createReader(), cfg, sourceEncoding); assertEquals(name, t.getName()); assertEquals(sourceName, t.getSourceName()); - assertEquals(READER_CONTENT, t.toString()); - assertEquals("UTF-16LE", t.getSourceEncoding()); + assertEquals(CONTENT, t.toString()); + assertEquals(StandardCharsets.UTF_16LE, t.getSourceEncoding()); } { - Template t = Template.createPlainTextTemplate(name, content, cfg); + Template t = Template.createPlainTextTemplate(name, CONTENT, cfg); assertEquals(name, t.getName()); assertEquals(name, t.getSourceName()); - assertEquals(content, t.toString()); + assertEquals(CONTENT, t.toString()); assertNull(t.getSourceEncoding()); } { try { - new Template(name, sourceName, createReaderForceUTF8(), cfg, encoding); + new Template(name, sourceName, createReaderForceUTF8(), cfg, sourceEncoding); fail(); } catch (WrongTemplateCharsetException e) { - assertThat(e.getMessage(), containsString("utf-8")); - assertThat(e.getMessage(), containsString(encoding)); + assertThat(e.getMessage(), containsString(StandardCharsets.UTF_8.name())); + assertThat(e.getMessage(), containsString(sourceEncoding.name())); } } } private Reader createReader() { - return new StringReader(READER_CONTENT); + return new StringReader(CONTENT); } private Reader createReaderForceUTF8() { - return new StringReader(READER_CONTENT_FORCE_UTF8); + return new StringReader(CONTENT_FORCE_UTF8); } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java b/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java index 685e6ba..c049cde 100644 --- a/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java +++ b/src/test/java/org/apache/freemarker/core/TemplateGetEncodingTest.java @@ -21,6 +21,7 @@ package org.apache.freemarker.core; import static org.junit.Assert.*; import java.io.IOException; +import java.nio.charset.Charset; import org.apache.freemarker.core.templateresolver.ConditionalTemplateConfigurationFactory; import org.apache.freemarker.core.templateresolver.FileNameGlobMatcher; @@ -30,11 +31,14 @@ import org.junit.Test; public class TemplateGetEncodingTest { + private static final Charset ISO_8859_2 = Charset.forName("ISO-8859-2"); + @Test public void test() throws IOException { + Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); { - cfg.setEncoding("ISO-8859-2"); + cfg.setSourceEncoding(ISO_8859_2); MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putBinaryTemplate("bin", "test"); tl.putBinaryTemplate("bin-static", "<#test>"); @@ -48,8 +52,8 @@ public class TemplateGetEncodingTest { cfg.setCacheStorage(new StrongCacheStorage()); } - assertEquals("ISO-8859-2", cfg.getTemplate("bin").getSourceEncoding()); - assertEquals("ISO-8859-2", cfg.getTemplate("bin-static").getSourceEncoding()); + assertEquals(ISO_8859_2, cfg.getTemplate("bin").getSourceEncoding()); + assertEquals(ISO_8859_2, cfg.getTemplate("bin-static").getSourceEncoding()); assertNull(cfg.getTemplate("text").getSourceEncoding()); assertNull(cfg.getTemplate("text-static").getSourceEncoding()); assertNull(new Template(null, "test", cfg).getSourceEncoding()); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java b/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java index 3605f4b..bfbcd5d 100644 --- a/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java +++ b/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java @@ -273,7 +273,7 @@ public class DefaultTemplateResolverTest { public void testWrongEncodingReload() throws IOException { Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); cfg.setLocale(Locale.US); - cfg.setEncoding("utf-8"); + cfg.setSourceEncoding(StandardCharsets.UTF_8); MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putBinaryTemplate("utf-8_en.ftl", "<#ftl encoding='utf-8'>Béka"); @@ -286,7 +286,7 @@ public class DefaultTemplateResolverTest { Template t = cfg.getTemplate("utf-8.ftl"); assertEquals("utf-8.ftl", t.getName()); assertEquals("utf-8_en.ftl", t.getSourceName()); - assertEquals("utf-8", t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); assertEquals("Béka", t.toString()); assertEquals( @@ -304,7 +304,7 @@ public class DefaultTemplateResolverTest { Template t = cfg.getTemplate("iso-8859-1.ftl"); assertEquals("iso-8859-1.ftl", t.getName()); assertEquals("iso-8859-1_en_US.ftl", t.getSourceName()); - assertEquals("ISO-8859-1", t.getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, t.getSourceEncoding()); assertEquals("Béka", t.toString()); assertEquals( @@ -320,7 +320,7 @@ public class DefaultTemplateResolverTest { public void testNoWrongEncodingForTemplateLoader2WithReader() throws IOException { Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); cfg.setLocale(Locale.US); - cfg.setEncoding("utf-8"); + cfg.setSourceEncoding(StandardCharsets.UTF_8); MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("foo_en.ftl", "<#ftl encoding='utf-8'>Å"); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/manualtest/GettingStartedExample.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/manualtest/GettingStartedExample.java b/src/test/java/org/apache/freemarker/manualtest/GettingStartedExample.java index f03e417..e6a244a 100644 --- a/src/test/java/org/apache/freemarker/manualtest/GettingStartedExample.java +++ b/src/test/java/org/apache/freemarker/manualtest/GettingStartedExample.java @@ -20,6 +20,7 @@ package org.apache.freemarker.manualtest; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -38,7 +39,7 @@ public class GettingStartedExample { /* Create and adjust the configuration singleton */ Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); cfg.setClassForTemplateLoading(GettingStartedExample.class, ""); - cfg.setEncoding("UTF-8"); + cfg.setSourceEncoding(StandardCharsets.UTF_8); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); cfg.setLogTemplateExceptions(false); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/manualtest/TemplateConfigurationExamples.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/manualtest/TemplateConfigurationExamples.java b/src/test/java/org/apache/freemarker/manualtest/TemplateConfigurationExamples.java index 49efff1..3896a2e 100644 --- a/src/test/java/org/apache/freemarker/manualtest/TemplateConfigurationExamples.java +++ b/src/test/java/org/apache/freemarker/manualtest/TemplateConfigurationExamples.java @@ -20,6 +20,7 @@ package org.apache.freemarker.manualtest; import static org.junit.Assert.*; +import java.nio.charset.StandardCharsets; import java.util.Date; import org.apache.freemarker.core.Configuration; @@ -48,7 +49,7 @@ public class TemplateConfigurationExamples extends ExamplesTest { addTemplate("t.xml", ""); TemplateConfiguration tcUTF8XML = new TemplateConfiguration(); - tcUTF8XML.setSourceEncoding("utf-8"); + tcUTF8XML.setSourceEncoding(StandardCharsets.UTF_8); tcUTF8XML.setOutputFormat(XMLOutputFormat.INSTANCE); { @@ -56,7 +57,7 @@ public class TemplateConfigurationExamples extends ExamplesTest { new FileExtensionMatcher("xml"), tcUTF8XML)); Template t = cfg.getTemplate("t.xml"); - assertEquals("utf-8", t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); assertEquals(XMLOutputFormat.INSTANCE, t.getOutputFormat()); } @@ -65,7 +66,7 @@ public class TemplateConfigurationExamples extends ExamplesTest { cfg.setSettings(loadPropertiesFile("TemplateConfigurationExamples1.properties")); Template t = cfg.getTemplate("t.xml"); - assertEquals("utf-8", t.getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, t.getSourceEncoding()); assertEquals(XMLOutputFormat.INSTANCE, t.getOutputFormat()); } } @@ -115,7 +116,7 @@ public class TemplateConfigurationExamples extends ExamplesTest { @Test public void example3() throws Exception { Configuration cfg = getConfiguration(); - cfg.setEncoding("ISO-8859-1"); + cfg.setSourceEncoding(StandardCharsets.ISO_8859_1); cfg.setSharedVariable("ts", new Date(1440431606011L)); addTemplate("t.stats.html", "${ts?datetime} ${ts?date} ${ts?time}"); @@ -131,7 +132,7 @@ public class TemplateConfigurationExamples extends ExamplesTest { tcStats.setTimeZone(_DateUtil.UTC); TemplateConfiguration tcMail = new TemplateConfiguration(); - tcMail.setSourceEncoding("utf-8"); + tcMail.setSourceEncoding(StandardCharsets.UTF_8); TemplateConfiguration tcHTML = new TemplateConfiguration(); tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE); @@ -161,12 +162,12 @@ public class TemplateConfigurationExamples extends ExamplesTest { ); assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat()); - assertEquals("ISO-8859-1", cfg.getTemplate("t.html").getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, cfg.getTemplate("t.html").getSourceEncoding()); assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat()); assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat()); assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.stats.html").getOutputFormat()); assertOutputForNamed("t.stats.html", "2015-08-24T15:53:26.011Z 2015-08-24 15:53:26.011Z"); - assertEquals("utf-8", cfg.getTemplate("mail/t.html").getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, cfg.getTemplate("mail/t.html").getSourceEncoding()); // From properties: @@ -174,12 +175,12 @@ public class TemplateConfigurationExamples extends ExamplesTest { cfg.setSettings(loadPropertiesFile("TemplateConfigurationExamples3.properties")); assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat()); - assertEquals("ISO-8859-1", cfg.getTemplate("t.html").getSourceEncoding()); + assertEquals(StandardCharsets.ISO_8859_1, cfg.getTemplate("t.html").getSourceEncoding()); assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat()); assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat()); assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.stats.html").getOutputFormat()); assertOutputForNamed("t.stats.html", "2015-08-24T15:53:26.011Z 2015-08-24 15:53:26.011Z"); - assertEquals("utf-8", cfg.getTemplate("mail/t.html").getSourceEncoding()); + assertEquals(StandardCharsets.UTF_8, cfg.getTemplate("mail/t.html").getSourceEncoding()); } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java b/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java index daa44a9..d638025 100644 --- a/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java +++ b/src/test/java/org/apache/freemarker/servlet/FreemarkerServletTest.java @@ -23,6 +23,7 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.io.IOException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.Locale; @@ -67,11 +68,13 @@ public class FreemarkerServletTest { private static final String STD_OUTPUT_FORMAT_RTF_FTL = "stdOutputFormatRTF.ftl"; private static final Locale DEFAULT_LOCALE = Locale.US; - private static final String CFG_DEFAULT_ENCODING = "US-ASCII"; + private static final Charset CFG_DEFAULT_ENCODING = StandardCharsets.US_ASCII; /** According to the Servlet Specification */ - private static final String SERVLET_RESPONSE_DEFAULT_CHARSET = "ISO-8859-1"; + private static final Charset SERVLET_RESPONSE_DEFAULT_CHARSET = StandardCharsets.ISO_8859_1; private static final String DEFAULT_CONTENT_TYPE = "text/html"; + private static final Charset ISO_8859_2 = Charset.forName("ISO-8859-2"); + private MockServletContext servletContext; @Before @@ -242,36 +245,36 @@ public class FreemarkerServletTest { FOO_FTL); // Legacy mode follows the source encoding of the template: assertOutputEncodingEquals( - "UTF-8", // <- expected response.characterEncoding + StandardCharsets.UTF_8, // <- expected response.characterEncoding null, // <- expected env.outputEncoding initParamValue, // <- init-param FOO_SRC_UTF8_FTL); // Legacy mode doesn't deal with outputEncoding, but it's inherited by the Environment from the Template: assertOutputEncodingEquals( CFG_DEFAULT_ENCODING, // <- expected response.characterEncoding - "UTF-8", // <- expected env.outputEncoding + StandardCharsets.UTF_8, // <- expected env.outputEncoding initParamValue, // <- init-param FOO_OUT_UTF8_FTL); // Charset in content type is the strongest: assertOutputEncodingEquals( - "ISO-8859-2", // <- expected response.characterEncoding + ISO_8859_2, // <- expected response.characterEncoding null, // <- expected env.outputEncoding initParamValue, // <- init-param "text/html; charset=ISO-8859-2", // ContentType init-param FOO_FTL); assertOutputEncodingEquals( - "ISO-8859-2", // <- expected response.characterEncoding + ISO_8859_2, // <- expected response.characterEncoding null, // <- expected env.outputEncoding initParamValue, // <- init-param "text/html; charset=ISO-8859-2", // ContentType init-param FOO_SRC_UTF8_FTL); assertOutputEncodingEquals( - "UTF-8", // <- expected response.characterEncoding + StandardCharsets.UTF_8, // <- expected response.characterEncoding null, // <- expected env.outputEncoding initParamValue, // <- init-param CONTENT_TYPE_ATTR_WITH_CHARSET_FTL); assertOutputEncodingEquals( - "UTF-8", // <- expected response.characterEncoding + StandardCharsets.UTF_8, // <- expected response.characterEncoding null, // <- expected env.outputEncoding initParamValue, // <- init-param "text/html; charset=ISO-8859-2", // ContentType init-param @@ -286,14 +289,14 @@ public class FreemarkerServletTest { FOO_FTL); // Non-legacy mode considers the template-specific outputEncoding: assertOutputEncodingEquals( - "UTF-8", // <- expected response.characterEncoding - "UTF-8", // <- expected env.outputEncoding + StandardCharsets.UTF_8, // <- expected response.characterEncoding + StandardCharsets.UTF_8, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FROM_TEMPLATE, // <- init-param FOO_OUT_UTF8_FTL); // Non-legacy mode uses the template source encoding as a fallback for outputEncoding: assertOutputEncodingEquals( - "UTF-8", // <- expected response.characterEncoding - "UTF-8", // <- expected env.outputEncoding + StandardCharsets.UTF_8, // <- expected response.characterEncoding + StandardCharsets.UTF_8, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FROM_TEMPLATE, // <- init-param FOO_SRC_UTF8_FTL); // Not allowed to specify the charset in the contentType init-param: @@ -310,8 +313,8 @@ public class FreemarkerServletTest { } // But the legacy content_type template attribute can still set the output charset: assertOutputEncodingEquals( - "UTF-8", // <- expected response.characterEncoding - "UTF-8", // <- expected env.outputEncoding + StandardCharsets.UTF_8, // <- expected response.characterEncoding + StandardCharsets.UTF_8, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FROM_TEMPLATE, // <- init-param CONTENT_TYPE_ATTR_WITH_CHARSET_FTL); @@ -352,18 +355,18 @@ public class FreemarkerServletTest { // Forced mode: assertOutputEncodingEquals( - "UTF-16LE", // <- expected response.characterEncoding - "UTF-16LE", // <- expected env.outputEncoding + StandardCharsets.UTF_16LE, // <- expected response.characterEncoding + StandardCharsets.UTF_16LE, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param FOO_FTL); assertOutputEncodingEquals( - "UTF-16LE", // <- expected response.characterEncoding - "UTF-16LE", // <- expected env.outputEncoding + StandardCharsets.UTF_16LE, // <- expected response.characterEncoding + StandardCharsets.UTF_16LE, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param FOO_SRC_UTF8_FTL); assertOutputEncodingEquals( - "UTF-16LE", // <- expected response.characterEncoding - "UTF-16LE", // <- expected env.outputEncoding + StandardCharsets.UTF_16LE, // <- expected response.characterEncoding + StandardCharsets.UTF_16LE, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param FOO_OUT_UTF8_FTL); try { @@ -379,8 +382,8 @@ public class FreemarkerServletTest { // Not allowed to specify the charset in the contentType init-param: try { assertOutputEncodingEquals( - "UTF-16LE", // <- expected response.characterEncoding - "UTF-16LE", // <- expected env.outputEncoding + StandardCharsets.UTF_16LE, // <- expected response.characterEncoding + StandardCharsets.UTF_16LE, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param "text/html; charset=ISO-8859-2", // ContentType init-param FOO_FTL); @@ -390,14 +393,14 @@ public class FreemarkerServletTest { } // The legacy content_type template attribute can still specify an output charset, though it will be overridden: assertOutputEncodingEquals( - "UTF-16LE", // <- expected response.characterEncoding - "UTF-16LE", // <- expected env.outputEncoding + StandardCharsets.UTF_16LE, // <- expected response.characterEncoding + StandardCharsets.UTF_16LE, // <- expected env.outputEncoding FreemarkerServlet.INIT_PARAM_VALUE_FORCE_PREFIX + "UTF-16LE", // <- init-param CONTENT_TYPE_ATTR_WITH_CHARSET_FTL); } private void assertResponseContentTypeEquals( - String exptectContentType, + String expectedContentType, String ctInitParam, String overrideCTInitParam, String templateName, String responseCT) throws ServletException, IOException { @@ -412,7 +415,7 @@ public class FreemarkerServletTest { } MockServletConfig servletConfig = new MockServletConfig(servletContext); - servletConfig.addInitParameter(Configuration.SOURCE_ENCODING_KEY, "UTF-8"); + servletConfig.addInitParameter(Configuration.SOURCE_ENCODING_KEY, "UtF-8"); if (ctInitParam != null) { servletConfig.addInitParameter(INIT_PARAM_CONTENT_TYPE, ctInitParam); } @@ -427,14 +430,14 @@ public class FreemarkerServletTest { freemarkerServlet.doGet(request, response); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); - assertEquals(exptectContentType, response.getContentType()); + assertEquals(expectedContentType, response.getContentType()); } finally { freemarkerServlet.destroy(); } } private void assertTemplateLocaleEquals( - Locale exptectLocale, + Locale expectedLocale, Locale requestLocale, String overrideResponseLocaleInitParam, String templateName) @@ -455,7 +458,7 @@ public class FreemarkerServletTest { freemarkerServlet.doGet(request, response); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); - assertEquals(exptectLocale, freemarkerServlet.lastLocale); + assertEquals(expectedLocale, freemarkerServlet.lastLocale); assertEquals(freemarkerServlet.lastLocale, freemarkerServlet.lastMainTemplate.getLocale()); } finally { freemarkerServlet.destroy(); @@ -463,19 +466,19 @@ public class FreemarkerServletTest { } private void assertOutputEncodingEquals( - String exptectRespCharacterEncoding, - String exptectEnvOutputEncoding, + Charset expectedRespCharacterEncoding, + Charset expectedEnvOutputEncoding, String responseCharacterEncodingInitParam, String templateName) throws ServletException, IOException { assertOutputEncodingEquals( - exptectRespCharacterEncoding, exptectEnvOutputEncoding, + expectedRespCharacterEncoding, expectedEnvOutputEncoding, responseCharacterEncodingInitParam, null, templateName); } private void assertOutputEncodingEquals( - String exptectRespCharacterEncoding, - String exptectEnvOutputEncoding, + Charset expectedRespCharacterEncoding, + Charset expectedEnvOutputEncoding, String responseCharacterEncodingInitParam, String contentTypeInitParam, String templateName) @@ -500,8 +503,9 @@ public class FreemarkerServletTest { freemarkerServlet.doGet(request, response); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); - assertEquals(exptectEnvOutputEncoding, freemarkerServlet.lastOutputEncoding); - assertEquals(exptectRespCharacterEncoding, response.getCharacterEncoding()); + assertEquals(expectedEnvOutputEncoding, freemarkerServlet.lastOutputEncoding); + assertEquals(expectedRespCharacterEncoding, + response.getCharacterEncoding() != null ? Charset.forName (response.getCharacterEncoding()) : null); } finally { freemarkerServlet.destroy(); } @@ -530,7 +534,7 @@ public class FreemarkerServletTest { private Template lastMainTemplate; private Locale lastLocale; - private String lastOutputEncoding; + private Charset lastOutputEncoding; @Override protected Configuration createConfiguration() { @@ -540,14 +544,14 @@ public class FreemarkerServletTest { // Set a test runner environment independent default locale: cfg.setLocale(DEFAULT_LOCALE); - cfg.setEncoding(CFG_DEFAULT_ENCODING); + cfg.setSourceEncoding(CFG_DEFAULT_ENCODING); { TemplateConfiguration outUtf8TC = new TemplateConfiguration(); - outUtf8TC.setOutputEncoding("UTF-8"); + outUtf8TC.setOutputEncoding(StandardCharsets.UTF_8); TemplateConfiguration srcUtf8TC = new TemplateConfiguration(); - srcUtf8TC.setSourceEncoding("UTF-8"); + srcUtf8TC.setSourceEncoding(StandardCharsets.UTF_8); cfg.setTemplateConfigurations( new FirstMatchTemplateConfigurationFactory( http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/test/ResourcesExtractor.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/ResourcesExtractor.java b/src/test/java/org/apache/freemarker/test/ResourcesExtractor.java index e36d845..ce9598e 100644 --- a/src/test/java/org/apache/freemarker/test/ResourcesExtractor.java +++ b/src/test/java/org/apache/freemarker/test/ResourcesExtractor.java @@ -25,6 +25,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.jar.JarOutputStream; import java.util.zip.ZipEntry; @@ -99,7 +100,7 @@ public final class ResourcesExtractor { deleteDstRootDir = !dstRootDir.exists(); } try { - BufferedReader contR = new BufferedReader(new InputStreamReader(contIn, "UTF-8")); + BufferedReader contR = new BufferedReader(new InputStreamReader(contIn, StandardCharsets.UTF_8)); try { String contLine; while ((contLine = contR.readLine()) != null) { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/test/TemplateTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/TemplateTest.java b/src/test/java/org/apache/freemarker/test/TemplateTest.java index 31ad3e0..d42984f 100644 --- a/src/test/java/org/apache/freemarker/test/TemplateTest.java +++ b/src/test/java/org/apache/freemarker/test/TemplateTest.java @@ -77,8 +77,8 @@ public abstract class TemplateTest { if (!configuration.isLocaleExplicitlySet()) { configuration.setLocale(Locale.US); } - if (!configuration.isDefaultEncodingExplicitlySet()) { - configuration.setEncoding(StandardCharsets.UTF_8.name()); + if (!configuration.isSourceEncodingExplicitlySet()) { + configuration.setSourceEncoding(StandardCharsets.UTF_8); } if (!configuration.isTimeZoneExplicitlySet()) { configuration.setTimeZone(TimeZone.getTimeZone("GMT+1")); @@ -115,7 +115,7 @@ public abstract class TemplateTest { throw new IOException("Reference output resource not found: " + getClass() + ", " + resName); } try { - expectedOut = TestUtil.removeTxtCopyrightComment(IOUtils.toString(in, "utf-8")); + expectedOut = TestUtil.removeTxtCopyrightComment(IOUtils.toString(in, StandardCharsets.UTF_8.name())); } finally { in.close(); } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/test/servlet/Model2TesterServlet.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/servlet/Model2TesterServlet.java b/src/test/java/org/apache/freemarker/test/servlet/Model2TesterServlet.java index e6a8eab..9a39705 100644 --- a/src/test/java/org/apache/freemarker/test/servlet/Model2TesterServlet.java +++ b/src/test/java/org/apache/freemarker/test/servlet/Model2TesterServlet.java @@ -20,6 +20,7 @@ package org.apache.freemarker.test.servlet; import java.io.IOException; +import java.nio.charset.StandardCharsets; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -52,7 +53,7 @@ public class Model2TesterServlet extends HttpServlet { private static final long serialVersionUID = 1L; - private static final String CHARSET = "UTF-8"; + private static final String CHARSET = StandardCharsets.UTF_8.name(); @Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/test/servlet/WebAppTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/servlet/WebAppTestCase.java b/src/test/java/org/apache/freemarker/test/servlet/WebAppTestCase.java index 1580932..5d68588 100644 --- a/src/test/java/org/apache/freemarker/test/servlet/WebAppTestCase.java +++ b/src/test/java/org/apache/freemarker/test/servlet/WebAppTestCase.java @@ -27,6 +27,7 @@ import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -125,7 +126,7 @@ public class WebAppTestCase { if (responseCode == 200) { InputStream in = httpCon.getInputStream(); try { - content = IOUtils.toString(in, "UTF-8"); + content = IOUtils.toString(in, StandardCharsets.UTF_8.name()); } finally { in.close(); } @@ -191,7 +192,9 @@ public class WebAppTestCase { throw new IOException("Test resource not found: " + expectedResource); } try { - expected = TestUtil.removeTxtCopyrightComment(normalizeWS(IOUtils.toString(in, "utf-8"), compressWS)); + expected = TestUtil.removeTxtCopyrightComment(normalizeWS( + IOUtils.toString(in, StandardCharsets.UTF_8.name()), + compressWS)); } finally { in.close(); } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java index fccef43..3e92a10 100644 --- a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java +++ b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java @@ -25,6 +25,8 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.math.BigDecimal; import java.math.BigInteger; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -146,8 +148,8 @@ public class TemplateTestCase extends FileTestCase { if (!st.hasMoreTokens()) fail("Expecting alias after 'as' in autoimport"); String alias = st.nextToken(); conf.addAutoImport(alias, libname); - } else if ("input_encoding".equals(param)) { - conf.setEncoding(value); + } else if ("source_encoding".equals(param)) { + conf.setSourceEncoding(Charset.forName(value)); // INCOMPATIBLE_IMPROVEMENTS is a list here, and was already set in the constructor. } else if (!Configuration.INCOMPATIBLE_IMPROVEMENTS_KEY.equals(param)) { try { @@ -425,8 +427,8 @@ public class TemplateTestCase extends FileTestCase { } @Override - protected String getDefaultCharset() { - return conf.getOutputEncoding() != null ? conf.getOutputEncoding() : "UTF-8"; + protected Charset getFileCharset() { + return conf.getOutputEncoding() != null ? conf.getOutputEncoding() : StandardCharsets.UTF_8; } @Override http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/java/org/apache/freemarker/test/util/FileTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/test/util/FileTestCase.java b/src/test/java/org/apache/freemarker/test/util/FileTestCase.java index 168012a..c0990ba 100644 --- a/src/test/java/org/apache/freemarker/test/util/FileTestCase.java +++ b/src/test/java/org/apache/freemarker/test/util/FileTestCase.java @@ -30,6 +30,8 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.net.URL; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.apache.freemarker.core.util._StringUtil; @@ -60,7 +62,7 @@ public abstract class FileTestCase extends TestCase { try { final File expectedFile = getExpectedFileFor(expectedFileName); - AssertionFailedError assertionExcepton = null; + AssertionFailedError assertionException = null; boolean successful = false; try { if (expectedFile.exists()) { @@ -68,7 +70,7 @@ public abstract class FileTestCase extends TestCase { successful = true; } } catch (AssertionFailedError e) { - assertionExcepton = e; + assertionException = e; } if (!successful) { @@ -76,8 +78,8 @@ public abstract class FileTestCase extends TestCase { saveString(actualFile, actualContent); reportActualFileSaved(actualFile); - if (assertionExcepton != null) { - throw assertionExcepton; + if (assertionException != null) { + throw assertionException; } else { throw new FileNotFoundException(expectedFile.getPath()); } @@ -106,7 +108,7 @@ public abstract class FileTestCase extends TestCase { } private void saveString(File actualFile, String actualContents) throws IOException { - Writer w = new OutputStreamWriter(new FileOutputStream(actualFile), "UTF-8"); + Writer w = new OutputStreamWriter(new FileOutputStream(actualFile), StandardCharsets.UTF_8); try { w.write(actualContents); } finally { @@ -145,30 +147,30 @@ public abstract class FileTestCase extends TestCase { } protected String loadFile(File f) throws IOException { - return TestUtil.removeTxtCopyrightComment(loadFile(f, getDefaultCharset())); + return TestUtil.removeTxtCopyrightComment(loadFile(f, getFileCharset())); } - protected String loadFile(File f, String charset) throws IOException { + protected String loadFile(File f, Charset charset) throws IOException { return loadString(new FileInputStream(f), charset); } protected String loadResource(String resourceName) throws IOException { - return loadResource(resourceName, getDefaultCharset()); + return loadResource(resourceName, getFileCharset()); } - protected String loadResource(String resourceName, String charset) throws IOException { + protected String loadResource(String resourceName, Charset charset) throws IOException { return loadString(new FileInputStream(new File(getTestClassDirectory(), resourceName)), charset); } - protected String getDefaultCharset() { - return "UTF-8"; + protected Charset getFileCharset() { + return StandardCharsets.UTF_8; } protected void reportActualFileSaved(File f) { System.out.println("Note: Saved actual output of the failed test to here: " + f.getAbsolutePath()); } - private static String loadString(InputStream in, String charset) throws IOException { + private static String loadString(InputStream in, Charset charset) throws IOException { Reader r = new InputStreamReader(in, charset); StringBuilder sb = new StringBuilder(1024); try { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples1.properties ---------------------------------------------------------------------- diff --git a/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples1.properties b/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples1.properties index 6c33890..faf8592 100644 --- a/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples1.properties +++ b/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples1.properties @@ -19,7 +19,7 @@ templateConfigurations = \ ConditionalTemplateConfigurationFactory( \ FileExtensionMatcher("xml"), \ TemplateConfiguration( \ - sourceEncoding = "utf-8", \ + sourceEncoding = Charset("utf-8"), \ outputFormat = XMLOutputFormat() \ ) \ ) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples3.properties ---------------------------------------------------------------------- diff --git a/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples3.properties b/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples3.properties index 27301bf..ce72fef 100644 --- a/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples3.properties +++ b/src/test/resources/org/apache/freemarker/manualtest/TemplateConfigurationExamples3.properties @@ -28,7 +28,7 @@ templateConfigurations = \ ), \ ConditionalTemplateConfigurationFactory( \ PathGlobMatcher("mail/**"), \ - TemplateConfiguration(sourceEncoding = "utf-8") \ + TemplateConfiguration(sourceEncoding = Charset("utf-8")) \ ), \ FirstMatchTemplateConfigurationFactory( \ ConditionalTemplateConfigurationFactory( \ http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/27a62ce0/src/test/resources/org/apache/freemarker/test/templatesuite/testcases.xml ---------------------------------------------------------------------- diff --git a/src/test/resources/org/apache/freemarker/test/templatesuite/testcases.xml b/src/test/resources/org/apache/freemarker/test/templatesuite/testcases.xml index e38110e..4ff2437 100644 --- a/src/test/resources/org/apache/freemarker/test/templatesuite/testcases.xml +++ b/src/test/resources/org/apache/freemarker/test/templatesuite/testcases.xml @@ -34,7 +34,7 @@ <!ELEMENT setting EMPTY> <!ATTLIST setting auto_import CDATA #IMPLIED - input_encoding CDATA #IMPLIED + source_encoding CDATA #IMPLIED locale CDATA #IMPLIED object_wrapper CDATA #IMPLIED output_encoding CDATA #IMPLIED @@ -52,7 +52,7 @@ Note that for the incompatible_improvements setting you can specify a list of ve --> <testCases> - <setting input_encoding="UTF-8" output_encoding="UTF-8" /> + <setting source_encoding="UTF-8" output_encoding="UTF-8" /> <testCase name="api-builtins" noOutput="true"> <setting api_builtin_enabled="true" /> @@ -84,7 +84,7 @@ Note that for the incompatible_improvements setting you can specify a list of ve </testCase> <testCase name="include" /> <testCase name="include2"> - <setting input_encoding="utf-8" /> + <setting source_encoding="utf-8" /> </testCase> <testCase name="interpret"/> <testCase name="iterators"/>
