001 /* 002 * $Id: PropertySetterTest.java 1103095 2011-05-14 13:18:29Z simonetripodi $ 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 package org.apache.commons.ognl.test; 021 022 import junit.framework.TestCase; 023 import org.apache.commons.ognl.Node; 024 import org.apache.commons.ognl.Ognl; 025 import org.apache.commons.ognl.OgnlContext; 026 027 import java.util.Map; 028 029 /** 030 * Tests being able to set property on object with interface that doesn't define setter. See OGNL-115. 031 */ 032 public class PropertySetterTest 033 extends TestCase 034 { 035 036 private Map map; 037 038 private TestObject testObject = new TestObject( "propertyValue" ); 039 040 private String propertyKey = "property"; 041 042 public interface TestInterface 043 { 044 public String getProperty(); 045 } 046 047 public class TestObject 048 implements TestInterface 049 { 050 051 private String property; 052 053 private Integer integerProperty = 1; 054 055 public TestObject( String property ) 056 { 057 this.property = property; 058 } 059 060 public String getProperty() 061 { 062 return property; 063 } 064 065 public void setProperty( String property ) 066 { 067 this.property = property; 068 } 069 070 public Integer getIntegerProperty() 071 { 072 return integerProperty; 073 } 074 } 075 076 public Map getMap() 077 { 078 return map; 079 } 080 081 public String getKey() 082 { 083 return "key"; 084 } 085 086 public TestObject getObject() 087 { 088 return testObject; 089 } 090 091 public TestInterface getInterfaceObject() 092 { 093 return testObject; 094 } 095 096 public String getPropertyKey() 097 { 098 return propertyKey; 099 } 100 101 public void testEnhancedOgnl() 102 throws Exception 103 { 104 OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null ); 105 Node expression = Ognl.compileExpression( context, this, "interfaceObject.property" ); 106 Ognl.setValue( expression, context, this, "hello" ); 107 assertEquals( "hello", getObject().getProperty() ); 108 109 // Fails if an interface is defined, but succeeds if not 110 context.clear(); 111 112 expression = Ognl.compileExpression( context, this.getObject(), "property" ); 113 Ognl.setValue( expression, context, this.getObject(), "hello" ); 114 assertEquals( "hello", getObject().getProperty() ); 115 } 116 }