View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.application;
20  
21  import java.io.File;
22  import java.io.FileNotFoundException;
23  import java.net.URI;
24  import java.net.URL;
25  
26  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  public class DefaultViewHandlerSupportTest extends AbstractJsfTestCase
31  {
32      private final String filePath = this.getDirectory();
33      
34      @Override
35      protected void setUpServletObjects() throws Exception
36      {
37          URI context = this.getContext();
38          super.setUpServletObjects();
39          servletContext.setDocumentRoot(new File(context));
40      }
41      
42      private String getDirectory()
43      {
44          return this.getClass().getName().substring(0,
45                  this.getClass().getName().lastIndexOf('.')).replace('.', '/')
46                  + "/";
47      }
48      
49      protected URI getContext()
50      {
51          try
52          {
53              ClassLoader cl = Thread.currentThread().getContextClassLoader();
54              URL url = cl.getResource(this.filePath);
55              if (url == null)
56              {
57                  throw new FileNotFoundException(cl.getResource("").getFile()
58                          + this.filePath + " was not found");
59              }
60              else
61              {
62                  return new URI(url.toString());
63              }
64          }
65          catch (Exception e)
66          {
67              throw new RuntimeException("Error Initializing Context", e);
68          }
69      }
70      
71      @Test
72      public void testDeriveViewId1() throws Exception
73      {
74          request.setPathElements("/testwebapp", "/view1.jsf", null , null);
75  
76          DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
77          
78          String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view1.jsf");
79          
80          Assert.assertNotNull(derivedViewId);
81      }
82      
83      @Test
84      public void testDeriveViewId2() throws Exception
85      {
86          DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
87          
88          request.setPathElements("/testwebapp", "/faces", "/view1.jsp" , null);
89          
90          String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view1.jsp");
91          
92          Assert.assertNotNull(derivedViewId);
93      }
94      
95      @Test
96      public void testDeriveViewId21() throws Exception
97      {
98          DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
99          
100         request.setPathElements("/testwebapp", "/faces", "/faces/view1.jsp" , null);
101         
102         String derivedViewId = support.calculateAndCheckViewId(facesContext, "/faces/view1.jsp");
103         
104         Assert.assertNotNull(derivedViewId);
105     }    
106     
107     @Test
108     public void testDeriveViewId3() throws Exception
109     {
110         DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
111         
112         request.setPathElements("/testwebapp", "/view2.jsf", null , null);
113         
114         String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view2.jsf");
115         
116         Assert.assertNotNull(derivedViewId);
117     }
118     
119     @Test
120     public void testDeriveViewId4() throws Exception
121     {
122         DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
123         
124         request.setPathElements("/testwebapp", "/faces", "/view2.xhtml" , null);
125         
126         String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view2.xhtml");
127         
128         Assert.assertNotNull(derivedViewId);
129     }
130 
131 
132     @Test
133     public void testDeriveViewId5() throws Exception
134     {
135         request.setPathElements("/testwebapp", "/noview1.jsf", null , null);
136 
137         DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
138         
139         String derivedViewId = support.calculateAndCheckViewId(facesContext, "/noview1.jsf");
140         
141         Assert.assertNull(derivedViewId);
142     }
143     
144     /**
145      * An empty prefix mapping can occur in Spring environments.
146      * @throws Exception
147      */
148     @Test
149     public void testEmptyPrefixMapping() throws Exception
150     {
151         DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
152         
153         request.setPathElements("/testwebapp", "", "/view2.xhtml" , null);
154         
155         String derivedViewId = support.calculateAndCheckViewId(facesContext, "/view2.xhtml");
156         
157         Assert.assertEquals("/view2.xhtml", derivedViewId);
158     }
159     
160     /**
161      * An empty prefix mapping can occur in Spring environments.
162      * However this should not only return the viewId, but also remove any
163      * leading double slashes.
164      * @throws Exception
165      */
166     @Test
167     public void testEmptyPrefixMappingDoubleSlash() throws Exception
168     {
169         DefaultViewHandlerSupport support = new DefaultViewHandlerSupport();
170         
171         request.setPathElements("/testwebapp", "", "//view2.xhtml" , null);
172         
173         String derivedViewId = support.calculateAndCheckViewId(facesContext, "//view2.xhtml");
174         
175         Assert.assertEquals("/view2.xhtml", derivedViewId);
176     }
177 }