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