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.event;
20  
21  import javax.faces.application.StateManager;
22  import javax.faces.component.UICommand;
23  import javax.faces.component.UIForm;
24  import javax.faces.component.UIPanel;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.event.ComponentSystemEvent;
27  import javax.faces.event.PhaseId;
28  
29  import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
30  import org.apache.myfaces.shared.util.WebConfigParamUtils;
31  import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
32  import org.easymock.EasyMock;
33  import org.easymock.IAnswer;
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  public class PostAddToViewEventTestCase extends AbstractMyFacesRequestTestCase
38  {
39      
40      
41      @Override
42      protected void setUpWebConfigParams() throws Exception
43      {
44          super.setUpWebConfigParams();
45          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
46          servletContext.addInitParameter("org.apache.myfaces.REFRESH_TRANSIENT_BUILD_ON_PSS", "true");
47          servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
48      }
49  
50      /**
51       * Test that UIViewRoot receive a PostAddToViewEvent after the view is
52       * populated.
53       * 
54       * @throws Exception
55       */
56      @Test
57      public void testPostAddToViewOnViewRoot() throws Exception
58      {
59          setupRequest("/postAddToViewEvent_1.xhtml");
60  
61          PostAddToViewEventBean bean = EasyMock.createMock(PostAddToViewEventBean.class);
62          bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
63          EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
64          {
65              public Object answer()
66              {
67                  ComponentSystemEvent e = (ComponentSystemEvent) EasyMock
68                          .getCurrentArguments()[0];
69                  Assert.assertTrue(e.getComponent() instanceof UIViewRoot);
70                  Assert.assertEquals(PhaseId.RENDER_RESPONSE, facesContext.getCurrentPhaseId());
71                  return null;
72              }
73          }).once();
74          EasyMock.replay(bean);
75          //Put on request map
76          request.setAttribute("postAddToViewEventBean", bean);
77          processLifecycleExecuteAndRender();
78          EasyMock.verify(bean);
79          
80          UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
81          submit(button);
82          
83          bean = EasyMock.createMock(PostAddToViewEventBean.class);
84          bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
85          // With PSS PostAddToViewEvent is called again on restore view phase
86          // but when building initial state.
87          if (WebConfigParamUtils.getBooleanInitParameter(externalContext, StateManager.PARTIAL_STATE_SAVING_PARAM_NAME))
88          {
89              EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
90              {
91                  public Object answer()
92                  {
93                      ComponentSystemEvent e = (ComponentSystemEvent) EasyMock
94                              .getCurrentArguments()[0];
95                      Assert.assertTrue(e.getComponent() instanceof UIViewRoot);
96                      Assert.assertEquals(PhaseId.RESTORE_VIEW, facesContext.getCurrentPhaseId());
97                      Assert.assertTrue(facesContext.getAttributes().containsKey("javax.faces.IS_BUILDING_INITIAL_STATE"));
98                      Assert.assertFalse(FaceletViewDeclarationLanguage.isRefreshingTransientBuild(facesContext));
99                      return null;
100                 }
101             }).once();
102         }
103         else
104         {
105             // Without PSS the listener should not be called, because it was already
106             // called on the first request
107             EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
108             {
109                 public Object answer()
110                 {
111                     Assert.fail();
112                     return null;
113                 }
114             }).anyTimes();
115         }
116         
117         EasyMock.replay(bean);
118         //Put on request map
119         request.setAttribute("postAddToViewEventBean", bean);
120         processLifecycleExecute();
121         EasyMock.verify(bean);
122     }
123 
124     public static interface PostAddToViewEventBean
125     {
126         public void invokePostAddToViewEvent(ComponentSystemEvent e);
127     }
128     
129     /**
130      * Test a component receive PostAddToViewEvent
131      * 
132      * @throws Exception
133      */
134     @Test
135     public void testPostAddToViewOnComponent() throws Exception
136     {
137         setupRequest("/postAddToViewEvent_2.xhtml");
138 
139         PostAddToViewEventBean bean = EasyMock.createMock(PostAddToViewEventBean.class);
140         bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
141         EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
142         {
143             public Object answer()
144             {
145                 ComponentSystemEvent e = (ComponentSystemEvent) EasyMock
146                         .getCurrentArguments()[0];
147                 Assert.assertTrue(e.getComponent() instanceof UIForm);
148                 Assert.assertEquals(PhaseId.RENDER_RESPONSE, facesContext.getCurrentPhaseId());
149                 return null;
150             }
151         }).once();
152         EasyMock.replay(bean);
153         //Put on request map
154         request.setAttribute("postAddToViewEventBean", bean);
155         processLifecycleExecuteAndRender();
156         EasyMock.verify(bean);
157         
158         UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
159         submit(button);
160         
161         bean = EasyMock.createMock(PostAddToViewEventBean.class);
162         bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
163         // With PSS PostAddToViewEvent is called again on restore view phase
164         // but when building initial state.
165         if (WebConfigParamUtils.getBooleanInitParameter(externalContext, StateManager.PARTIAL_STATE_SAVING_PARAM_NAME))
166         {
167             EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
168             {
169                 public Object answer()
170                 {
171                     ComponentSystemEvent e = (ComponentSystemEvent) EasyMock
172                             .getCurrentArguments()[0];
173                     Assert.assertTrue(e.getComponent() instanceof UIForm);
174                     Assert.assertEquals(PhaseId.RESTORE_VIEW, facesContext.getCurrentPhaseId());
175                     Assert.assertTrue(facesContext.getAttributes().containsKey("javax.faces.IS_BUILDING_INITIAL_STATE"));
176                     Assert.assertFalse(FaceletViewDeclarationLanguage.isRefreshingTransientBuild(facesContext));
177                     return null;
178                 }
179             }).once();
180         }
181         else
182         {
183             // Without PSS the listener should not be called, because it was already
184             // called on the first request
185             EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
186             {
187                 public Object answer()
188                 {
189                     Assert.fail();
190                     return null;
191                 }
192             }).anyTimes();
193         }
194         
195         EasyMock.replay(bean);
196         //Put on request map
197         request.setAttribute("postAddToViewEventBean", bean);
198         processLifecycleExecute();
199         EasyMock.verify(bean);
200     }
201 
202     /**
203      * Test that a component added dynamically should receive PostAddToViewEvent
204      * 
205      * @throws Exception
206      */
207     @Test
208     public void testPostAddToViewOnComponentCif() throws Exception
209     {
210         setupRequest("/postAddToViewEvent_3.xhtml");
211 
212         PostAddToViewEventBean bean = EasyMock.createMock(PostAddToViewEventBean.class);
213         bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
214         EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
215         {
216             public Object answer()
217             {
218                 Assert.fail();
219                 return null;
220             }
221         }).anyTimes();
222         EasyMock.replay(bean);
223         //Put on request map
224         request.setAttribute("postAddToViewEventBean", bean);
225         request.setAttribute("condition", Boolean.FALSE);
226         processLifecycleExecuteAndRender();
227         EasyMock.verify(bean);
228         
229         UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
230         submit(button);
231         
232         bean = EasyMock.createMock(PostAddToViewEventBean.class);
233         bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
234         EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
235         {
236             public Object answer()
237             {
238                 Assert.fail();
239                 return null;
240             }
241         }).anyTimes();
242         
243         EasyMock.replay(bean);
244         //Put on request map
245         request.setAttribute("postAddToViewEventBean", bean);
246         processLifecycleExecute();
247         EasyMock.verify(bean);
248         
249         bean = EasyMock.createMock(PostAddToViewEventBean.class);
250         bean.invokePostAddToViewEvent(EasyMock.isA(ComponentSystemEvent.class));
251         EasyMock.expectLastCall().andAnswer(new IAnswer<Object>()
252         {
253             public Object answer()
254             {
255                 ComponentSystemEvent e = (ComponentSystemEvent) EasyMock
256                         .getCurrentArguments()[0];
257                 Assert.assertTrue(e.getComponent() instanceof UIPanel);
258                 Assert.assertEquals(PhaseId.RENDER_RESPONSE, facesContext.getCurrentPhaseId());
259                 return null;
260             }
261         }).once();
262         EasyMock.replay(bean);
263         
264         request.setAttribute("postAddToViewEventBean", bean);
265         request.setAttribute("condition", Boolean.TRUE);
266         processRender();
267         EasyMock.verify(bean);
268     }
269 }