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.flow;
20  
21  import javax.faces.context.FacesContext;
22  import javax.faces.flow.Flow;
23  import javax.faces.flow.FlowCallNode;
24  import javax.faces.flow.MethodCallNode;
25  import javax.faces.flow.Parameter;
26  import javax.faces.flow.ReturnNode;
27  import javax.faces.flow.SwitchCase;
28  import javax.faces.flow.SwitchNode;
29  import javax.faces.flow.ViewNode;
30  import javax.faces.flow.builder.FlowBuilder;
31  import javax.faces.flow.builder.SwitchCaseBuilder;
32  import org.apache.myfaces.flow.builder.FlowBuilderImpl;
33  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
34  import org.junit.Assert;
35  import org.junit.Test;
36  
37  /**
38   *
39   * @author lu4242
40   */
41  public class FlowBuilderTestCase extends AbstractJsfTestCase
42  {
43      
44      @Test
45      public void testFlowBuilderSimple()
46      {
47          FlowBuilder flowBuilder = new FlowBuilderImpl();
48          flowBuilder.id("faces-flow1.xhtml", "flow1").
49              initializer("#{bean.init}").finalizer("#{bean.destroy}");
50          
51          Flow flow = flowBuilder.getFlow();
52          Assert.assertNotNull(flow);
53          Assert.assertEquals("flow1", flow.getId());
54          Assert.assertEquals("faces-flow1.xhtml", flow.getDefiningDocumentId());
55          Assert.assertEquals("#{bean.init}", flow.getInitializer().getExpressionString());
56          Assert.assertEquals("#{bean.destroy}", flow.getFinalizer().getExpressionString());
57      }
58      
59      @Test
60      public void testFlowBuilderReturn()
61      {
62          FlowBuilder flowBuilder = new FlowBuilderImpl();
63          flowBuilder.id("faces-flow1.xhtml", "flow1");
64          flowBuilder.returnNode("returnNode").markAsStartNode().fromOutcome("mynode");
65          
66          Flow flow = flowBuilder.getFlow();
67          Assert.assertNotNull(flow);
68          Assert.assertEquals("returnNode", flow.getStartNodeId());
69          
70          ReturnNode returnNode = flow.getReturns().get("returnNode");
71          Assert.assertNotNull(returnNode);
72          Assert.assertEquals("returnNode", returnNode.getId());
73          Assert.assertEquals("mynode", returnNode.getFromOutcome(facesContext));
74      }    
75  
76      @Test
77      public void testFlowBuilderSwitch()
78      {
79          externalContext.getRequestMap().put("bean", new SimpleBean());
80          FlowBuilder flowBuilder = new FlowBuilderImpl();
81          flowBuilder.id("faces-flow1.xhtml", "flow1");
82          SwitchCaseBuilder switchCaseBuilder = flowBuilder.switchNode("switch1")
83              .markAsStartNode().defaultOutcome("#{bean.outcome1}");
84          switchCaseBuilder
85              .switchCase().condition("true").fromOutcome("case1");
86          switchCaseBuilder
87              .switchCase().condition("#{bean.checkCond}").fromOutcome("caseB");
88          switchCaseBuilder
89              .switchCase().condition(
90                  application.getExpressionFactory().createValueExpression(Boolean.TRUE, Boolean.class))
91                      .fromOutcome("caseC");
92          
93          Flow flow = flowBuilder.getFlow();
94          Assert.assertNotNull(flow);
95          Assert.assertEquals("switch1", flow.getStartNodeId());
96          
97          SwitchNode switchNode = flow.getSwitches().get("switch1");
98          Assert.assertNotNull(switchNode);
99          Assert.assertEquals("exit", switchNode.getDefaultOutcome(facesContext));
100         
101         SwitchCase scn = switchNode.getCases().get(0);
102         Assert.assertTrue(scn.getCondition(facesContext));
103         Assert.assertEquals("case1", scn.getFromOutcome());
104         
105         scn = switchNode.getCases().get(1);
106         Assert.assertTrue(scn.getCondition(facesContext));
107         Assert.assertEquals("caseB", scn.getFromOutcome());
108 
109         scn = switchNode.getCases().get(2);
110         Assert.assertTrue(scn.getCondition(facesContext));
111         Assert.assertEquals("caseC", scn.getFromOutcome());
112     }
113 
114     public static class SimpleBean {
115         public boolean isCheckCond()
116         {
117             return true;
118         }
119         public String getOutcome1()
120         {
121             return "exit";
122         }
123         public String getValue()
124         {
125             return "asdf";
126         }
127         public void check()
128         {
129         }
130         public void checkTo(FacesContext context)
131         {
132         }
133     }
134     
135     @Test
136     public void testFlowBuilderView()
137     {
138         FlowBuilder flowBuilder = new FlowBuilderImpl();
139         flowBuilder.id("faces-flow1.xhtml", "flow1");
140         flowBuilder.viewNode("x", "x.xhtml");
141         flowBuilder.viewNode("y", "y.xhtml").markAsStartNode();
142         
143         Flow flow = flowBuilder.getFlow();
144         Assert.assertNotNull(flow);
145         Assert.assertEquals("y", flow.getStartNodeId());
146         
147         ViewNode viewNode = flow.getViews().get(0);
148         Assert.assertEquals("x", viewNode.getId());
149         Assert.assertEquals("x.xhtml", viewNode.getVdlDocumentId());
150         
151         viewNode = flow.getViews().get(1);
152         Assert.assertEquals("y", viewNode.getId());
153         Assert.assertEquals("y.xhtml", viewNode.getVdlDocumentId());
154     }
155     
156     @Test
157     public void testFlowBuilderInboundParameter()
158     {
159         externalContext.getRequestMap().put("bean", new SimpleBean());
160         FlowBuilder flowBuilder = new FlowBuilderImpl();
161         flowBuilder.id("faces-flow1.xhtml", "flow1");
162         flowBuilder.inboundParameter("name1", "value1");
163         flowBuilder.inboundParameter("name2", "#{bean.value}");
164         flowBuilder.inboundParameter("name3", 
165             application.getExpressionFactory().createValueExpression("value3", String.class));
166         
167         Flow flow = flowBuilder.getFlow();
168         Assert.assertNotNull(flow);
169 
170         Parameter param = flow.getInboundParameters().get("name1");
171         Assert.assertEquals("name1", param.getName());
172         Assert.assertEquals("value1", param.getValue().getValue(facesContext.getELContext()));
173         
174         param = flow.getInboundParameters().get("name2");
175         Assert.assertEquals("name2", param.getName());
176         Assert.assertEquals("asdf", param.getValue().getValue(facesContext.getELContext()));        
177         
178         param = flow.getInboundParameters().get("name3");
179         Assert.assertEquals("name3", param.getName());
180         Assert.assertEquals("value3", param.getValue().getValue(facesContext.getELContext()));        
181     }
182 
183     @Test
184     public void testFlowBuilderMethodCall()
185     {
186         FlowBuilder flowBuilder = new FlowBuilderImpl();
187         flowBuilder.id("faces-flow1.xhtml", "flow1");
188         flowBuilder.methodCallNode("method1").expression("#{bean.check}").defaultOutcome("case1").markAsStartNode();
189         flowBuilder.methodCallNode("method2").expression("#{bean.checkTo}",
190             new Class[]{FacesContext.class}).defaultOutcome("case2");
191         
192         Flow flow = flowBuilder.getFlow();
193         Assert.assertNotNull(flow);
194         Assert.assertEquals("method1", flow.getStartNodeId());
195         
196         MethodCallNode mcn = flow.getMethodCalls().get(0);
197         Assert.assertEquals("method1", mcn.getId());
198         Assert.assertEquals("case1", mcn.getOutcome().getValue(facesContext.getELContext()));
199         Assert.assertEquals("#{bean.check}", mcn.getMethodExpression().getExpressionString());
200         
201         mcn = flow.getMethodCalls().get(1);
202         Assert.assertEquals("method2", mcn.getId());
203         Assert.assertEquals("case2", mcn.getOutcome().getValue(facesContext.getELContext()));
204         Assert.assertEquals("#{bean.checkTo}", mcn.getMethodExpression().getExpressionString());
205     }
206     
207     @Test
208     public void testFlowBuilderFlowCall()
209     {
210         FlowBuilder flowBuilder = new FlowBuilderImpl();
211         flowBuilder.id("faces-flow1.xhtml", "flow1");
212 
213         flowBuilder.flowCallNode("goToFlow2").outboundParameter("name1", "value1").
214             flowReference("faces-flow2.xhtml", "flow2").markAsStartNode();
215         
216         Flow flow = flowBuilder.getFlow();
217         Assert.assertNotNull(flow);
218         Assert.assertEquals("goToFlow2", flow.getStartNodeId());
219         
220         FlowCallNode flowCallNode = flow.getFlowCalls().get("goToFlow2");
221         Assert.assertNotNull(flowCallNode);
222         Assert.assertEquals("flow2", flowCallNode.getCalledFlowId(facesContext));
223         Assert.assertEquals("faces-flow2.xhtml", flowCallNode.getCalledFlowDocumentId(facesContext));
224         
225         Parameter param = flowCallNode.getOutboundParameters().get("name1");
226         Assert.assertEquals("name1", param.getName());
227         Assert.assertEquals("value1", param.getValue().getValue(facesContext.getELContext()));
228     }
229 
230 }