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.webapp;
20  
21  import static org.easymock.EasyMock.anyObject;
22  import static org.easymock.EasyMock.eq;
23  import static org.easymock.EasyMock.expect;
24  import static org.easymock.EasyMock.expectLastCall;
25  import static org.easymock.EasyMock.isA;
26  
27  import javax.el.ExpressionFactory;
28  import javax.faces.webapp.FacesServlet;
29  import javax.servlet.ServletContext;
30  import javax.servlet.jsp.JspApplicationContext;
31  import javax.servlet.jsp.JspFactory;
32  
33  import org.apache.myfaces.config.RuntimeConfig;
34  import org.apache.myfaces.el.DefaultPropertyResolver;
35  import org.apache.myfaces.el.VariableResolverImpl;
36  import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver;
37  import org.apache.myfaces.test.base.AbstractJsfTestCase;
38  import org.easymock.IAnswer;
39  import org.easymock.classextension.EasyMock;
40  import org.easymock.classextension.IMocksControl;
41  
42  /**
43   * @author Mathias Broekelmann (latest modification by $Author: bommel $)
44   * @version $Revision: 1187701 $ $Date: 2011-10-22 07:21:54 -0500 (Sat, 22 Oct 2011) $
45   */
46  public class Jsp21FacesInitializerTest extends AbstractJsfTestCase
47  {
48  
49      public Jsp21FacesInitializerTest(String name)
50      {
51          super(name);
52      }
53  
54      /**
55       * Test method for {@link org.apache.myfaces.webapp.DefaultFacesInitializer#initFaces(javax.servlet.ServletContext)}.
56       * @throws Exception 
57       */
58      public void testInitFaces() throws Exception
59      {
60          // TODO adapt this test case for MyFaces 2.0, because currently it checks nothing!
61          
62          Jsp21FacesInitializer initializer = new Jsp21FacesInitializer();
63          IMocksControl control = EasyMock.createControl();
64          
65          JspFactory jspFactory = control.createMock(JspFactory.class);
66          initializer.setJspFactory(jspFactory);
67          
68          RuntimeConfig runtimeConfig = control.createMock(RuntimeConfig.class);
69          
70          ServletContext context = control.createMock(ServletContext.class);
71          ExpressionFactory expressionFactory = control.createMock(ExpressionFactory.class);
72          runtimeConfig.setExpressionFactory(expressionFactory);
73          runtimeConfig.setPropertyResolverChainHead(isA(DefaultPropertyResolver.class));
74          runtimeConfig.setVariableResolverChainHead(isA(VariableResolverImpl.class));
75          
76          expect(context.getAttribute(eq(RuntimeConfig.class.getName()))).andReturn(runtimeConfig).anyTimes();
77          
78          expect(context.getInitParameter(eq(FacesServlet.CONFIG_FILES_ATTR))).andReturn(null);
79          expect(context.getResourceAsStream(eq("/WEB-INF/faces-config.xml"))).andReturn(null);
80          expect(context.getInitParameter(eq(FacesServlet.LIFECYCLE_ID_ATTR))).andReturn(null);
81  
82          // TODO: add myfaces specific tests
83          expect(context.getResource(isA(String.class))).andReturn(null);
84          expect(context.getResourceAsStream(isA(String.class))).andReturn(null);
85          expect(context.getInitParameter(isA(String.class))).andReturn(null).anyTimes();
86          expect(context.getAttribute(isA(String.class))).andReturn(null).anyTimes();
87          context.setAttribute(isA(String.class), anyObject());
88          expectLastCall().anyTimes();
89          expect(context.getRealPath(isA(String.class))).andAnswer(new IAnswer<String>() {
90              public String answer() throws Throwable
91              {
92                  return (String) org.easymock.EasyMock.getCurrentArguments()[0];
93              }
94          });
95          
96          JspApplicationContext jspAppCtx = control.createMock(JspApplicationContext.class);
97          expect(jspAppCtx.getExpressionFactory()).andReturn(expressionFactory);
98          jspAppCtx.addELContextListener(isA(FacesELContextListener.class));
99          expect(jspFactory.getJspApplicationContext(eq(context))).andReturn(jspAppCtx);
100         jspAppCtx.addELResolver(isA(FacesCompositeELResolver.class));
101 
102         control.replay();
103         initializer.initFaces(context);
104 
105         // In MYFACES-1222: The Jsp21FacesInitializer isn't practicable anymore.
106         // The ServletContext-Mock won't return its WebXml instance, so Jsp21FacesInitializer will stop initializing.
107         // This is why the next line is commented:
108         
109         //control.verify();
110     }
111 
112 }