2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.tiger.faces;
19  
20  import java.io.File;
21  import javax.servlet.ServletContextEvent;
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  import org.apache.shale.test.base.AbstractJsfTestCase;
25  import org.apache.shale.tiger.config.FacesConfigConfig;
26  import org.apache.shale.tiger.config.TestBean;
27  import org.apache.shale.tiger.view.faces.LifecycleListener2;
28  import org.apache.shale.view.faces.LifecycleListener;
29  
30  /***
31   * <p>Test case for <code>org.apache.shale.tiger.faces.VariableResolverImpl</code>.</p>
32   */
33  public class VariableResolverImplTestCase extends AbstractJsfTestCase {
34      
35  
36      // ------------------------------------------------------------ Constructors
37  
38  
39      // Construct a new instance of this test case
40      public VariableResolverImplTestCase(String name) {
41          super(name);
42      }
43      
44  
45      // ---------------------------------------------------- Overall Test Methods
46  
47  
48      // Set up instance variables required by this test case.
49      protected void setUp() throws Exception {
50  
51          // Set up mock web application environment
52          super.setUp();
53          servletContext.addInitParameter("javax.faces.CONFIG_FILES",
54                                          "/WEB-INF/test-config-0.xml," +
55                                          "/WEB-INF/test-config-1.xml," +
56                                          "/WEB-INF/test-config-2.xml," +
57                                          "/WEB-INF/test-config-3.xml");
58          File root = new File(System.getProperty("basedir") + "/target/test-webapp");
59          servletContext.setDocumentRoot(root);
60  
61          // Process our configuration information
62          listener = new LifecycleListener2();
63          listener.contextInitialized(new ServletContextEvent(servletContext));
64  
65          // Create resolver instance to be tested
66          // (Force NPEs on delegation use cases by default)
67          resolver = new VariableResolverImpl(null);
68  
69      }
70  
71  
72      // Return the tests included in this test case.
73      public static Test suite() {
74          return new TestSuite(VariableResolverImplTestCase.class);
75      }
76  
77  
78      // Tear down instance variables required by this test case
79      protected void tearDown() throws Exception {
80  
81          // Release tested instances
82          resolver = null;
83  
84          // Finalize our context listener
85          listener.contextDestroyed(new ServletContextEvent(servletContext));
86          listener = null;
87  
88          // Tear down the mock web application environment
89          super.tearDown();
90  
91      }
92  
93  
94      // ------------------------------------------------------ Instance Variables
95  
96  
97      // LifecycleListener instance to be tested
98      LifecycleListener listener = null;
99  
100     // VariableResolverImpl instance to be tested
101     VariableResolverImpl resolver = null;
102 
103 
104     // ------------------------------------------------------------ Test Methods
105 
106 
107     // Test creating "bean0"
108     public void testBean0() {
109 
110         TestBean bean = (TestBean) resolver.resolveVariable(facesContext, "bean0");
111         assertNotNull(bean);
112         assertEquals((byte) 1, bean.getByteProperty());
113         assertEquals('a', bean.getCharProperty());
114         assertEquals((double) 2.0, bean.getDoubleProperty());
115         assertEquals((float) 3.0, bean.getFloatProperty());
116         assertEquals(4, bean.getIntProperty());
117         assertEquals((long) 5, bean.getLongProperty());
118         assertEquals((short) 6, bean.getShortProperty());
119         assertNull(bean.getStringProperty()); // Overridden in test-config-1.xml
120 
121     }
122 
123 
124     // Test creating "bean1"
125     public void testBean1() {
126 
127         TestBean bean = (TestBean) resolver.resolveVariable(facesContext, "bean1");
128         assertNotNull(bean);
129         assertEquals((byte) 11, bean.getByteProperty()); // Configured
130         assertEquals('a', bean.getCharProperty()); // Defaulted
131         assertEquals((double) 222.0, bean.getDoubleProperty()); // Configured and overridden
132         assertEquals((float) 3.0, bean.getFloatProperty()); // Defaulted
133         assertEquals(44, bean.getIntProperty()); // Configured
134         assertEquals((long) 5, bean.getLongProperty()); // Defaulted
135         assertEquals((short) 6, bean.getShortProperty()); // Defaulted
136         assertEquals("String", bean.getStringProperty()); // Defaulted
137 
138     }
139 
140 
141     // Test creating "bean2"
142     public void testBean2() {
143 
144         TestBean bean = (TestBean) resolver.resolveVariable(facesContext, "bean2");
145         assertNotNull(bean);
146         assertEquals((byte) -1, bean.getByteProperty()); // Annotated
147         assertEquals('z', bean.getCharProperty()); // Annotated
148         assertEquals((double) -2.0, bean.getDoubleProperty()); // Annotated
149         assertEquals((float) -3.0, bean.getFloatProperty()); // Annotated
150         assertEquals(-4, bean.getIntProperty()); // Annotated
151         assertEquals((long) -5, bean.getLongProperty()); // Annotated
152         assertEquals((short) -6, bean.getShortProperty()); // Annotated
153         assertEquals("Override The Annotation", bean.getStringProperty()); // Annotated and overridden
154 
155     }
156 
157 
158     // Test creating "bean3"
159     public void testBean3() {
160 
161         // We need to hook into the rest of the expression evaluation framework,
162         // since we will be evaluating expressions to initialize property values
163         resolver = new VariableResolverImpl(application.getVariableResolver());
164         application.setVariableResolver(resolver);
165 
166         // The configuration metadata for "bean3" sets *all* of the properties
167         // of the new bean to match the corresponding property values for "bean1".
168         // Therefore, all of the assertions below should match the corresponding
169         // assertions from testBean1().
170         TestBean bean = (TestBean) resolver.resolveVariable(facesContext, "bean3");
171         assertNotNull(bean);
172         assertEquals((byte) 11, bean.getByteProperty()); // Configured
173         assertEquals('a', bean.getCharProperty()); // Defaulted
174         assertEquals((double) 222.0, bean.getDoubleProperty()); // Configured and overridden
175         assertEquals((float) 3.0, bean.getFloatProperty()); // Defaulted
176         assertEquals(44, bean.getIntProperty()); // Configured
177         assertEquals((long) 5, bean.getLongProperty()); // Defaulted
178         assertEquals((short) 6, bean.getShortProperty()); // Defaulted
179         assertEquals("String", bean.getStringProperty()); // Defaulted
180 
181     }
182 
183 
184     // Test creating existing bean name (delegating)
185     public void testExistingDelegating() {
186 
187         resolver = new VariableResolverImpl(application.getVariableResolver());
188         application.setVariableResolver(resolver);
189         externalContext.getRequestMap().put("existing", "This is an existing object");
190         Object instance = resolver.resolveVariable(facesContext, "existing");
191         assertNotNull(instance);
192         assertTrue(instance instanceof String);
193         assertEquals("This is an existing object", instance);
194 
195     }
196 
197 
198     // Test creating existing bean name (plain)
199     public void testExistingPlain() {
200 
201         externalContext.getRequestMap().put("existing", "This is an existing object");
202         Object instance = null;
203         try {
204             instance = resolver.resolveVariable(facesContext, "existing");
205             fail("Should have thrown NullPointerException");
206         } catch (NullPointerException e) {
207             ; // Expected result
208         }
209 
210     }
211 
212 
213     // Test pristine instance of the resolver
214     public void testPristine() {
215 
216         assertNotNull(resolver);
217         assertNotNull(application.getVariableResolver());
218         FacesConfigConfig config = (FacesConfigConfig)
219             externalContext.getApplicationMap().
220             get(LifecycleListener2.FACES_CONFIG_CONFIG);
221         assertNotNull(config);
222         assertEquals(9, config.getManagedBeans().size());
223 
224     }
225 
226 
227     // Test creating unknown bean name (delegating)
228     public void testUnknownDelegating() {
229 
230         resolver = new VariableResolverImpl(application.getVariableResolver());
231         application.setVariableResolver(resolver);
232         Object instance = resolver.resolveVariable(facesContext, "unknown");
233         assertNull(instance);
234 
235     }
236 
237 
238     // Test creating unknown bean name (plain)
239     public void testUnknownPlain() {
240 
241         Object instance = null;
242         try {
243             instance = resolver.resolveVariable(facesContext, "unknown");
244             fail("Should have thrown NullPointerException");
245         } catch (NullPointerException e) {
246             ; // Expected result
247         }
248 
249     }
250 
251 
252 }