myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject [myfaces] branch master updated: MYFACES-4225 tests
Date Tue, 06 Oct 2020 11:38:20 GMT
This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/master by this push:
     new 8bb2ddf  MYFACES-4225 tests
8bb2ddf is described below

commit 8bb2ddf9f0dce365f4082a03d3d3272710854e11
Author: Thomas Andraschko <tandraschko@apache.org>
AuthorDate: Tue Oct 6 13:38:10 2020 +0200

    MYFACES-4225 tests
---
 .../myfaces/application/ResourceHandlerImpl.java   | 56 +++++++++++-----------
 .../resource/ClassLoaderResourceLoader.java        |  4 ++
 .../myfaces/resource/ResourceHandlerCache.java     |  2 +-
 .../application/ResourceHandlerImplTest.java       | 33 +++++++++++++
 4 files changed, 66 insertions(+), 29 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java b/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
index 5a7be96..63e0470 100644
--- a/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/application/ResourceHandlerImpl.java
@@ -164,7 +164,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         // contract name.
         if (contractPreferred != null)
         {
-            resourceValue = getResourceLoaderCache().getResource(
+            resourceValue = getResourceHandlerCache().getResource(
                     resourceName, libraryName, contentType, localePrefix, contractPreferred);
         }
         if (resourceValue == null && !contracts.isEmpty())
@@ -172,7 +172,7 @@ public class ResourceHandlerImpl extends ResourceHandler
             // Try to get resource but try with a contract name
             for (String contract : contracts)
             {
-                resourceValue = getResourceLoaderCache().getResource(
+                resourceValue = getResourceHandlerCache().getResource(
                     resourceName, libraryName, contentType, localePrefix, contract);
                 if (resourceValue != null)
                 {
@@ -184,7 +184,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         if (resourceValue == null)
         {
             // Try to get resource without contract name
-            resourceValue = getResourceLoaderCache().getResource(resourceName, libraryName,
contentType, localePrefix);
+            resourceValue = getResourceHandlerCache().getResource(resourceName, libraryName,
contentType, localePrefix);
         }
         
         if(resourceValue != null)
@@ -210,7 +210,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                             getResourceHandlerSupport(), contentType);
 
                         // cache it
-                        getResourceLoaderCache().putResource(resourceName, libraryName, contentType,
+                        getResourceHandlerCache().putResource(resourceName, libraryName,
contentType,
                                 localePrefix, contractPreferred, resourceMeta, loader, 
                                 new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                         resolved = true;
@@ -234,7 +234,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                                 getResourceHandlerSupport(), contentType);
 
                             // cache it
-                            getResourceLoaderCache().putResource(
+                            getResourceHandlerCache().putResource(
                                     resourceName, libraryName, contentType,
                                     localePrefix, contract, resourceMeta, loader,
                                     new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
@@ -257,7 +257,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                             resourceMeta, loader, getResourceHandlerSupport(), contentType);
 
                         // cache it
-                        getResourceLoaderCache().putResource(resourceName, libraryName, contentType,
+                        getResourceHandlerCache().putResource(resourceName, libraryName,
contentType,
                                 localePrefix, null, resourceMeta, loader, 
                                 new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                         break;
@@ -810,13 +810,13 @@ public class ResourceHandlerImpl extends ResourceHandler
             //Check with locale
             pathToLib = localePrefix + '/' + libraryName;
 
-            libraryFound = getResourceLoaderCache().libraryExists(pathToLib);
+            libraryFound = getResourceHandlerCache().libraryExists(pathToLib);
             if (libraryFound != null)
             {
                 return libraryFound;
             }
         }
-        libraryFound = getResourceLoaderCache().libraryExists(libraryName);
+        libraryFound = getResourceHandlerCache().libraryExists(libraryName);
         if (libraryFound != null)
         {
             return libraryFound;
@@ -832,7 +832,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                     {
                         if (loader.libraryExists(pathToLib, contract))
                         {
-                            getResourceLoaderCache().confirmLibraryExists(pathToLib);
+                            getResourceHandlerCache().confirmLibraryExists(pathToLib);
                             return true;
                         }
                     }
@@ -843,7 +843,7 @@ public class ResourceHandlerImpl extends ResourceHandler
             {
                 if (loader.libraryExists(pathToLib))
                 {
-                    getResourceLoaderCache().confirmLibraryExists(pathToLib);
+                    getResourceHandlerCache().confirmLibraryExists(pathToLib);
                     return true;
                 }
             }            
@@ -858,7 +858,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                 {
                     if (loader.libraryExists(libraryName, contract))
                     {
-                        getResourceLoaderCache().confirmLibraryExists(libraryName);
+                        getResourceHandlerCache().confirmLibraryExists(libraryName);
                         return true;
                     }
                 }
@@ -869,7 +869,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         {
             if (loader.libraryExists(libraryName))
             {
-                getResourceLoaderCache().confirmLibraryExists(libraryName);
+                getResourceHandlerCache().confirmLibraryExists(libraryName);
                 return true;
             }
         }
@@ -877,11 +877,11 @@ public class ResourceHandlerImpl extends ResourceHandler
         if (localePrefix != null)
         {
             //Check with locale
-            getResourceLoaderCache().confirmLibraryNotExists(pathToLib);
+            getResourceHandlerCache().confirmLibraryNotExists(pathToLib);
         }
         else
         {
-            getResourceLoaderCache().confirmLibraryNotExists(libraryName);
+            getResourceHandlerCache().confirmLibraryNotExists(libraryName);
         }
         return false;
     }
@@ -900,7 +900,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         return _resourceHandlerSupport;
     }
 
-    private ResourceHandlerCache getResourceLoaderCache()
+    protected ResourceHandlerCache getResourceHandlerCache()
     {
         if (_resourceHandlerCache == null)
         {
@@ -909,7 +909,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         return _resourceHandlerCache;
     }
 
-    private String _getContentType(Resource resource, ExternalContext externalContext)
+    protected String _getContentType(Resource resource, ExternalContext externalContext)
     {
         String contentType = resource.getContentType();
 
@@ -996,14 +996,14 @@ public class ResourceHandlerImpl extends ResourceHandler
         // a contract.
         if (contractPreferred != null)
         {
-            resourceValue = getResourceLoaderCache().getResource(resourceId, contractPreferred);
+            resourceValue = getResourceHandlerCache().getResource(resourceId, contractPreferred);
         }
         if (resourceValue == null && !contracts.isEmpty())
         {
             // Try to get resource but try with a contract name
             for (String contract : contracts)
             {
-                resourceValue = getResourceLoaderCache().getResource(resourceId, contract);
+                resourceValue = getResourceHandlerCache().getResource(resourceId, contract);
                 if (resourceValue != null)
                 {
                     break;
@@ -1013,7 +1013,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         if (resourceValue == null)
         {
             // Try to get resource without contract name
-            resourceValue = getResourceLoaderCache().getResource(resourceId);
+            resourceValue = getResourceHandlerCache().getResource(resourceId);
         }
         
         if (resourceValue != null)
@@ -1045,7 +1045,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                             getResourceHandlerSupport(), contentType);
 
                         // cache it
-                        getResourceLoaderCache().putResource(resourceId, resourceMeta, loader,

+                        getResourceHandlerCache().putResource(resourceId, resourceMeta, loader,

                             new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                         
                         resolved = true;
@@ -1071,7 +1071,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                                 getResourceHandlerSupport(), contentType);
 
                             // cache it
-                            getResourceLoaderCache().putResource(resourceId, resourceMeta,
loader, 
+                            getResourceHandlerCache().putResource(resourceId, resourceMeta,
loader, 
                                 new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
 
                             resolved = true;
@@ -1094,7 +1094,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                         resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(),
contentType);
 
                         // cache it
-                        getResourceLoaderCache().putResource(resourceId, resourceMeta, loader,

+                        getResourceHandlerCache().putResource(resourceId, resourceMeta, loader,

                             new ResourceCachedInfo(resource.getURL(), resource.getRequestPath()));
                         break;
                     }
@@ -1577,7 +1577,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         // a contract.
         if (contractPreferred != null)
         {
-            resourceValue = getResourceLoaderCache().getViewResource(
+            resourceValue = getResourceHandlerCache().getViewResource(
                     resourceName, contentType, localePrefix, contractPreferred);
         }
         if (resourceValue == null && !contracts.isEmpty())
@@ -1585,7 +1585,7 @@ public class ResourceHandlerImpl extends ResourceHandler
             // Try to get resource but try with a contract name
             for (String contract : contracts)
             {
-                resourceValue = getResourceLoaderCache().getViewResource(
+                resourceValue = getResourceHandlerCache().getViewResource(
                     resourceName, contentType, localePrefix, contract);
                 if (resourceValue != null)
                 {
@@ -1596,7 +1596,7 @@ public class ResourceHandlerImpl extends ResourceHandler
         if (resourceValue == null)
         {
             // Try to get resource without contract name
-            resourceValue = getResourceLoaderCache().getViewResource(resourceName, contentType,
localePrefix);
+            resourceValue = getResourceHandlerCache().getViewResource(resourceName, contentType,
localePrefix);
         }
 
         if(resourceValue != null)
@@ -1619,7 +1619,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                         resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(),
contentType);
 
                         // cache it
-                        getResourceLoaderCache().putViewResource(
+                        getResourceHandlerCache().putViewResource(
                             resourceName, contentType, localePrefix, contractPreferred, resourceMeta,
loader, 
                             new ResourceCachedInfo(resource.getURL(), null));
                         
@@ -1643,7 +1643,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                                     getResourceHandlerSupport(), contentType);
 
                             // cache it
-                            getResourceLoaderCache().putViewResource(
+                            getResourceHandlerCache().putViewResource(
                                 resourceName, contentType, localePrefix, contract, resourceMeta,
loader,
                                 new ResourceCachedInfo(resource.getURL(), null));
 
@@ -1669,7 +1669,7 @@ public class ResourceHandlerImpl extends ResourceHandler
                         resource = new ResourceImpl(resourceMeta, loader, getResourceHandlerSupport(),
contentType);
 
                         // cache it
-                        getResourceLoaderCache().putViewResource(
+                        getResourceHandlerCache().putViewResource(
                             resourceName, contentType, localePrefix, resourceMeta, loader,
                             new ResourceCachedInfo(resource.getURL(), null));
                         break;
diff --git a/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
b/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
index c95f3b8..caf9ecd 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/ClassLoaderResourceLoader.java
@@ -100,6 +100,10 @@ public class ClassLoaderResourceLoader extends ResourceLoader
     @Override
     public URL getResourceURL(ResourceMeta resourceMeta)
     {
+        if (resourceMeta == null)
+        {
+            return null;
+        }
         return getResourceURL(resourceMeta.getResourceIdentifier());
     }
 
diff --git a/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java b/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java
index cfd89fa..367aed2 100644
--- a/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java
+++ b/impl/src/main/java/org/apache/myfaces/resource/ResourceHandlerCache.java
@@ -149,7 +149,7 @@ public class ResourceHandlerCache
             log.log(Level.FINE, "Attemping to get resource from cache for " + resourceId);
         }
 
-        return _resourceCacheMap.get(contractName+':'+resourceId);
+        return _resourceCacheMap.get(contractName + ':' + resourceId);
     }
     
     public boolean containsResource(String resourceId, String contractName)
diff --git a/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
b/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
index 607fa66..7452e1b 100644
--- a/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
+++ b/impl/src/test/java/org/apache/myfaces/application/ResourceHandlerImplTest.java
@@ -29,9 +29,13 @@ import java.net.URL;
 import java.util.Locale;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import org.apache.myfaces.resource.ClassLoaderResourceLoader;
+import org.apache.myfaces.resource.ResourceHandlerCache;
+import org.apache.myfaces.resource.ResourceHandlerSupport;
 import org.apache.myfaces.resource.ResourceLoader;
 import org.apache.myfaces.resource.ResourceMeta;
 import org.apache.myfaces.resource.ResourceMetaImpl;
+import org.mockito.Mockito;
 
 /**
  * Test cases for org.apache.myfaces.application.ResourceHandlerImpl.
@@ -227,4 +231,33 @@ public class ResourceHandlerImplTest extends AbstractJsfTestCase
         
         Assert.assertTrue(didNPEOccur);
     }
+
+    @Test
+    public void testCache()
+    {
+        ResourceLoader loader = Mockito.spy(new ClassLoaderResourceLoader(null));
+        Mockito.when(loader.resourceExists(Mockito.any())).thenReturn(true);
+
+        ResourceHandlerCache cache = Mockito.spy(new ResourceHandlerCache());   
+        
+        ResourceHandlerSupport support = Mockito.spy(new DefaultResourceHandlerSupport());
+        Mockito.when(support.getResourceLoaders()).thenReturn(new ResourceLoader[] { loader
});
+        
+        resourceHandler = Mockito.spy(resourceHandler);
+        Mockito.when(resourceHandler.getResourceHandlerCache()).thenReturn(cache);
+        Mockito.when(resourceHandler.getResourceHandlerSupport()).thenReturn(support);
+        
+        resourceHandler.createResource("test.png", "test", "test");
+        resourceHandler.createResource("test.png", "test", "test");
+        resourceHandler.createResource("test.png", "test", "test");
+        resourceHandler.createResource("test.png", "test", "test");
+
+        Mockito.verify(cache, Mockito.times(4)).getResource(
+                Mockito.eq("test.png"), Mockito.eq("test"), Mockito.eq("test"), Mockito.any());
+        Mockito.verify(cache, Mockito.times(1)).putResource(
+                Mockito.eq("test.png"), Mockito.eq("test"), Mockito.eq("test"), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
+        Mockito.verify(loader, Mockito.times(1)).createResourceMeta(
+                Mockito.any(), Mockito.eq("test"), Mockito.any(), Mockito.eq("test.png"),
Mockito.any());
+
+    }
 }


Mime
View raw message