freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject incubator-freemarker git commit: Multiple TemplateConfiguration.apply test. AutoImports merged Map ordering fix.
Date Thu, 09 Jun 2016 06:54:40 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/2.3-gae 6c335728f -> 4857c0457


Multiple TemplateConfiguration.apply test. AutoImports merged Map ordering fix.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/4857c045
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/4857c045
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/4857c045

Branch: refs/heads/2.3-gae
Commit: 4857c045736e415349da5d42a774369b7dd013d4
Parents: 6c33572
Author: ddekany <ddekany@apache.org>
Authored: Thu Jun 9 08:54:24 2016 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Thu Jun 9 08:54:24 2016 +0200

----------------------------------------------------------------------
 .../freemarker/core/TemplateConfiguration.java  | 13 ++++---
 .../core/TemplateConfigurationTest.java         | 40 ++++++++++++++++++--
 2 files changed, 45 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4857c045/src/main/java/freemarker/core/TemplateConfiguration.java
----------------------------------------------------------------------
diff --git a/src/main/java/freemarker/core/TemplateConfiguration.java b/src/main/java/freemarker/core/TemplateConfiguration.java
index 9fb9cb3..b6ae122 100644
--- a/src/main/java/freemarker/core/TemplateConfiguration.java
+++ b/src/main/java/freemarker/core/TemplateConfiguration.java
@@ -173,10 +173,10 @@ public final class TemplateConfiguration extends Configurable implements
ParserC
             setClassicCompatibleAsInt(tc.getClassicCompatibleAsInt());
         }
         if (tc.isCustomDateFormatsSet()) {
-            setCustomDateFormats(mergeMaps(getCustomDateFormats(), tc.getCustomDateFormats()));
+            setCustomDateFormats(mergeMaps(getCustomDateFormats(), tc.getCustomDateFormats(),
false));
         }
         if (tc.isCustomNumberFormatsSet()) {
-            setCustomNumberFormats(mergeMaps(getCustomNumberFormats(), tc.getCustomNumberFormats()));
+            setCustomNumberFormats(mergeMaps(getCustomNumberFormats(), tc.getCustomNumberFormats(),
false));
         }
         if (tc.isDateFormatSet()) {
             setDateFormat(tc.getDateFormat());
@@ -245,7 +245,7 @@ public final class TemplateConfiguration extends Configurable implements
ParserC
             setTabSize(tc.getTabSize());
         }
         if (tc.isAutoImportsSet()) {
-            setAutoImports(mergeMaps(getAutoImports(), tc.getAutoImports()));
+            setAutoImports(mergeMaps(getAutoImports(), tc.getAutoImports(), true));
         }
         if (tc.isAutoIncludesSet()) {
             setAutoIncludes(mergeLists(getAutoIncludes(), tc.getAutoIncludes()));
@@ -351,7 +351,7 @@ public final class TemplateConfiguration extends Configurable implements
ParserC
             // - Existing template-level imports have precedence over those coming from the
TC (just as with the others
             //   apply()-ed settings), thus for clashing import prefixes they must win.
             // - Template-level imports count as more specific, and so come after the more
generic ones from TC.
-            template.setAutoImports(mergeMaps(getAutoImports(), template.getAutoImportsWithoutFallback()));
+            template.setAutoImports(mergeMaps(getAutoImports(), template.getAutoImportsWithoutFallback(),
true));
         }
         if (isAutoIncludesSet()) {
             template.setAutoIncludes(mergeLists(getAutoIncludes(), template.getAutoIncludesWithoutFallback()));
@@ -620,7 +620,7 @@ public final class TemplateConfiguration extends Configurable implements
ParserC
                 || isURLEscapingCharsetSet();
     }
     
-    private Map mergeMaps(Map m1, Map m2) {
+    private Map mergeMaps(Map m1, Map m2, boolean overwriteOverwritesOrder) {
         if (m1 == null) return m2;
         if (m2 == null) return m1;
         if (m1.isEmpty()) return m2 != null ? m2 : m1;
@@ -628,6 +628,9 @@ public final class TemplateConfiguration extends Configurable implements
ParserC
         
         LinkedHashMap mergedM = new LinkedHashMap((m1.size() + m2.size()) * 4 / 3 + 1, 0.75f);
         mergedM.putAll(m1);
+        for (Object m2Key : m2.keySet()) {
+            mergedM.remove(m2Key); // So that duplicate keys are moved after m1 keys
+        }
         mergedM.putAll(m2);
         return mergedM;
     }

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4857c045/src/test/java/freemarker/core/TemplateConfigurationTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/core/TemplateConfigurationTest.java b/src/test/java/freemarker/core/TemplateConfigurationTest.java
index 861d1f7..5dd3756 100644
--- a/src/test/java/freemarker/core/TemplateConfigurationTest.java
+++ b/src/test/java/freemarker/core/TemplateConfigurationTest.java
@@ -371,14 +371,14 @@ public class TemplateConfigurationTest {
     @Test
     public void testMergeListSettings() throws Exception {
         TemplateConfiguration tc1 = new TemplateConfiguration();
-        tc1.setAutoIncludes(ImmutableList.of("a.ftl", "b.ftl"));
+        tc1.setAutoIncludes(ImmutableList.of("a.ftl", "x.ftl", "b.ftl"));
         
         TemplateConfiguration tc2 = new TemplateConfiguration();
-        tc2.setAutoIncludes(ImmutableList.of("c.ftl", "d.ftl"));
+        tc2.setAutoIncludes(ImmutableList.of("c.ftl", "x.ftl", "d.ftl"));
         
         tc1.merge(tc2);
         
-        assertEquals(ImmutableList.of("a.ftl", "b.ftl", "c.ftl", "d.ftl"), tc1.getAutoIncludes());
+        assertEquals(ImmutableList.of("a.ftl", "b.ftl", "c.ftl", "x.ftl", "d.ftl"), tc1.getAutoIncludes());
     }
     
     @Test
@@ -484,6 +484,40 @@ public class TemplateConfigurationTest {
         assertNull(CA2.get(tc1));
         assertNull(CA3.get(tc1));
     }
+    
+    @Test
+    public void applyOrder() throws Exception {
+        Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
+        Template t = new Template(null, "", cfg);
+        
+        {
+            TemplateConfiguration  tc = new TemplateConfiguration();
+            tc.setParentConfiguration(cfg);
+            tc.setBooleanFormat("Y,N");
+            tc.setAutoImports(ImmutableMap.of("a", "a.ftl", "b", "b.ftl", "c", "c.ftl"));
+            tc.setAutoIncludes(ImmutableList.of("i1.ftl", "i2.ftl", "i3.ftl"));
+            
+            tc.apply(t);
+        }
+        assertEquals("Y,N", t.getBooleanFormat());
+        assertEquals(ImmutableMap.of("a", "a.ftl", "b", "b.ftl", "c", "c.ftl"), t.getAutoImports());
+        assertEquals(ImmutableList.of("a", "b", "c"), new ArrayList<String>(t.getAutoImports().keySet()));
+        assertEquals(ImmutableList.of("i1.ftl", "i2.ftl", "i3.ftl"), t.getAutoIncludes());
+        
+        {
+            TemplateConfiguration  tc = new TemplateConfiguration();
+            tc.setParentConfiguration(cfg);
+            tc.setBooleanFormat("J,N");
+            tc.setAutoImports(ImmutableMap.of("b", "b2.ftl", "d", "d.ftl"));
+            tc.setAutoIncludes(ImmutableList.of("i2.ftl", "i4.ftl"));
+            
+            tc.apply(t);
+        }
+        assertEquals("Y,N", t.getBooleanFormat());
+        assertEquals(ImmutableMap.of("d", "d.ftl", "a", "a.ftl", "b", "b.ftl", "c", "c.ftl"),
t.getAutoImports());
+        assertEquals(ImmutableList.of("d", "a", "b", "c"), new ArrayList<String>(t.getAutoImports().keySet()));
+        assertEquals(ImmutableList.of("i4.ftl", "i1.ftl", "i2.ftl", "i3.ftl"), t.getAutoIncludes());
+    }
 
     @Test
     public void testConfigureNonParserConfig() throws Exception {


Mime
View raw message