You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2010/08/03 19:28:55 UTC

svn commit: r981981 - in /myfaces/shared/trunk/core/src: main/java/org/apache/myfaces/shared/application/ test/java/org/apache/myfaces/shared/application/ test/resources/org/ test/resources/org/apache/ test/resources/org/apache/myfaces/ test/resources/...

Author: lu4242
Date: Tue Aug  3 17:28:55 2010
New Revision: 981981

URL: http://svn.apache.org/viewvc?rev=981981&view=rev
Log:
MYFACES-2856 Unnecesary calls to checkResourceExists found on DefaultViewHandlerSupport and DefaultRestoreViewSupport

Added:
    myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/application/
    myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupportTest.java
    myfaces/shared/trunk/core/src/test/resources/org/
    myfaces/shared/trunk/core/src/test/resources/org/apache/
    myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/
    myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/
    myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/
    myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view1.jsp
    myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view2.xhtml
Modified:
    myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java

Modified: myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java?rev=981981&r1=981980&r2=981981&view=diff
==============================================================================
--- myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java (original)
+++ myfaces/shared/trunk/core/src/main/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupport.java Tue Aug  3 17:28:55 2010
@@ -90,15 +90,22 @@ public class DefaultViewHandlerSupport i
         else if(mapping.isPrefixMapping())
         {
             viewId = handlePrefixMapping(viewId,mapping.getPrefix());
+
+            if(viewId != null)
+            {
+                return (checkResourceExists(context,viewId) ? viewId : null);
+            }
         }
         else if (viewId != null && mapping.getUrlPattern().startsWith(viewId))
         {
             throw new InvalidViewIdException(viewId);
         }
-
-        if(viewId != null)
+        else
         {
-            return (checkResourceExists(context,viewId) ? viewId : null);
+            if(viewId != null)
+            {
+                return (checkResourceExists(context,viewId) ? viewId : null);
+            }
         }
 
         return viewId;    // return null if no physical resource exists
@@ -355,29 +362,45 @@ public class DefaultViewHandlerSupport i
             // forced facelets mappings did not match or there were no entries in faceletsViewMappings array
             if(checkResourceExists(context,candidateViewId))
                 return candidateViewId;
-                       
+        
         }
         
         //jsp suffixes didn't match, try facelets suffix
         String faceletsDefaultSuffix = this.getFaceletsContextSuffix(context);
-        StringBuilder builder = new StringBuilder(requestViewId);
-        
-        if (extensionPos > -1 && extensionPos > slashPos)
+        if (faceletsDefaultSuffix != null)
         {
-            builder.replace(extensionPos, requestViewId.length(), faceletsDefaultSuffix);
+            for (String defaultSuffix : jspDefaultSuffixes)
+            {
+                if (faceletsDefaultSuffix.equals(defaultSuffix))
+                {
+                    faceletsDefaultSuffix = null;
+                    break;
+                }
+            }
         }
-        else
+        if (faceletsDefaultSuffix != null)
         {
-            builder.append(faceletsDefaultSuffix);
+            StringBuilder builder = new StringBuilder(requestViewId);
+            
+            if (extensionPos > -1 && extensionPos > slashPos)
+            {
+                builder.replace(extensionPos, requestViewId.length(), faceletsDefaultSuffix);
+            }
+            else
+            {
+                builder.append(faceletsDefaultSuffix);
+            }
+            
+            String candidateViewId = builder.toString();
+            if(checkResourceExists(context,candidateViewId))
+                return candidateViewId;
         }
-        
-        String candidateViewId = builder.toString();
-        if(checkResourceExists(context,candidateViewId))
-            return candidateViewId;
 
+        // Otherwise, if a physical resource exists with the name requestViewId let that value be viewId.
         if(checkResourceExists(context,requestViewId))
             return requestViewId;
         
+        //Otherwise return null.
         return null;
     }
     
@@ -388,7 +411,7 @@ public class DefaultViewHandlerSupport i
             if (context.getExternalContext().getResource(viewId) != null)
             {
                 return true;
-            }                                 
+            }
         }
         catch(MalformedURLException e)
         {

Added: myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupportTest.java
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupportTest.java?rev=981981&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupportTest.java (added)
+++ myfaces/shared/trunk/core/src/test/java/org/apache/myfaces/shared/application/DefaultViewHandlerSupportTest.java Tue Aug  3 17:28:55 2010
@@ -0,0 +1,143 @@
+/*
+ * 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.shared.application;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DefaultViewHandlerSupportTest extends AbstractJsfTestCase
+{
+    private final String filePath = this.getDirectory();
+    
+    @Override
+    protected void setUpServletObjects() throws Exception
+    {
+        URI context = this.getContext();
+        super.setUpServletObjects();
+        servletContext.setDocumentRoot(new File(context));
+    }
+    
+    private String getDirectory()
+    {
+        return this.getClass().getName().substring(0,
+                this.getClass().getName().lastIndexOf('.')).replace('.', '/')
+                + "/";
+    }
+    
+    protected URI getContext()
+    {
+        try
+        {
+            ClassLoader cl = Thread.currentThread().getContextClassLoader();
+            URL url = cl.getResource(this.filePath);
+            if (url == null)
+            {
+                throw new FileNotFoundException(cl.getResource("").getFile()
+                        + this.filePath + " was not found");
+            }
+            else
+            {
+                return new URI(url.toString());
+            }
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException("Error Initializing Context", e);
+        }
+    }
+    
+    @Test
+    public void testDeriveViewId1() throws Exception
+    {
+        request.setPathElements("/testwebapp", "/view1.jsf", null , null);
+
+        DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
+        
+        String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view1.jsf");
+        
+        Assert.assertNotNull(derivedViewId);
+    }
+    
+    @Test
+    public void testDeriveViewId2() throws Exception
+    {
+        DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
+        
+        request.setPathElements("/testwebapp", "/faces", "/view1.jsp" , null);
+        
+        String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view1.jsp");
+        
+        Assert.assertNotNull(derivedViewId);
+    }
+    
+    @Test
+    public void testDeriveViewId21() throws Exception
+    {
+        DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
+        
+        request.setPathElements("/testwebapp", "/faces", "/faces/view1.jsp" , null);
+        
+        String derivedViewId = support.calculateAndCheckViewId(facesContext, "/faces/view1.jsp");
+        
+        Assert.assertNotNull(derivedViewId);
+    }    
+    
+    @Test
+    public void testDeriveViewId3() throws Exception
+    {
+        DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
+        
+        request.setPathElements("/testwebapp", "/view2.jsf", null , null);
+        
+        String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view2.jsf");
+        
+        Assert.assertNotNull(derivedViewId);
+    }
+    
+    @Test
+    public void testDeriveViewId4() throws Exception
+    {
+        DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
+        
+        request.setPathElements("/testwebapp", "/faces", "/view2.xhtml" , null);
+        
+        String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view2.xhtml");
+        
+        Assert.assertNotNull(derivedViewId);
+    }
+
+
+    @Test
+    public void testDeriveViewId5() throws Exception
+    {
+        request.setPathElements("/testwebapp", "/noview1.jsf", null , null);
+
+        DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
+        
+        String derivedViewId = support.calculateAndCheckViewId(facesContext, "/noview1.jsf");
+        
+        Assert.assertNull(derivedViewId);
+    }
+}

Added: myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view1.jsp
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view1.jsp?rev=981981&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view1.jsp (added)
+++ myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view1.jsp Tue Aug  3 17:28:55 2010
@@ -0,0 +1,12 @@
+<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
+    pageEncoding="ISO-8859-1"%>
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<title>Insert title here</title>
+</head>
+<body>
+
+</body>
+</html>
\ No newline at end of file

Added: myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view2.xhtml
URL: http://svn.apache.org/viewvc/myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view2.xhtml?rev=981981&view=auto
==============================================================================
--- myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view2.xhtml (added)
+++ myfaces/shared/trunk/core/src/test/resources/org/apache/myfaces/shared/application/view2.xhtml Tue Aug  3 17:28:55 2010
@@ -0,0 +1,10 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
+<title>Insert title here</title>
+</head>
+<body>
+
+</body>
+</html>
\ No newline at end of file