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  
20  package org.apache.myfaces.el.unified.resolver;
21  
22  import javax.el.ELResolver;
23  import javax.faces.context.FacesContext;
24  
25  import org.apache.myfaces.config.RuntimeConfig;
26  import org.apache.myfaces.config.impl.digester.elements.ManagedBean;
27  import org.apache.myfaces.el.unified.resolver.GuiceResolver;
28  import org.apache.shale.test.base.AbstractJsfTestCase;
29  
30  import com.google.inject.Guice;
31  import com.google.inject.Injector;
32  
33  public class GuiceResolverTestCase extends AbstractJsfTestCase {
34  
35      public GuiceResolverTestCase(String name) {
36          super(name);
37      }
38  
39      @Override
40      protected void setUp() throws Exception {
41          
42          super.setUp();
43          
44          // simulate a ServletContextListener
45          Injector injector = Guice.createInjector(new ShoppingModule());
46          servletContext.setAttribute(GuiceResolver.KEY, injector);
47          
48          // simulate Myfaces starting up
49          RuntimeConfig rc = RuntimeConfig.getCurrentInstance(externalContext);
50          ManagedBean bean = new ManagedBean();
51          bean.setBeanClass(ShoppingCart.class.getName());
52          bean.setScope("request");
53          rc.addManagedBean("shoppingCart", bean);
54          
55      }
56  
57      public void testResolve() {
58          
59          ELResolver resolver = new GuiceResolver();
60          
61          ShoppingCart cart = (ShoppingCart) resolver.getValue(facesContext.getELContext(), ((Object)null), ((Object)"shoppingCart"));
62          
63          assertNotNull(cart);
64          
65          assertEquals(new BulkOrder().toString(), cart.getOrder().toString());
66          
67          cart = (ShoppingCart) resolver.getValue(facesContext.getELContext(), ((Object)null), ((Object)"XXXshoppingCart"));
68          
69          assertNull(cart);
70      }
71      
72  }