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.config;
20  
21  /**
22   * Creates an environment to easily test the creation and initialization of
23   * managed beans.
24   * 
25   * @author Dennis C. Byrne
26   */
27  
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.apache.myfaces.config.impl.digester.elements.ListEntries;
34  import org.apache.myfaces.config.impl.digester.elements.ManagedBean;
35  import org.apache.myfaces.config.impl.digester.elements.ManagedProperty;
36  import org.apache.myfaces.config.impl.digester.elements.MapEntries;
37  import org.apache.myfaces.config.impl.digester.elements.ListEntries.Entry;
38  import org.apache.myfaces.test.base.AbstractJsfTestCase;
39  
40  public abstract class AbstractManagedBeanBuilderTestCase extends AbstractJsfTestCase {
41  
42      public AbstractManagedBeanBuilderTestCase(String name) {
43          super(name);
44      }
45  
46      protected MangedBeanExample example;
47      
48      // managed property values
49      protected static final List<String> MANAGED_LIST = new ArrayList<String>();
50      protected static final Map<String, String> MANAGED_MAP = new HashMap<String, String>();
51      protected static final String INJECTED_VALUE = "tatiana";
52      
53      /**
54       * Skips digester and manually builds and configures a managed bean.
55       */
56      
57      protected void setUp() throws Exception
58    {
59          super.setUp();
60          ManagedBeanBuilder managedBeanBuilder = new ManagedBeanBuilder();
61          ManagedBean managedBean = new ManagedBean();
62          
63          managedBean.setBeanClass(MangedBeanExample.class.getName());
64          managedBean.setName("managed");
65          managedBean.setScope("request");
66          
67          // test methods of children will want to make sure these values come 
68          // out on the other end of this.
69          MANAGED_LIST.add("0");
70          MANAGED_LIST.add("1");
71          MANAGED_LIST.add("2");
72          MANAGED_MAP.put("0", "0");
73          MANAGED_MAP.put("1", "1");
74          MANAGED_MAP.put("2", "2");
75          
76          ManagedProperty managedProperty = new ManagedProperty();
77          managedProperty.setPropertyName("managedProperty");
78          managedProperty.setValue(INJECTED_VALUE);
79          
80          ManagedProperty managedList = new ManagedProperty();
81          managedList.setPropertyName("managedList");
82          ListEntries listEntries = makeListEntries();
83          managedList.setListEntries(listEntries);
84          
85          ManagedProperty writeOnlyList = new ManagedProperty();
86          writeOnlyList.setPropertyName("writeOnlyList");
87          ListEntries writeOnlyListEntries = makeListEntries();
88          writeOnlyList.setListEntries(writeOnlyListEntries);
89          
90          ManagedProperty managedMap = new ManagedProperty();
91          managedMap.setPropertyName("managedMap");
92          MapEntries mapEntries = makeMapEntries();
93          managedMap.setMapEntries(mapEntries);
94          
95          ManagedProperty writeOnlyMap = new ManagedProperty();
96          writeOnlyMap.setPropertyName("writeOnlyMap");
97          MapEntries writeOnlyMapEntries = makeMapEntries();
98          writeOnlyMap.setMapEntries(writeOnlyMapEntries);        
99          
100         managedBean.addProperty(managedProperty);
101         managedBean.addProperty(managedList);
102         managedBean.addProperty(writeOnlyList);
103         managedBean.addProperty(managedMap);
104         managedBean.addProperty(writeOnlyMap);
105 
106         // simulate a managed bean creation
107         example = (MangedBeanExample) managedBeanBuilder
108             .buildManagedBean(facesContext, managedBean);
109     }
110     
111     public void tearDown() throws Exception{
112         super.tearDown();
113         example = null;
114         MANAGED_LIST.clear();
115         MANAGED_MAP.clear();
116     }
117     
118     private ListEntries makeListEntries(){
119         ListEntries listEntries = new ListEntries();
120         
121         for(int i = 0; i < MANAGED_LIST.size(); i++){
122             Entry entry = new Entry();
123             entry.setValue((String) MANAGED_LIST.get(i));
124             listEntries.addEntry(entry);
125         }
126         return listEntries;
127     }
128     
129     private MapEntries makeMapEntries(){
130         MapEntries mapEntries = new MapEntries();
131         
132         for(int i = 0 ; i < MANAGED_MAP.size(); i++){
133             MapEntries.Entry mapEntry = new MapEntries.Entry();
134             mapEntry.setKey((String) MANAGED_MAP.get(i + ""));
135             mapEntry.setValue((String) MANAGED_MAP.get(i + ""));
136             mapEntries.addEntry(mapEntry);
137         }
138         return mapEntries;
139     }
140     
141 }