001 /*
002 * $Id: TestExpressionCompiler.java 1230454 2012-01-12 09:35:53Z mcucchiara $
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 /**
021 *
022 */
023 package org.apache.commons.ognl.test.enhance;
024
025 import org.apache.commons.ognl.Node;
026 import org.apache.commons.ognl.Ognl;
027 import org.apache.commons.ognl.OgnlContext;
028 import org.apache.commons.ognl.enhance.ExpressionCompiler;
029 import org.apache.commons.ognl.enhance.OgnlExpressionCompiler;
030 import org.apache.commons.ognl.test.objects.Bean1;
031 import org.apache.commons.ognl.test.objects.GenericRoot;
032 import org.apache.commons.ognl.test.objects.IndexedMapObject;
033 import org.apache.commons.ognl.test.objects.Inherited;
034 import org.apache.commons.ognl.test.objects.Root;
035 import org.apache.commons.ognl.test.objects.TestInherited1;
036 import org.apache.commons.ognl.test.objects.TestInherited2;
037 import org.junit.Before;
038 import org.junit.Test;
039
040 import java.util.Collection;
041 import java.util.HashMap;
042 import java.util.Map;
043
044 import static junit.framework.Assert.*;
045
046 /**
047 * Tests functionality of {@link ExpressionCompiler}.
048 */
049 public class TestExpressionCompiler
050 {
051 OgnlExpressionCompiler _compiler;
052
053 OgnlContext _context = (OgnlContext) Ognl.createDefaultContext( null );
054
055 @Before
056 public void setUp()
057 {
058 _compiler = new ExpressionCompiler();
059 }
060
061 @Test
062 public void test_Get_Property_Access()
063 throws Throwable
064 {
065 Node expr = (Node) Ognl.parseExpression( "bean2" );
066 Bean1 root = new Bean1();
067
068 _compiler.compileExpression( _context, expr, root );
069
070 assertNotNull( expr.getAccessor().get( _context, root ) );
071 }
072
073 @Test
074 public void test_Get_Indexed_Property()
075 throws Throwable
076 {
077 Node expr = (Node) Ognl.parseExpression( "bean2.bean3.indexedValue[25]" );
078 Bean1 root = new Bean1();
079
080 assertNull( Ognl.getValue( expr, _context, root ) );
081
082 _compiler.compileExpression( _context, expr, root );
083
084 assertNull( expr.getAccessor().get( _context, root ) );
085 }
086
087 @Test
088 public void test_Set_Indexed_Property()
089 throws Throwable
090 {
091 Node expr = (Node) Ognl.parseExpression( "bean2.bean3.indexedValue[25]" );
092 Bean1 root = new Bean1();
093
094 assertNull( Ognl.getValue( expr, _context, root ) );
095
096 _compiler.compileExpression( _context, expr, root );
097
098 expr.getAccessor().set( _context, root, "test string" );
099
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 }