http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/94d39312/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateCacheTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateCacheTest.java b/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateCacheTest.java deleted file mode 100644 index 23ce85e..0000000 --- a/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateCacheTest.java +++ /dev/null @@ -1,326 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.freemarker.core.ast; - -import static org.junit.Assert.*; - -import java.io.IOException; -import java.io.StringWriter; -import java.io.UnsupportedEncodingException; -import java.util.Locale; - -import org.apache.freemarker.core.Configuration; -import org.apache.freemarker.core.Template; -import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.ast.CustomAttribute; -import org.apache.freemarker.core.ast.TemplateConfiguration; -import org.apache.freemarker.core.templateresolver.ByteArrayTemplateLoader; -import org.apache.freemarker.core.templateresolver.ConditionalTemplateConfigurationFactory; -import org.apache.freemarker.core.templateresolver.FileNameGlobMatcher; -import org.apache.freemarker.core.templateresolver.FirstMatchTemplateConfigurationFactory; -import org.apache.freemarker.core.templateresolver.MergingTemplateConfigurationFactory; -import org.apache.freemarker.core.templateresolver.StringTemplateLoader; -import org.junit.Test; - -public class TemplateConfigurationWithTemplateCacheTest { - - private static final String TEXT_WITH_ACCENTS = "pr\u00F3ba"; - - private static final CustomAttribute CUST_ATT_1 = new CustomAttribute(CustomAttribute.SCOPE_TEMPLATE); - private static final CustomAttribute CUST_ATT_2 = new CustomAttribute(CustomAttribute.SCOPE_TEMPLATE); - - @Test - public void testEncoding() throws Exception { - Configuration cfg = createCommonEncodingTesterConfig(); - - { - Template t = cfg.getTemplate("utf8.ftl"); - assertEquals("utf-8", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("utf8.ftl", "iso-8859-1"); - assertEquals("utf-8", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("utf16.ftl"); - assertEquals("utf-16", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("default.ftl"); - assertEquals("iso-8859-1", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("default.ftl", "iso-8859-5"); - assertEquals("iso-8859-5", t.getEncoding()); - assertEquals(new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5"), - getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("utf8-latin2.ftl"); - assertEquals("iso-8859-2", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("default-latin2.ftl"); - assertEquals("iso-8859-2", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - } - - @Test - public void testIncludeAndEncoding() throws Exception { - Configuration cfg = createCommonEncodingTesterConfig(); - ByteArrayTemplateLoader tl = (ByteArrayTemplateLoader) cfg.getTemplateLoader(); - tl.putTemplate("main.ftl", ( - "<#include 'utf8.ftl'>" - + "<#include 'utf16.ftl'>" - + "<#include 'default.ftl'>" - + "<#include 'utf8-latin2.ftl'>" - // With mostly ignored encoding params: - + "<#include 'utf8.ftl' encoding='utf-16'>" - + "<#include 'utf16.ftl' encoding='iso-8859-5'>" - + "<#include 'default.ftl' encoding='iso-8859-5'>" - + "<#include 'utf8-latin2.ftl' encoding='iso-8859-5'>" - ).getBytes("iso-8859-1")); - assertEquals( - TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS - + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS - + new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5") - + TEXT_WITH_ACCENTS, - getTemplateOutput(cfg.getTemplate("main.ftl"))); - } - - @Test - public void testLocale() throws Exception { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_23); - cfg.setLocale(Locale.US); - - StringTemplateLoader tl = new StringTemplateLoader(); - tl.putTemplate("(de).ftl", "${.locale}"); - tl.putTemplate("default.ftl", "${.locale}"); - tl.putTemplate("(de)-fr.ftl", - ("<#ftl locale='fr_FR'>${.locale}")); - tl.putTemplate("default-fr.ftl", - ("<#ftl locale='fr_FR'>${.locale}")); - cfg.setTemplateLoader(tl); - - TemplateConfiguration tcDe = new TemplateConfiguration(); - tcDe.setLocale(Locale.GERMANY); - cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(de)*"), tcDe)); - - { - Template t = cfg.getTemplate("(de).ftl"); - assertEquals(Locale.GERMANY, t.getLocale()); - assertEquals("de_DE", getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("(de).ftl", Locale.ITALY); - assertEquals(Locale.GERMANY, t.getLocale()); - assertEquals("de_DE", getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("default.ftl"); - assertEquals(Locale.US, t.getLocale()); - assertEquals("en_US", getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("default.ftl", Locale.ITALY); - assertEquals(Locale.ITALY, t.getLocale()); - assertEquals("it_IT", getTemplateOutput(t)); - } - } - - @Test - public void testPlainText() throws Exception { - Configuration cfg = createCommonEncodingTesterConfig(); - cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_22); - - TemplateConfiguration tcDE = new TemplateConfiguration(); - tcDE.setLocale(Locale.GERMANY); - TemplateConfiguration tcYN = new TemplateConfiguration(); - tcYN.setBooleanFormat("Y,N"); - cfg.setTemplateConfigurations( - new MergingTemplateConfigurationFactory( - cfg.getTemplateConfigurations(), - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcDE), - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcYN) - ) - ); - - { - Template t = cfg.getTemplate("utf8.ftl", null, null, false); - assertEquals("utf-8", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - assertEquals(Locale.US, t.getLocale()); - assertEquals("true,false", t.getBooleanFormat()); - } - { - Template t = cfg.getTemplate("utf8.ftl", null, "iso-8859-1", false); - assertEquals("utf-8", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - { - Template t = cfg.getTemplate("utf16.ftl", null, null, false); - assertEquals("utf-16", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - assertEquals(Locale.GERMANY, t.getLocale()); - assertEquals("Y,N", t.getBooleanFormat()); - } - { - Template t = cfg.getTemplate("default.ftl", null, null, false); - assertEquals("iso-8859-1", t.getEncoding()); - assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); - } - } - - @Test - public void testConfigurableSettings() throws Exception { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); - cfg.setLocale(Locale.US); - - TemplateConfiguration tcFR = new TemplateConfiguration(); - tcFR.setLocale(Locale.FRANCE); - TemplateConfiguration tcYN = new TemplateConfiguration(); - tcYN.setBooleanFormat("Y,N"); - TemplateConfiguration tc00 = new TemplateConfiguration(); - tc00.setNumberFormat("0.00"); - cfg.setTemplateConfigurations( - new MergingTemplateConfigurationFactory( - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(fr)*"), tcFR), - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(yn)*"), tcYN), - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(00)*"), tc00) - ) - ); - - String commonFTL = "${.locale} ${true?string} ${1.2}"; - StringTemplateLoader tl = new StringTemplateLoader(); - tl.putTemplate("default", commonFTL); - tl.putTemplate("(fr)", commonFTL); - tl.putTemplate("(yn)(00)", commonFTL); - tl.putTemplate("(00)(fr)", commonFTL); - cfg.setTemplateLoader(tl); - - assertEquals("en_US true 1.2", getTemplateOutput(cfg.getTemplate("default"))); - assertEquals("fr_FR true 1,2", getTemplateOutput(cfg.getTemplate("(fr)"))); - assertEquals("en_US Y 1.20", getTemplateOutput(cfg.getTemplate("(yn)(00)"))); - assertEquals("fr_FR true 1,20", getTemplateOutput(cfg.getTemplate("(00)(fr)"))); - } - - @Test - public void testCustomAttributes() throws Exception { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); - - TemplateConfiguration tc1 = new TemplateConfiguration(); - tc1.setCustomAttribute("a1", "a1tc1"); - tc1.setCustomAttribute("a2", "a2tc1"); - tc1.setCustomAttribute("a3", "a3tc1"); - CUST_ATT_1.set("ca1tc1", tc1); - CUST_ATT_2.set("ca2tc1", tc1); - - TemplateConfiguration tc2 = new TemplateConfiguration(); - tc2.setCustomAttribute("a1", "a1tc2"); - CUST_ATT_1.set("ca1tc2", tc2); - - cfg.setTemplateConfigurations( - new MergingTemplateConfigurationFactory( - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(tc1)*"), tc1), - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(tc2)*"), tc2) - ) - ); - - String commonFTL = "<#ftl attributes={ 'a3': 'a3temp' }>"; - StringTemplateLoader tl = new StringTemplateLoader(); - tl.putTemplate("(tc1)", commonFTL); - tl.putTemplate("(tc1)noHeader", ""); - tl.putTemplate("(tc2)", commonFTL); - tl.putTemplate("(tc1)(tc2)", commonFTL); - cfg.setTemplateLoader(tl); - - { - Template t = cfg.getTemplate("(tc1)"); - assertEquals("a1tc1", t.getCustomAttribute("a1")); - assertEquals("a2tc1", t.getCustomAttribute("a2")); - assertEquals("a3temp", t.getCustomAttribute("a3")); - assertEquals("ca1tc1", CUST_ATT_1.get(t)); - assertEquals("ca2tc1", CUST_ATT_2.get(t)); - } - { - Template t = cfg.getTemplate("(tc1)noHeader"); - assertEquals("a1tc1", t.getCustomAttribute("a1")); - assertEquals("a2tc1", t.getCustomAttribute("a2")); - assertEquals("a3tc1", t.getCustomAttribute("a3")); - assertEquals("ca1tc1", CUST_ATT_1.get(t)); - assertEquals("ca2tc1", CUST_ATT_2.get(t)); - } - { - Template t = cfg.getTemplate("(tc2)"); - assertEquals("a1tc2", t.getCustomAttribute("a1")); - assertNull(t.getCustomAttribute("a2")); - assertEquals("a3temp", t.getCustomAttribute("a3")); - assertEquals("ca1tc2", CUST_ATT_1.get(t)); - assertNull(CUST_ATT_2.get(t)); - } - { - Template t = cfg.getTemplate("(tc1)(tc2)"); - assertEquals("a1tc2", t.getCustomAttribute("a1")); - assertEquals("a2tc1", t.getCustomAttribute("a2")); - assertEquals("a3temp", t.getCustomAttribute("a3")); - assertEquals("ca1tc2", CUST_ATT_1.get(t)); - assertEquals("ca2tc1", CUST_ATT_2.get(t)); - } - } - - private String getTemplateOutput(Template t) throws TemplateException, IOException { - StringWriter sw = new StringWriter(); - t.process(null, sw); - return sw.toString(); - } - - private Configuration createCommonEncodingTesterConfig() throws UnsupportedEncodingException { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_0); - cfg.setDefaultEncoding("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-latin2.ftl", - ("<#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")); - cfg.setTemplateLoader(tl); - - TemplateConfiguration tcUtf8 = new TemplateConfiguration(); - tcUtf8.setEncoding("utf-8"); - TemplateConfiguration tcUtf16 = new TemplateConfiguration(); - tcUtf16.setEncoding("utf-16"); - cfg.setTemplateConfigurations( - new FirstMatchTemplateConfigurationFactory( - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*utf8*"), tcUtf8), - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*utf16*"), tcUtf16) - ).allowNoMatch(true)); - return cfg; - } - -}
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/94d39312/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateResolverTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateResolverTest.java b/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateResolverTest.java new file mode 100644 index 0000000..b9cbc61 --- /dev/null +++ b/src/test/java/org/apache/freemarker/core/ast/TemplateConfigurationWithTemplateResolverTest.java @@ -0,0 +1,326 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.freemarker.core.ast; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.UnsupportedEncodingException; +import java.util.Locale; + +import org.apache.freemarker.core.Configuration; +import org.apache.freemarker.core.Template; +import org.apache.freemarker.core.TemplateException; +import org.apache.freemarker.core.ast.CustomAttribute; +import org.apache.freemarker.core.ast.TemplateConfiguration; +import org.apache.freemarker.core.templateresolver.ByteArrayTemplateLoader; +import org.apache.freemarker.core.templateresolver.ConditionalTemplateConfigurationFactory; +import org.apache.freemarker.core.templateresolver.FileNameGlobMatcher; +import org.apache.freemarker.core.templateresolver.FirstMatchTemplateConfigurationFactory; +import org.apache.freemarker.core.templateresolver.MergingTemplateConfigurationFactory; +import org.apache.freemarker.core.templateresolver.StringTemplateLoader; +import org.junit.Test; + +public class TemplateConfigurationWithTemplateResolverTest { + + private static final String TEXT_WITH_ACCENTS = "pr\u00F3ba"; + + private static final CustomAttribute CUST_ATT_1 = new CustomAttribute(CustomAttribute.SCOPE_TEMPLATE); + private static final CustomAttribute CUST_ATT_2 = new CustomAttribute(CustomAttribute.SCOPE_TEMPLATE); + + @Test + public void testEncoding() throws Exception { + Configuration cfg = createCommonEncodingTesterConfig(); + + { + Template t = cfg.getTemplate("utf8.ftl"); + assertEquals("utf-8", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("utf8.ftl", "iso-8859-1"); + assertEquals("utf-8", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("utf16.ftl"); + assertEquals("utf-16", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("default.ftl"); + assertEquals("iso-8859-1", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("default.ftl", "iso-8859-5"); + assertEquals("iso-8859-5", t.getEncoding()); + assertEquals(new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5"), + getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("utf8-latin2.ftl"); + assertEquals("iso-8859-2", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("default-latin2.ftl"); + assertEquals("iso-8859-2", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + } + + @Test + public void testIncludeAndEncoding() throws Exception { + Configuration cfg = createCommonEncodingTesterConfig(); + ByteArrayTemplateLoader tl = (ByteArrayTemplateLoader) cfg.getTemplateLoader(); + tl.putTemplate("main.ftl", ( + "<#include 'utf8.ftl'>" + + "<#include 'utf16.ftl'>" + + "<#include 'default.ftl'>" + + "<#include 'utf8-latin2.ftl'>" + // With mostly ignored encoding params: + + "<#include 'utf8.ftl' encoding='utf-16'>" + + "<#include 'utf16.ftl' encoding='iso-8859-5'>" + + "<#include 'default.ftl' encoding='iso-8859-5'>" + + "<#include 'utf8-latin2.ftl' encoding='iso-8859-5'>" + ).getBytes("iso-8859-1")); + assertEquals( + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + + new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5") + + TEXT_WITH_ACCENTS, + getTemplateOutput(cfg.getTemplate("main.ftl"))); + } + + @Test + public void testLocale() throws Exception { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_23); + cfg.setLocale(Locale.US); + + StringTemplateLoader tl = new StringTemplateLoader(); + tl.putTemplate("(de).ftl", "${.locale}"); + tl.putTemplate("default.ftl", "${.locale}"); + tl.putTemplate("(de)-fr.ftl", + ("<#ftl locale='fr_FR'>${.locale}")); + tl.putTemplate("default-fr.ftl", + ("<#ftl locale='fr_FR'>${.locale}")); + cfg.setTemplateLoader(tl); + + TemplateConfiguration tcDe = new TemplateConfiguration(); + tcDe.setLocale(Locale.GERMANY); + cfg.setTemplateConfigurations(new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(de)*"), tcDe)); + + { + Template t = cfg.getTemplate("(de).ftl"); + assertEquals(Locale.GERMANY, t.getLocale()); + assertEquals("de_DE", getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("(de).ftl", Locale.ITALY); + assertEquals(Locale.GERMANY, t.getLocale()); + assertEquals("de_DE", getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("default.ftl"); + assertEquals(Locale.US, t.getLocale()); + assertEquals("en_US", getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("default.ftl", Locale.ITALY); + assertEquals(Locale.ITALY, t.getLocale()); + assertEquals("it_IT", getTemplateOutput(t)); + } + } + + @Test + public void testPlainText() throws Exception { + Configuration cfg = createCommonEncodingTesterConfig(); + cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_22); + + TemplateConfiguration tcDE = new TemplateConfiguration(); + tcDE.setLocale(Locale.GERMANY); + TemplateConfiguration tcYN = new TemplateConfiguration(); + tcYN.setBooleanFormat("Y,N"); + cfg.setTemplateConfigurations( + new MergingTemplateConfigurationFactory( + cfg.getTemplateConfigurations(), + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcDE), + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("utf16.ftl"), tcYN) + ) + ); + + { + Template t = cfg.getTemplate("utf8.ftl", null, null, false); + assertEquals("utf-8", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + assertEquals(Locale.US, t.getLocale()); + assertEquals("true,false", t.getBooleanFormat()); + } + { + Template t = cfg.getTemplate("utf8.ftl", null, "iso-8859-1", false); + assertEquals("utf-8", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + { + Template t = cfg.getTemplate("utf16.ftl", null, null, false); + assertEquals("utf-16", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + assertEquals(Locale.GERMANY, t.getLocale()); + assertEquals("Y,N", t.getBooleanFormat()); + } + { + Template t = cfg.getTemplate("default.ftl", null, null, false); + assertEquals("iso-8859-1", t.getEncoding()); + assertEquals(TEXT_WITH_ACCENTS, getTemplateOutput(t)); + } + } + + @Test + public void testConfigurableSettings() throws Exception { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); + cfg.setLocale(Locale.US); + + TemplateConfiguration tcFR = new TemplateConfiguration(); + tcFR.setLocale(Locale.FRANCE); + TemplateConfiguration tcYN = new TemplateConfiguration(); + tcYN.setBooleanFormat("Y,N"); + TemplateConfiguration tc00 = new TemplateConfiguration(); + tc00.setNumberFormat("0.00"); + cfg.setTemplateConfigurations( + new MergingTemplateConfigurationFactory( + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(fr)*"), tcFR), + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(yn)*"), tcYN), + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(00)*"), tc00) + ) + ); + + String commonFTL = "${.locale} ${true?string} ${1.2}"; + StringTemplateLoader tl = new StringTemplateLoader(); + tl.putTemplate("default", commonFTL); + tl.putTemplate("(fr)", commonFTL); + tl.putTemplate("(yn)(00)", commonFTL); + tl.putTemplate("(00)(fr)", commonFTL); + cfg.setTemplateLoader(tl); + + assertEquals("en_US true 1.2", getTemplateOutput(cfg.getTemplate("default"))); + assertEquals("fr_FR true 1,2", getTemplateOutput(cfg.getTemplate("(fr)"))); + assertEquals("en_US Y 1.20", getTemplateOutput(cfg.getTemplate("(yn)(00)"))); + assertEquals("fr_FR true 1,20", getTemplateOutput(cfg.getTemplate("(00)(fr)"))); + } + + @Test + public void testCustomAttributes() throws Exception { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); + + TemplateConfiguration tc1 = new TemplateConfiguration(); + tc1.setCustomAttribute("a1", "a1tc1"); + tc1.setCustomAttribute("a2", "a2tc1"); + tc1.setCustomAttribute("a3", "a3tc1"); + CUST_ATT_1.set("ca1tc1", tc1); + CUST_ATT_2.set("ca2tc1", tc1); + + TemplateConfiguration tc2 = new TemplateConfiguration(); + tc2.setCustomAttribute("a1", "a1tc2"); + CUST_ATT_1.set("ca1tc2", tc2); + + cfg.setTemplateConfigurations( + new MergingTemplateConfigurationFactory( + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(tc1)*"), tc1), + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*(tc2)*"), tc2) + ) + ); + + String commonFTL = "<#ftl attributes={ 'a3': 'a3temp' }>"; + StringTemplateLoader tl = new StringTemplateLoader(); + tl.putTemplate("(tc1)", commonFTL); + tl.putTemplate("(tc1)noHeader", ""); + tl.putTemplate("(tc2)", commonFTL); + tl.putTemplate("(tc1)(tc2)", commonFTL); + cfg.setTemplateLoader(tl); + + { + Template t = cfg.getTemplate("(tc1)"); + assertEquals("a1tc1", t.getCustomAttribute("a1")); + assertEquals("a2tc1", t.getCustomAttribute("a2")); + assertEquals("a3temp", t.getCustomAttribute("a3")); + assertEquals("ca1tc1", CUST_ATT_1.get(t)); + assertEquals("ca2tc1", CUST_ATT_2.get(t)); + } + { + Template t = cfg.getTemplate("(tc1)noHeader"); + assertEquals("a1tc1", t.getCustomAttribute("a1")); + assertEquals("a2tc1", t.getCustomAttribute("a2")); + assertEquals("a3tc1", t.getCustomAttribute("a3")); + assertEquals("ca1tc1", CUST_ATT_1.get(t)); + assertEquals("ca2tc1", CUST_ATT_2.get(t)); + } + { + Template t = cfg.getTemplate("(tc2)"); + assertEquals("a1tc2", t.getCustomAttribute("a1")); + assertNull(t.getCustomAttribute("a2")); + assertEquals("a3temp", t.getCustomAttribute("a3")); + assertEquals("ca1tc2", CUST_ATT_1.get(t)); + assertNull(CUST_ATT_2.get(t)); + } + { + Template t = cfg.getTemplate("(tc1)(tc2)"); + assertEquals("a1tc2", t.getCustomAttribute("a1")); + assertEquals("a2tc1", t.getCustomAttribute("a2")); + assertEquals("a3temp", t.getCustomAttribute("a3")); + assertEquals("ca1tc2", CUST_ATT_1.get(t)); + assertEquals("ca2tc1", CUST_ATT_2.get(t)); + } + } + + private String getTemplateOutput(Template t) throws TemplateException, IOException { + StringWriter sw = new StringWriter(); + t.process(null, sw); + return sw.toString(); + } + + private Configuration createCommonEncodingTesterConfig() throws UnsupportedEncodingException { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_0); + cfg.setDefaultEncoding("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-latin2.ftl", + ("<#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")); + cfg.setTemplateLoader(tl); + + TemplateConfiguration tcUtf8 = new TemplateConfiguration(); + tcUtf8.setEncoding("utf-8"); + TemplateConfiguration tcUtf16 = new TemplateConfiguration(); + tcUtf16.setEncoding("utf-16"); + cfg.setTemplateConfigurations( + new FirstMatchTemplateConfigurationFactory( + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*utf8*"), tcUtf8), + new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*utf16*"), tcUtf16) + ).allowNoMatch(true)); + return cfg; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/94d39312/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 new file mode 100644 index 0000000..42690b6 --- /dev/null +++ b/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java @@ -0,0 +1,479 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.freemarker.core.templateresolver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.io.Serializable; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Locale; + +import org.apache.freemarker.core.Configuration; +import org.apache.freemarker.core.Template; +import org.apache.freemarker.core.TemplateNotFoundException; +import org.apache.freemarker.core.ast.ParseException; +import org.apache.freemarker.test.MonitoredTemplateLoader; +import org.apache.freemarker.test.MonitoredTemplateLoader.CloseSessionEvent; +import org.apache.freemarker.test.MonitoredTemplateLoader.CreateSessionEvent; +import org.apache.freemarker.test.MonitoredTemplateLoader.LoadEvent; +import org.hamcrest.Matchers; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; + +public class DefaultTemplateResolverTest { + + @Test + public void testCachedException() throws Exception { + MockTemplateLoader loader = new MockTemplateLoader(); + DefaultTemplateResolver tr = new DefaultTemplateResolver( + loader, new StrongCacheStorage(), new Configuration(Configuration.VERSION_3_0_0)); + tr.setTemplateUpdateDelayMilliseconds(1000L); + loader.setThrowException(true); + try { + tr.getTemplate("t", Locale.getDefault(), null, "", true).getTemplate(); + fail(); + } catch (IOException e) { + assertEquals("mock IO exception", e.getMessage()); + assertEquals(1, loader.getLoadAttemptCount()); + try { + tr.getTemplate("t", Locale.getDefault(), null, "", true).getTemplate(); + fail(); + } catch (IOException e2) { + // Still 1 - returned cached exception + assertThat(e2.getMessage(), + Matchers.allOf(Matchers.containsString("There was an error loading the template on an " + + "earlier attempt"))); + assertSame(e, e2.getCause()); + assertEquals(1, loader.getLoadAttemptCount()); + try { + Thread.sleep(1100L); + tr.getTemplate("t", Locale.getDefault(), null, "", true).getTemplate(); + fail(); + } catch (IOException e3) { + // Cache had to retest + assertEquals("mock IO exception", e.getMessage()); + assertEquals(2, loader.getLoadAttemptCount()); + } + } + } + } + + @Test + public void testCachedNotFound() throws Exception { + MockTemplateLoader loader = new MockTemplateLoader(); + DefaultTemplateResolver cache = new DefaultTemplateResolver(loader, new StrongCacheStorage(), new Configuration()); + cache.setTemplateUpdateDelayMilliseconds(1000L); + cache.setLocalizedLookup(false); + assertNull(cache.getTemplate("t", Locale.getDefault(), null, "", true).getTemplate()); + assertEquals(1, loader.getLoadAttemptCount()); + assertNull(cache.getTemplate("t", Locale.getDefault(), null, "", true).getTemplate()); + // Still 1 - returned cached exception + assertEquals(1, loader.getLoadAttemptCount()); + Thread.sleep(1100L); + assertNull(cache.getTemplate("t", Locale.getDefault(), null, "", true).getTemplate()); + // Cache had to retest + assertEquals(2, loader.getLoadAttemptCount()); + } + + private static class MockTemplateLoader implements TemplateLoader { + private boolean throwException; + private int loadAttemptCount; + + public void setThrowException(boolean throwException) { + this.throwException = throwException; + } + + public int getLoadAttemptCount() { + return loadAttemptCount; + } + + @Override + public TemplateLoaderSession createSession() { + return null; + } + + @Override + public TemplateLoadingResult load(String name, TemplateLoadingSource ifSourceDiffersFrom, + Serializable ifVersionDiffersFrom, TemplateLoaderSession session) throws IOException { + ++loadAttemptCount; + if (throwException) { + throw new IOException("mock IO exception"); + } + return TemplateLoadingResult.NOT_FOUND; + } + + @Override + public void resetState() { + // + } + + } + + @Test + public void testManualRemovalPlain() throws IOException { + Configuration cfg = new Configuration(); + cfg.setCacheStorage(new StrongCacheStorage()); + StringTemplateLoader loader = new StringTemplateLoader(); + cfg.setTemplateLoader(loader); + cfg.setTemplateUpdateDelay(Integer.MAX_VALUE); + + loader.putTemplate("1.ftl", "1 v1"); + loader.putTemplate("2.ftl", "2 v1"); + assertEquals("1 v1", cfg.getTemplate("1.ftl").toString()); + assertEquals("2 v1", cfg.getTemplate("2.ftl").toString()); + + loader.putTemplate("1.ftl", "1 v2"); + loader.putTemplate("2.ftl", "2 v2"); + assertEquals("1 v1", cfg.getTemplate("1.ftl").toString()); // no change + assertEquals("2 v1", cfg.getTemplate("2.ftl").toString()); // no change + + cfg.removeTemplateFromCache("1.ftl"); + assertEquals("1 v2", cfg.getTemplate("1.ftl").toString()); // changed + assertEquals("2 v1", cfg.getTemplate("2.ftl").toString()); + + cfg.removeTemplateFromCache("2.ftl"); + assertEquals("1 v2", cfg.getTemplate("1.ftl").toString()); + assertEquals("2 v2", cfg.getTemplate("2.ftl").toString()); // changed + } + + @Test + public void testManualRemovalI18ed() throws IOException { + Configuration cfg = new Configuration(); + cfg.setCacheStorage(new StrongCacheStorage()); + cfg.setLocale(Locale.US); + StringTemplateLoader loader = new StringTemplateLoader(); + cfg.setTemplateLoader(loader); + cfg.setTemplateUpdateDelay(Integer.MAX_VALUE); + + loader.putTemplate("1_en_US.ftl", "1_en_US v1"); + loader.putTemplate("1_en.ftl", "1_en v1"); + loader.putTemplate("1.ftl", "1 v1"); + + assertEquals("1_en_US v1", cfg.getTemplate("1.ftl").toString()); + assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); + assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); + + loader.putTemplate("1_en_US.ftl", "1_en_US v2"); + loader.putTemplate("1_en.ftl", "1_en v2"); + loader.putTemplate("1.ftl", "1 v2"); + assertEquals("1_en_US v1", cfg.getTemplate("1.ftl").toString()); + assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); + assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); + + cfg.removeTemplateFromCache("1.ftl"); + assertEquals("1_en_US v2", cfg.getTemplate("1.ftl").toString()); + assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); + assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); + assertEquals("1 v2", cfg.getTemplate("1.ftl", Locale.ITALY).toString()); + + cfg.removeTemplateFromCache("1.ftl", Locale.GERMANY); + assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); + assertEquals("1 v2", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); + + cfg.removeTemplateFromCache("1.ftl", Locale.CANADA); + assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); + + cfg.removeTemplateFromCache("1.ftl", Locale.UK); + assertEquals("1_en v2", cfg.getTemplate("1.ftl", Locale.UK).toString()); + } + + @Test + public void testZeroUpdateDelay() throws IOException, InterruptedException { + Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); + cfg.setLocale(Locale.US); + cfg.setCacheStorage(new StrongCacheStorage()); + MonitoredTemplateLoader loader = new MonitoredTemplateLoader(); + cfg.setTemplateLoader(loader); + cfg.setTemplateUpdateDelayMilliseconds(0); + for (int i = 1; i <= 3; i++) { + loader.putTextTemplate("t.ftl", "v" + i); + assertEquals("v" + i, cfg.getTemplate("t.ftl").toString()); + } + + loader.clearEvents(); + loader.putTextTemplate("t.ftl", "v8"); + assertEquals("v8", cfg.getTemplate("t.ftl").toString()); + assertEquals("v8", cfg.getTemplate("t.ftl").toString()); + loader.putTextTemplate("t.ftl", "v9"); + assertEquals("v9", cfg.getTemplate("t.ftl").toString()); + assertEquals("v9", cfg.getTemplate("t.ftl").toString()); + assertEquals( + ImmutableList.of( + new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v8 + new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), + + new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v8 + new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), + + new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v9 + new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), + + new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v9 + new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED) + ), + loader.getEvents(LoadEvent.class)); + + cfg.setLocalizedLookup(false); + loader.clearEvents(); + loader.putTextTemplate("t.ftl", "v10"); + assertEquals("v10", cfg.getTemplate("t.ftl").toString()); + loader.putTextTemplate("t.ftl", "v11"); // same time stamp, different content + assertEquals("v11", cfg.getTemplate("t.ftl").toString()); + assertEquals("v11", cfg.getTemplate("t.ftl").toString()); + assertEquals("v11", cfg.getTemplate("t.ftl").toString()); + Thread.sleep(17L); + assertEquals("v11", cfg.getTemplate("t.ftl").toString()); + loader.putTextTemplate("t.ftl", "v12"); + assertEquals("v12", cfg.getTemplate("t.ftl").toString()); + assertEquals("v12", cfg.getTemplate("t.ftl").toString()); + assertEquals( + ImmutableList.of( + new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), // v10 + new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), // v11 + new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), + new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), + new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), + new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), // v12 + new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED) + ), + loader.getEvents(LoadEvent.class)); + } + + @Test + public void testWrongEncodingReload() throws IOException { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); + cfg.setLocale(Locale.US); + + MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); + tl.putBinaryTemplate("utf-8_en.ftl", "<#ftl encoding='utf-8'>Béka"); + tl.putBinaryTemplate("utf-8.ftl", "Bar"); + tl.putBinaryTemplate("iso-8859-1_en_US.ftl", "<#ftl encoding='ISO-8859-1'>Béka", StandardCharsets.ISO_8859_1, + "v1"); + 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("Béka", t.toString()); + + assertEquals( + ImmutableList.of( + CreateSessionEvent.INSTANCE, + new LoadEvent("utf-8_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("utf-8_en.ftl", TemplateLoadingResultStatus.OPENED), + CloseSessionEvent.INSTANCE), + tl.getEvents()); + } + + { + tl.clearEvents(); + + Template t = cfg.getTemplate("utf-8.ftl", "ISO-8859-5"); + assertEquals("utf-8.ftl", t.getName()); + assertEquals("utf-8_en.ftl", t.getSourceName()); + assertEquals("utf-8", t.getEncoding()); + assertEquals("Béka", t.toString()); + + assertEquals( + ImmutableList.of( + CreateSessionEvent.INSTANCE, + new LoadEvent("utf-8_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("utf-8_en.ftl", TemplateLoadingResultStatus.OPENED), + CloseSessionEvent.INSTANCE), + tl.getEvents()); + } + + { + tl.clearEvents(); + + Template t = cfg.getTemplate("iso-8859-1.ftl", "utf-8"); + assertEquals("iso-8859-1.ftl", t.getName()); + assertEquals("iso-8859-1_en_US.ftl", t.getSourceName()); + assertEquals("ISO-8859-1", t.getEncoding()); + assertEquals("Béka", t.toString()); + + assertEquals( + ImmutableList.of( + CreateSessionEvent.INSTANCE, + new LoadEvent("iso-8859-1_en_US.ftl", TemplateLoadingResultStatus.OPENED), + CloseSessionEvent.INSTANCE), + tl.getEvents()); + } + } + + @Test + public void testNoWrongEncodingForTemplateLoader2WithReader() throws IOException { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); + cfg.setLocale(Locale.US); + + MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); + tl.putTextTemplate("foo_en.ftl", "<#ftl encoding='utf-8'>Å"); + tl.putTextTemplate("foo.ftl", "B"); + cfg.setTemplateLoader(tl); + + { + Template t = cfg.getTemplate("foo.ftl", "Utf-8"); + assertEquals("foo.ftl", t.getName()); + assertEquals("foo_en.ftl", t.getSourceName()); + assertNull(t.getEncoding()); + assertEquals("Å", t.toString()); + + assertEquals( + ImmutableList.of( + CreateSessionEvent.INSTANCE, + new LoadEvent("foo_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("foo_en.ftl", TemplateLoadingResultStatus.OPENED), + CloseSessionEvent.INSTANCE), + tl.getEvents()); + } + + { + tl.clearEvents(); + + Template t = cfg.getTemplate("foo.ftl", "iso-8859-1"); + assertEquals("foo.ftl", t.getName()); + assertEquals("foo_en.ftl", t.getSourceName()); + assertNull(t.getEncoding()); + assertEquals("Å", t.toString()); + + assertEquals( + ImmutableList.of( + CreateSessionEvent.INSTANCE, + new LoadEvent("foo_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), + new LoadEvent("foo_en.ftl", TemplateLoadingResultStatus.OPENED), + CloseSessionEvent.INSTANCE), + tl.getEvents()); + } + } + + @Test + public void testEncodingSelection() throws IOException { + Locale hungary = new Locale("hu", "HU"); + + Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); + cfg.setDefaultEncoding("utf-8"); + + MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); + tl.putBinaryTemplate("t.ftl", "Foo"); + tl.putBinaryTemplate("t_de.ftl", "Vuu"); + tl.putBinaryTemplate("t2.ftl", "<#ftl encoding='ISO-8859-5'>пÑимеÑ", Charset.forName("ISO-8859-5"), "v1"); + tl.putBinaryTemplate("t2_de.ftl", "<#ftl encoding='ISO-8859-5'>пÑимеÑ", Charset.forName("ISO-8859-5"), "v1"); + cfg.setTemplateLoader(tl); + + // No locale-to-encoding mapping exists yet: + { + Template t = cfg.getTemplate("t.ftl", Locale.GERMANY); + assertEquals("t.ftl", t.getName()); + assertEquals("t_de.ftl", t.getSourceName()); + assertEquals("utf-8", t.getEncoding()); + assertEquals("Vuu", t.toString()); + } + + cfg.setEncoding(Locale.GERMANY, "ISO-8859-1"); + cfg.setEncoding(hungary, "ISO-8859-2"); + { + Template t = cfg.getTemplate("t.ftl", Locale.CHINESE); + assertEquals("t.ftl", t.getName()); + assertEquals("t.ftl", t.getSourceName()); + assertEquals("utf-8", t.getEncoding()); + assertEquals("Foo", t.toString()); + } + { + Template t = cfg.getTemplate("t.ftl", Locale.GERMANY); + assertEquals("t.ftl", t.getName()); + assertEquals("t_de.ftl", t.getSourceName()); + assertEquals("ISO-8859-1", t.getEncoding()); + assertEquals("Vuu", t.toString()); + } + { + Template t = cfg.getTemplate("t.ftl", hungary); + assertEquals("t.ftl", t.getName()); + assertEquals("t.ftl", t.getSourceName()); + assertEquals("ISO-8859-2", t.getEncoding()); + assertEquals("Foo", t.toString()); + } + + // #ftl header overrides: + { + Template t = cfg.getTemplate("t2.ftl", Locale.CHINESE); + assertEquals("t2.ftl", t.getName()); + assertEquals("t2.ftl", t.getSourceName()); + assertEquals("ISO-8859-5", t.getEncoding()); + assertEquals("пÑимеÑ", t.toString()); + } + { + Template t = cfg.getTemplate("t2.ftl", Locale.GERMANY); + assertEquals("t2.ftl", t.getName()); + assertEquals("t2_de.ftl", t.getSourceName()); + assertEquals("ISO-8859-5", t.getEncoding()); + assertEquals("пÑимеÑ", t.toString()); + } + { + Template t = cfg.getTemplate("t2.ftl", hungary); + assertEquals("t2.ftl", t.getName()); + assertEquals("t2.ftl", t.getSourceName()); + assertEquals("ISO-8859-5", t.getEncoding()); + assertEquals("пÑимеÑ", t.toString()); + } + } + + @Test + public void testTemplateNameFormatExceptionAndBackwardCompatibility() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException { + Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); + + assertNull(cfg.getTemplate("../x", null, null, null, true, true)); + try { + cfg.getTemplate("../x"); + fail(); + } catch (TemplateNotFoundException e) { + // expected + } + + // [2.4] Test it with IcI 2.4 + + cfg.setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_4_0); + try { + cfg.getTemplate("../x", null, null, null, true, true); + fail(); + } catch (MalformedTemplateNameException e) { + // expected + } + try { + cfg.getTemplate("../x"); + fail(); + } catch (MalformedTemplateNameException e) { + // expected + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/94d39312/src/test/java/org/apache/freemarker/core/templateresolver/TemplateCacheTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/templateresolver/TemplateCacheTest.java b/src/test/java/org/apache/freemarker/core/templateresolver/TemplateCacheTest.java deleted file mode 100644 index ed1da36..0000000 --- a/src/test/java/org/apache/freemarker/core/templateresolver/TemplateCacheTest.java +++ /dev/null @@ -1,488 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.freemarker.core.templateresolver; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.io.Serializable; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.util.Locale; - -import org.apache.freemarker.core.Configuration; -import org.apache.freemarker.core.Template; -import org.apache.freemarker.core.TemplateNotFoundException; -import org.apache.freemarker.core.ast.ParseException; -import org.apache.freemarker.core.templateresolver.StringTemplateLoader; -import org.apache.freemarker.core.templateresolver.StrongCacheStorage; -import org.apache.freemarker.core.templateresolver.TemplateCache; -import org.apache.freemarker.core.templateresolver.TemplateLoader; -import org.apache.freemarker.core.templateresolver.TemplateLoaderSession; -import org.apache.freemarker.core.templateresolver.TemplateLoadingResult; -import org.apache.freemarker.core.templateresolver.TemplateLoadingResultStatus; -import org.apache.freemarker.core.templateresolver.TemplateLoadingSource; -import org.apache.freemarker.core.templateresolver.TemplateNameFormat; -import org.apache.freemarker.test.MonitoredTemplateLoader; -import org.apache.freemarker.test.MonitoredTemplateLoader.CloseSessionEvent; -import org.apache.freemarker.test.MonitoredTemplateLoader.CreateSessionEvent; -import org.apache.freemarker.test.MonitoredTemplateLoader.LoadEvent; -import org.hamcrest.Matchers; -import org.junit.Test; - -import com.google.common.collect.ImmutableList; - -public class TemplateCacheTest { - - @Test - public void testCachedException() throws Exception { - MockTemplateLoader loader = new MockTemplateLoader(); - TemplateCache cache = new TemplateCache( - loader, new StrongCacheStorage(), new Configuration(Configuration.VERSION_3_0_0)); - cache.setDelay(1000L); - loader.setThrowException(true); - try { - cache.getTemplate("t", Locale.getDefault(), "", true); - fail(); - } catch (IOException e) { - assertEquals("mock IO exception", e.getMessage()); - assertEquals(1, loader.getLoadAttemptCount()); - try { - cache.getTemplate("t", Locale.getDefault(), "", true); - fail(); - } catch (IOException e2) { - // Still 1 - returned cached exception - assertThat(e2.getMessage(), - Matchers.allOf(Matchers.containsString("There was an error loading the template on an " + - "earlier attempt"))); - assertSame(e, e2.getCause()); - assertEquals(1, loader.getLoadAttemptCount()); - try { - Thread.sleep(1100L); - cache.getTemplate("t", Locale.getDefault(), "", true); - fail(); - } catch (IOException e3) { - // Cache had to retest - assertEquals("mock IO exception", e.getMessage()); - assertEquals(2, loader.getLoadAttemptCount()); - } - } - } - } - - @Test - public void testCachedNotFound() throws Exception { - MockTemplateLoader loader = new MockTemplateLoader(); - TemplateCache cache = new TemplateCache(loader, new StrongCacheStorage(), new Configuration()); - cache.setDelay(1000L); - cache.setLocalizedLookup(false); - assertNull(cache.getTemplate("t", Locale.getDefault(), "", true)); - assertEquals(1, loader.getLoadAttemptCount()); - assertNull(cache.getTemplate("t", Locale.getDefault(), "", true)); - // Still 1 - returned cached exception - assertEquals(1, loader.getLoadAttemptCount()); - Thread.sleep(1100L); - assertNull(cache.getTemplate("t", Locale.getDefault(), "", true)); - // Cache had to retest - assertEquals(2, loader.getLoadAttemptCount()); - } - - private static class MockTemplateLoader implements TemplateLoader { - private boolean throwException; - private int loadAttemptCount; - - public void setThrowException(boolean throwException) { - this.throwException = throwException; - } - - public int getLoadAttemptCount() { - return loadAttemptCount; - } - - @Override - public TemplateLoaderSession createSession() { - return null; - } - - @Override - public TemplateLoadingResult load(String name, TemplateLoadingSource ifSourceDiffersFrom, - Serializable ifVersionDiffersFrom, TemplateLoaderSession session) throws IOException { - ++loadAttemptCount; - if (throwException) { - throw new IOException("mock IO exception"); - } - return TemplateLoadingResult.NOT_FOUND; - } - - @Override - public void resetState() { - // - } - - } - - @Test - public void testManualRemovalPlain() throws IOException { - Configuration cfg = new Configuration(); - cfg.setCacheStorage(new StrongCacheStorage()); - StringTemplateLoader loader = new StringTemplateLoader(); - cfg.setTemplateLoader(loader); - cfg.setTemplateUpdateDelay(Integer.MAX_VALUE); - - loader.putTemplate("1.ftl", "1 v1"); - loader.putTemplate("2.ftl", "2 v1"); - assertEquals("1 v1", cfg.getTemplate("1.ftl").toString()); - assertEquals("2 v1", cfg.getTemplate("2.ftl").toString()); - - loader.putTemplate("1.ftl", "1 v2"); - loader.putTemplate("2.ftl", "2 v2"); - assertEquals("1 v1", cfg.getTemplate("1.ftl").toString()); // no change - assertEquals("2 v1", cfg.getTemplate("2.ftl").toString()); // no change - - cfg.removeTemplateFromCache("1.ftl"); - assertEquals("1 v2", cfg.getTemplate("1.ftl").toString()); // changed - assertEquals("2 v1", cfg.getTemplate("2.ftl").toString()); - - cfg.removeTemplateFromCache("2.ftl"); - assertEquals("1 v2", cfg.getTemplate("1.ftl").toString()); - assertEquals("2 v2", cfg.getTemplate("2.ftl").toString()); // changed - } - - @Test - public void testManualRemovalI18ed() throws IOException { - Configuration cfg = new Configuration(); - cfg.setCacheStorage(new StrongCacheStorage()); - cfg.setLocale(Locale.US); - StringTemplateLoader loader = new StringTemplateLoader(); - cfg.setTemplateLoader(loader); - cfg.setTemplateUpdateDelay(Integer.MAX_VALUE); - - loader.putTemplate("1_en_US.ftl", "1_en_US v1"); - loader.putTemplate("1_en.ftl", "1_en v1"); - loader.putTemplate("1.ftl", "1 v1"); - - assertEquals("1_en_US v1", cfg.getTemplate("1.ftl").toString()); - assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); - assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); - - loader.putTemplate("1_en_US.ftl", "1_en_US v2"); - loader.putTemplate("1_en.ftl", "1_en v2"); - loader.putTemplate("1.ftl", "1 v2"); - assertEquals("1_en_US v1", cfg.getTemplate("1.ftl").toString()); - assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); - assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); - - cfg.removeTemplateFromCache("1.ftl"); - assertEquals("1_en_US v2", cfg.getTemplate("1.ftl").toString()); - assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); - assertEquals("1 v1", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); - assertEquals("1 v2", cfg.getTemplate("1.ftl", Locale.ITALY).toString()); - - cfg.removeTemplateFromCache("1.ftl", Locale.GERMANY); - assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); - assertEquals("1 v2", cfg.getTemplate("1.ftl", Locale.GERMANY).toString()); - - cfg.removeTemplateFromCache("1.ftl", Locale.CANADA); - assertEquals("1_en v1", cfg.getTemplate("1.ftl", Locale.UK).toString()); - - cfg.removeTemplateFromCache("1.ftl", Locale.UK); - assertEquals("1_en v2", cfg.getTemplate("1.ftl", Locale.UK).toString()); - } - - @Test - public void testZeroUpdateDelay() throws IOException, InterruptedException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setLocale(Locale.US); - cfg.setCacheStorage(new StrongCacheStorage()); - MonitoredTemplateLoader loader = new MonitoredTemplateLoader(); - cfg.setTemplateLoader(loader); - cfg.setTemplateUpdateDelayMilliseconds(0); - for (int i = 1; i <= 3; i++) { - loader.putTextTemplate("t.ftl", "v" + i); - assertEquals("v" + i, cfg.getTemplate("t.ftl").toString()); - } - - loader.clearEvents(); - loader.putTextTemplate("t.ftl", "v8"); - assertEquals("v8", cfg.getTemplate("t.ftl").toString()); - assertEquals("v8", cfg.getTemplate("t.ftl").toString()); - loader.putTextTemplate("t.ftl", "v9"); - assertEquals("v9", cfg.getTemplate("t.ftl").toString()); - assertEquals("v9", cfg.getTemplate("t.ftl").toString()); - assertEquals( - ImmutableList.of( - new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v8 - new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), - - new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v8 - new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), - - new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v9 - new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), - - new LoadEvent("t_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), // v9 - new LoadEvent("t_en.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED) - ), - loader.getEvents(LoadEvent.class)); - - cfg.setLocalizedLookup(false); - loader.clearEvents(); - loader.putTextTemplate("t.ftl", "v10"); - assertEquals("v10", cfg.getTemplate("t.ftl").toString()); - loader.putTextTemplate("t.ftl", "v11"); // same time stamp, different content - assertEquals("v11", cfg.getTemplate("t.ftl").toString()); - assertEquals("v11", cfg.getTemplate("t.ftl").toString()); - assertEquals("v11", cfg.getTemplate("t.ftl").toString()); - Thread.sleep(17L); - assertEquals("v11", cfg.getTemplate("t.ftl").toString()); - loader.putTextTemplate("t.ftl", "v12"); - assertEquals("v12", cfg.getTemplate("t.ftl").toString()); - assertEquals("v12", cfg.getTemplate("t.ftl").toString()); - assertEquals( - ImmutableList.of( - new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), // v10 - new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), // v11 - new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), - new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), - new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED), - new LoadEvent("t.ftl", TemplateLoadingResultStatus.OPENED), // v12 - new LoadEvent("t.ftl", TemplateLoadingResultStatus.NOT_MODIFIED) - ), - loader.getEvents(LoadEvent.class)); - } - - @Test - public void testWrongEncodingReload() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); - cfg.setLocale(Locale.US); - - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); - tl.putBinaryTemplate("utf-8_en.ftl", "<#ftl encoding='utf-8'>Béka"); - tl.putBinaryTemplate("utf-8.ftl", "Bar"); - tl.putBinaryTemplate("iso-8859-1_en_US.ftl", "<#ftl encoding='ISO-8859-1'>Béka", StandardCharsets.ISO_8859_1, - "v1"); - 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("Béka", t.toString()); - - assertEquals( - ImmutableList.of( - CreateSessionEvent.INSTANCE, - new LoadEvent("utf-8_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("utf-8_en.ftl", TemplateLoadingResultStatus.OPENED), - CloseSessionEvent.INSTANCE), - tl.getEvents()); - } - - { - tl.clearEvents(); - - Template t = cfg.getTemplate("utf-8.ftl", "ISO-8859-5"); - assertEquals("utf-8.ftl", t.getName()); - assertEquals("utf-8_en.ftl", t.getSourceName()); - assertEquals("utf-8", t.getEncoding()); - assertEquals("Béka", t.toString()); - - assertEquals( - ImmutableList.of( - CreateSessionEvent.INSTANCE, - new LoadEvent("utf-8_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("utf-8_en.ftl", TemplateLoadingResultStatus.OPENED), - CloseSessionEvent.INSTANCE), - tl.getEvents()); - } - - { - tl.clearEvents(); - - Template t = cfg.getTemplate("iso-8859-1.ftl", "utf-8"); - assertEquals("iso-8859-1.ftl", t.getName()); - assertEquals("iso-8859-1_en_US.ftl", t.getSourceName()); - assertEquals("ISO-8859-1", t.getEncoding()); - assertEquals("Béka", t.toString()); - - assertEquals( - ImmutableList.of( - CreateSessionEvent.INSTANCE, - new LoadEvent("iso-8859-1_en_US.ftl", TemplateLoadingResultStatus.OPENED), - CloseSessionEvent.INSTANCE), - tl.getEvents()); - } - } - - @Test - public void testNoWrongEncodingForTemplateLoader2WithReader() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); - cfg.setLocale(Locale.US); - - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); - tl.putTextTemplate("foo_en.ftl", "<#ftl encoding='utf-8'>Å"); - tl.putTextTemplate("foo.ftl", "B"); - cfg.setTemplateLoader(tl); - - { - Template t = cfg.getTemplate("foo.ftl", "Utf-8"); - assertEquals("foo.ftl", t.getName()); - assertEquals("foo_en.ftl", t.getSourceName()); - assertNull(t.getEncoding()); - assertEquals("Å", t.toString()); - - assertEquals( - ImmutableList.of( - CreateSessionEvent.INSTANCE, - new LoadEvent("foo_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("foo_en.ftl", TemplateLoadingResultStatus.OPENED), - CloseSessionEvent.INSTANCE), - tl.getEvents()); - } - - { - tl.clearEvents(); - - Template t = cfg.getTemplate("foo.ftl", "iso-8859-1"); - assertEquals("foo.ftl", t.getName()); - assertEquals("foo_en.ftl", t.getSourceName()); - assertNull(t.getEncoding()); - assertEquals("Å", t.toString()); - - assertEquals( - ImmutableList.of( - CreateSessionEvent.INSTANCE, - new LoadEvent("foo_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), - new LoadEvent("foo_en.ftl", TemplateLoadingResultStatus.OPENED), - CloseSessionEvent.INSTANCE), - tl.getEvents()); - } - } - - @Test - public void testEncodingSelection() throws IOException { - Locale hungary = new Locale("hu", "HU"); - - Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); - cfg.setDefaultEncoding("utf-8"); - - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); - tl.putBinaryTemplate("t.ftl", "Foo"); - tl.putBinaryTemplate("t_de.ftl", "Vuu"); - tl.putBinaryTemplate("t2.ftl", "<#ftl encoding='ISO-8859-5'>пÑимеÑ", Charset.forName("ISO-8859-5"), "v1"); - tl.putBinaryTemplate("t2_de.ftl", "<#ftl encoding='ISO-8859-5'>пÑимеÑ", Charset.forName("ISO-8859-5"), "v1"); - cfg.setTemplateLoader(tl); - - // No locale-to-encoding mapping exists yet: - { - Template t = cfg.getTemplate("t.ftl", Locale.GERMANY); - assertEquals("t.ftl", t.getName()); - assertEquals("t_de.ftl", t.getSourceName()); - assertEquals("utf-8", t.getEncoding()); - assertEquals("Vuu", t.toString()); - } - - cfg.setEncoding(Locale.GERMANY, "ISO-8859-1"); - cfg.setEncoding(hungary, "ISO-8859-2"); - { - Template t = cfg.getTemplate("t.ftl", Locale.CHINESE); - assertEquals("t.ftl", t.getName()); - assertEquals("t.ftl", t.getSourceName()); - assertEquals("utf-8", t.getEncoding()); - assertEquals("Foo", t.toString()); - } - { - Template t = cfg.getTemplate("t.ftl", Locale.GERMANY); - assertEquals("t.ftl", t.getName()); - assertEquals("t_de.ftl", t.getSourceName()); - assertEquals("ISO-8859-1", t.getEncoding()); - assertEquals("Vuu", t.toString()); - } - { - Template t = cfg.getTemplate("t.ftl", hungary); - assertEquals("t.ftl", t.getName()); - assertEquals("t.ftl", t.getSourceName()); - assertEquals("ISO-8859-2", t.getEncoding()); - assertEquals("Foo", t.toString()); - } - - // #ftl header overrides: - { - Template t = cfg.getTemplate("t2.ftl", Locale.CHINESE); - assertEquals("t2.ftl", t.getName()); - assertEquals("t2.ftl", t.getSourceName()); - assertEquals("ISO-8859-5", t.getEncoding()); - assertEquals("пÑимеÑ", t.toString()); - } - { - Template t = cfg.getTemplate("t2.ftl", Locale.GERMANY); - assertEquals("t2.ftl", t.getName()); - assertEquals("t2_de.ftl", t.getSourceName()); - assertEquals("ISO-8859-5", t.getEncoding()); - assertEquals("пÑимеÑ", t.toString()); - } - { - Template t = cfg.getTemplate("t2.ftl", hungary); - assertEquals("t2.ftl", t.getName()); - assertEquals("t2.ftl", t.getSourceName()); - assertEquals("ISO-8859-5", t.getEncoding()); - assertEquals("пÑимеÑ", t.toString()); - } - } - - @Test - public void testTemplateNameFormatExceptionAndBackwardCompatibility() throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException { - Configuration cfg = new Configuration(Configuration.VERSION_2_3_22); - - assertNull(cfg.getTemplate("../x", null, null, null, true, true)); - try { - cfg.getTemplate("../x"); - fail(); - } catch (TemplateNotFoundException e) { - // expected - } - - // [2.4] Test it with IcI 2.4 - - cfg.setTemplateNameFormat(TemplateNameFormat.DEFAULT_2_4_0); - try { - cfg.getTemplate("../x", null, null, null, true, true); - fail(); - } catch (MalformedTemplateNameException e) { - // expected - } - try { - cfg.getTemplate("../x"); - fail(); - } catch (MalformedTemplateNameException e) { - // expected - } - } - -}
