From bcf907cb767853a7ab6f2a70d70569280bd54176 Mon Sep 17 00:00:00 2001
From: Andreas Antener <andreas.antener@deimos.ch>
Date: Fri, 8 Feb 2013 09:28:05 +0100
Subject: [PATCH] - Use area i18nBasename for label resolution of area beeing
 rendered - Added unit test to confirm the use of area
 i18nBasename

---
 .../magnolia/templating/elements/AreaElement.java  |  10 +-
 .../freemarker/AreaDirectiveI18nBasenameTest.java  | 114 +++++++++++++++++++++
 2 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 magnolia-templating/src/test/java/info/magnolia/templating/freemarker/AreaDirectiveI18nBasenameTest.java

diff --git a/magnolia-templating/src/main/java/info/magnolia/templating/elements/AreaElement.java b/magnolia-templating/src/main/java/info/magnolia/templating/elements/AreaElement.java
index 1e111e2..0bac554 100644
--- a/magnolia-templating/src/main/java/info/magnolia/templating/elements/AreaElement.java
+++ b/magnolia-templating/src/main/java/info/magnolia/templating/elements/AreaElement.java
@@ -120,7 +120,6 @@ public class AreaElement extends AbstractContentTemplatingElement {
     public void begin(Appendable out) throws IOException, RenderException {
 
         this.templateDefinition = resolveTemplateDefinition();
-        Messages messages = MessagesManager.getMessages(templateDefinition.getI18nBasename());
 
         this.areaDefinition = resolveAreaDefinition();
 
@@ -145,6 +144,15 @@ public class AreaElement extends AbstractContentTemplatingElement {
         if(this.areaDefinition == null){
             buildAdHocAreaDefinition();
         }
+        
+        // i18nBasename on area has priority over i18nBasename from parent definition
+        Messages messages;
+        if (null != this.areaDefinition.getI18nBasename()) {
+            messages = MessagesManager.getMessages(this.areaDefinition.getI18nBasename());
+        }
+        else {
+            messages = MessagesManager.getMessages(this.templateDefinition.getI18nBasename());
+        }
 
         this.maxComponents = resolveMaximumOfComponents();
 
diff --git a/magnolia-templating/src/test/java/info/magnolia/templating/freemarker/AreaDirectiveI18nBasenameTest.java b/magnolia-templating/src/test/java/info/magnolia/templating/freemarker/AreaDirectiveI18nBasenameTest.java
new file mode 100644
index 0000000..ee55998
--- /dev/null
+++ b/magnolia-templating/src/test/java/info/magnolia/templating/freemarker/AreaDirectiveI18nBasenameTest.java
@@ -0,0 +1,114 @@
+/**
+ * This file Copyright (c) 2010-2011 Magnolia International
+ * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
+ *
+ *
+ * This file is dual-licensed under both the Magnolia
+ * Network Agreement and the GNU General Public License.
+ * You may elect to use one or the other of these licenses.
+ *
+ * This file is distributed in the hope that it will be
+ * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
+ * Redistribution, except as permitted by whichever of the GPL
+ * or MNA you select, is prohibited.
+ *
+ * 1. For the GPL license (GPL), you can redistribute and/or
+ * modify this file under the terms of the GNU General
+ * Public License, Version 3, as published by the Free Software
+ * Foundation.  You should have received a copy of the GNU
+ * General Public License, Version 3 along with this program;
+ * if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * 2. For the Magnolia Network Agreement (MNA), this file
+ * and the accompanying materials are made available under the
+ * terms of the MNA which accompanies this distribution, and
+ * is available at http://www.magnolia-cms.com/mna.html
+ *
+ * Any modifications to this file must keep this entire header
+ * intact.
+ *
+ */
+package info.magnolia.templating.freemarker;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import info.magnolia.cms.i18n.DefaultMessagesManager;
+import info.magnolia.cms.i18n.Messages;
+import info.magnolia.cms.i18n.MessagesManager;
+import info.magnolia.context.MgnlContext;
+import info.magnolia.rendering.template.configured.ConfiguredAreaDefinition;
+import info.magnolia.rendering.template.configured.ConfiguredTemplateDefinition;
+
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * $Id$
+ */
+public class AreaDirectiveI18nBasenameTest extends AbstractDirectiveTestCase {
+    private ConfiguredTemplateDefinition renderableDef;
+    private ConfiguredAreaDefinition areaDefinition;
+    private DefaultMessagesManager messagesManager;
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        renderableDef = new ConfiguredTemplateDefinition();
+        areaDefinition = new ConfiguredAreaDefinition();
+        renderableDef.addArea("stage", areaDefinition);
+        areaDefinition.setTitle("area.title");
+        
+        // Create message two different message bundles, one for template definition and one for area definition
+        Messages areaMessageBundle = mock(Messages.class);
+        when(areaMessageBundle.getWithDefault(eq("area.title"), anyString())).thenReturn("newStageName");
+        
+        Messages parentMessageBundle = mock(Messages.class);
+        when(parentMessageBundle.getWithDefault(eq("area.title"), anyString())).thenReturn("oldStageName");
+        
+        Map<DefaultMessagesManager.MessagesID, Messages> map = new HashMap<DefaultMessagesManager.MessagesID, Messages>();
+        map.put(new DefaultMessagesManager.MessagesID("area.basename", MgnlContext.getLocale()), areaMessageBundle);
+        map.put(new DefaultMessagesManager.MessagesID("parent.basename", MgnlContext.getLocale()), parentMessageBundle);
+        
+        // FIXME: Use a different approach to set message bundles
+        messagesManager = (DefaultMessagesManager)MessagesManager.getInstance();
+        messagesManager.setMessages(map);
+    }
+
+    @Test
+    public void testRenderAreaWithAreaBasename() throws Exception {
+        // GIVEN
+        renderableDef.setI18nBasename("parent.basename");
+        areaDefinition.setI18nBasename("area.basename");
+
+        // WHEN
+        final String result = renderForTest("[@cms.area name=\"stage\" /]", renderableDef);
+
+        // THEN
+        assertEquals(
+                "<!-- cms:area content=\"testWorkspace:/foo/bar/paragraphs/1/stage\" name=\"stage\" availableComponents=\"\" type=\"list\" label=\"newStageName\" inherit=\"false\" optional=\"false\" showAddButton=\"true\" -->"
+                + "\n<!-- /cms:area -->\n", result);
+    }
+    
+    @Test
+    public void testRenderAreaWithParentBasename() throws Exception {
+        // GIVEN
+        renderableDef.setI18nBasename("parent.basename");
+
+        // WHEN
+        final String result = renderForTest("[@cms.area name=\"stage\" /]", renderableDef);
+
+        // THEN
+        assertEquals(
+                "<!-- cms:area content=\"testWorkspace:/foo/bar/paragraphs/1/stage\" name=\"stage\" availableComponents=\"\" type=\"list\" label=\"oldStageName\" inherit=\"false\" optional=\"false\" showAddButton=\"true\" -->"
+                + "\n<!-- /cms:area -->\n", result);
+    }
+
+}
-- 
1.7.11.1

