http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/TemplateLookupStrategyTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TemplateLookupStrategyTest.java b/src/test/java/org/apache/freemarker/core/TemplateLookupStrategyTest.java index 95a396d..f0e63a8 100644 --- a/src/test/java/org/apache/freemarker/core/TemplateLookupStrategyTest.java +++ b/src/test/java/org/apache/freemarker/core/TemplateLookupStrategyTest.java @@ -19,6 +19,7 @@ package org.apache.freemarker.core; +import static org.apache.freemarker.core.Configuration.ExtendableBuilder.TEMPLATE_LOOKUP_STRATEGY_KEY; import static org.junit.Assert.*; import java.io.IOException; @@ -39,26 +40,35 @@ public class TemplateLookupStrategyTest { @Test public void testSetSetting() throws Exception { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - assertSame(DefaultTemplateLookupStrategy.INSTANCE, cfg.getTemplateLookupStrategy()); + assertSame( + DefaultTemplateLookupStrategy.INSTANCE, + new Configuration.Builder(Configuration.VERSION_3_0_0).build() + .getTemplateLookupStrategy()); - cfg.setSetting(Configuration.TEMPLATE_LOOKUP_STRATEGY_KEY, MyTemplateLookupStrategy.class.getName() + "()"); - assertTrue(cfg.getTemplateLookupStrategy() instanceof MyTemplateLookupStrategy); - - cfg.setSetting(Configuration.TEMPLATE_LOOKUP_STRATEGY_KEY, "dEfault"); - assertSame(DefaultTemplateLookupStrategy.INSTANCE, cfg.getTemplateLookupStrategy()); + assertTrue( + new Configuration.Builder(Configuration.VERSION_3_0_0) + .setting(TEMPLATE_LOOKUP_STRATEGY_KEY, MyTemplateLookupStrategy.class.getName() + "()") + .build() + .getTemplateLookupStrategy() instanceof MyTemplateLookupStrategy); + + assertSame( + DefaultTemplateLookupStrategy.INSTANCE, + new Configuration.Builder(Configuration.VERSION_3_0_0) + .setting(TEMPLATE_LOOKUP_STRATEGY_KEY, "dEfault") + .build() + .getTemplateLookupStrategy()); } @Test public void testCustomStrategy() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("test.ftl", ""); tl.putTextTemplate("aa/test.ftl", ""); - cfg.setTemplateLoader(tl); - - cfg.setTemplateLookupStrategy(MyTemplateLookupStrategy.INSTANCE); + + Configuration cfg = new Configuration.Builder(Configuration.VERSION_3_0_0) + .templateLoader(tl) + .templateLookupStrategy(MyTemplateLookupStrategy.INSTANCE) + .build(); final Locale locale = new Locale("aa", "BB", "CC_DD"); @@ -86,18 +96,16 @@ public class TemplateLookupStrategyTest { @Test public void testDefaultStrategy() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("test.ftl", ""); tl.putTextTemplate("test_aa.ftl", ""); tl.putTextTemplate("test_aa_BB.ftl", ""); tl.putTextTemplate("test_aa_BB_CC.ftl", ""); tl.putTextTemplate("test_aa_BB_CC_DD.ftl", ""); - cfg.setTemplateLoader(tl); - + try { - cfg.getTemplate("missing.ftl", new Locale("aa", "BB", "CC_DD")); + new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate("missing.ftl", new Locale("aa", "BB", "CC_DD")); fail(); } catch (TemplateNotFoundException e) { assertEquals("missing.ftl", e.getTemplateName()); @@ -110,12 +118,12 @@ public class TemplateLookupStrategyTest { "missing.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } - - cfg.setLocale(new Locale("xx")); + try { - cfg.getTemplate("missing.ftl"); + new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl) + .locale(new Locale("xx")).build() + .getTemplate("missing.ftl"); fail(); } catch (TemplateNotFoundException e) { assertEquals("missing.ftl", e.getTemplateName()); @@ -123,12 +131,13 @@ public class TemplateLookupStrategyTest { ImmutableList.of("missing_xx.ftl", "missing.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } - cfg.setLocalizedLookup(false); try { - cfg.getTemplate("missing.ftl"); + new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl) + .locale(new Locale("xx")) + .localizedLookup(false).build() + .getTemplate("missing.ftl"); fail(); } catch (TemplateNotFoundException e) { assertEquals("missing.ftl", e.getTemplateName()); @@ -136,12 +145,11 @@ public class TemplateLookupStrategyTest { ImmutableList.of("missing.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } - cfg.setLocalizedLookup(true); - + try { - cfg.getTemplate("_a_b_.ftl", new Locale("xx", "yy")); + new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate("_a_b_.ftl", new Locale("xx", "yy")); fail(); } catch (TemplateNotFoundException e) { assertEquals("_a_b_.ftl", e.getTemplateName()); @@ -149,13 +157,13 @@ public class TemplateLookupStrategyTest { ImmutableList.of("_a_b__xx_YY.ftl", "_a_b__xx.ftl", "_a_b_.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } for (String templateName : new String[] { "test.ftl", "./test.ftl", "/test.ftl", "x/foo/../../test.ftl" }) { { final Locale locale = new Locale("aa", "BB", "CC_DD"); - final Template t = cfg.getTemplate("test.ftl", locale); + final Template t = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate("test.ftl", locale); assertEquals("test.ftl", t.getLookupName()); assertEquals("test_aa_BB_CC_DD.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); @@ -163,24 +171,24 @@ public class TemplateLookupStrategyTest { assertEquals(ImmutableList.of("test_aa_BB_CC_DD.ftl"), tl.getLoadNames()); assertNull(t.getCustomLookupCondition()); tl.clearEvents(); - cfg.clearTemplateCache(); } { final Locale locale = new Locale("aa", "BB", "CC_XX"); - final Template t = cfg.getTemplate(templateName, locale); + final Template t = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate(templateName, locale); assertEquals("test.ftl", t.getLookupName()); assertEquals("test_aa_BB_CC.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); assertNull(t.getCustomLookupCondition()); assertEquals(ImmutableList.of("test_aa_BB_CC_XX.ftl", "test_aa_BB_CC.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } { final Locale locale = new Locale("aa", "BB", "XX_XX"); - final Template t = cfg.getTemplate(templateName, locale); + final Template t = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate(templateName, locale); assertEquals("test.ftl", t.getLookupName()); assertEquals("test_aa_BB.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); @@ -189,13 +197,13 @@ public class TemplateLookupStrategyTest { ImmutableList.of("test_aa_BB_XX_XX.ftl", "test_aa_BB_XX.ftl", "test_aa_BB.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } { - cfg.setLocalizedLookup(false); final Locale locale = new Locale("aa", "BB", "XX_XX"); - final Template t = cfg.getTemplate(templateName, locale); + final Template t = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl) + .localizedLookup(false).build() + .getTemplate(templateName, locale); assertEquals("test.ftl", t.getLookupName()); assertEquals("test.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); @@ -204,13 +212,12 @@ public class TemplateLookupStrategyTest { ImmutableList.of("test.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); - cfg.setLocalizedLookup(true); } { final Locale locale = new Locale("aa", "XX", "XX_XX"); - final Template t = cfg.getTemplate(templateName, locale); + final Template t = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate(templateName, locale); assertEquals("test.ftl", t.getLookupName()); assertEquals("test_aa.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); @@ -219,12 +226,12 @@ public class TemplateLookupStrategyTest { ImmutableList.of("test_aa_XX_XX_XX.ftl", "test_aa_XX_XX.ftl", "test_aa_XX.ftl", "test_aa.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } { final Locale locale = new Locale("xx", "XX", "XX_XX"); - final Template t = cfg.getTemplate(templateName, locale); + final Template t = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate(templateName, locale); assertEquals("test.ftl", t.getLookupName()); assertEquals("test.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); @@ -234,12 +241,12 @@ public class TemplateLookupStrategyTest { "test_xx_XX_XX_XX.ftl", "test_xx_XX_XX.ftl", "test_xx_XX.ftl", "test_xx.ftl", "test.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } { final Locale locale = new Locale("xx", "BB", "CC_DD"); - final Template t = cfg.getTemplate(templateName, locale); + final Template t = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build() + .getTemplate(templateName, locale); assertEquals("test.ftl", t.getLookupName()); assertEquals("test.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); @@ -249,20 +256,18 @@ public class TemplateLookupStrategyTest { "test_xx_BB_CC_DD.ftl", "test_xx_BB_CC.ftl", "test_xx_BB.ftl", "test_xx.ftl", "test.ftl"), tl.getLoadNames()); tl.clearEvents(); - cfg.clearTemplateCache(); } } } @Test public void testAcquisition() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("t.ftl", ""); tl.putTextTemplate("sub/i.ftl", ""); tl.putTextTemplate("x/sub/i.ftl", ""); - cfg.setTemplateLoader(tl); + + Configuration cfg = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build(); final Locale locale = new Locale("xx"); @@ -299,8 +304,18 @@ public class TemplateLookupStrategyTest { @Test public void testCustomLookupCondition() throws IOException, TemplateException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - + MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); + + final Configuration cfg; + final Configuration cfgNoLocLU; + { + Configuration.Builder cfgB = new Configuration.Builder(Configuration.VERSION_3_0_0) + .templateLoader(tl) + .templateLookupStrategy(new DomainTemplateLookupStrategy()); + cfg = cfgB.build(); + cfgNoLocLU = cfgB.localizedLookup(false).build(); + } + final String iAtDefaultContent = "i at default"; final String iXxAtDefaultContent = "i_xx at default"; final String iAtBaazComContent = "i at baaz.com"; @@ -314,7 +329,6 @@ public class TemplateLookupStrategyTest { final String t2XxLocaleExpectedOutput = "i3_xx at foo.com"; final String t2OtherLocaleExpectedOutput = "i3 at foo.com"; - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("@foo.com/t.ftl", tAtFooComContent); tl.putTextTemplate("@bar.com/t.ftl", tAtBarComContent); tl.putTextTemplate("@default/t.ftl", tAtDefaultContent); @@ -326,10 +340,7 @@ public class TemplateLookupStrategyTest { tl.putTextTemplate("@default/i2.ftl", "<#import 'i3.ftl' as i3 />"); tl.putTextTemplate("@foo.com/i3.ftl", "<#global proof = '" + t2OtherLocaleExpectedOutput + "'>"); tl.putTextTemplate("@foo.com/i3_xx.ftl", "<#global proof = '" + t2XxLocaleExpectedOutput + "'>"); - cfg.setTemplateLoader(tl); - - cfg.setTemplateLookupStrategy(new DomainTemplateLookupStrategy()); - + { final Locale locale = new Locale("xx"); final Domain domain = new Domain("foo.com"); @@ -423,10 +434,9 @@ public class TemplateLookupStrategyTest { } { - cfg.setLocalizedLookup(false); final Locale locale = new Locale("xx", "YY"); final Domain domain = new Domain("nosuch.com"); - final Template t = cfg.getTemplate("i.ftl", locale, domain); + final Template t = cfgNoLocLU.getTemplate("i.ftl", locale, domain); assertEquals("i.ftl", t.getLookupName()); assertEquals("@default/i.ftl", t.getSourceName()); assertEquals(locale, t.getLocale()); @@ -437,8 +447,7 @@ public class TemplateLookupStrategyTest { tl.getLoadNames()); tl.clearEvents(); - cfg.setLocalizedLookup(true); - cfg.clearTemplateCache(); + cfgNoLocLU.clearTemplateCache(); } { @@ -474,10 +483,9 @@ public class TemplateLookupStrategyTest { } { - cfg.setLocalizedLookup(false); final Locale locale = new Locale("xx"); final Domain domain = new Domain("foo.com"); - final Template t = cfg.getTemplate("t2.ftl", locale, domain); + final Template t = cfgNoLocLU.getTemplate("t2.ftl", locale, domain); assertOutputEquals(t2OtherLocaleExpectedOutput, t); assertEquals( ImmutableList.of( @@ -487,8 +495,7 @@ public class TemplateLookupStrategyTest { tl.getLoadNames()); tl.clearEvents(); - cfg.setLocalizedLookup(true); - cfg.clearTemplateCache(); + cfgNoLocLU.clearTemplateCache(); } { @@ -549,13 +556,12 @@ public class TemplateLookupStrategyTest { @Test public void testNonparsed() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("test.txt", ""); tl.putTextTemplate("test_aa.txt", ""); - cfg.setTemplateLoader(tl); - + + Configuration cfg = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build(); + try { cfg.getTemplate("missing.txt", new Locale("aa", "BB"), null, false); fail(); @@ -585,12 +591,11 @@ public class TemplateLookupStrategyTest { @Test public void testParseError() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("test.ftl", ""); tl.putTextTemplate("test_aa.ftl", "<#wrong>"); - cfg.setTemplateLoader(tl); + + Configuration cfg = new Configuration.Builder(Configuration.VERSION_3_0_0).templateLoader(tl).build(); try { cfg.getTemplate("test.ftl", new Locale("aa", "BB"));
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/TemplateNameSpecialVariablesTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TemplateNameSpecialVariablesTest.java b/src/test/java/org/apache/freemarker/core/TemplateNameSpecialVariablesTest.java index 71f761c..7c9a98f 100644 --- a/src/test/java/org/apache/freemarker/core/TemplateNameSpecialVariablesTest.java +++ b/src/test/java/org/apache/freemarker/core/TemplateNameSpecialVariablesTest.java @@ -20,64 +20,18 @@ package org.apache.freemarker.core; import java.io.IOException; -import org.apache.freemarker.core.templateresolver.TemplateLoader; -import org.apache.freemarker.core.templateresolver.impl.StringTemplateLoader; import org.apache.freemarker.test.TemplateTest; -import org.junit.Before; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; public class TemplateNameSpecialVariablesTest extends TemplateTest { - private static TemplateLoader createTemplateLoader(String specVar) { - StringTemplateLoader tl = new StringTemplateLoader(); - tl.putTemplate("main.ftl", - "In main: ${" + specVar + "}\n" - + "<#import 'imp.ftl' as i>" - + "In imp: ${inImp}\n" - + "In main: ${" + specVar + "}\n" - + "<@i.impM>${" + specVar + "}</@>\n" - + "<@i.impM2 />\n" - + "In main: ${" + specVar + "}\n" - + "<#include 'inc.ftl'>" - + "In main: ${" + specVar + "}\n" - + "<@incM>${" + specVar + "}</@>\n" - + "<@incM2 />\n" - + "In main: ${" + specVar + "}\n" - ); - tl.putTemplate("imp.ftl", - "<#global inImp = " + specVar + ">" - + "<#macro impM>" - + "${" + specVar + "}\n" - + "{<#nested>}" - + "</#macro>" - + "<#macro impM2>" - + "In imp call imp:\n" - + "<@impM>${" + specVar + "}</@>\n" - + "After: ${" + specVar + "}" - + "</#macro>" - ); - tl.putTemplate("inc.ftl", - "In inc: ${" + specVar + "}\n" - + "In inc call imp:\n" - + "<@i.impM>${" + specVar + "}</@>\n" - + "<#macro incM>" - + "${" + specVar + "}\n" - + "{<#nested>}" - + "</#macro>" - + "<#macro incM2>" - + "In inc call imp:\n" - + "<@i.impM>${" + specVar + "}</@>" - + "</#macro>" - ); - return tl; - } - private static final String PRINT_ALL_FTL = "ct=${.currentTemplateName!'-'}, mt=${.mainTemplateName!'-'}"; - + @Test public void testMainTemplateName() throws IOException, TemplateException { - getConfiguration().setTemplateLoader(createTemplateLoader(".mainTemplateName")); + addTemplateNameTestTemplates(".mainTemplateName"); assertOutputForNamed("main.ftl", "In main: main.ftl\n" + "In imp: main.ftl\n" @@ -104,7 +58,7 @@ public class TemplateNameSpecialVariablesTest extends TemplateTest { @Test public void testCurrentTemplateName() throws IOException, TemplateException { - getConfiguration().setTemplateLoader(createTemplateLoader(".currentTemplateName")); + addTemplateNameTestTemplates(".currentTemplateName"); assertOutputForNamed("main.ftl", "In main: main.ftl\n" + "In imp: imp.ftl\n" @@ -129,18 +83,52 @@ public class TemplateNameSpecialVariablesTest extends TemplateTest { + "In main: main.ftl\n"); } - @Before - public void setup() { - Configuration cfg = getConfiguration(); - cfg.setWhitespaceStripping(false); + private void addTemplateNameTestTemplates(String specVar) { + addTemplate("main.ftl", + "In main: ${" + specVar + "}\n" + + "<#import 'imp.ftl' as i>" + + "In imp: ${inImp}\n" + + "In main: ${" + specVar + "}\n" + + "<@i.impM>${" + specVar + "}</@>\n" + + "<@i.impM2 />\n" + + "In main: ${" + specVar + "}\n" + + "<#include 'inc.ftl'>" + + "In main: ${" + specVar + "}\n" + + "<@incM>${" + specVar + "}</@>\n" + + "<@incM2 />\n" + + "In main: ${" + specVar + "}\n" + ); + addTemplate("imp.ftl", + "<#global inImp = " + specVar + ">" + + "<#macro impM>" + + "${" + specVar + "}\n" + + "{<#nested>}" + + "</#macro>" + + "<#macro impM2>" + + "In imp call imp:\n" + + "<@impM>${" + specVar + "}</@>\n" + + "After: ${" + specVar + "}" + + "</#macro>" + ); + addTemplate("inc.ftl", + "In inc: ${" + specVar + "}\n" + + "In inc call imp:\n" + + "<@i.impM>${" + specVar + "}</@>\n" + + "<#macro incM>" + + "${" + specVar + "}\n" + + "{<#nested>}" + + "</#macro>" + + "<#macro incM2>" + + "In inc call imp:\n" + + "<@i.impM>${" + specVar + "}</@>" + + "</#macro>" + ); } - + @Test public void testInAdhocTemplate() throws TemplateException, IOException { - StringTemplateLoader tl = new StringTemplateLoader(); - tl.putTemplate("inc.ftl", "Inc: " + PRINT_ALL_FTL); - getConfiguration().setTemplateLoader(tl); - + addTemplate("inc.ftl", "Inc: " + PRINT_ALL_FTL); + // In nameless templates, the deprecated .templateName is "", but the new variables are missing values. assertOutput(new Template(null, PRINT_ALL_FTL + "; <#include 'inc.ftl'>", getConfiguration()), "ct=-, mt=-; Inc: ct=inc.ftl, mt=-"); @@ -151,7 +139,7 @@ public class TemplateNameSpecialVariablesTest extends TemplateTest { @Test public void testInInterpretTemplate() throws TemplateException, IOException { - getConfiguration().setSharedVariable("t", PRINT_ALL_FTL); + addToDataModel("t", PRINT_ALL_FTL); assertOutput(new Template("foo.ftl", PRINT_ALL_FTL + "; <@t?interpret />", getConfiguration()), "ct=foo.ftl, mt=foo.ftl; " + "ct=foo.ftl->anonymous_interpreted, mt=foo.ftl"); @@ -162,5 +150,10 @@ public class TemplateNameSpecialVariablesTest extends TemplateTest { "ct=foo.ftl, mt=foo.ftl; " + "ct=foo.ftl->bar, mt=foo.ftl"); } - + + @Override + protected Configuration createDefaultConfiguration() throws Exception { + return new TestConfigurationBuilder().whitespaceStripping(false).build(); + } + } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/TemplateNotFoundMessageTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TemplateNotFoundMessageTest.java b/src/test/java/org/apache/freemarker/core/TemplateNotFoundMessageTest.java index 7348a69..9e6d540 100644 --- a/src/test/java/org/apache/freemarker/core/TemplateNotFoundMessageTest.java +++ b/src/test/java/org/apache/freemarker/core/TemplateNotFoundMessageTest.java @@ -98,19 +98,17 @@ public class TemplateNotFoundMessageTest { @Test public void testDefaultTemplateLoader() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - String errMsg = failWith(cfg); + String errMsg = failWith(null); showErrorMessage(errMsg); assertThat(errMsg, allOf(containsString("setTemplateLoader"), containsString("null"))); } @Test public void testOtherMessageDetails() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setTemplateLoader(new StringTemplateLoader()); - + // Non-null TemplateLoader: + StringTemplateLoader emptyLoader = new StringTemplateLoader(); { - String errMsg = failWith("../x", cfg); + String errMsg = failWith(emptyLoader, "../x"); showErrorMessage(errMsg); assertThat(errMsg, allOf( @@ -118,7 +116,7 @@ public class TemplateNotFoundMessageTest { containsStringIgnoringCase("root directory"))); } { - String errMsg = failWith("x\u0000y", cfg); + String errMsg = failWith(emptyLoader, "x\u0000y"); showErrorMessage(errMsg); assertThat(errMsg, allOf( @@ -126,48 +124,54 @@ public class TemplateNotFoundMessageTest { containsStringIgnoringCase("null character"))); } { - String errMsg = failWith("x\\y", cfg); + String errMsg = failWith(emptyLoader, "x\\y"); showErrorMessage(errMsg); assertThat(errMsg, allOf(containsStringIgnoringCase("warning"), containsStringIgnoringCase("backslash"))); } { - String errMsg = failWith("x/./y", cfg); + String errMsg = failWith(emptyLoader, "x/./y"); showErrorMessage(errMsg); assertThat(errMsg, allOf(containsStringIgnoringCase("normalized"), containsStringIgnoringCase("x/y"))); } { - String errMsg = failWith("/x/y", cfg); + String errMsg = failWith(emptyLoader, "/x/y"); showErrorMessage(errMsg); assertThat(errMsg, not(containsStringIgnoringCase("normalized"))); } { - String errMsg = failWith("x/y", cfg); + String errMsg = failWith(emptyLoader, "x/y"); showErrorMessage(errMsg); assertThat(errMsg, not(containsStringIgnoringCase("normalized"))); assertThat(errMsg, not(containsStringIgnoringCase("lookup strategy"))); } - - cfg.setTemplateLookupStrategy(new TemplateLookupStrategy() { - @Override - public TemplateLookupResult lookup(TemplateLookupContext ctx) throws IOException { - return ctx.lookupWithAcquisitionStrategy(ctx.getTemplateName()); - } - }); + + Configuration.Builder cfgB = new Configuration.Builder(Configuration.VERSION_3_0_0) + .templateLoader(new StringTemplateLoader()) + .templateLookupStrategy( + new TemplateLookupStrategy() { + @Override + public TemplateLookupResult lookup(TemplateLookupContext ctx) throws IOException { + return ctx.lookupWithAcquisitionStrategy(ctx.getTemplateName()); + } + } + ); { - String errMsg = failWith("x/y", cfg); + String errMsg = failWith(emptyLoader, "x/y", + new TemplateLookupStrategy() { + @Override + public TemplateLookupResult lookup(TemplateLookupContext ctx) throws IOException { + return ctx.lookupWithAcquisitionStrategy(ctx.getTemplateName()); + } + } + ); showErrorMessage(errMsg); assertThat(errMsg, containsStringIgnoringCase("lookup strategy")); } try { - cfg.getTemplate("./missing", null, new Serializable() { - @Override - public String toString() { - return "example.com"; - } - }); + cfgB.build().getTemplate("./missing", null, new DomainLookupCondition()); fail(); } catch (TemplateNotFoundException e) { showErrorMessage(e.getMessage()); @@ -179,12 +183,14 @@ public class TemplateNotFoundMessageTest { // System.out.println(errMsg); } - private String failWith(TemplateLoader tl, String name, Configuration cfg) { - if (tl != null) { - cfg.setTemplateLoader(tl); - } + private String failWith(TemplateLoader tl, String name, TemplateLookupStrategy templateLookupStrategy) { try { - cfg.getTemplate(name); + Configuration.Builder cfgB = new Configuration.Builder(Configuration.VERSION_3_0_0); + cfgB.setTemplateLoader(tl); + if (templateLookupStrategy != null) { + cfgB.setTemplateLookupStrategy(templateLookupStrategy); + } + cfgB.build().getTemplate(name); fail(); } catch (TemplateNotFoundException | MalformedTemplateNameException e) { return e.getMessage(); @@ -193,17 +199,21 @@ public class TemplateNotFoundMessageTest { } return null; } - - private String failWith(TemplateLoader tl) { - return failWith(tl, "missing.ftl", new Configuration(Configuration.VERSION_3_0_0)); + + private String failWith(TemplateLoader tl, String name) { + return failWith(tl, name, null); } - private String failWith(Configuration cfg) { - return failWith(null, "missing.ftl", cfg); + private String failWith(TemplateLoader tl) { + return failWith(tl, "missing.ftl", null); } - private String failWith(String name, Configuration cfg) { - return failWith(null, name, cfg); + @SuppressWarnings("serial") + private static final class DomainLookupCondition implements Serializable { + @Override + public String toString() { + return "example.com"; + } } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java b/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java index 54f2a36..a0174b3 100644 --- a/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java +++ b/src/test/java/org/apache/freemarker/core/TheadInterruptingSupportTest.java @@ -29,6 +29,7 @@ import org.apache.freemarker.core.model.TemplateDirectiveBody; import org.apache.freemarker.core.model.TemplateDirectiveModel; import org.apache.freemarker.core.model.TemplateModel; import org.apache.freemarker.core.util._NullWriter; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,7 +39,7 @@ public class TheadInterruptingSupportTest { private static final Logger LOG = LoggerFactory.getLogger(TheadInterruptingSupportTest.class); private static final int TEMPLATE_INTERRUPTION_TIMEOUT = 5000; - private final Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); + private final Configuration cfg = new TestConfigurationBuilder().build(); @Test public void test() throws IOException, InterruptedException { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/UnclosedCommentTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/UnclosedCommentTest.java b/src/test/java/org/apache/freemarker/core/UnclosedCommentTest.java index df8ae38..99bf5d3 100644 --- a/src/test/java/org/apache/freemarker/core/UnclosedCommentTest.java +++ b/src/test/java/org/apache/freemarker/core/UnclosedCommentTest.java @@ -28,7 +28,6 @@ public class UnclosedCommentTest extends TemplateTest { @Test public void test() throws IOException, TemplateException { - setConfiguration(new Configuration(Configuration.VERSION_3_0_0)); assertErrorContains("foo<#--", "end of file"); // Not too good... assertErrorContains("foo<#-- ", "Unclosed", "<#--"); assertErrorContains("foo<#--bar", "Unclosed", "<#--"); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/WhitespaceStrippingTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/WhitespaceStrippingTest.java b/src/test/java/org/apache/freemarker/core/WhitespaceStrippingTest.java index 604bb48..8e5b52e 100644 --- a/src/test/java/org/apache/freemarker/core/WhitespaceStrippingTest.java +++ b/src/test/java/org/apache/freemarker/core/WhitespaceStrippingTest.java @@ -22,17 +22,11 @@ package org.apache.freemarker.core; import java.io.IOException; import org.apache.freemarker.test.TemplateTest; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; public class WhitespaceStrippingTest extends TemplateTest { - private final Configuration cfgStripWS = new Configuration(Configuration.VERSION_3_0_0); - - private final Configuration cfgNoStripWS = new Configuration(Configuration.VERSION_3_0_0); - { - cfgNoStripWS.setWhitespaceStripping(false); - } - @Test public void testBasics() throws Exception { assertOutput("<#assign x = 1>\n<#assign y = 2>\n${x}\n${y}", "1\n2", "\n\n1\n2"); @@ -59,10 +53,10 @@ public class WhitespaceStrippingTest extends TemplateTest { private void assertOutput(String ftl, String expectedOutStripped, String expectedOutNonStripped) throws IOException, TemplateException { - setConfiguration(cfgStripWS); + setConfiguration(new TestConfigurationBuilder().build()); assertOutput(ftl, expectedOutStripped); - setConfiguration(cfgNoStripWS); + setConfiguration(new TestConfigurationBuilder().whitespaceStripping(false).build()); assertOutput(ftl, expectedOutNonStripped); } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/model/impl/DefaultObjectWrapperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/model/impl/DefaultObjectWrapperTest.java b/src/test/java/org/apache/freemarker/core/model/impl/DefaultObjectWrapperTest.java index 93d8a31..6e9ae25 100644 --- a/src/test/java/org/apache/freemarker/core/model/impl/DefaultObjectWrapperTest.java +++ b/src/test/java/org/apache/freemarker/core/model/impl/DefaultObjectWrapperTest.java @@ -68,6 +68,7 @@ import org.apache.freemarker.core.model.TemplateScalarModel; import org.apache.freemarker.core.model.TemplateSequenceModel; import org.apache.freemarker.core.model.WrapperTemplateModel; import org.apache.freemarker.core.model.WrappingTemplateModel; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; import org.w3c.dom.Document; import org.xml.sax.InputSource; @@ -242,6 +243,7 @@ public class DefaultObjectWrapperTest { assertRoundtrip(ow, linkedList, DefaultListAdapter.class, LinkedList.class, linkedList.toString()); assertRoundtrip(ow, intArray, DefaultArrayAdapter.class, int[].class, null); assertRoundtrip(ow, stringArray, DefaultArrayAdapter.class, String[].class, null); + assertRoundtrip(ow, pureIterable, DefaultIterableAdapter.class, PureIterable.class, pureIterable.toString()); assertRoundtrip(ow, hashSet, DefaultNonListCollectionAdapter.class, HashSet.class, hashSet.toString()); } @@ -452,7 +454,7 @@ public class DefaultObjectWrapperTest { String expectedPojoToString) throws TemplateModelException { final TemplateModel objTM = dow.wrap(obj); - assertTrue(expectedTMClass.isAssignableFrom(objTM.getClass())); + assertThat(objTM.getClass(), typeCompatibleWith(expectedTMClass)); final TemplateHashModel testBeanTM = (TemplateHashModel) dow.wrap(new RoundtripTesterBean()); @@ -460,7 +462,7 @@ public class DefaultObjectWrapperTest { TemplateMethodModelEx getClassM = (TemplateMethodModelEx) testBeanTM.get("getClass"); Object r = getClassM.exec(Collections.singletonList(objTM)); final Class rClass = (Class) ((WrapperTemplateModel) r).getWrappedObject(); - assertTrue(rClass + " instanceof " + expectedPojoClass, expectedPojoClass.isAssignableFrom(rClass)); + assertThat(rClass, typeCompatibleWith(expectedPojoClass)); } if (expectedPojoToString != null) { @@ -758,9 +760,10 @@ public class DefaultObjectWrapperTest { private String processTemplate(ObjectWrapper objectWrapper, Object value, String ftl) throws TemplateException, IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setLogTemplateExceptions(false); - cfg.setObjectWrapper(objectWrapper); + Configuration cfg = new TestConfigurationBuilder() + .logTemplateExceptions(false) + .objectWrapper(objectWrapper) + .build(); StringWriter out = new StringWriter(); new Template(null, ftl, cfg).process(ImmutableMap.of("value", value), out); return out.toString(); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/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 e402ba2..a7063fd 100644 --- a/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java +++ b/src/test/java/org/apache/freemarker/core/templateresolver/DefaultTemplateResolverTest.java @@ -37,6 +37,7 @@ 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.apache.freemarker.test.TestConfigurationBuilder; import org.hamcrest.Matchers; import org.junit.Test; @@ -48,10 +49,12 @@ public class DefaultTemplateResolverTest { public void testCachedException() throws Exception { MockTemplateLoader loader = new MockTemplateLoader(); DefaultTemplateResolver tr = new DefaultTemplateResolver( - loader, new StrongCacheStorage(), - DefaultTemplateLookupStrategy.INSTANCE, DefaultTemplateNameFormat.INSTANCE, - new Configuration(Configuration.VERSION_3_0_0)); - tr.setTemplateUpdateDelayMilliseconds(1000L); + loader, + new StrongCacheStorage(), 1000L, + DefaultTemplateLookupStrategy.INSTANCE, true, + DefaultTemplateNameFormat.INSTANCE, + null, + new TestConfigurationBuilder().build()); loader.setThrowException(true); try { tr.getTemplate("t", Locale.getDefault(), null).getTemplate(); @@ -86,10 +89,11 @@ public class DefaultTemplateResolverTest { public void testCachedNotFound() throws Exception { MockTemplateLoader loader = new MockTemplateLoader(); DefaultTemplateResolver cache = new DefaultTemplateResolver( - loader, new StrongCacheStorage(), DefaultTemplateLookupStrategy.INSTANCE, - DefaultTemplateNameFormat.INSTANCE, new Configuration()); - cache.setTemplateUpdateDelayMilliseconds(1000L); - cache.setLocalizedLookup(false); + loader, + new StrongCacheStorage(), 1000L, + DefaultTemplateLookupStrategy.INSTANCE, false, + DefaultTemplateNameFormat.INSTANCE, + null, new TestConfigurationBuilder().build()); assertNull(cache.getTemplate("t", Locale.getDefault(), null).getTemplate()); assertEquals(1, loader.getLoadAttemptCount()); assertNull(cache.getTemplate("t", Locale.getDefault(), null).getTemplate()); @@ -136,13 +140,14 @@ public class DefaultTemplateResolverTest { } @Test - public void testManualRemovalPlain() throws IOException { - Configuration cfg = new Configuration(); - cfg.setCacheStorage(new StrongCacheStorage()); + public void testManualRemovalPlain() throws Exception { StringTemplateLoader loader = new StringTemplateLoader(); - cfg.setTemplateLoader(loader); - cfg.setTemplateUpdateDelayMilliseconds(Integer.MAX_VALUE); - + Configuration cfg = new TestConfigurationBuilder() + .cacheStorage(new StrongCacheStorage()) + .templateLoader(loader) + .templateUpdateDelayMilliseconds(Long.MAX_VALUE) + .build(); + loader.putTemplate("1.ftl", "1 v1"); loader.putTemplate("2.ftl", "2 v1"); assertEquals("1 v1", cfg.getTemplate("1.ftl").toString()); @@ -163,14 +168,14 @@ public class DefaultTemplateResolverTest { } @Test - public void testManualRemovalI18ed() throws IOException { - Configuration cfg = new Configuration(); - cfg.setCacheStorage(new StrongCacheStorage()); - cfg.setLocale(Locale.US); + public void testManualRemovalI18ed() throws Exception { StringTemplateLoader loader = new StringTemplateLoader(); - cfg.setTemplateLoader(loader); - cfg.setTemplateUpdateDelayMilliseconds(Integer.MAX_VALUE); - + Configuration cfg = new TestConfigurationBuilder() + .cacheStorage(new StrongCacheStorage()) + .templateLoader(loader) + .templateUpdateDelayMilliseconds(Long.MAX_VALUE) + .build(); + loader.putTemplate("1_en_US.ftl", "1_en_US v1"); loader.putTemplate("1_en.ftl", "1_en v1"); loader.putTemplate("1.ftl", "1 v1"); @@ -204,13 +209,15 @@ public class DefaultTemplateResolverTest { } @Test - public void testZeroUpdateDelay() throws IOException, InterruptedException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setLocale(Locale.US); - cfg.setCacheStorage(new StrongCacheStorage()); + public void testZeroUpdateDelay() throws Exception { MonitoredTemplateLoader loader = new MonitoredTemplateLoader(); - cfg.setTemplateLoader(loader); - cfg.setTemplateUpdateDelayMilliseconds(0); + TestConfigurationBuilder cfgB = new TestConfigurationBuilder() + .cacheStorage(new StrongCacheStorage()) + .templateLoader(loader) + .templateUpdateDelayMilliseconds(0); + + Configuration cfg = cfgB.build(); + for (int i = 1; i <= 3; i++) { loader.putTextTemplate("t.ftl", "v" + i); assertEquals("v" + i, cfg.getTemplate("t.ftl").toString()); @@ -243,7 +250,7 @@ public class DefaultTemplateResolverTest { ), loader.getEvents(LoadEvent.class)); - cfg.setLocalizedLookup(false); + cfg = cfgB.localizedLookup(false).build(); loader.clearEvents(); loader.putTextTemplate("t.ftl", "v10"); assertEquals("v10", cfg.getTemplate("t.ftl").toString()); @@ -270,18 +277,14 @@ public class DefaultTemplateResolverTest { } @Test - public void testWrongEncodingReload() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setLocale(Locale.US); - cfg.setSourceEncoding(StandardCharsets.UTF_8); - - 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); - + public void testWrongEncodingReload() throws Exception { + MonitoredTemplateLoader loader = new MonitoredTemplateLoader(); + loader.putBinaryTemplate("utf-8_en.ftl", "<#ftl encoding='utf-8'>Béka"); + loader.putBinaryTemplate("utf-8.ftl", "Bar"); + loader.putBinaryTemplate("iso-8859-1_en_US.ftl", "<#ftl encoding='ISO-8859-1'>Béka", + StandardCharsets.ISO_8859_1, "v1"); + Configuration cfg = new TestConfigurationBuilder().templateLoader(loader).build(); + { Template t = cfg.getTemplate("utf-8.ftl"); assertEquals("utf-8.ftl", t.getLookupName()); @@ -295,11 +298,11 @@ public class DefaultTemplateResolverTest { new LoadEvent("utf-8_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), new LoadEvent("utf-8_en.ftl", TemplateLoadingResultStatus.OPENED), CloseSessionEvent.INSTANCE), - tl.getEvents()); + loader.getEvents()); } { - tl.clearEvents(); + loader.clearEvents(); Template t = cfg.getTemplate("iso-8859-1.ftl"); assertEquals("iso-8859-1.ftl", t.getLookupName()); @@ -312,20 +315,16 @@ public class DefaultTemplateResolverTest { CreateSessionEvent.INSTANCE, new LoadEvent("iso-8859-1_en_US.ftl", TemplateLoadingResultStatus.OPENED), CloseSessionEvent.INSTANCE), - tl.getEvents()); + loader.getEvents()); } } @Test - public void testNoWrongEncodingForTemplateLoader2WithReader() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setLocale(Locale.US); - cfg.setSourceEncoding(StandardCharsets.UTF_8); - - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); - tl.putTextTemplate("foo_en.ftl", "<#ftl encoding='utf-8'>Å"); - tl.putTextTemplate("foo.ftl", "B"); - cfg.setTemplateLoader(tl); + public void testNoWrongEncodingForTemplateLoader2WithReader() throws Exception { + MonitoredTemplateLoader loader = new MonitoredTemplateLoader(); + loader.putTextTemplate("foo_en.ftl", "<#ftl encoding='utf-8'>Å"); + loader.putTextTemplate("foo.ftl", "B"); + Configuration cfg = new TestConfigurationBuilder().templateLoader(loader).build(); { Template t = cfg.getTemplate("foo.ftl"); @@ -340,14 +339,15 @@ public class DefaultTemplateResolverTest { new LoadEvent("foo_en_US.ftl", TemplateLoadingResultStatus.NOT_FOUND), new LoadEvent("foo_en.ftl", TemplateLoadingResultStatus.OPENED), CloseSessionEvent.INSTANCE), - tl.getEvents()); + loader.getEvents()); } } @Test - public void testTemplateNameFormatException() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setTemplateNameFormat(DefaultTemplateNameFormat.INSTANCE); + public void testTemplateNameFormatException() throws Exception { + Configuration cfg = new TestConfigurationBuilder() + .templateNameFormat(DefaultTemplateNameFormat.INSTANCE) + .build(); try { cfg.getTemplate("../x"); fail(); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/templateresolver/FileTemplateLoaderTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/templateresolver/FileTemplateLoaderTest.java b/src/test/java/org/apache/freemarker/core/templateresolver/FileTemplateLoaderTest.java index 0b5861d..8efb83c 100644 --- a/src/test/java/org/apache/freemarker/core/templateresolver/FileTemplateLoaderTest.java +++ b/src/test/java/org/apache/freemarker/core/templateresolver/FileTemplateLoaderTest.java @@ -29,6 +29,7 @@ import org.apache.commons.lang.SystemUtils; import org.apache.freemarker.core.Configuration; import org.apache.freemarker.core.TemplateNotFoundException; import org.apache.freemarker.core.templateresolver.impl.FileTemplateLoader; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -39,7 +40,7 @@ public class FileTemplateLoaderTest { private File templateRootDir; - private Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); + private Configuration cfg; @Before public void setup() throws IOException { @@ -51,8 +52,8 @@ public class FileTemplateLoaderTest { } File tFile = new File(sub2Dir, "t.ftl"); FileUtils.write(tFile, "foo"); - - cfg.setDirectoryForTemplateLoading(templateRootDir); + + cfg = new TestConfigurationBuilder().templateLoader(new FileTemplateLoader(templateRootDir)).build(); } @Test http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/templateresolver/TemplateConfigurationFactoryTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/templateresolver/TemplateConfigurationFactoryTest.java b/src/test/java/org/apache/freemarker/core/templateresolver/TemplateConfigurationFactoryTest.java index 727faa1..a7259d8 100644 --- a/src/test/java/org/apache/freemarker/core/templateresolver/TemplateConfigurationFactoryTest.java +++ b/src/test/java/org/apache/freemarker/core/templateresolver/TemplateConfigurationFactoryTest.java @@ -25,14 +25,11 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.apache.freemarker.core.Configuration; import org.apache.freemarker.core.TemplateConfiguration; import org.junit.Test; public class TemplateConfigurationFactoryTest { - private Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - @Test public void testCondition1() throws IOException, TemplateConfigurationFactoryException { TemplateConfiguration tc = newTemplateConfiguration(1); @@ -168,7 +165,7 @@ public class TemplateConfigurationFactoryTest { private void assertApplicable(TemplateConfigurationFactory tcf, String sourceName, TemplateConfiguration... expectedTCs) throws IOException, TemplateConfigurationFactoryException { TemplateConfiguration mergedTC = tcf.get(sourceName, DummyTemplateLoadingSource.INSTANCE); - List<Object> mergedTCAttNames = new ArrayList<Object>(mergedTC.getCustomAttributes().keySet()); + List<Object> mergedTCAttNames = new ArrayList<>(mergedTC.getCustomAttributes().keySet()); for (TemplateConfiguration expectedTC : expectedTCs) { Integer tcId = (Integer) expectedTC.getCustomAttribute("id"); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/templateresolver/TemplateNameFormatTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/templateresolver/TemplateNameFormatTest.java b/src/test/java/org/apache/freemarker/core/templateresolver/TemplateNameFormatTest.java index 013fd64..3a24bfc 100644 --- a/src/test/java/org/apache/freemarker/core/templateresolver/TemplateNameFormatTest.java +++ b/src/test/java/org/apache/freemarker/core/templateresolver/TemplateNameFormatTest.java @@ -33,6 +33,7 @@ import org.apache.freemarker.core.templateresolver.impl.ByteArrayTemplateLoader; import org.apache.freemarker.core.templateresolver.impl.DefaultTemplateNameFormat; import org.apache.freemarker.core.templateresolver.impl.DefaultTemplateNameFormatFM2; import org.apache.freemarker.test.MonitoredTemplateLoader; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; import com.google.common.collect.ImmutableList; @@ -224,11 +225,10 @@ public class TemplateNameFormatTest { @Test public void assertBackslashNotSpecialWith23() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - MonitoredTemplateLoader tl = new MonitoredTemplateLoader(); tl.putTextTemplate("foo\\bar.ftl", ""); - cfg.setTemplateLoader(tl); + + Configuration cfg = new TestConfigurationBuilder().templateLoader(tl).build(); { final String name = "foo\\bar.ftl"; @@ -274,9 +274,10 @@ public class TemplateNameFormatTest { @Test public void assertBackslashNotAllowed() throws IOException { - Configuration cfg = new Configuration(Configuration.VERSION_3_0_0); - cfg.setTemplateLoader(new ByteArrayTemplateLoader()); - cfg.setTemplateNameFormat(DefaultTemplateNameFormat.INSTANCE); + Configuration cfg = new TestConfigurationBuilder() + .templateLoader(new ByteArrayTemplateLoader()) + .templateNameFormat(DefaultTemplateNameFormat.INSTANCE) + .build(); try { cfg.getTemplate("././foo\\bar.ftl", Locale.US); fail(); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java b/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java index 327bd94..4f22d73 100644 --- a/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java +++ b/src/test/java/org/apache/freemarker/core/valueformat/NumberFormatTest.java @@ -41,13 +41,14 @@ import org.apache.freemarker.core.model.TemplateNumberModel; import org.apache.freemarker.core.model.impl.SimpleNumber; import org.apache.freemarker.core.templateresolver.ConditionalTemplateConfigurationFactory; import org.apache.freemarker.core.templateresolver.FileNameGlobMatcher; +import org.apache.freemarker.core.templateresolver.TemplateConfigurationFactory; import org.apache.freemarker.core.userpkg.BaseNTemplateNumberFormatFactory; import org.apache.freemarker.core.userpkg.HexTemplateNumberFormatFactory; import org.apache.freemarker.core.userpkg.LocaleSensitiveTemplateNumberFormatFactory; import org.apache.freemarker.core.userpkg.PrintfGTemplateNumberFormatFactory; import org.apache.freemarker.core.valueformat.impl.AliasTemplateNumberFormatFactory; import org.apache.freemarker.test.TemplateTest; -import org.junit.Before; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Ignore; import org.junit.Test; @@ -56,28 +57,16 @@ import com.google.common.collect.ImmutableMap; @SuppressWarnings("boxing") public class NumberFormatTest extends TemplateTest { - @Before - public void setup() { - Configuration cfg = getConfiguration(); - cfg.setLocale(Locale.US); - - cfg.setCustomNumberFormats(ImmutableMap.of( - "hex", HexTemplateNumberFormatFactory.INSTANCE, - "loc", LocaleSensitiveTemplateNumberFormatFactory.INSTANCE, - "base", BaseNTemplateNumberFormatFactory.INSTANCE, - "printfG", PrintfGTemplateNumberFormatFactory.INSTANCE)); - } - @Test public void testUnknownCustomFormat() throws Exception { { - getConfiguration().setNumberFormat("@noSuchFormat"); + setConfigurationWithNumberFormat("@noSuchFormat"); Throwable exc = assertErrorContains("${1}", "\"@noSuchFormat\"", "\"noSuchFormat\""); assertThat(exc.getCause(), instanceOf(UndefinedCustomFormatException.class)); } { - getConfiguration().setNumberFormat("number"); + setConfigurationWithNumberFormat("number"); Throwable exc = assertErrorContains("${1?string('@noSuchFormat2')}", "\"@noSuchFormat2\"", "\"noSuchFormat2\""); assertThat(exc.getCause(), instanceOf(UndefinedCustomFormatException.class)); @@ -86,17 +75,19 @@ public class NumberFormatTest extends TemplateTest { @Test public void testStringBI() throws Exception { + setConfigurationWithNumberFormat(null); assertOutput("${11} ${11?string.@hex} ${12} ${12?string.@hex}", "11 b 12 c"); } @Test public void testSetting() throws Exception { - getConfiguration().setNumberFormat("@hex"); + setConfigurationWithNumberFormat("@hex"); assertOutput("${11?string.number} ${11} ${12?string.number} ${12}", "11 b 12 c"); } @Test public void testSetting2() throws Exception { + setConfigurationWithNumberFormat(null); assertOutput( "<#setting numberFormat='@hex'>${11?string.number} ${11} ${12?string.number} ${12} ${13?string}" + "<#setting numberFormat='@loc'>${11?string.number} ${11} ${12?string.number} ${12} ${13?string}", @@ -106,43 +97,40 @@ public class NumberFormatTest extends TemplateTest { @Test public void testUnformattableNumber() throws Exception { - getConfiguration().setNumberFormat("@hex"); + setConfigurationWithNumberFormat("@hex"); assertErrorContains("${1.1}", "hexadecimal int", "doesn't fit into an int"); } @Test public void testLocaleSensitive() throws Exception { - Configuration cfg = getConfiguration(); - cfg.setNumberFormat("@loc"); + setConfigurationWithNumberFormat("@loc"); assertOutput("${1.1}", "1.1_en_US"); - cfg.setLocale(Locale.GERMANY); + setConfigurationWithNumberFormat("@loc", null, null, Locale.GERMANY); assertOutput("${1.1}", "1.1_de_DE"); } @Test public void testLocaleSensitive2() throws Exception { - Configuration cfg = getConfiguration(); - cfg.setNumberFormat("@loc"); + setConfigurationWithNumberFormat("@loc"); assertOutput("${1.1} <#setting locale='de_DE'>${1.1}", "1.1_en_US 1.1_de_DE"); } @Test public void testCustomParameterized() throws Exception { - Configuration cfg = getConfiguration(); - cfg.setNumberFormat("@base 2"); + setConfigurationWithNumberFormat("@base 2"); assertOutput("${11}", "1011"); assertOutput("${11?string}", "1011"); assertOutput("${11?string.@base_3}", "102"); assertErrorContains("${11?string.@base_xyz}", "\"@base_xyz\"", "\"xyz\""); - cfg.setNumberFormat("@base"); + setConfigurationWithNumberFormat("@base"); assertErrorContains("${11}", "\"@base\"", "format parameter is required"); } @Test public void testCustomWithFallback() throws Exception { Configuration cfg = getConfiguration(); - cfg.setNumberFormat("@base 2|0.0#"); + setConfigurationWithNumberFormat("@base 2|0.0#"); assertOutput("${11}", "1011"); assertOutput("${11.34}", "11.34"); assertOutput("${11?string('@base 3|0.00')}", "102"); @@ -151,6 +139,8 @@ public class NumberFormatTest extends TemplateTest { @Test public void testEnvironmentGetters() throws Exception { + setConfigurationWithNumberFormat(null); + Template t = new Template(null, "", getConfiguration()); Environment env = t.createProcessingEnvironment(null, null); @@ -220,38 +210,43 @@ public class NumberFormatTest extends TemplateTest { @Test public void testAtPrefix() throws Exception { Configuration cfg = getConfiguration(); - - cfg.setNumberFormat("@hex"); + + setConfigurationWithNumberFormat("@hex"); assertOutput("${10}", "a"); - cfg.setNumberFormat("'@'0"); + setConfigurationWithNumberFormat("'@'0"); assertOutput("${10}", "@10"); - cfg.setNumberFormat("@@0"); + setConfigurationWithNumberFormat("@@0"); assertOutput("${10}", "@@10"); - - cfg.setCustomNumberFormats(Collections.<String, TemplateNumberFormatFactory>emptyMap()); - cfg.setNumberFormat("@hex"); + + setConfigurationWithNumberFormat( + "@hex", Collections.<String, TemplateNumberFormatFactory>emptyMap()); assertErrorContains("${10}", "custom", "\"hex\""); - cfg.setNumberFormat("'@'0"); + + setConfigurationWithNumberFormat( + "'@'0", Collections.<String, TemplateNumberFormatFactory>emptyMap()); assertOutput("${10}", "@10"); - cfg.setNumberFormat("@@0"); + + setConfigurationWithNumberFormat( + "@@0", Collections.<String, TemplateNumberFormatFactory>emptyMap()); assertOutput("${10}", "@@10"); } @Test public void testAlieses() throws Exception { - Configuration cfg = getConfiguration(); - cfg.setCustomNumberFormats(ImmutableMap.of( - "f", new AliasTemplateNumberFormatFactory("0.#'f'"), - "d", new AliasTemplateNumberFormatFactory("0.0#"), - "hex", HexTemplateNumberFormatFactory.INSTANCE)); - - TemplateConfiguration.Builder tcb = new TemplateConfiguration.Builder(); - tcb.setCustomNumberFormats(ImmutableMap.of( - "d", new AliasTemplateNumberFormatFactory("0.#'d'"), - "i", new AliasTemplateNumberFormatFactory("@hex"))); - cfg.setTemplateConfigurations( - new ConditionalTemplateConfigurationFactory(new FileNameGlobMatcher("*2*"), tcb.build())); - + setConfigurationWithNumberFormat( + "'@'0", + ImmutableMap.of( + "f", new AliasTemplateNumberFormatFactory("0.#'f'"), + "d", new AliasTemplateNumberFormatFactory("0.0#"), + "hex", HexTemplateNumberFormatFactory.INSTANCE), + new ConditionalTemplateConfigurationFactory( + new FileNameGlobMatcher("*2*"), + new TemplateConfiguration.Builder() + .customNumberFormats(ImmutableMap.of( + "d", new AliasTemplateNumberFormatFactory("0.#'d'"), + "i", new AliasTemplateNumberFormatFactory("@hex"))) + .build())); + String commonFtl = "${1?string.@f} ${1?string.@d} " + "<#setting locale='fr_FR'>${1.5?string.@d} " + "<#attempt>${10?string.@i}<#recover>E</#attempt>"; @@ -264,14 +259,14 @@ public class NumberFormatTest extends TemplateTest { @Test public void testAlieses2() throws Exception { - Configuration cfg = getConfiguration(); - cfg.setCustomNumberFormats(ImmutableMap.of( - "n", new AliasTemplateNumberFormatFactory("0.0", - ImmutableMap.of( - new Locale("en"), "0.0'_en'", - Locale.UK, "0.0'_en_GB'", - Locale.FRANCE, "0.0'_fr_FR'")))); - cfg.setNumberFormat("@n"); + setConfigurationWithNumberFormat( + "@n", + ImmutableMap.of( + "n", new AliasTemplateNumberFormatFactory("0.0", + ImmutableMap.of( + new Locale("en"), "0.0'_en'", + Locale.UK, "0.0'_en_GB'", + Locale.FRANCE, "0.0'_fr_FR'")))); assertOutput( "<#setting locale='en_US'>${1} " + "<#setting locale='en_GB'>${1} " @@ -283,7 +278,7 @@ public class NumberFormatTest extends TemplateTest { @Test public void testMarkupFormat() throws IOException, TemplateException { - getConfiguration().setNumberFormat("@printfG_3"); + setConfigurationWithNumberFormat("@printfG_3"); String commonFTL = "${1234567} ${'cat:' + 1234567} ${0.0000123}"; String commonOutput = "1.23*10<sup>6</sup> cat:1.23*10<sup>6</sup> 1.23*10<sup>-5</sup>"; @@ -298,6 +293,7 @@ public class NumberFormatTest extends TemplateTest { @Test public void testPrintG() throws IOException, TemplateException { + setConfigurationWithNumberFormat(null); for (Number n : new Number[] { 1234567, 1234567L, 1234567d, 1234567f, BigInteger.valueOf(1234567), BigDecimal.valueOf(1234567) }) { addToDataModel("n", n); @@ -308,7 +304,49 @@ public class NumberFormatTest extends TemplateTest { assertOutput("${0.0000123?string.@printfG}", "1.23000E-05"); } } - + + private void setConfigurationWithNumberFormat( + String numberFormat, + Map<String, TemplateNumberFormatFactory> customNumberFormats, + TemplateConfigurationFactory templateConfigurationFactory, + Locale locale) { + TestConfigurationBuilder cfgB = new TestConfigurationBuilder(Configuration.VERSION_3_0_0); + + if (numberFormat != null) { + cfgB.setNumberFormat(numberFormat); + } + cfgB.setCustomNumberFormats( + customNumberFormats != null ? customNumberFormats + : ImmutableMap.of( + "hex", HexTemplateNumberFormatFactory.INSTANCE, + "loc", LocaleSensitiveTemplateNumberFormatFactory.INSTANCE, + "base", BaseNTemplateNumberFormatFactory.INSTANCE, + "printfG", PrintfGTemplateNumberFormatFactory.INSTANCE)); + if (locale != null) { + cfgB.setLocale(locale); + } + if (templateConfigurationFactory != null) { + cfgB.setTemplateConfigurations(templateConfigurationFactory); + } + + setConfiguration(cfgB.build()); + } + + private void setConfigurationWithNumberFormat(String numberFormat) { + setConfigurationWithNumberFormat(numberFormat, null, null, null); + } + + private void setConfigurationWithNumberFormat( + String numberFormat, Map<String, TemplateNumberFormatFactory> customNumberFormats) { + setConfigurationWithNumberFormat(numberFormat, customNumberFormats, null, null); + } + + private void setConfigurationWithNumberFormat( + String numberFormat, Map<String, TemplateNumberFormatFactory> customNumberFormats, + TemplateConfigurationFactory templateConfigurationFactory) { + setConfigurationWithNumberFormat(numberFormat, customNumberFormats, templateConfigurationFactory, null); + } + private static class MutableTemplateNumberModel implements TemplateNumberModel { private Number number; http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/core/valueformat/impl/ExtendedDecimalFormatTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/valueformat/impl/ExtendedDecimalFormatTest.java b/src/test/java/org/apache/freemarker/core/valueformat/impl/ExtendedDecimalFormatTest.java index 9625a5e..76c0bfc 100644 --- a/src/test/java/org/apache/freemarker/core/valueformat/impl/ExtendedDecimalFormatTest.java +++ b/src/test/java/org/apache/freemarker/core/valueformat/impl/ExtendedDecimalFormatTest.java @@ -28,9 +28,9 @@ import java.text.DecimalFormatSymbols; import java.text.ParseException; import java.util.Locale; -import org.apache.freemarker.core.Configuration; import org.apache.freemarker.core.TemplateException; import org.apache.freemarker.test.TemplateTest; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; public class ExtendedDecimalFormatTest extends TemplateTest { @@ -276,16 +276,15 @@ public class ExtendedDecimalFormatTest extends TemplateTest { @Test public void testTemplates() throws IOException, TemplateException { - Configuration cfg = getConfiguration(); - cfg.setLocale(Locale.US); - - cfg.setNumberFormat(",000.#"); + TestConfigurationBuilder cfgB = new TestConfigurationBuilder(); + + setConfiguration(cfgB.numberFormat(",000.#").build()); assertOutput("${1000.15} ${1000.25}", "1,000.2 1,000.2"); - cfg.setNumberFormat(",000.#;; roundingMode=halfUp groupingSeparator=_"); + setConfiguration(cfgB.numberFormat(",000.#;; roundingMode=halfUp groupingSeparator=_").build());; assertOutput("${1000.15} ${1000.25}", "1_000.2 1_000.3"); - cfg.setLocale(Locale.GERMANY); + setConfiguration(cfgB.locale(Locale.GERMANY).build());; assertOutput("${1000.15} ${1000.25}", "1_000,2 1_000,3"); - cfg.setLocale(Locale.US); + setConfiguration(cfgB.locale(Locale.US).build());; assertOutput( "${1000.15}; " + "${1000.15?string(',##.#;;groupingSeparator=\" \"')}; " http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/manualtest/AutoEscapingExample.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/manualtest/AutoEscapingExample.java b/src/test/java/org/apache/freemarker/manualtest/AutoEscapingExample.java index e8dedce..036bace 100644 --- a/src/test/java/org/apache/freemarker/manualtest/AutoEscapingExample.java +++ b/src/test/java/org/apache/freemarker/manualtest/AutoEscapingExample.java @@ -18,9 +18,12 @@ */ package org.apache.freemarker.manualtest; +import org.apache.freemarker.core.Configuration; +import org.apache.freemarker.test.TemplateTest; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; -public class AutoEscapingExample extends ExamplesTest { +public class AutoEscapingExample extends TemplateTest { @Test public void testInfoBox() throws Exception { @@ -61,5 +64,9 @@ public class AutoEscapingExample extends ExamplesTest { public void testStringConcat() throws Exception { assertOutputForNamed("AutoEscapingExample-stringConcat.ftlh"); } - + + @Override + protected Configuration createDefaultConfiguration() throws Exception { + return new TestConfigurationBuilder(AutoEscapingExample.class).build(); + } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/manualtest/ConfigureOutputFormatExamples.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/manualtest/ConfigureOutputFormatExamples.java b/src/test/java/org/apache/freemarker/manualtest/ConfigureOutputFormatExamples.java index 85ffa81..40c1297 100644 --- a/src/test/java/org/apache/freemarker/manualtest/ConfigureOutputFormatExamples.java +++ b/src/test/java/org/apache/freemarker/manualtest/ConfigureOutputFormatExamples.java @@ -20,7 +20,8 @@ package org.apache.freemarker.manualtest; import static org.junit.Assert.*; -import org.apache.freemarker.core.Configuration; +import java.io.IOException; + import org.apache.freemarker.core.TemplateConfiguration; import org.apache.freemarker.core.outputformat.impl.HTMLOutputFormat; import org.apache.freemarker.core.outputformat.impl.RTFOutputFormat; @@ -30,87 +31,75 @@ import org.apache.freemarker.core.templateresolver.FileExtensionMatcher; import org.apache.freemarker.core.templateresolver.FirstMatchTemplateConfigurationFactory; import org.apache.freemarker.core.templateresolver.OrMatcher; import org.apache.freemarker.core.templateresolver.PathGlobMatcher; +import org.apache.freemarker.test.TemplateTest; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; -public class ConfigureOutputFormatExamples extends ExamplesTest { +public class ConfigureOutputFormatExamples extends TemplateTest { @Test public void test() throws Exception { - Configuration cfg = getConfiguration(); - addTemplate("mail/t.ftl", ""); addTemplate("t.html", ""); addTemplate("t.htm", ""); addTemplate("t.xml", ""); addTemplate("t.rtf", ""); - - // Example 2/a: - { - TemplateConfiguration.Builder tcHTML = new TemplateConfiguration.Builder(); - tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE); - - cfg.setTemplateConfigurations( - new ConditionalTemplateConfigurationFactory( - new PathGlobMatcher("mail/**"), - tcHTML.build())); - - assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.ftl").getOutputFormat()); - } - // Example 2/b: - { - cfg.setTemplateConfigurations(null); // Just to be sure... - - cfg.setSettings(loadPropertiesFile("ConfigureOutputFormatExamples1.properties")); - - assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("mail/t.ftl").getOutputFormat()); - } - - // Example 3/a: - { - TemplateConfiguration.Builder tcHTML = new TemplateConfiguration.Builder(); - tcHTML.setOutputFormat(HTMLOutputFormat.INSTANCE); - - TemplateConfiguration.Builder tcXML = new TemplateConfiguration.Builder(); - tcXML.setOutputFormat(XMLOutputFormat.INSTANCE); + example2(true); + example2(false); + example3(true); + example3(false); + } - TemplateConfiguration.Builder tcRTF = new TemplateConfiguration.Builder(); - tcRTF.setOutputFormat(RTFOutputFormat.INSTANCE); - - cfg.setTemplateConfigurations( - new FirstMatchTemplateConfigurationFactory( - new ConditionalTemplateConfigurationFactory( - new FileExtensionMatcher("xml"), - tcXML.build()), - new ConditionalTemplateConfigurationFactory( - new OrMatcher( - new FileExtensionMatcher("html"), - new FileExtensionMatcher("htm")), - tcHTML.build()), - new ConditionalTemplateConfigurationFactory( - new FileExtensionMatcher("rtf"), - tcRTF.build()) - ).allowNoMatch(true) - ); - - assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat()); - assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat()); - assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat()); - assertEquals(RTFOutputFormat.INSTANCE, cfg.getTemplate("t.rtf").getOutputFormat()); - } + private void example2(boolean javaCfg) throws IOException { + setConfiguration( + javaCfg + ? new TestConfigurationBuilder() + .templateConfigurations( + new ConditionalTemplateConfigurationFactory( + new PathGlobMatcher("mail/**"), + new TemplateConfiguration.Builder() + .outputFormat(HTMLOutputFormat.INSTANCE) + .build())) + .build() + : new TestConfigurationBuilder() + .settings(loadPropertiesFile("ConfigureOutputFormatExamples1.properties")) + .build()); + assertEquals(HTMLOutputFormat.INSTANCE, getConfiguration().getTemplate("mail/t.ftl").getOutputFormat()); + } - // Example 3/b: - { - cfg.setTemplateConfigurations(null); // Just to be sure... - - cfg.setSettings(loadPropertiesFile("ConfigureOutputFormatExamples2.properties")); - - assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.html").getOutputFormat()); - assertEquals(HTMLOutputFormat.INSTANCE, cfg.getTemplate("t.htm").getOutputFormat()); - assertEquals(XMLOutputFormat.INSTANCE, cfg.getTemplate("t.xml").getOutputFormat()); - assertEquals(RTFOutputFormat.INSTANCE, cfg.getTemplate("t.rtf").getOutputFormat()); - } - + private void example3(boolean javaCfg) throws IOException { + setConfiguration( + javaCfg + ? new TestConfigurationBuilder() + .templateConfigurations( + new FirstMatchTemplateConfigurationFactory( + new ConditionalTemplateConfigurationFactory( + new FileExtensionMatcher("xml"), + new TemplateConfiguration.Builder() + .outputFormat(XMLOutputFormat.INSTANCE) + .build()), + new ConditionalTemplateConfigurationFactory( + new OrMatcher( + new FileExtensionMatcher("html"), + new FileExtensionMatcher("htm")), + new TemplateConfiguration.Builder() + .outputFormat(HTMLOutputFormat.INSTANCE) + .build()), + new ConditionalTemplateConfigurationFactory( + new FileExtensionMatcher("rtf"), + new TemplateConfiguration.Builder() + .outputFormat(RTFOutputFormat.INSTANCE) + .build())) + .allowNoMatch(true)) + .build() + : new TestConfigurationBuilder() + .settings(loadPropertiesFile("ConfigureOutputFormatExamples2.properties")) + .build()); + assertEquals(HTMLOutputFormat.INSTANCE, getConfiguration().getTemplate("t.html").getOutputFormat()); + assertEquals(HTMLOutputFormat.INSTANCE, getConfiguration().getTemplate("t.htm").getOutputFormat()); + assertEquals(XMLOutputFormat.INSTANCE, getConfiguration().getTemplate("t.xml").getOutputFormat()); + assertEquals(RTFOutputFormat.INSTANCE, getConfiguration().getTemplate("t.rtf").getOutputFormat()); } - + } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7d61a45d/src/test/java/org/apache/freemarker/manualtest/CustomFormatsExample.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/manualtest/CustomFormatsExample.java b/src/test/java/org/apache/freemarker/manualtest/CustomFormatsExample.java index 212129c..5da6615 100644 --- a/src/test/java/org/apache/freemarker/manualtest/CustomFormatsExample.java +++ b/src/test/java/org/apache/freemarker/manualtest/CustomFormatsExample.java @@ -21,36 +21,31 @@ package org.apache.freemarker.manualtest; import java.io.IOException; import java.math.BigDecimal; import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import org.apache.freemarker.core.Configuration; import org.apache.freemarker.core.TemplateException; import org.apache.freemarker.core.userpkg.BaseNTemplateNumberFormatFactory; -import org.apache.freemarker.core.valueformat.TemplateDateFormatFactory; -import org.apache.freemarker.core.valueformat.TemplateNumberFormatFactory; import org.apache.freemarker.core.valueformat.impl.AliasTemplateDateFormatFactory; import org.apache.freemarker.core.valueformat.impl.AliasTemplateNumberFormatFactory; +import org.apache.freemarker.test.TemplateTest; +import org.apache.freemarker.test.TestConfigurationBuilder; import org.junit.Test; +import com.google.common.collect.ImmutableMap; + @SuppressWarnings("boxing") -public class CustomFormatsExample extends ExamplesTest { +public class CustomFormatsExample extends TemplateTest { @Test public void aliases1() throws IOException, TemplateException { - Configuration cfg = getConfiguration(); - - Map<String, TemplateNumberFormatFactory> customNumberFormats - = new HashMap<>(); - customNumberFormats.put("price", new AliasTemplateNumberFormatFactory(",000.00")); - customNumberFormats.put("weight", new AliasTemplateNumberFormatFactory("0.##;; roundingMode=halfUp")); - cfg.setCustomNumberFormats(customNumberFormats); - - Map<String, TemplateDateFormatFactory> customDateFormats - = new HashMap<>(); - customDateFormats.put("fileDate", new AliasTemplateDateFormatFactory("dd/MMM/yy hh:mm a")); - customDateFormats.put("logEventTime", new AliasTemplateDateFormatFactory("iso ms u")); - cfg.setCustomDateFormats(customDateFormats); + setConfiguration(new TestConfigurationBuilder(this.getClass()) + .customNumberFormats(ImmutableMap.of( + "price", new AliasTemplateNumberFormatFactory(",000.00"), + "weight", new AliasTemplateNumberFormatFactory("0.##;; roundingMode=halfUp"))) + .customDateFormats(ImmutableMap.of( + "fileDate", new AliasTemplateDateFormatFactory("dd/MMM/yy hh:mm a"), + "logEventTime", new AliasTemplateDateFormatFactory("iso ms u") + )) + .build()); addToDataModel("p", 10000); addToDataModel("w", new BigDecimal("10.305")); @@ -62,26 +57,22 @@ public class CustomFormatsExample extends ExamplesTest { @Test public void aliases2() throws IOException, TemplateException { - Configuration cfg = getConfiguration(); + setConfiguration(new TestConfigurationBuilder(this.getClass()) + .customNumberFormats(ImmutableMap.of( + "base", BaseNTemplateNumberFormatFactory.INSTANCE, + "oct", new AliasTemplateNumberFormatFactory("@base 8"))) + .build()); - Map<String, TemplateNumberFormatFactory> customNumberFormats - = new HashMap<>(); - customNumberFormats.put("base", BaseNTemplateNumberFormatFactory.INSTANCE); - customNumberFormats.put("oct", new AliasTemplateNumberFormatFactory("@base 8")); - cfg.setCustomNumberFormats(customNumberFormats); - assertOutputForNamed("CustomFormatsExample-alias2.ftlh"); } @Test public void modelAware() throws IOException, TemplateException { - Configuration cfg = getConfiguration(); - - Map<String, TemplateNumberFormatFactory> customNumberFormats - = new HashMap<>(); - customNumberFormats.put("ua", UnitAwareTemplateNumberFormatFactory.INSTANCE); - cfg.setCustomNumberFormats(customNumberFormats); - cfg.setNumberFormat("@ua 0.####;; roundingMode=halfUp"); + setConfiguration(new TestConfigurationBuilder(this.getClass()) + .customNumberFormats(ImmutableMap.of( + "ua", UnitAwareTemplateNumberFormatFactory.INSTANCE)) + .numberFormat("@ua 0.####;; roundingMode=halfUp") + .build()); addToDataModel("weight", new UnitAwareTemplateNumberModel(1.5, "kg"));
