You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2011/04/13 12:14:59 UTC

svn commit: r1091727 - in /myfaces: core/trunk/impl/src/main/java/org/apache/myfaces/application/ core/trunk/impl/src/test/java/org/apache/myfaces/application/ core/trunk/impl/src/test/resources/META-INF/resources/ core/trunk/impl/src/test/resources/ME...

Author: jakobk
Date: Wed Apr 13 10:14:58 2011
New Revision: 1091727

URL: http://svn.apache.org/viewvc?rev=1091727&view=rev
Log:
MYFACES-3108 ResourceHandlerCache does not honor localePrefix (fix + test case)

Added:
    myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
    myfaces/core/trunk/impl/src/test/resources/META-INF/resources/
    myfaces/core/trunk/impl/src/test/resources/META-INF/resources/en/
    myfaces/core/trunk/impl/src/test/resources/META-INF/resources/en/testResource.xhtml
    myfaces/core/trunk/impl/src/test/resources/META-INF/resources/testResource.xhtml
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_de.properties
    myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_en.properties
Modified:
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java?rev=1091727&r1=1091726&r2=1091727&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java Wed Apr 13 10:14:58 2011
@@ -90,27 +90,31 @@ public class ResourceHandlerImpl extends
             //Resolve contentType using ExternalContext.getMimeType
             contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(resourceName);
         }
-        
-        if(getResourceLoaderCache().containsResource(resourceName, libraryName, contentType))
+
+        final String localePrefix = getLocalePrefixForLocateResource();
+
+        // check cache
+        if(getResourceLoaderCache().containsResource(resourceName, libraryName, contentType, localePrefix))
         {
-            ResourceValue resourceValue = getResourceLoaderCache().getResource(resourceName, libraryName, contentType);
+            ResourceValue resourceValue = getResourceLoaderCache().getResource(
+                    resourceName, libraryName, contentType, localePrefix);
+            
             resource = new ResourceImpl(resourceValue.getResourceMeta(), resourceValue.getResourceLoader(),
                     getResourceHandlerSupport(), contentType);
         }
         else
         {
-            for (ResourceLoader loader : getResourceHandlerSupport()
-                    .getResourceLoaders())
+            for (ResourceLoader loader : getResourceHandlerSupport().getResourceLoaders())
             {
-                ResourceMeta resourceMeta = deriveResourceMeta(loader,
-                        resourceName, libraryName);
+                ResourceMeta resourceMeta = deriveResourceMeta(loader, resourceName, libraryName, localePrefix);
     
                 if (resourceMeta != null)
                 {
-                    resource = new ResourceImpl(resourceMeta, loader,
-                            getResourceHandlerSupport(), contentType);
-                    
-                    getResourceLoaderCache().putResource(resourceName, libraryName, contentType, resourceMeta, loader);
+                    resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(), contentType);
+
+                    // cache it
+                    getResourceLoaderCache().putResource(resourceName, libraryName, contentType,
+                            localePrefix, resourceMeta, loader);
                     break;
                 }
             }
@@ -126,9 +130,8 @@ public class ResourceHandlerImpl extends
      * next registered ResourceLoader. 
      */
     protected ResourceMeta deriveResourceMeta(ResourceLoader resourceLoader,
-            String resourceName, String libraryName)
+            String resourceName, String libraryName, String localePrefix)
     {
-        String localePrefix = getLocalePrefixForLocateResource();
         String resourceVersion = null;
         String libraryVersion = null;
         ResourceMeta resourceId = null;

Added: myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java?rev=1091727&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java (added)
+++ myfaces/core/trunk/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java Wed Apr 13 10:14:58 2011
@@ -0,0 +1,88 @@
+/*
+ * 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.myfaces.application;
+
+import org.apache.myfaces.test.base.AbstractJsfTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.faces.application.Resource;
+import java.net.URL;
+import java.util.Locale;
+
+/**
+ * Test cases for org.apache.myfaces.application.ResourceHandlerImpl.
+ *
+ * @author Jakob Korherr
+ */
+public class ResourceHandlerImplTest extends AbstractJsfTestCase
+{
+
+    private ResourceHandlerImpl resourceHandler;
+
+    public ResourceHandlerImplTest(String name)
+    {
+        super(name);
+    }
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+
+        resourceHandler = new ResourceHandlerImpl();
+    }
+
+    @Override
+    protected void tearDown() throws Exception
+    {
+        resourceHandler = null;
+
+        super.tearDown();    
+    }
+
+    @Test
+    public void testCreateResource_ResourceNotNull() throws Exception
+    {
+        Resource resource = resourceHandler.createResource("testResource.xhtml");
+
+        Assert.assertNotNull(resource);
+    }
+
+    @Test
+    public void testCreateResource_cacheHonorsLocale() throws Exception
+    {
+        // setup message bundle to use
+        application.setMessageBundle("org/apache/myfaces/application/resourcehandler/messages");
+
+        // get english resource
+        application.setDefaultLocale(Locale.ENGLISH);
+        Resource resourceEn = resourceHandler.createResource("testResource.xhtml");
+        URL urlEn = resourceEn.getURL();
+
+        // get german resource
+        application.setDefaultLocale(Locale.GERMAN);
+        Resource resourceDe = resourceHandler.createResource("testResource.xhtml");
+        URL urlDe = resourceDe.getURL();
+
+        // URLs MUST be different, since there is an english and a german version of the resource
+        Assert.assertFalse("Resources must be different", urlEn.equals(urlDe));
+    }
+
+}

Added: myfaces/core/trunk/impl/src/test/resources/META-INF/resources/en/testResource.xhtml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/META-INF/resources/en/testResource.xhtml?rev=1091727&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/META-INF/resources/en/testResource.xhtml (added)
+++ myfaces/core/trunk/impl/src/test/resources/META-INF/resources/en/testResource.xhtml Wed Apr 13 10:14:58 2011
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<!DOCTYPE html
+        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+    <title>Test Resource</title>
+</head>
+<body>
+    <p>English test resource referenced by org.apache.myfaces.application.ResourceHandlerImplTest</p>
+</body>
+</html>

Added: myfaces/core/trunk/impl/src/test/resources/META-INF/resources/testResource.xhtml
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/META-INF/resources/testResource.xhtml?rev=1091727&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/META-INF/resources/testResource.xhtml (added)
+++ myfaces/core/trunk/impl/src/test/resources/META-INF/resources/testResource.xhtml Wed Apr 13 10:14:58 2011
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<!DOCTYPE html
+        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+    <title>Test Resource</title>
+</head>
+<body>
+    <p>Test resource referenced by org.apache.myfaces.application.ResourceHandlerImplTest</p>
+</body>
+</html>

Added: myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_de.properties
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_de.properties?rev=1091727&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_de.properties (added)
+++ myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_de.properties Wed Apr 13 10:14:58 2011
@@ -0,0 +1,18 @@
+#  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.
+
+javax.faces.resource.localePrefix=de

Added: myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_en.properties
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_en.properties?rev=1091727&view=auto
==============================================================================
--- myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_en.properties (added)
+++ myfaces/core/trunk/impl/src/test/resources/org/apache/myfaces/application/resourcehandler/messages_en.properties Wed Apr 13 10:14:58 2011
@@ -0,0 +1,18 @@
+#  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.
+
+javax.faces.resource.localePrefix=en

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java?rev=1091727&r1=1091726&r2=1091727&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/resource/ResourceHandlerCache.java Wed Apr 13 10:14:58 2011
@@ -18,19 +18,18 @@
  */
 package org.apache.myfaces.shared.resource;
 
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
+import org.apache.myfaces.shared.util.WebConfigParamUtils;
+
+import javax.faces.application.ProjectStage;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
 import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import javax.faces.application.ProjectStage;
-import javax.faces.context.ExternalContext;
-import javax.faces.context.FacesContext;
-
-import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
-import org.apache.myfaces.shared.util.WebConfigParamUtils;
-
 public class ResourceHandlerCache
 {
     private static final Logger log = Logger
@@ -48,7 +47,7 @@ public class ResourceHandlerCache
     private static final boolean RESOURCE_HANDLER_CACHE_ENABLED_DEFAULT = true;
 
     public ResourceValue getResource(String resourceName, String libraryName,
-            String contentType)
+            String contentType, String localePrefix)
     {
         if (!isResourceCachingEnabled() || _resourceCacheMap == null)
             return null;
@@ -57,24 +56,22 @@ public class ResourceHandlerCache
             log.log(Level.FINE, "Attemping to get resource from cache for "
                     + resourceName);
 
-        ResourceKey key = new ResourceKey(resourceName, libraryName,
-                contentType);
+        ResourceKey key = new ResourceKey(resourceName, libraryName, contentType, localePrefix);
 
         return _resourceCacheMap.get(key);
     }
     
-    public boolean containsResource(String resourceName, String libraryName,
-            String contentType)
+    public boolean containsResource(String resourceName, String libraryName, String contentType, String localePrefix)
     {
         if (!isResourceCachingEnabled() || _resourceCacheMap == null)
             return false;
-        ResourceKey key = new ResourceKey(resourceName, libraryName,
-                contentType);
+
+        ResourceKey key = new ResourceKey(resourceName, libraryName, contentType, localePrefix);
         return _resourceCacheMap.containsKey(key);
     }
 
     public void putResource(String resourceName, String libraryName,
-            String contentType, ResourceMeta resource, ResourceLoader loader)
+            String contentType, String localePrefix, ResourceMeta resource, ResourceLoader loader)
     {
         if (!isResourceCachingEnabled())
             return;
@@ -93,7 +90,7 @@ public class ResourceHandlerCache
         }
 
         _resourceCacheMap.put(new ResourceKey(resourceName, libraryName,
-                contentType), new ResourceValue(resource, loader));
+                contentType, localePrefix), new ResourceValue(resource, loader));
     }
 
     private boolean isResourceCachingEnabled()
@@ -140,64 +137,62 @@ public class ResourceHandlerCache
         private String resourceName;
         private String libraryName;
         private String contentType;
+        private String localePrefix;
 
         public ResourceKey(String resourceName, String libraryName,
-                String contentType)
+                String contentType, String localePrefix)
         {
             this.resourceName = resourceName;
             this.libraryName = libraryName;
             this.contentType = contentType;
+            this.localePrefix = localePrefix;
         }
 
         @Override
-        public int hashCode()
+        public boolean equals(Object o)
         {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result
-                    + ((contentType == null) ? 0 : contentType.hashCode());
-            result = prime * result
-                    + ((libraryName == null) ? 0 : libraryName.hashCode());
-            result = prime * result
-                    + ((resourceName == null) ? 0 : resourceName.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj)
-        {
-            if (this == obj)
-                return true;
-            if (obj == null)
-                return false;
-            if (getClass() != obj.getClass())
-                return false;
-            ResourceKey other = (ResourceKey) obj;
-            if (contentType == null)
+            if (this == o)
             {
-                if (other.contentType != null)
-                    return false;
+                return true;
             }
-            else if (!contentType.equals(other.contentType))
+            if (o == null || getClass() != o.getClass())
+            {
                 return false;
-            if (libraryName == null)
+            }
+
+            ResourceKey that = (ResourceKey) o;
+
+            if (contentType != null ? !contentType.equals(that.contentType) : that.contentType != null)
             {
-                if (other.libraryName != null)
-                    return false;
+                return false;
             }
-            else if (!libraryName.equals(other.libraryName))
+            if (libraryName != null ? !libraryName.equals(that.libraryName) : that.libraryName != null)
+            {
                 return false;
-            if (resourceName == null)
+            }
+            if (localePrefix != null ? !localePrefix.equals(that.localePrefix) : that.localePrefix != null)
             {
-                if (other.resourceName != null)
-                    return false;
+                return false;
             }
-            else if (!resourceName.equals(other.resourceName))
+            if (resourceName != null ? !resourceName.equals(that.resourceName) : that.resourceName != null)
+            {
                 return false;
+            }
+
             return true;
         }
+
+        @Override
+        public int hashCode()
+        {
+            int result = resourceName != null ? resourceName.hashCode() : 0;
+            result = 31 * result + (libraryName != null ? libraryName.hashCode() : 0);
+            result = 31 * result + (contentType != null ? contentType.hashCode() : 0);
+            result = 31 * result + (localePrefix != null ? localePrefix.hashCode() : 0);
+            return result;
+        }
     }
-    
+
     public static class ResourceValue
     {
         private ResourceMeta resourceMeta;