KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > el > SetValueBindingTest


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.el;
17
18 import javax.faces.el.ValueBinding;
19
20
21 /**
22  * @author Manfred Geiler (latest modification by $Author: matze $)
23  * @author Anton Koinov
24  * @version $Revision: 1.10 $ $Date: 2004/10/13 11:50:59 $
25  */

26 public class SetValueBindingTest extends ELBaseTest
27 {
28     //~ Constructors -------------------------------------------------------------------------------
29

30     public SetValueBindingTest(String JavaDoc name)
31     {
32         super(name);
33     }
34
35     //~ Methods ------------------------------------------------------------------------------------
36

37     public void testSetValue() throws Exception JavaDoc
38     {
39         ValueBinding vb;
40
41         vb = _application.createValueBinding("#{theA.theB.name}");
42         vb.setValue(_facesContext, "test");
43
44         vb = _application.createValueBinding("#{testmap.newValue}");
45         vb.setValue(_facesContext, "newValue");
46         assertSame("newValue", vb.getValue(_facesContext));
47
48         vb = _application.createValueBinding("#{ testmap [ \"o\" ] [ 'obj' ] }");
49         vb.setValue(_facesContext, "NEW_OBJECT");
50         assertSame("NEW_OBJECT", vb.getValue(_facesContext));
51
52         vb = _application.createValueBinding("#{ testmap [ 0 ] [ 0 ]}");
53         vb.setValue(_facesContext, _theA);
54         assertSame(_theA, vb.getValue(_facesContext));
55         
56         vb = _application.createValueBinding(
57             "#{testmap.list[4].list[testmap.list[4][1][0][testmap.list[4][0][1]]][0][0]}");
58         vb.setValue(_facesContext, "zzz");
59         assertSame("zzz", vb.getValue(_facesContext));
60         
61         vb = _application.createValueBinding("#{nonExistingValueBlahBlahBlah}");
62         vb.setValue(_facesContext, new Double JavaDoc(5.5));
63         assertEquals(new Double JavaDoc(5.5), vb.getValue(_facesContext));
64     }
65     
66     public void testSetRootValueDefaultScope()
67     {
68         ValueBinding vb;
69
70         // --------------------------------------------------
71
// Test setting/updating value in the default request scope
72

73         // set to a new variable
74
vb = _application.createValueBinding("#{newVar}");
75         vb.setValue(_facesContext, "test-value");
76         assertSame("test-value", vb.getValue(_facesContext));
77
78         // update existing variable
79
vb.setValue(_facesContext, "another-value");
80         assertSame("another-value", vb.getValue(_facesContext));
81
82         // make sure it was created in requestScope
83
vb = _application.createValueBinding("#{requestScope.newVar}");
84         assertSame("another-value", vb.getValue(_facesContext));
85     }
86     
87     public void testSetRootValueNonDefaultScope()
88     {
89         ValueBinding vb;
90         
91         // set to a new variable in application scope
92
vb = _application.createValueBinding("#{applicationScope.newVar1}");
93         vb.setValue(_facesContext, "test-value");
94         assertSame("test-value", vb.getValue(_facesContext));
95
96         // update existing variable without providing scope
97
vb = _application.createValueBinding("#{newVar1}");
98         vb.setValue(_facesContext, "another-value");
99         assertSame("another-value", vb.getValue(_facesContext));
100
101         // make sure it was updated in application scope
102
vb = _application.createValueBinding("#{applicationScope.newVar1}");
103         assertSame("another-value", vb.getValue(_facesContext));
104     }
105     
106     public void testSetWithCoercion()
107     {
108         ValueBinding vb;
109         
110         // set to a new variable in session scope
111
vb = _application.createValueBinding("#{sessionScope.newVar2}");
112         vb.setValue(_facesContext, new Integer JavaDoc(123));
113         assertEquals(new Integer JavaDoc(123), vb.getValue(_facesContext));
114
115         // update existing variable without providing scope
116
vb = _application.createValueBinding("#{newVar2}");
117         vb.setValue(_facesContext, new Double JavaDoc(321.123));
118         assertEquals(new Integer JavaDoc(321), vb.getValue(_facesContext));
119
120         // make sure it was updated in session scope
121
vb = _application.createValueBinding("#{sessionScope.newVar2}");
122         assertEquals(new Integer JavaDoc(321), vb.getValue(_facesContext));
123         
124         try
125         {
126             vb.setValue(_facesContext, new B());
127             assertTrue(false);
128         }
129         catch (Exception JavaDoc e)
130         {
131             // expected: error because B cannot be coerced Integer
132
}
133     }
134     
135     public void testCoercion()
136     {
137         ValueBinding vb;
138
139         // test with no coercion needed
140
vb = _application.createValueBinding("#{arrd[0]}");
141         vb.setValue(_facesContext, new Double JavaDoc(666.666));
142         assertEquals(new Double JavaDoc(666.666), vb.getValue(_facesContext));
143
144         vb = _application.createValueBinding("#{arri[0]}");
145         vb.setValue(_facesContext, new Integer JavaDoc(667));
146         assertEquals(new Integer JavaDoc(667), vb.getValue(_facesContext));
147
148         vb = _application.createValueBinding("#{arrD[0]}");
149         vb.setValue(_facesContext, new Double JavaDoc(668.666));
150         assertEquals(new Double JavaDoc(668.666), vb.getValue(_facesContext));
151
152         vb = _application.createValueBinding("#{arrI[0]}");
153         vb.setValue(_facesContext, new Integer JavaDoc(669));
154         assertEquals(new Integer JavaDoc(669), vb.getValue(_facesContext));
155
156         // test with coercion
157
vb = _application.createValueBinding("#{arrd[0]}");
158         vb.setValue(_facesContext, new Integer JavaDoc(666));
159         assertEquals(new Double JavaDoc(666), vb.getValue(_facesContext));
160
161         vb = _application.createValueBinding("#{arri[0]}");
162         vb.setValue(_facesContext, new Double JavaDoc(667.666));
163         assertEquals(new Integer JavaDoc(667), vb.getValue(_facesContext));
164
165         vb = _application.createValueBinding("#{arrD[0]}");
166         vb.setValue(_facesContext, new Integer JavaDoc(668));
167         assertEquals(new Double JavaDoc(668), vb.getValue(_facesContext));
168
169         vb = _application.createValueBinding("#{arrI[0]}");
170         vb.setValue(_facesContext, new Double JavaDoc(669.666));
171         assertEquals(new Integer JavaDoc(669), vb.getValue(_facesContext));
172     }
173     
174     public void testSetImplicitObject()
175     {
176         try
177         {
178             _application.createValueBinding("#{cookie}").setValue(_facesContext, null);
179             assertTrue(false);
180         }
181         catch (Exception JavaDoc e) {
182             // ignore, error expected
183
}
184     }
185     
186     public void testSetManagedBean()
187     {
188         ValueBinding vb;
189         
190         vb = _application.createValueBinding("#{testBean_B}");
191         try
192         {
193             vb.setValue(_facesContext, new Double JavaDoc(5.5));
194             assertTrue(false);
195         }
196         catch (Exception JavaDoc e)
197         {
198             // expected: error because Double cannot be coerced to Managed Bean B's class
199
}
200
201         // The above setValue must not have created the bean
202
vb = _application.createValueBinding("#{sessionScope.testBean_B}");
203         assertNull(vb.getValue(_facesContext));
204
205         B b = new B();
206         b.setName("differentName");
207         vb = _application.createValueBinding("#{testBean_B}");
208         vb.setValue(_facesContext, b);
209         
210         // check that the managed bean was created in the correct scope
211
vb = _application.createValueBinding("#{sessionScope.testBean_B.name}");
212         assertEquals("differentName", vb.getValue(_facesContext));
213         
214         vb = _application.createValueBinding("#{testBean_B.name}");
215         assertEquals("differentName", vb.getValue(_facesContext));
216     }
217     
218     public void testSetNullValue()
219     {
220         ValueBinding vb;
221
222         vb = _application.createValueBinding("#{testmap.o.obj}");
223         assertNotNull(vb.getValue(_facesContext));
224         vb.setValue(_facesContext, null);
225         assertNull(vb.getValue(_facesContext));
226     }
227 }
228
Popular Tags