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.lifecycle;
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.Test;
28  import org.testng.Assert;
29  
30  public class DefaultRestoreViewSupport2Test 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          DefaultRestoreViewSupport support = new DefaultRestoreViewSupport();
77          
78          String derivedViewId = support.deriveViewId(facesContext, "/view1.jsf");
79          
80          Assert.assertNotNull(derivedViewId);
81      }
82      
83      @Test
84      public void testDeriveViewId2() throws Exception
85      {
86          DefaultRestoreViewSupport support = new DefaultRestoreViewSupport();
87          
88          request.setPathElements("/testwebapp", "/faces", "/view1.jsp" , null);
89          
90          String derivedViewId = support.deriveViewId(facesContext, "/view1.jsp");
91          
92          Assert.assertNotNull(derivedViewId);
93      }
94      
95      @Test
96      public void testDeriveViewId3() throws Exception
97      {
98          DefaultRestoreViewSupport support = new DefaultRestoreViewSupport();
99          
100         request.setPathElements("/testwebapp", "/view2.jsf", null , null);
101         
102         String derivedViewId = support.deriveViewId(facesContext, "/view2.jsf");
103         
104         Assert.assertNotNull(derivedViewId);
105     }
106     
107     @Test
108     public void testDeriveViewId4() throws Exception
109     {
110         DefaultRestoreViewSupport support = new DefaultRestoreViewSupport();
111         
112         request.setPathElements("/testwebapp", "/faces", "/view2.xhtml" , null);
113         
114         String derivedViewId = support.deriveViewId(facesContext, "/view2.xhtml");
115         
116         Assert.assertNotNull(derivedViewId);
117     }
118 
119 
120     @Test
121     public void testDeriveViewId5() throws Exception
122     {
123         request.setPathElements("/testwebapp", "/noview1.jsf", null , null);
124 
125         DefaultRestoreViewSupport support = new DefaultRestoreViewSupport();
126         
127         String derivedViewId = support.deriveViewId(facesContext, "/noview1.jsf");
128         
129         Assert.assertNull(derivedViewId);
130     }
131 }