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.mc.test.core;
20  
21  import javax.faces.FacesException;
22  import javax.faces.context.ExternalContext;
23  import javax.servlet.ServletContext;
24  import org.apache.myfaces.shared.util.ClassUtils;
25  import org.apache.myfaces.spi.InjectionProvider;
26  import org.apache.myfaces.spi.InjectionProviderException;
27  import org.apache.myfaces.spi.InjectionProviderFactory;
28  import org.apache.myfaces.spi.impl.CDIAnnotationDelegateInjectionProvider;
29  import org.apache.myfaces.webapp.AbstractFacesInitializer;
30  
31  /**
32   *
33   */
34  public class AbstractMyFacesCDIRequestTestCase extends AbstractMyFacesRequestTestCase
35  {
36      
37      //protected WebBeansConfigurationListener owbListener;
38      private Object owbListener;
39      protected InjectionProvider injectionProvider;
40      
41      @Override
42      protected void setUpWebConfigParams() throws Exception
43      {
44          super.setUpWebConfigParams();
45          servletContext.addInitParameter("org.apache.myfaces.spi.InjectionProvider", 
46              CDIAnnotationDelegateInjectionProvider.class.getName());
47     }
48      
49      @Override
50      protected void setUpServletListeners() throws Exception
51      {
52          Class listenerClass = ClassUtils.classForName("org.apache.webbeans.servlet.WebBeansConfigurationListener");
53          if (listenerClass == null)
54          {
55              listenerClass = ClassUtils.classForName("org.jboss.weld.environment.servlet.Listener");
56          }
57          if (listenerClass != null)
58          {
59              owbListener = ClassUtils.newInstance(listenerClass);
60              webContainer.subscribeListener(owbListener);
61          }
62          super.setUpServletListeners();
63      }
64  
65      @Override
66      protected void tearDownServletListeners() throws Exception
67      {
68          super.tearDownServletListeners();
69          owbListener = null;
70      }
71  
72      @Override
73      protected AbstractFacesInitializer createFacesInitializer()
74      {
75          return new CDIJUnitFacesInitializer(this);
76      }
77      
78      protected class CDIJUnitFacesInitializer extends AbstractMyFacesTestCase.JUnitFacesInitializer
79      {
80          private Object testCaseCreationMetadata;
81  
82          public CDIJUnitFacesInitializer(AbstractMyFacesTestCase testCase)
83          {
84              super(testCase);
85          }
86  
87          @Override
88          protected void initContainerIntegration(ServletContext servletContext, ExternalContext externalContext)
89          {
90              super.initContainerIntegration(servletContext, externalContext);
91              
92              InjectionProviderFactory ipf = InjectionProviderFactory.getInjectionProviderFactory();
93              injectionProvider = ipf.getInjectionProvider(externalContext);
94              AbstractMyFacesTestCase testCase = getTestCase();
95              try
96              {
97                  testCaseCreationMetadata = injectionProvider.inject(testCase);
98                  injectionProvider.postConstruct(testCase, testCaseCreationMetadata);
99              }
100             catch (InjectionProviderException ex)
101             {
102                 throw new FacesException("Cannot inject JUnit Test case", ex);
103             }
104         }
105 
106         @Override
107         public void destroyFaces(ServletContext servletContext)
108         {
109             try
110             {
111                 injectionProvider.preDestroy(getTestCase(), testCaseCreationMetadata);
112             }
113             catch (InjectionProviderException ex)
114             {
115                 throw new FacesException("Cannot call @PreDestroy over inject JUnit Test case", ex);
116             }
117             super.destroyFaces(servletContext);
118         }
119         
120         
121     }
122 }