View Javadoc

1   /*
2    * Copyright 2004-2006 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  
17  
18  package org.apache.myfaces.config;
19  
20  import java.io.ByteArrayInputStream;
21  import java.util.Collection;
22  import java.util.List;
23  
24  import org.apache.myfaces.config.element.ManagedBean;
25  import org.apache.myfaces.config.element.NavigationRule;
26  import org.apache.myfaces.config.impl.digester.DigesterFacesConfigDispenserImpl;
27  import org.apache.myfaces.config.impl.digester.DigesterFacesConfigUnmarshallerImpl;
28  import org.apache.myfaces.config.impl.digester.elements.FacesConfig;
29  import org.apache.myfaces.test.base.AbstractJsfTestCase;
30  
31  public class FacesConfigValidatorTestCase extends AbstractJsfTestCase
32  {
33  
34      private FacesConfigDispenser dispenser;
35      private FacesConfigUnmarshaller<FacesConfig> unmarshaller;
36      
37      public FacesConfigValidatorTestCase(String name)
38      {
39          super(name);
40      }
41      
42      protected void setUp() throws Exception
43      {
44  
45          super.setUp();
46          
47          dispenser = new DigesterFacesConfigDispenserImpl();
48          unmarshaller = new DigesterFacesConfigUnmarshallerImpl(externalContext);
49          try
50          {
51              ByteArrayInputStream bais = new ByteArrayInputStream(testFacesConfig.getBytes());
52              dispenser.feed(unmarshaller.getFacesConfig(bais, null));
53          }
54          catch (Exception e)
55          {
56              e.printStackTrace();
57          }
58          
59      }
60      
61      public void testVerifyExistence(){
62          
63          Collection<ManagedBean> managedBeans = dispenser.getManagedBeans();
64          Collection<NavigationRule> navRules = dispenser.getNavigationRules();
65          
66          List<String> list = FacesConfigValidator.validate(managedBeans, navRules, externalContext);
67          
68          int expected = 3;
69          
70          assertTrue(list.size() + " should equal " + expected, list.size() == expected);
71          
72      }
73      
74      private static final String testFacesConfig =
75          "<?xml version='1.0' encoding='UTF-8'?>" +
76          "<!DOCTYPE faces-config PUBLIC " +
77              "\"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN\" " +
78              "\"http://java.sun.com/dtd/web-facesconfig_1_1.dtd\">" +
79              "<faces-config>" +
80              "<navigation-rule>" +
81              "    <from-view-id>/doesNotExist.jsp</from-view-id>" +
82              "    <navigation-case>" +
83              "        <from-outcome>doesNotMatter</from-outcome>" +
84              "        <to-view-id>/doesNotExist2.jsp</to-view-id>" +
85              "    </navigation-case>" +
86              "</navigation-rule>" +
87              "<managed-bean>" +
88              "    <managed-bean-name>exist</managed-bean-name>" +
89              "    <managed-bean-class>org.apache.myfaces.config.FacesConfigValidatorTestCase</managed-bean-class>" +
90              "    <managed-bean-scope>request</managed-bean-scope>" +
91              "</managed-bean>" +
92              "<managed-bean>" +
93              "    <managed-bean-name>nonExist</managed-bean-name>" +
94              "    <managed-bean-class>org.apache.myfaces.config.NonExist</managed-bean-class>" +
95              "    <managed-bean-scope>request</managed-bean-scope>" +
96              "</managed-bean>" +
97         "</faces-config>";
98  }