2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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      // ------------------------------------------------------------ Constructors
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          // Try to load the 1.2 version of our mock FacesContext class
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              // We are not running on JSF 1.2, so go to our fallback
57              clazz = null;
58              constructor = null;
59          } catch (ClassNotFoundException e) {
60              // Same as above
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          // Fall back to the 1.1 version if we could not load the 1.2 version
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      // ----------------------------------------------------- Mock Object Methods
86  
87  
88      // ------------------------------------------------------ Instance Variables
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     // --------------------------------------------- FacesContextFactory Methods
121 
122 
123     /*** {@inheritDoc} */
124     public FacesContext getFacesContext(Object context, Object request,
125                                         Object response,
126                                         Lifecycle lifecycle) throws FacesException {
127 
128         // Select the appropriate MockExternalContext implementation class
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         // Select the constructor we wish to call
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         // Construct an appropriate MockExternalContext instance
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         // Construct an appropriate MockFacesContext instance and return it
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 }