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.application.flow;
20  
21  import javax.enterprise.context.ContextNotActiveException;
22  import javax.faces.application.ConfigurableNavigationHandler;
23  import javax.faces.application.NavigationCase;
24  import javax.faces.application.StateManager;
25  import javax.faces.component.UICommand;
26  import javax.faces.flow.Flow;
27  import org.apache.myfaces.mc.test.core.AbstractMyFacesCDIRequestTestCase;
28  import org.apache.myfaces.shared.config.MyfacesConfig;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  /**
33   * This test is the same as FlowMyFacesRequestTestCase with the diference that
34   * in this case CDI is enabled and the other alternative is used.
35   */
36  public class FlowMyFacesCDIRequestTestCase extends AbstractMyFacesCDIRequestTestCase
37  {
38  
39      @Override
40      protected boolean isScanAnnotations()
41      {
42          return true;
43      }
44  
45      @Override
46      protected void setUpWebConfigParams() throws Exception
47      {
48          super.setUpWebConfigParams();
49          servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES","org.apache.myfaces.application.flow");
50          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
51          servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
52          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
53          servletContext.addInitParameter("javax.faces.CONFIG_FILES", "/WEB-INF/flow1-flow.xml");
54          servletContext.addInitParameter("javax.faces.CLIENT_WINDOW_MODE", "url");
55      }
56      
57      @Test
58      public void testFlow1_1() throws Exception
59      {
60          startViewRequest("/flow1_1.xhtml");
61          processLifecycleExecute();
62          
63          ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) facesContext.getApplication().getNavigationHandler();
64          
65          NavigationCase navCase = handler.getNavigationCase(facesContext, null, "flow1");
66          
67          Assert.assertNotNull(navCase);
68          
69          NavigationCase contentCase = handler.getNavigationCase(facesContext, null, "flow1_content");
70          
71          Assert.assertNull(contentCase);
72          
73          // Check begin view node
74          Assert.assertEquals("/flow1/begin.xhtml", navCase.getToViewId(facesContext));
75          
76          renderResponse();
77         
78          //Enter flow 1
79          UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:startFlow1");
80          client.submit(button);
81          
82          processLifecycleExecute();
83          
84          Flow currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
85          Assert.assertNotNull(currentFlow);
86          
87          // Check Flow1Bean can be created
88          Flow1Bean bean1 = facesContext.getApplication().evaluateExpressionGet(
89              facesContext, "#{flow1Bean}", Flow1Bean.class);
90          Assert.assertNotNull(bean1);
91          Assert.assertEquals(bean1.getPostConstructCalled(), "true");
92          
93          contentCase = handler.getNavigationCase(facesContext, null, "flow1_content");
94          
95          Assert.assertNotNull(contentCase);
96      }
97      
98      @Test
99      public void testFlow1_2() throws Exception
100     {
101         startViewRequest("/flow1_2.xhtml");
102         processLifecycleExecute();
103         
104         ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) facesContext.getApplication().getNavigationHandler();
105         
106         NavigationCase navCase = handler.getNavigationCase(facesContext, null, "flow1");
107         
108         Assert.assertNotNull(navCase);
109         
110         NavigationCase contentCase = handler.getNavigationCase(facesContext, null, "flow1_content");
111         
112         Assert.assertNull(contentCase);
113         
114         // Check begin view node
115         Assert.assertEquals("/flow1/begin.xhtml", navCase.getToViewId(facesContext));
116         
117         renderResponse();
118        
119         //Enter flow 1
120         UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:startFlow1");
121         client.submit(button);
122         
123         processLifecycleExecute();
124         
125         Flow currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
126         Assert.assertNotNull(currentFlow);
127         Assert.assertEquals("flow1", currentFlow.getId());
128         
129         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flow1","value1");
130         
131         // Check the bean with @FlowScoped annotation can be instantiated
132         Flow1Bean bean1 = facesContext.getApplication().evaluateExpressionGet(
133             facesContext, "#{flow1Bean}", Flow1Bean.class);
134         Assert.assertNotNull(bean1);
135         Assert.assertEquals(bean1.getPostConstructCalled(), "true");        
136         bean1.setName("John");
137         
138         renderResponse();
139         
140         UICommand button2 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:call_flow2");
141         client.submit(button2);
142         
143         processLifecycleExecute();
144         
145         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
146         Assert.assertNotNull(currentFlow);
147         Assert.assertEquals("flow2", currentFlow.getId());
148         Assert.assertFalse(facesContext.getApplication().getFlowHandler().getCurrentFlowScope().containsKey("flow1"));
149         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flow2","value2");
150         
151         Flow2Bean bean2 = facesContext.getApplication().evaluateExpressionGet(
152             facesContext, "#{flow2Bean}", Flow2Bean.class);
153         Assert.assertNotNull(bean2);
154         Assert.assertEquals(bean2.getPostConstructCalled(), "true");
155         
156         Flow1Bean bean1_1 = facesContext.getApplication().evaluateExpressionGet(
157             facesContext, "#{flow1Bean}", Flow1Bean.class);
158         Assert.assertEquals(bean1_1.getName(), "John");
159         
160         Flow21Bean bean2_1 = facesContext.getApplication().evaluateExpressionGet(
161             facesContext, "#{flow21Bean}", Flow21Bean.class);
162         Assert.assertNotNull(bean2_1);
163         Assert.assertEquals(bean2_1.getPostConstructCalled(), "true");
164         Assert.assertNotNull(bean2_1.getFlow1Bean());
165 
166         renderResponse();
167         
168         //Check current view is the begin of flow2
169         Assert.assertEquals("/flow2/begin.xhtml", facesContext.getViewRoot().getViewId());
170         
171         UICommand button3 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:content");
172         client.submit(button3);
173         processLifecycleExecute();
174         renderResponse();
175         
176         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
177         Assert.assertNotNull(currentFlow);
178         Assert.assertEquals("flow2", currentFlow.getId());
179 
180         NavigationCase endCase = handler.getNavigationCase(facesContext, null, "end");
181         Assert.assertNotNull(endCase);
182         
183         UICommand button4 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:end_flow");
184         client.submit(button4);
185         
186         processLifecycleExecute();
187         
188         // The interesting thing here is that it requires to get out from two consecutive flows, and it needs
189         // to chain all commands. The difficulty here resides in the context should be resolved properly, and
190         // there are a couple of recursive calls that needs to be solved.
191         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
192         Assert.assertNull(currentFlow);
193         Assert.assertEquals("/flow1_end.xhtml", facesContext.getViewRoot().getViewId());
194         
195         try
196         {
197             Flow1Bean bean1_2 = facesContext.getApplication().evaluateExpressionGet(
198                 facesContext, "#{flow1Bean}", Flow1Bean.class);
199             bean1_2.getName();
200             Assert.fail("Invocation show throw NullPointerException or ContextNotActiveException");
201         }
202         catch (ContextNotActiveException e)
203         {
204         }
205         catch (NullPointerException e)
206         {
207         }
208     }
209 
210     /**
211      * Check outbound parameter is initialized before call initializer method
212      * 
213      * @throws Exception 
214      */
215     @Test
216     public void testFlow1_12() throws Exception
217     {
218         startViewRequest("/flow_base.xhtml");
219         processLifecycleExecute();
220         
221         ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) facesContext.getApplication().getNavigationHandler();
222         
223         renderResponse();
224        
225         //Enter flow 1
226         UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:startFlow4");
227         client.submit(button);
228         
229         processLifecycleExecute();
230         
231         Assert.assertEquals("/flow4/flow4.xhtml", facesContext.getViewRoot().getViewId());
232         
233         Flow currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
234         Assert.assertNotNull(currentFlow);
235         Assert.assertEquals(currentFlow.getId(), "flow4");
236         
237         renderResponse();
238         
239         NavigationCase goFlowBase = handler.getNavigationCase(facesContext, null, "call_flow5_4");
240         Assert.assertNotNull(goFlowBase);
241         
242         UICommand button2 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:call_flow5");
243         client.submit(button2);
244         
245         processLifecycleExecute();
246         
247         Assert.assertEquals("/flow5/flow5.xhtml", facesContext.getViewRoot().getViewId());
248 
249         renderResponse();
250     }
251     
252     /**
253      * This tests do the following:
254      * 
255      * - Start Flow A
256      * - Call Flow B
257      * - Call Flow A
258      * - Call Flow B
259      * - Return from Flow B
260      * - Return from Flow A
261      * - Return from Flow B
262      */
263     @Test
264     public void testFlowA_1() throws Exception
265     {
266         startViewRequest("/flow_base.xhtml");
267         processLifecycleExecute();
268         
269         ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) facesContext.getApplication().getNavigationHandler();
270         
271         renderResponse();
272        
273         //Enter flow 1
274         UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:startFlowA");
275         client.submit(button);
276         
277         processLifecycleExecute();
278         
279         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowA","valueA_1");
280         Assert.assertEquals("/flowA/flowA.xhtml", facesContext.getViewRoot().getViewId());
281         
282         Flow currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
283         Assert.assertNotNull(currentFlow);
284         Assert.assertEquals(currentFlow.getId(), "flowA");
285         
286         renderResponse();
287         
288         UICommand button2 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:call_flow_B");
289         client.submit(button2);
290         
291         processLifecycleExecute();
292         
293         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
294         Assert.assertNotNull(currentFlow);
295         Assert.assertEquals("flowB", currentFlow.getId());
296         Assert.assertFalse(facesContext.getApplication().getFlowHandler().getCurrentFlowScope().containsKey("flowA"));
297         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowB","valueB_1");
298         
299         renderResponse();
300         
301         //Check current view is the begin of flow2
302         Assert.assertEquals("/flowB/flowB.xhtml", facesContext.getViewRoot().getViewId());
303         
304         UICommand button3 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:call_flow_A");
305         client.submit(button3);
306         
307         processLifecycleExecute();
308         
309         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
310         Assert.assertNotNull(currentFlow);
311         Assert.assertEquals("flowA", currentFlow.getId());
312         Assert.assertFalse(facesContext.getApplication().getFlowHandler().getCurrentFlowScope().containsKey("flowB"));
313         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowA","valueA_2");
314         
315         renderResponse();
316         
317         //Check current view is the begin of flow2
318         Assert.assertEquals("/flowA/flowA.xhtml", facesContext.getViewRoot().getViewId());
319         
320         UICommand button4 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:call_flow_B");
321         client.submit(button4);
322         
323         processLifecycleExecute();
324         
325         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
326         Assert.assertNotNull(currentFlow);
327         Assert.assertEquals("flowB", currentFlow.getId());
328         Assert.assertFalse(facesContext.getApplication().getFlowHandler().getCurrentFlowScope().containsKey("flowA"));
329         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowB","valueB_2");
330         
331         renderResponse();
332         
333         //Check current view is the begin of flow2
334         Assert.assertEquals("/flowB/flowB.xhtml", facesContext.getViewRoot().getViewId());
335         
336         UICommand button5 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:end_flow");
337         client.submit(button5);
338         
339         processLifecycleExecute();
340         
341         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
342         Assert.assertNotNull(currentFlow);
343         Assert.assertEquals("flowA", currentFlow.getId());
344         Assert.assertEquals("valueA_2", facesContext.getApplication().getFlowHandler().getCurrentFlowScope().get("flowA"));
345         
346         renderResponse();
347         
348         UICommand button6 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:end_flow");
349         client.submit(button6);
350         
351         processLifecycleExecute();
352         
353         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
354         Assert.assertNotNull(currentFlow);
355         Assert.assertEquals("flowB", currentFlow.getId());
356         Assert.assertEquals("valueB_1", facesContext.getApplication().getFlowHandler().getCurrentFlowScope().get("flowB"));
357 
358         renderResponse();
359         
360         UICommand button7 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:end_flow");
361         client.submit(button7);
362         
363         processLifecycleExecute();
364         
365         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
366         Assert.assertNotNull(currentFlow);
367         Assert.assertEquals("flowA", currentFlow.getId());
368         Assert.assertEquals("valueA_1", facesContext.getApplication().getFlowHandler().getCurrentFlowScope().get("flowA"));
369         
370         renderResponse();
371     }
372     
373     @Test
374     public void testFlowA_2() throws Exception
375     {
376         startViewRequest("/flow_base.xhtml");
377         processLifecycleExecute();
378         
379         ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) facesContext.getApplication().getNavigationHandler();
380         
381         renderResponse();
382        
383         //Enter flow A
384         UICommand button = (UICommand) facesContext.getViewRoot().findComponent("mainForm:startFlowA");
385         client.submit(button);
386         
387         processLifecycleExecute();
388         
389         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowA","valueA_1");
390         Assert.assertEquals("/flowA/flowA.xhtml", facesContext.getViewRoot().getViewId());
391         
392         Flow currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
393         Assert.assertNotNull(currentFlow);
394         Assert.assertEquals(currentFlow.getId(), "flowA");
395         
396         renderResponse();
397         
398         //Go to base
399         UICommand button2 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:go_flow_base");
400         client.submit(button2);
401         
402         processLifecycleExecute();
403         
404         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
405         Assert.assertNotNull(currentFlow);
406         // We are still on flowA, just returned to base
407         Assert.assertEquals("flowA", currentFlow.getId());
408         
409         renderResponse();
410         
411         //Enter flow B
412         UICommand button3 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:startFlowB");
413         client.submit(button3);
414         
415         processLifecycleExecute();
416         
417         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowB","valueB_1");
418         Assert.assertEquals("/flowB/flowB.xhtml", facesContext.getViewRoot().getViewId());
419         
420         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
421         Assert.assertNotNull(currentFlow);
422         Assert.assertEquals(currentFlow.getId(), "flowB");
423         
424         renderResponse();
425         
426         // Call flow A through flow call
427         UICommand button4 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:call_flow_A");
428         client.submit(button4);
429         
430         processLifecycleExecute();
431         
432         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
433         Assert.assertNotNull(currentFlow);
434         Assert.assertEquals("flowA", currentFlow.getId());
435         Assert.assertFalse(facesContext.getApplication().getFlowHandler().getCurrentFlowScope().containsKey("flowB"));
436         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowA","valueA_2");
437         
438         renderResponse();
439         
440         //Check current view is the begin of flow2
441         Assert.assertEquals("/flowA/flowA.xhtml", facesContext.getViewRoot().getViewId());
442 
443         // Call flow B through flow call
444         UICommand button5 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:call_flow_B");
445         client.submit(button5);
446         
447         processLifecycleExecute();
448         
449         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
450         Assert.assertNotNull(currentFlow);
451         Assert.assertEquals("flowB", currentFlow.getId());
452         Assert.assertFalse(facesContext.getApplication().getFlowHandler().getCurrentFlowScope().containsKey("flowA"));
453         facesContext.getApplication().getFlowHandler().getCurrentFlowScope().put("flowB","valueB_2");
454         
455         renderResponse();
456         
457         //Check current view is the begin of flow2
458         Assert.assertEquals("/flowB/flowB.xhtml", facesContext.getViewRoot().getViewId());
459         
460         UICommand button6 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:end_flow");
461         client.submit(button6);
462         
463         processLifecycleExecute();
464         
465         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
466         Assert.assertNotNull(currentFlow);
467         Assert.assertEquals("flowA", currentFlow.getId());
468         Assert.assertEquals("valueA_2", facesContext.getApplication().getFlowHandler().getCurrentFlowScope().get("flowA"));
469         
470         renderResponse();
471         
472         UICommand button7 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:end_flow");
473         client.submit(button7);
474         
475         processLifecycleExecute();
476         
477         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
478         Assert.assertNotNull(currentFlow);
479         Assert.assertEquals("flowB", currentFlow.getId());
480         Assert.assertEquals("valueB_1", facesContext.getApplication().getFlowHandler().getCurrentFlowScope().get("flowB"));
481 
482         renderResponse();
483         
484         UICommand button8 = (UICommand) facesContext.getViewRoot().findComponent("mainForm:end_flow");
485         client.submit(button8);
486         
487         processLifecycleExecute();
488         
489         currentFlow = facesContext.getApplication().getFlowHandler().getCurrentFlow(facesContext);
490         Assert.assertNotNull(currentFlow);
491         Assert.assertEquals("flowA", currentFlow.getId());
492         Assert.assertEquals("valueA_1", facesContext.getApplication().getFlowHandler().getCurrentFlowScope().get("flowA"));
493         
494         renderResponse();
495         
496     }
497 }