2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.test.mock;
19
20 import java.lang.reflect.Constructor;
21 import javax.faces.FacesException;
22 import javax.faces.context.ExternalContext;
23 import javax.faces.context.FacesContext;
24 import javax.faces.context.FacesContextFactory;
25 import javax.faces.lifecycle.Lifecycle;
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 /***
31 * <p>Mock implementation of <code>FacesContextFactory</code>.</p>
32 *
33 * $Id$
34 */
35
36 public class MockFacesContextFactory extends FacesContextFactory {
37
38
39
40
41
42 /***
43 * <p>Look up the constructor we will use for creating <code>MockFacesContext</code>
44 * instances.</p>
45 */
46 public MockFacesContextFactory() {
47
48 Class clazz = null;
49
50
51 try {
52 clazz = this.getClass().getClassLoader().loadClass("org.apache.shale.test.mock.MockFacesContext12");
53 constructor = clazz.getConstructor(facesContextSignature);
54 jsf12 = true;
55 } catch (NoClassDefFoundError e) {
56
57 clazz = null;
58 constructor = null;
59 } catch (ClassNotFoundException e) {
60
61 clazz = null;
62 constructor = null;
63 } catch (RuntimeException e) {
64 throw e;
65 } catch (Exception e) {
66 throw new FacesException(e);
67 }
68
69
70 try {
71 if (clazz == null) {
72 clazz = this.getClass().getClassLoader().loadClass("org.apache.shale.test.mock.MockFacesContext");
73 constructor = clazz.getConstructor(facesContextSignature);
74 jsf12 = false;
75 }
76 } catch (RuntimeException e) {
77 throw e;
78 } catch (Exception e) {
79 throw new FacesException(e);
80 }
81
82 }
83
84
85
86
87
88
89
90
91 /***
92 * <p>The constructor for creating a <code>FacesContext</code> instance,
93 * taking an <code>ExternalContext</code> and <code>Lifecycle</code>.</p>
94 */
95 private Constructor constructor = null;
96
97
98 /***
99 * <p>The parameter signature of the ExternalContext constructor we wish to call.</p>
100 */
101 private static Class[] externalContextSignature = new Class[] {
102 ServletContext.class, HttpServletRequest.class, HttpServletResponse.class
103 };
104
105
106 /***
107 * <p>The parameter signature of the FacesContext constructor we wish to call.</p>
108 */
109 private static Class[] facesContextSignature = new Class[] {
110 ExternalContext.class, Lifecycle.class
111 };
112
113
114 /***
115 * <p>Flag indicating that we are running in a JSF 1.2 environment.</p>
116 */
117 private boolean jsf12 = false;
118
119
120
121
122
123 /*** {@inheritDoc} */
124 public FacesContext getFacesContext(Object context, Object request,
125 Object response,
126 Lifecycle lifecycle) throws FacesException {
127
128
129 Class clazz = MockExternalContext.class;
130 if (jsf12) {
131 try {
132 clazz = this.getClass().getClassLoader().loadClass
133 ("org.apache.shale.test.mock.MockExternalContext12");
134 } catch (RuntimeException e) {
135 throw e;
136 } catch (Exception e) {
137 throw new FacesException(e);
138 }
139 }
140
141
142 Constructor mecConstructor = null;
143 try {
144 mecConstructor = clazz.getConstructor(externalContextSignature);
145 } catch (RuntimeException e) {
146 throw e;
147 } catch (Exception e) {
148 throw new FacesException(e);
149 }
150
151
152 MockExternalContext externalContext = null;
153 try {
154 externalContext = (MockExternalContext) mecConstructor.newInstance
155 (new Object[] { context, request, response });
156 } catch (RuntimeException e) {
157 throw e;
158 } catch (Exception e) {
159 throw new FacesException(e);
160 }
161
162
163 try {
164 return (MockFacesContext)
165 constructor.newInstance(new Object[] { externalContext, lifecycle });
166 } catch (RuntimeException e) {
167 throw e;
168 } catch (Exception e) {
169 throw new FacesException(e);
170 }
171
172 }
173
174
175 }