1 16 package org.apache.myfaces.el; 17 18 import javax.faces.el.ValueBinding; 19 20 21 26 public class SetValueBindingTest extends ELBaseTest 27 { 28 30 public SetValueBindingTest(String name) 31 { 32 super(name); 33 } 34 35 37 public void testSetValue() throws Exception 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 (5.5)); 63 assertEquals(new Double (5.5), vb.getValue(_facesContext)); 64 } 65 66 public void testSetRootValueDefaultScope() 67 { 68 ValueBinding vb; 69 70 73 vb = _application.createValueBinding("#{newVar}"); 75 vb.setValue(_facesContext, "test-value"); 76 assertSame("test-value", vb.getValue(_facesContext)); 77 78 vb.setValue(_facesContext, "another-value"); 80 assertSame("another-value", vb.getValue(_facesContext)); 81 82 vb = _application.createValueBinding("#{requestScope.newVar}"); 84 assertSame("another-value", vb.getValue(_facesContext)); 85 } 86 87 public void testSetRootValueNonDefaultScope() 88 { 89 ValueBinding vb; 90 91 vb = _application.createValueBinding("#{applicationScope.newVar1}"); 93 vb.setValue(_facesContext, "test-value"); 94 assertSame("test-value", vb.getValue(_facesContext)); 95 96 vb = _application.createValueBinding("#{newVar1}"); 98 vb.setValue(_facesContext, "another-value"); 99 assertSame("another-value", vb.getValue(_facesContext)); 100 101 vb = _application.createValueBinding("#{applicationScope.newVar1}"); 103 assertSame("another-value", vb.getValue(_facesContext)); 104 } 105 106 public void testSetWithCoercion() 107 { 108 ValueBinding vb; 109 110 vb = _application.createValueBinding("#{sessionScope.newVar2}"); 112 vb.setValue(_facesContext, new Integer (123)); 113 assertEquals(new Integer (123), vb.getValue(_facesContext)); 114 115 vb = _application.createValueBinding("#{newVar2}"); 117 vb.setValue(_facesContext, new Double (321.123)); 118 assertEquals(new Integer (321), vb.getValue(_facesContext)); 119 120 vb = _application.createValueBinding("#{sessionScope.newVar2}"); 122 assertEquals(new Integer (321), vb.getValue(_facesContext)); 123 124 try 125 { 126 vb.setValue(_facesContext, new B()); 127 assertTrue(false); 128 } 129 catch (Exception e) 130 { 131 } 133 } 134 135 public void testCoercion() 136 { 137 ValueBinding vb; 138 139 vb = _application.createValueBinding("#{arrd[0]}"); 141 vb.setValue(_facesContext, new Double (666.666)); 142 assertEquals(new Double (666.666), vb.getValue(_facesContext)); 143 144 vb = _application.createValueBinding("#{arri[0]}"); 145 vb.setValue(_facesContext, new Integer (667)); 146 assertEquals(new Integer (667), vb.getValue(_facesContext)); 147 148 vb = _application.createValueBinding("#{arrD[0]}"); 149 vb.setValue(_facesContext, new Double (668.666)); 150 assertEquals(new Double (668.666), vb.getValue(_facesContext)); 151 152 vb = _application.createValueBinding("#{arrI[0]}"); 153 vb.setValue(_facesContext, new Integer (669)); 154 assertEquals(new Integer (669), vb.getValue(_facesContext)); 155 156 vb = _application.createValueBinding("#{arrd[0]}"); 158 vb.setValue(_facesContext, new Integer (666)); 159 assertEquals(new Double (666), vb.getValue(_facesContext)); 160 161 vb = _application.createValueBinding("#{arri[0]}"); 162 vb.setValue(_facesContext, new Double (667.666)); 163 assertEquals(new Integer (667), vb.getValue(_facesContext)); 164 165 vb = _application.createValueBinding("#{arrD[0]}"); 166 vb.setValue(_facesContext, new Integer (668)); 167 assertEquals(new Double (668), vb.getValue(_facesContext)); 168 169 vb = _application.createValueBinding("#{arrI[0]}"); 170 vb.setValue(_facesContext, new Double (669.666)); 171 assertEquals(new Integer (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 e) { 182 } 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 (5.5)); 194 assertTrue(false); 195 } 196 catch (Exception e) 197 { 198 } 200 201 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 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 |