View Javadoc

1   /*
2    * $Id: TestExpressionCompiler.java 1230454 2012-01-12 09:35:53Z mcucchiara $
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  /**
21   *
22   */
23  package org.apache.commons.ognl.test.enhance;
24  
25  import org.apache.commons.ognl.Node;
26  import org.apache.commons.ognl.Ognl;
27  import org.apache.commons.ognl.OgnlContext;
28  import org.apache.commons.ognl.enhance.ExpressionCompiler;
29  import org.apache.commons.ognl.enhance.OgnlExpressionCompiler;
30  import org.apache.commons.ognl.test.objects.Bean1;
31  import org.apache.commons.ognl.test.objects.GenericRoot;
32  import org.apache.commons.ognl.test.objects.IndexedMapObject;
33  import org.apache.commons.ognl.test.objects.Inherited;
34  import org.apache.commons.ognl.test.objects.Root;
35  import org.apache.commons.ognl.test.objects.TestInherited1;
36  import org.apache.commons.ognl.test.objects.TestInherited2;
37  import org.junit.Before;
38  import org.junit.Test;
39  
40  import java.util.Collection;
41  import java.util.HashMap;
42  import java.util.Map;
43  
44  import static junit.framework.Assert.*;
45  
46  /**
47   * Tests functionality of {@link ExpressionCompiler}.
48   */
49  public class TestExpressionCompiler
50  {
51      OgnlExpressionCompiler _compiler;
52  
53      OgnlContext _context = (OgnlContext) Ognl.createDefaultContext( null );
54  
55      @Before
56      public void setUp()
57      {
58          _compiler = new ExpressionCompiler();
59      }
60  
61      @Test
62      public void test_Get_Property_Access()
63          throws Throwable
64      {
65          Node expr = (Node) Ognl.parseExpression( "bean2" );
66          Bean1 root = new Bean1();
67  
68          _compiler.compileExpression( _context, expr, root );
69  
70          assertNotNull( expr.getAccessor().get( _context, root ) );
71      }
72  
73      @Test
74      public void test_Get_Indexed_Property()
75          throws Throwable
76      {
77          Node expr = (Node) Ognl.parseExpression( "bean2.bean3.indexedValue[25]" );
78          Bean1 root = new Bean1();
79  
80          assertNull( Ognl.getValue( expr, _context, root ) );
81  
82          _compiler.compileExpression( _context, expr, root );
83  
84          assertNull( expr.getAccessor().get( _context, root ) );
85      }
86  
87      @Test
88      public void test_Set_Indexed_Property()
89          throws Throwable
90      {
91          Node expr = (Node) Ognl.parseExpression( "bean2.bean3.indexedValue[25]" );
92          Bean1 root = new Bean1();
93  
94          assertNull( Ognl.getValue( expr, _context, root ) );
95  
96          _compiler.compileExpression( _context, expr, root );
97  
98          expr.getAccessor().set( _context, root, "test string" );
99  
100         assertEquals( "test string", expr.getAccessor().get( _context, root ) );
101     }
102 
103     @Test
104     public void test_Expression()
105         throws Throwable
106     {
107         Node expr = (Node) Ognl.parseExpression( "bean2.bean3.value <= 24" );
108         Bean1 root = new Bean1();
109 
110         assertEquals( Boolean.FALSE, Ognl.getValue( expr, _context, root ) );
111 
112         _compiler.compileExpression( _context, expr, root );
113 
114         assertEquals( Boolean.FALSE, expr.getAccessor().get( _context, root ) );
115     }
116 
117     @Test
118     public void test_Get_Context_Property()
119         throws Throwable
120     {
121         _context.put( "key", "foo" );
122         Node expr = (Node) Ognl.parseExpression( "bean2.bean3.map[#key]" );
123         Bean1 root = new Bean1();
124 
125         assertEquals( "bar", Ognl.getValue( expr, _context, root ) );
126 
127         _compiler.compileExpression( _context, expr, root );
128 
129         assertEquals( "bar", expr.getAccessor().get( _context, root ) );
130 
131         _context.put( "key", "bar" );
132 
133         assertEquals( "baz", Ognl.getValue( expr, _context, root ) );
134         assertEquals( "baz", expr.getAccessor().get( _context, root ) );
135     }
136 
137     @Test
138     public void test_Set_Context_Property()
139         throws Throwable
140     {
141         _context.put( "key", "foo" );
142         Node expr = (Node) Ognl.parseExpression( "bean2.bean3.map[#key]" );
143         Bean1 root = new Bean1();
144 
145         _compiler.compileExpression( _context, expr, root );
146 
147         assertEquals( "bar", expr.getAccessor().get( _context, root ) );
148 
149         _context.put( "key", "bar" );
150         assertEquals( "baz", expr.getAccessor().get( _context, root ) );
151 
152         expr.getAccessor().set( _context, root, "bam" );
153         assertEquals( "bam", expr.getAccessor().get( _context, root ) );
154     }
155 
156     @Test
157     public void test_Property_Index()
158         throws Throwable
159     {
160         Root root = new Root();
161         Node expr = Ognl.compileExpression( _context, root, "{index + 1}" );
162 
163         Object ret = expr.getAccessor().get( _context, root );
164 
165         assertTrue( Collection.class.isInstance( ret ) );
166     }
167 
168     @Test
169     public void test_Root_Expression_Inheritance()
170         throws Throwable
171     {
172         Inherited obj1 = new TestInherited1();
173         Inherited obj2 = new TestInherited2();
174 
175         Node expr = Ognl.compileExpression( _context, obj1, "myString" );
176 
177         assertEquals( expr.getAccessor().get( _context, obj1 ), "inherited1" );
178         assertEquals( expr.getAccessor().get( _context, obj2 ), "inherited2" );
179     }
180 
181     @Test
182     public void test_Create_Empty_Collection()
183         throws Throwable
184     {
185         Node expr = Ognl.compileExpression( _context, null, "{}" );
186 
187         Object ret = expr.getAccessor().get( _context, null );
188 
189         assertNotNull( ret );
190         assertTrue( Collection.class.isAssignableFrom( ret.getClass() ) );
191     }
192 
193     public String getKey()
194     {
195         return "key";
196     }
197 
198     @Test
199     public void test_Indexed_Property()
200         throws Throwable
201     {
202         Map<String,String> map = new HashMap<String,String>();
203         map.put( "key", "value" );
204 
205         Node expression = Ognl.compileExpression( _context, this, "key" );
206         assertEquals( "key", expression.getAccessor().get( _context, this ) );
207     }
208 
209     IndexedMapObject mapObject = new IndexedMapObject( "propertyValue" );
210 
211     public IndexedMapObject getObject()
212     {
213         return mapObject;
214     }
215 
216     public String getPropertyKey()
217     {
218         return "property";
219     }
220 
221     @Test
222     public void test_Indexed_Map_Property()
223         throws Throwable
224     {
225         assertEquals( "propertyValue", Ognl.getValue( "object[propertyKey]", this ) );
226 
227         _context.clear();
228         Node expression = Ognl.compileExpression( _context, this, "object[#this.propertyKey]" );
229         assertEquals( "propertyValue", expression.getAccessor().get( _context, this ) );
230 
231         _context.clear();
232         expression = Ognl.compileExpression( _context, this, "object[propertyKey]" );
233         assertEquals( "propertyValue", expression.getAccessor().get( _context, this ) );
234     }
235 
236     @Test
237     public void test_Set_Generic_Property()
238         throws Exception
239     {
240         _context.clear();
241 
242         GenericRoot root = new GenericRoot();
243 
244         Node node = Ognl.compileExpression( _context, root, "cracker.param" );
245         assertEquals( null, node.getAccessor().get( _context, root ) );
246 
247         node.getAccessor().set( _context, root, 0 );
248         assertEquals( 0, node.getAccessor().get( _context, root ) );
249 
250         node.getAccessor().set( _context, root, 12 );
251         assertEquals( 12, node.getAccessor().get( _context, root ) );
252     }
253 }