View Javadoc

1   /*
2    * $Id: PropertySetterTest.java 1103095 2011-05-14 13:18:29Z simonetripodi $
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  package org.apache.commons.ognl.test;
21  
22  import junit.framework.TestCase;
23  import org.apache.commons.ognl.Node;
24  import org.apache.commons.ognl.Ognl;
25  import org.apache.commons.ognl.OgnlContext;
26  
27  import java.util.Map;
28  
29  /**
30   * Tests being able to set property on object with interface that doesn't define setter. See OGNL-115.
31   */
32  public class PropertySetterTest
33      extends TestCase
34  {
35  
36      private Map map;
37  
38      private TestObject testObject = new TestObject( "propertyValue" );
39  
40      private String propertyKey = "property";
41  
42      public interface TestInterface
43      {
44          public String getProperty();
45      }
46  
47      public class TestObject
48          implements TestInterface
49      {
50  
51          private String property;
52  
53          private Integer integerProperty = 1;
54  
55          public TestObject( String property )
56          {
57              this.property = property;
58          }
59  
60          public String getProperty()
61          {
62              return property;
63          }
64  
65          public void setProperty( String property )
66          {
67              this.property = property;
68          }
69  
70          public Integer getIntegerProperty()
71          {
72              return integerProperty;
73          }
74      }
75  
76      public Map getMap()
77      {
78          return map;
79      }
80  
81      public String getKey()
82      {
83          return "key";
84      }
85  
86      public TestObject getObject()
87      {
88          return testObject;
89      }
90  
91      public TestInterface getInterfaceObject()
92      {
93          return testObject;
94      }
95  
96      public String getPropertyKey()
97      {
98          return propertyKey;
99      }
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 }