View Javadoc

1   /*
2    * Copyright 2007 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.myfaces.config.impl.digister;
17  
18  import java.util.List;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.myfaces.config.impl.digester.DigesterFacesConfigUnmarshallerImpl;
23  import org.apache.myfaces.config.element.Application;
24  import org.apache.myfaces.config.element.ContractMapping;
25  import org.apache.myfaces.config.element.FacesConfig;
26  import org.apache.myfaces.config.element.FacesConfigNameSlot;
27  import org.apache.myfaces.config.element.FacesFlowCall;
28  import org.apache.myfaces.config.element.FacesFlowDefinition;
29  import org.apache.myfaces.config.element.FacesFlowMethodCall;
30  import org.apache.myfaces.config.element.FacesFlowParameter;
31  import org.apache.myfaces.config.element.FacesFlowReturn;
32  import org.apache.myfaces.config.element.FacesFlowSwitch;
33  import org.apache.myfaces.config.element.FacesFlowView;
34  import org.apache.myfaces.config.element.LocaleConfig;
35  import org.apache.myfaces.config.element.NavigationCase;
36  import org.apache.myfaces.config.element.NavigationRule;
37  import org.apache.myfaces.config.element.OrderSlot;
38  
39  /**
40   * @author Mathias Broekelmann (latest modification by $Author$)
41   * @version $Revision$ $Date$
42   */
43  public class DigesterFacesConfigUnmarshallerImplTest extends TestCase
44  {
45      private DigesterFacesConfigUnmarshallerImpl _impl;
46  
47      protected void setUp() throws Exception
48      {
49          _impl = new DigesterFacesConfigUnmarshallerImpl(null);
50      }
51  
52      public void testEmptyConfig() throws Exception
53      {
54          FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
55                  "empty-config.xml"), "empty-config.xml");
56          assertNotNull(cfg);
57          assertTrue(cfg.getApplications().isEmpty());
58          assertTrue(cfg.getComponents().isEmpty());
59          assertTrue(cfg.getConverters().isEmpty());
60          assertTrue(cfg.getFactories().isEmpty());
61          assertTrue(cfg.getLifecyclePhaseListener().isEmpty());
62          assertTrue(cfg.getManagedBeans().isEmpty());
63          assertTrue(cfg.getNavigationRules().isEmpty());
64          assertTrue(cfg.getRenderKits().isEmpty());
65          assertTrue(cfg.getValidators().isEmpty());
66      }
67  
68      public void testApplicationConfig() throws Exception
69      {
70          FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
71                  "application-config.xml"), "application-config.xml");
72          assertNotNull(cfg);
73          assertEquals(1, cfg.getApplications().size());
74          Application app = cfg.getApplications().get(0);
75          assertEquals(2, app.getActionListener().size());
76          assertEquals("action-listener1", app.getActionListener().get(0));
77          assertEquals("action-listener2", app.getActionListener().get(1));
78          assertEquals(1, app.getDefaultRenderkitId().size());
79          assertEquals("default-render-kit-id", app.getDefaultRenderkitId()
80                  .get(0));
81          assertLocaleConfig(app.getLocaleConfig());
82          assertEquals(1, app.getMessageBundle().size());
83          assertEquals("message-bundle", app.getMessageBundle().get(0));
84          assertEquals(1, app.getNavigationHandler().size());
85          assertEquals("navigation-handler", app.getNavigationHandler().get(0));
86          assertEquals(1, app.getPropertyResolver().size());
87          assertEquals("property-resolver", app.getPropertyResolver().get(0));
88  
89          assertEquals(1, app.getStateManager().size());
90          assertEquals("state-manager", app.getStateManager().get(0));
91  
92          assertEquals(1, app.getVariableResolver().size());
93          assertEquals("variable-resolver", app.getVariableResolver().get(0));
94  
95          assertEquals(1, app.getViewHandler().size());
96          assertEquals("view-handler", app.getViewHandler().get(0));
97  
98          assertEquals(1, app.getElResolver().size());
99          assertEquals("el-resolver", app.getElResolver().get(0));
100 
101         assertEquals(1, app.getResourceBundle().size());
102         assertEquals("base-name", app.getResourceBundle().get(0).getBaseName());
103         assertEquals("var", app.getResourceBundle().get(0).getVar());
104     }
105 
106     /**
107      * @param localeConfig
108      */
109     private void assertLocaleConfig(List<LocaleConfig> localeConfig)
110     {
111         assertEquals(1, localeConfig.size());
112         LocaleConfig cfg = localeConfig.get(0);
113         assertEquals("aa", cfg.getDefaultLocale());
114         assertEquals(2, cfg.getSupportedLocales().size());
115         assertEquals("aa", cfg.getSupportedLocales().get(0));
116         assertEquals("bb", cfg.getSupportedLocales().get(1));
117     }
118     
119     public void testAbsoluteOrderingConfig() throws Exception
120     {
121         FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
122                 "absolute-ordering-config.xml"), "absolute-ordering-config.xml");
123         assertNotNull(cfg);
124         assertEquals("true", cfg.getMetadataComplete());
125         assertEquals("a",cfg.getName());
126 
127         List<OrderSlot> orderList = cfg.getAbsoluteOrdering().getOrderList();
128         
129         assertEquals("b", ((FacesConfigNameSlot) orderList.get(0)).getName());
130         assertEquals("c", ((FacesConfigNameSlot) orderList.get(1)).getName());
131         assertEquals(org.apache.myfaces.config.impl.digester.elements.ConfigOthersSlotImpl.class, orderList.get(2).getClass());
132         assertEquals("d", ((FacesConfigNameSlot) orderList.get(3)).getName());
133         
134         assertTrue(cfg.getApplications().isEmpty());
135         assertTrue(cfg.getComponents().isEmpty());
136         assertTrue(cfg.getConverters().isEmpty());
137         assertTrue(cfg.getFactories().isEmpty());
138         assertTrue(cfg.getLifecyclePhaseListener().isEmpty());
139         assertTrue(cfg.getManagedBeans().isEmpty());
140         assertTrue(cfg.getNavigationRules().isEmpty());
141         assertTrue(cfg.getRenderKits().isEmpty());
142         assertTrue(cfg.getValidators().isEmpty());
143     }
144     
145     public void testOrderingConfig() throws Exception
146     {
147         FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
148                 "ordering-config.xml"), "ordering-config.xml");
149         assertNotNull(cfg);
150         assertEquals("a",cfg.getName());
151 
152         List<OrderSlot> orderList = cfg.getOrdering().getBeforeList();        
153         assertEquals("b", ((FacesConfigNameSlot) orderList.get(0)).getName());
154         assertEquals("c", ((FacesConfigNameSlot) orderList.get(1)).getName());
155         assertEquals(org.apache.myfaces.config.impl.digester.elements.ConfigOthersSlotImpl.class, orderList.get(2).getClass());
156         
157         orderList = cfg.getOrdering().getAfterList();        
158         assertEquals("d", ((FacesConfigNameSlot) orderList.get(0)).getName());
159         
160         assertTrue(cfg.getApplications().isEmpty());
161         assertTrue(cfg.getComponents().isEmpty());
162         assertTrue(cfg.getConverters().isEmpty());
163         assertTrue(cfg.getFactories().isEmpty());
164         assertTrue(cfg.getLifecyclePhaseListener().isEmpty());
165         assertTrue(cfg.getManagedBeans().isEmpty());
166         assertTrue(cfg.getNavigationRules().isEmpty());
167         assertTrue(cfg.getRenderKits().isEmpty());
168         assertTrue(cfg.getValidators().isEmpty());
169     }
170     
171     public void testFacesFlowConfig() throws Exception
172     {/*
173         FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
174                 "faces-flow.xml"), "faces-flow.xml");
175 
176         assertNotNull(cfg);
177         assertEquals(1, cfg.getFacesFlowDefinitions().size());
178         FacesFlowDefinition facesFlowDefinition = cfg.getFacesFlowDefinitions().get(0);
179         
180         assertEquals("flow1", facesFlowDefinition.getId());
181         assertEquals("node1", facesFlowDefinition.getStartNode());
182         assertEquals("#{flowBean.init}", facesFlowDefinition.getInitializer());
183         assertEquals("#{flowBean.finalize}", facesFlowDefinition.getFinalizer());
184         
185         //view
186         assertEquals(1, facesFlowDefinition.getViewList().size());
187         FacesFlowView facesFlowView = facesFlowDefinition.getViewList().get(0);
188         assertEquals("outcome2", facesFlowView.getId());
189         assertEquals("outcome-to-2.xhtml", facesFlowView.getVdlDocument());
190 
191         //switch
192         assertEquals(1, facesFlowDefinition.getSwitchList().size());
193         FacesFlowSwitch facesFlowSwitch = facesFlowDefinition.getSwitchList().get(0);
194         assertEquals("switch1", facesFlowSwitch.getId());
195         assertEquals("outcome2", facesFlowSwitch.getDefaultOutcome().getFromOutcome());
196         NavigationCase swNavigationCase = facesFlowSwitch.getNavigationCaseList().get(0);
197         assertEquals("#{flowBean.token > 0}", swNavigationCase.getIf());
198         assertEquals("outcome2", swNavigationCase.getFromOutcome());
199 
200         //flow return
201         assertEquals(1, facesFlowDefinition.getReturnList().size());
202         FacesFlowReturn facesFlowReturn = facesFlowDefinition.getReturnList().get(0);
203         assertEquals("flowReturn1", facesFlowReturn.getId());
204         assertEquals("/outcome1", facesFlowReturn.getNavigationCase().getFromOutcome());
205         
206         //navigation rule
207         assertEquals(1, facesFlowDefinition.getNavigationRuleList().size());
208         NavigationRule navigationRule = facesFlowDefinition.getNavigationRuleList().get(0);
209         assertEquals("/x.xhtml", navigationRule.getFromViewId());
210         assertEquals(1, navigationRule.getNavigationCases().size());
211         NavigationCase navigationCase = navigationRule.getNavigationCases().get(0);
212         assertEquals("go", navigationCase.getFromOutcome());
213         assertEquals("#{test.true}", navigationCase.getIf());
214         assertEquals("/y.xhtml", navigationCase.getToViewId());
215 
216         //flow call
217         assertEquals(1, facesFlowDefinition.getFlowCallList().size());
218         FacesFlowCall facesFlowCall = facesFlowDefinition.getFlowCallList().get(0);
219         assertEquals("flowCall", facesFlowCall.getId());
220         assertEquals("flow2", facesFlowCall.getFlowReference().getFlowId());
221         assertEquals(1, facesFlowCall.getOutboundParameterList().size());
222         FacesFlowParameter facesFlowOutboundParameter = facesFlowCall.getOutboundParameterList().get(0);
223         assertEquals("name1", facesFlowOutboundParameter.getName());
224         assertEquals("value1", facesFlowOutboundParameter.getValue());
225         
226         //method call
227         assertEquals(1, facesFlowDefinition.getMethodCallList().size());
228         FacesFlowMethodCall facesFlowMethodCall = facesFlowDefinition.getMethodCallList().get(0);
229         assertEquals("method1", facesFlowMethodCall.getId());
230         assertEquals("#{flowBean.doSomething}", facesFlowMethodCall.getMethod());
231         assertEquals("outcome2", facesFlowMethodCall.getDefaultOutcome());
232         
233         //inbound param
234         assertEquals(1, facesFlowDefinition.getInboundParameterList().size());
235         FacesFlowParameter facesFlowParameter = facesFlowDefinition.getInboundParameterList().get(0);
236         assertEquals("name1", facesFlowParameter.getName());
237         assertEquals("value1", facesFlowParameter.getValue());*/
238     }
239     
240     public void testCsrf() throws Exception
241     {
242         FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
243                 "csrf-and-contracts.xml"), "csrf-and-contracts.xml");
244         
245         assertNotNull(cfg);
246         assertEquals(2, cfg.getProtectedViewsUrlPatternList().size());
247         assertEquals("/files/*.xhtml", cfg.getProtectedViewsUrlPatternList().get(0));
248         assertEquals("/files2/*.xhtml", cfg.getProtectedViewsUrlPatternList().get(1));
249         
250         
251     }
252     
253     public void testContracts() throws Exception
254     {
255         FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
256                 "csrf-and-contracts.xml"), "csrf-and-contracts.xml");
257         
258         assertNotNull(cfg);
259 
260         Application app = cfg.getApplications().get(0);
261         assertNotNull(app);
262         assertEquals(1, app.getResourceLibraryContractMappings().size());
263         
264         ContractMapping mapping = app.getResourceLibraryContractMappings().get(0);
265         assertEquals("/files/*.xhtml", mapping.getUrlPatternList().get(0));
266         assertEquals("contractA contractB", mapping.getContractList().get(0));
267     }
268     
269     public void testContracts2() throws Exception
270     {
271         FacesConfig cfg = _impl.getFacesConfig(getClass().getResourceAsStream(
272                 "contracts2.xml"), "contracts2.xml");
273         
274         assertNotNull(cfg);
275 
276         Application app = cfg.getApplications().get(0);
277         assertNotNull(app);
278         assertEquals(1, app.getResourceLibraryContractMappings().size());
279         
280         ContractMapping mapping = app.getResourceLibraryContractMappings().get(0);
281         assertTrue(mapping.getUrlPatternList().contains("/files/*.xhtml"));
282         assertTrue(mapping.getUrlPatternList().contains("/files2/*.xhtml"));
283         assertTrue(mapping.getContractList().contains("contractA"));
284         assertTrue(mapping.getContractList().contains("contractB"));
285     }    
286 }