}
public void testELSet() throws Exception
{
TestBean testBean = new TestBean();
InnerBean newInner, oldInner = new InnerBean();
testBean.setInner(oldInner);
ValueExpression valueExpression = null;
Object result = null;
ExternalContext extContext = getFacesContext().getExternalContext();
Map myMap = new HashMap();
TestBean myBean = new TestBean();
myMap.put("myBean", myBean);
extContext.getRequestMap().put("myMap", myMap);
//
// Set tests
//
valueExpression = this.create("myMap.myBean.one");
valueExpression.setValue(getFacesContext().getELContext(), "one");
Map map = (Map) extContext.getRequestMap().get("myMap");
assertTrue("one".equals(((TestBean) map.get("myBean")).getOne()));
myBean = new TestBean();
map.put("myBean", myBean);
extContext.getRequestMap().put("myMap", myMap);
// test that we can set null as the value
valueExpression = this.create("myMap.myBean.prop");
valueExpression.setValue(getFacesContext().getELContext(), null);
map = (Map) extContext.getRequestMap().get("myMap");
assertEquals(null, ((TestBean) map.get("myBean")).getOne());
myBean = new TestBean();
map.put("myBean", myBean);
extContext.getRequestMap().put("myMap", myMap);
valueExpression = this.create("myMap[\"myBean\"].one");
valueExpression.setValue(getFacesContext().getELContext(), "one");
map = (Map) extContext.getRequestMap().get("myMap");
assertTrue("one".equals(((TestBean) map.get("myBean")).getOne()));
myBean = new TestBean();
map.put("myBean", myBean);
extContext.getRequestMap().put("myMap", myMap);
// test that we can set the property to null
valueExpression = this.create("myMap[\"myBean\"].prop");
valueExpression.setValue(getFacesContext().getELContext(), null);
map = (Map) extContext.getRequestMap().get("myMap");
String msg = "Default Message";
if (((TestBean) map.get("myBean")).getProp() != null)
{
msg = ((TestBean) map.get("myBean")).getProp().getClass().getName();
}
assertEquals(msg, null, ((TestBean) map.get("myBean")).getProp());
myBean = new TestBean();
map.put("myBean", myBean);
extContext.getRequestMap().put("myMap", myMap);
valueExpression = this.create("myMap.myBean['one']");
valueExpression.setValue(getFacesContext().getELContext(), "one");
map = (Map) extContext.getRequestMap().get("myMap");
assertTrue("one".equals(((TestBean) map.get("myBean")).getOne()));
myBean = new TestBean();
map.put("myBean", myBean);
extContext.getRequestMap().put("myMap", myMap);
// set the prop to null
valueExpression = this.create("myMap.myBean['prop']");
valueExpression.setValue(getFacesContext().getELContext(), null);
map = (Map) extContext.getRequestMap().get("myMap");
assertEquals(null, ((TestBean) map.get("myBean")).getOne());
myBean = new TestBean();
map.put("myBean", myBean);
extContext.getRequestMap().put("myMap", myMap);
valueExpression = this.create("NonExist");
valueExpression.setValue(getFacesContext().getELContext(), "value");
result = extContext.getRequestMap().get("NonExist");
assertTrue("value".equals(result));
extContext.getRequestMap().remove("NonExist");
extContext.getSessionMap().put("Key", "oldValue");
valueExpression = this.create("Key");
valueExpression.setValue(getFacesContext().getELContext(), "newValue");
result = extContext.getSessionMap().get("Key");
assertTrue("newValue".equals(result));
extContext.getSessionMap().remove("Key");
newInner = new InnerBean();
valueExpression = this.create("TestBean.inner");
valueExpression.setValue(getFacesContext().getELContext(), newInner);
result = valueExpression.getValue(getFacesContext().getELContext());
assertTrue(result == newInner);
assertTrue(oldInner != newInner);
oldInner = newInner;
newInner = new InnerBean();
valueExpression = this.create("TestBean[\"inner\"]");
valueExpression.setValue(getFacesContext().getELContext(), newInner);
result = valueExpression.getValue(getFacesContext().getELContext());
assertTrue(result == newInner);
assertTrue(oldInner != newInner);