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