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.el;
19  
20  import javax.el.ELContext;
21  import javax.el.PropertyNotWritableException;
22  import javax.el.ValueExpression;
23  import javax.faces.context.FacesContext;
24  
25  /***
26   * <p>Mock implementation of <code>ValueExpression</code> that wraps a variable.</p>
27   */
28  public class MockVariableValueExpression extends ValueExpression {
29      
30  
31      // ------------------------------------------------------------ Constructors
32  
33  
34      /***
35       * Serial version UID.
36       */
37      private static final long serialVersionUID = 4475919948345298291L;
38  
39  
40      /***
41       * <p>Construct a new expression for the specified instance.</p>
42       *
43       * @param instance Variable instance to be wrapped
44       * @param expectedType Expected type of the result
45       */
46      public MockVariableValueExpression(Object instance, Class expectedType) {
47  
48          if (instance == null) {
49              throw new NullPointerException("Instance cannot be null");
50          }
51          this.instance = instance;
52          this.expectedType = expectedType;
53  
54      }
55  
56  
57      // ------------------------------------------------------ Instance Variables
58  
59  
60      /***
61       * <p>The expected result type for <code>getValue()</code> calls.</p>
62       */
63      private Class expectedType = null;
64  
65  
66      /***
67       * <p>The variable instance being wrapped by this expression.</p>
68       */
69      private Object instance = null;
70  
71  
72      // ------------------------------------------------------ Expression Methods
73  
74  
75      /***
76       * <p>Return <code>true</code> if this expression is equal to the
77       * specified expression.</p>
78       *
79       * @param obj Object to be compared
80       */
81      public boolean equals(Object obj) {
82  
83          if ((obj != null) & (obj instanceof ValueExpression)) {
84              return instance.toString().equals(((ValueExpression) obj).getExpressionString());
85          } else {
86              return false;
87          }
88  
89      }
90  
91  
92      /***
93       * <p>Return the original String used to create this expression,
94       * unmodified.</p>
95       */
96      public String getExpressionString() {
97  
98          return this.instance.toString();
99  
100     }
101 
102 
103     /***
104      * <p>Return the hash code for this expression.</p>
105      */
106     public int hashCode() {
107 
108         return this.instance.toString().hashCode();
109 
110     }
111 
112 
113     /***
114      * <p>Return <code>true</code> if the expression string for this expression
115      * contains only literal text.</p>
116      */
117     public boolean isLiteralText() {
118 
119         return true;
120 
121     }
122 
123 
124     // ------------------------------------------------- ValueExpression Methods
125 
126 
127     /***
128      * <p>Return the type that the result of this expression will
129      * be coerced to.</p>
130      */
131     public Class getExpectedType() {
132 
133         return this.expectedType;
134 
135     }
136 
137 
138     /***
139      * <p>Evaluate this expression relative to the specified context,
140      * and return the most general type that is acceptable for the
141      * value passed in a <code>setValue()</code> call.</p>
142      *
143      * @param context ELContext for this evaluation
144      */
145     public Class getType(ELContext context) {
146 
147         if (context == null) {
148             throw new NullPointerException();
149         }
150         return this.instance.getClass();
151 
152     }
153 
154 
155     /***
156      * <p>Evaluate this expression relative to the specified context,
157      * and return the result.</p>
158      *
159      * @param context ELContext for this evaluation
160      */
161     public Object getValue(ELContext context) {
162 
163         if (context == null) {
164             throw new NullPointerException();
165         }
166         FacesContext fcontext = (FacesContext) context.getContext(FacesContext.class);
167         return fcontext.getApplication().getExpressionFactory().coerceToType(instance, expectedType);
168 
169     }
170 
171 
172     /***
173      * <p>Evaluate this expression relative to the specified context,
174      * and return <code>true</code> if a call to <code>setValue()</code>
175      * will always fail.</p>
176      *
177      * @param context ELContext for this evaluation
178      */
179     public boolean isReadOnly(ELContext context) {
180 
181         if (context == null) {
182             throw new NullPointerException();
183         }
184         return true;
185 
186     }
187 
188 
189 
190     /***
191      * <p>Evaluate this expression relative to the specified context,
192      * and set the result to the specified value.</p>
193      *
194      * @param context ELContext for this evaluation
195      * @param value Value to which the result should be set
196      */
197     public void setValue(ELContext context, Object value) {
198 
199         if (context == null) {
200             throw new NullPointerException();
201         }
202 
203         throw new PropertyNotWritableException();
204 
205     }
206 
207 
208 }