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.view.jsp;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIViewRoot;
24  import javax.faces.context.FacesContext;
25  
26  import org.apache.myfaces.test.base.AbstractJsfTestCase;
27  
28  /**
29   * Test class for JspViewDeclarationLanguage.
30   * 
31   * @author Jakob Korherr (latest modification by $Author: jakobk $)
32   * @version $Revision: 936293 $ $Date: 2010-04-21 08:12:35 -0500 (Wed, 21 Apr 2010) $
33   */
34  public class JspViewDeclarationLanguageTest extends AbstractJsfTestCase
35  {
36  
37      private TrackingJspViewDeclarationLanguage jspVdl;
38      
39      public JspViewDeclarationLanguageTest(String name)
40      {
41          super(name);
42      }
43      
44      @Override
45      protected void setUp() throws Exception
46      {
47          super.setUp();
48          
49          jspVdl = new TrackingJspViewDeclarationLanguage();
50      }
51  
52      @Override
53      protected void tearDown() throws Exception
54      {
55          jspVdl = null;
56          
57          super.tearDown();
58      }
59  
60      /**
61       * Tests if renderView() implicitly calls buildView() if there was no call
62       * to buildView() for the given UIViewRoot yet. This is needed in order to
63       * support legacy ViewHandlers which return null on getViewDeclarationLanguage()
64       * and thus vdl.buildView() was not called yet when renderView() is invoked.
65       */
66      public void testBuildViewCalledBeforeViewRendered() 
67      {
68          try
69          {
70              jspVdl.renderView(facesContext, facesContext.getViewRoot());
71          }
72          catch (Exception e)
73          {
74              // we're not testing the real behavior here, so Exceptions may occur
75          }
76          
77          // assert that buildView() was implicitly called once (by renderView())
78          assertEquals(1, jspVdl._buildViewCalled);
79      }
80      
81      /**
82       * Tests if buildView() is not called twice if it has already been called
83       * before renderView() is invoked.
84       * This test is related to testBuildViewCalledBeforeViewRendered.
85       */
86      public void testBuildViewNotCalledTwiceInRenderView()
87      {
88          try
89          {
90              jspVdl.buildView(facesContext, facesContext.getViewRoot());
91          }
92          catch (Exception e)
93          {
94              // we're not testing the real behavior here, so Exceptions may occur
95          }
96          try
97          {
98              jspVdl.renderView(facesContext, facesContext.getViewRoot());
99          }
100         catch (Exception e)
101         {
102             // we're not testing the real behavior here, so Exceptions may occur
103         }
104         
105         // assert that buildView() was only called once
106         assertEquals(1, jspVdl._buildViewCalled);
107     }
108     
109     /**
110      * Tests if the direct/implicit calls to builView() work correctly
111      * for different views.
112      * This test is related to testBuildViewCalledBeforeViewRendered
113      * and testBuildViewNotCalledTwiceInRenderView.
114      */
115     public void testBuildViewRenderViewContractForDifferentViews()
116     {
117         UIViewRoot firstView = facesContext.getViewRoot();
118         UIViewRoot secondView = new UIViewRoot();
119         
120         try
121         {
122             jspVdl.buildView(facesContext, firstView);
123         }
124         catch (Exception e)
125         {
126             // we're not testing the real behavior here, so Exceptions may occur
127         }
128         try
129         {
130             jspVdl.renderView(facesContext, firstView);
131         }
132         catch (Exception e)
133         {
134             // we're not testing the real behavior here, so Exceptions may occur
135         }
136         try
137         {
138             jspVdl.renderView(facesContext, secondView);
139         }
140         catch (Exception e)
141         {
142             // we're not testing the real behavior here, so Exceptions may occur
143         }
144         
145         // assert that buildView() was called twice:
146         // the first time directly by jspVdl.buildView() for firstView
147         // the second time implicitly by jspVdl.renderView() for secondView
148         assertEquals(2, jspVdl._buildViewCalled);
149     }
150     
151     
152     /**
153      * Extends JspViewDeclarationLanguage to count the calls to buildView().
154      * 
155      * @author Jakob Korherr
156      */
157     private class TrackingJspViewDeclarationLanguage extends JspViewDeclarationLanguage
158     {
159 
160         private int _buildViewCalled = 0;
161         
162         @Override
163         public void buildView(FacesContext context, UIViewRoot view)
164                 throws IOException
165         {
166             _buildViewCalled++;
167             
168             super.buildView(context, view);
169         }
170         
171     }
172     
173 }