View Javadoc

1   /*
2    * $Id: ASTPropertyTest.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.ASTChain;
24  import org.apache.commons.ognl.ASTConst;
25  import org.apache.commons.ognl.ASTProperty;
26  import org.apache.commons.ognl.Ognl;
27  import org.apache.commons.ognl.OgnlContext;
28  import org.apache.commons.ognl.OgnlRuntime;
29  import org.apache.commons.ognl.SimpleNode;
30  import org.apache.commons.ognl.test.objects.BaseGeneric;
31  import org.apache.commons.ognl.test.objects.GameGeneric;
32  import org.apache.commons.ognl.test.objects.GameGenericObject;
33  import org.apache.commons.ognl.test.objects.GenericRoot;
34  import org.apache.commons.ognl.test.objects.Root;
35  
36  import java.util.List;
37  import java.util.Map;
38  
39  import static org.apache.commons.ognl.test.OgnlTestCase.isEqual;
40  
41  /**
42   * Tests functionality of {@link org.apache.commons.ognl.ASTProperty}.
43   */
44  public class ASTPropertyTest
45      extends TestCase
46  {
47  
48      public void test_Get_Indexed_Property_Type()
49          throws Exception
50      {
51          ASTProperty p = new ASTProperty( 0 );
52          p.setIndexedAccess( false );
53          ASTConst pRef = new ASTConst( 0 );
54          pRef.setValue( "nested" );
55          pRef.jjtSetParent( p );
56          p.jjtAddChild( pRef, 0 );
57  
58          Map root = new Root().getMap();
59  
60          OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
61          context.setRoot( root );
62          context.setCurrentObject( root );
63          context.setCurrentNode( pRef );
64  
65          assertEquals( root.getClass(), context.getCurrentType() );
66          assertEquals( null, context.getPreviousType() );
67          assertEquals( root, context.getCurrentObject() );
68          assertEquals( null, context.getCurrentAccessor() );
69          assertEquals( null, context.getPreviousAccessor() );
70  
71          int type = p.getIndexedPropertyType( context, root );
72  
73          assertEquals( OgnlRuntime.INDEXED_PROPERTY_NONE, type );
74          assertEquals( root.getClass(), context.getCurrentType() );
75          assertEquals( null, context.getPreviousType() );
76          assertEquals( null, context.getCurrentAccessor() );
77          assertEquals( null, context.getPreviousAccessor() );
78      }
79  
80      public void test_Get_Value_Body()
81          throws Exception
82      {
83          ASTProperty p = new ASTProperty( 0 );
84          p.setIndexedAccess( false );
85          ASTConst pRef = new ASTConst( 0 );
86          pRef.setValue( "nested" );
87          pRef.jjtSetParent( p );
88          p.jjtAddChild( pRef, 0 );
89  
90          Map root = new Root().getMap();
91  
92          OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
93          context.setRoot( root );
94          context.setCurrentObject( root );
95          context.setCurrentNode( pRef );
96  
97          assertEquals( root.getClass(), context.getCurrentType() );
98          assertEquals( null, context.getPreviousType() );
99          assertEquals( root, context.getCurrentObject() );
100         assertEquals( null, context.getCurrentAccessor() );
101         assertEquals( null, context.getPreviousAccessor() );
102 
103         Object value = p.getValue( context, root );
104 
105         assertEquals( root.get( "nested" ), value );
106         assertEquals( root.getClass(), context.getCurrentType() );
107         assertEquals( null, context.getPreviousType() );
108         assertEquals( null, context.getCurrentAccessor() );
109         assertEquals( null, context.getPreviousAccessor() );
110     }
111 
112     public void test_Get_Source()
113         throws Throwable
114     {
115         ASTProperty p = new ASTProperty( 0 );
116         p.setIndexedAccess( false );
117         ASTConst pRef = new ASTConst( 0 );
118         pRef.setValue( "nested" );
119         pRef.jjtSetParent( p );
120         p.jjtAddChild( pRef, 0 );
121 
122         Map root = new Root().getMap();
123 
124         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
125         context.setRoot( root );
126         context.setCurrentObject( root );
127         context.setCurrentNode( pRef );
128 
129         assertEquals( ".get(\"nested\")", p.toGetSourceString( context, root ) );
130         assertEquals( Object.class, context.getCurrentType() );
131         assertEquals( Map.class, context.getCurrentAccessor() );
132         assertEquals( root.getClass(), context.getPreviousType() );
133         assertEquals( null, context.getPreviousAccessor() );
134 
135         assertEquals( root.get( "nested" ), context.getCurrentObject() );
136 
137         assert Map.class.isAssignableFrom( context.getCurrentAccessor() );
138 
139         assertEquals( root.getClass(), context.getPreviousType() );
140         assertEquals( null, context.getPreviousAccessor() );
141     }
142 
143     public void test_Set_Source()
144         throws Throwable
145     {
146         ASTProperty p = new ASTProperty( 0 );
147         p.setIndexedAccess( false );
148         ASTConst pRef = new ASTConst( 0 );
149         pRef.setValue( "nested" );
150         pRef.jjtSetParent( p );
151         p.jjtAddChild( pRef, 0 );
152 
153         Map root = new Root().getMap();
154 
155         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
156         context.setRoot( root );
157         context.setCurrentObject( root );
158         context.setCurrentNode( pRef );
159 
160         assertEquals( ".put(\"nested\", $3)", p.toSetSourceString( context, root ) );
161         assertEquals( Object.class, context.getCurrentType() );
162         assertEquals( root.get( "nested" ), context.getCurrentObject() );
163 
164         assert Map.class.isAssignableFrom( context.getCurrentAccessor() );
165 
166         assertEquals( root.getClass(), context.getPreviousType() );
167         assertEquals( null, context.getPreviousAccessor() );
168     }
169 
170     public void test_Indexed_Object_Type()
171         throws Throwable
172     {
173         // ASTChain chain = new ASTChain(0);
174 
175         ASTProperty listp = new ASTProperty( 0 );
176         listp.setIndexedAccess( false );
177         // listp.jjtSetParent(chain);
178 
179         ASTConst listc = new ASTConst( 0 );
180         listc.setValue( "list" );
181         listc.jjtSetParent( listp );
182         listp.jjtAddChild( listc, 0 );
183 
184         // chain.jjtAddChild(listp, 0);
185 
186         ASTProperty p = new ASTProperty( 0 );
187         p.setIndexedAccess( true );
188 
189         ASTProperty pindex = new ASTProperty( 0 );
190 
191         ASTConst pRef = new ASTConst( 0 );
192         pRef.setValue( "genericIndex" );
193         pRef.jjtSetParent( pindex );
194         pindex.jjtAddChild( pRef, 0 );
195 
196         p.jjtAddChild( pindex, 0 );
197         // chain.jjtAddChild(p, 1);
198 
199         Root root = new Root();
200 
201         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
202         context.setRoot( root );
203         context.setCurrentObject( root );
204         context.setCurrentNode( listp );
205 
206         assertEquals( ".getList()", listp.toGetSourceString( context, root ) );
207         assertEquals( List.class, context.getCurrentType() );
208         assertEquals( Root.class, context.getCurrentAccessor() );
209         assertEquals( null, context.getPreviousAccessor() );
210         assertEquals( root.getClass(), context.getPreviousType() );
211         assertEquals( root.getList(), context.getCurrentObject() );
212 
213         // re test with chain
214 
215         context = (OgnlContext) Ognl.createDefaultContext( null );
216         context.setRoot( root );
217         context.setCurrentObject( root );
218 
219         ASTChain chain = new ASTChain( 0 );
220         listp.jjtSetParent( chain );
221         chain.jjtAddChild( listp, 0 );
222 
223         context.setCurrentNode( chain );
224 
225         assertEquals( ".getList()", chain.toGetSourceString( context, root ) );
226         assertEquals( List.class, context.getCurrentType() );
227         assertEquals( Root.class, context.getCurrentAccessor() );
228         assertEquals( null, context.getPreviousAccessor() );
229         assertEquals( Root.class, context.getPreviousType() );
230         assertEquals( root.getList(), context.getCurrentObject() );
231 
232         // test with only getIndex
233 
234         assertEquals( ".get(org.apache.commons.ognl.OgnlOps#getIntValue(((org.apache.commons.ognl.test.objects.Root)$2)..getGenericIndex().toString()))",
235                       p.toGetSourceString( context, root.getList() ) );
236         assertEquals( root.getArray(), context.getCurrentObject() );
237         assertEquals( Object.class, context.getCurrentType() );
238     }
239 
240     public void test_Complicated_List()
241         throws Exception
242     {
243         Root root = new Root();
244         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
245 
246         SimpleNode node =
247             (SimpleNode) Ognl.compileExpression( context,
248                                                  root,
249                                                  "{ new org.apache.commons.ognl.test.objects.MenuItem('Home', 'Main', "
250                                                      + "{ new org.apache.commons.ognl.test.objects.MenuItem('Help', 'Help'), "
251                                                      + "new org.apache.commons.ognl.test.objects.MenuItem('Contact', 'Contact') }), " // end
252                                                                                                                                       // first
253                                                                                                                                       // item
254                                                      + "new org.apache.commons.ognl.test.objects.MenuItem('UserList', getMessages().getMessage('menu.members')), "
255                                                      + "new org.apache.commons.ognl.test.objects.MenuItem('account/BetSlipList', getMessages().getMessage('menu.account'), "
256                                                      + "{ new org.apache.commons.ognl.test.objects.MenuItem('account/BetSlipList', 'My Bets'), "
257                                                      + "new org.apache.commons.ognl.test.objects.MenuItem('account/TransactionList', 'My Transactions') }), "
258                                                      + "new org.apache.commons.ognl.test.objects.MenuItem('About', 'About'), "
259                                                      + "new org.apache.commons.ognl.test.objects.MenuItem('admin/Admin', getMessages().getMessage('menu.admin'), "
260                                                      + "{ new org.apache.commons.ognl.test.objects.MenuItem('admin/AddEvent', 'Add event'), "
261                                                      + "new org.apache.commons.ognl.test.objects.MenuItem('admin/AddResult', 'Add result') })}" );
262 
263         assertTrue( List.class.isAssignableFrom( node.getAccessor().get( context, root ).getClass() ) );
264     }
265 
266     public void test_Set_Chain_Indexed_Property()
267         throws Exception
268     {
269         Root root = new Root();
270         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
271 
272         context.setRoot( root );
273         context.setCurrentObject( root );
274 
275         SimpleNode node = (SimpleNode) Ognl.parseExpression( "tab.searchCriteriaSelections[index1][index2]" );
276         node.setValue( context, root, Boolean.FALSE );
277     }
278 
279     public void test_Set_Generic_Property()
280         throws Exception
281     {
282         GenericRoot root = new GenericRoot();
283         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
284 
285         context.setRoot( root );
286         context.setCurrentObject( root );
287 
288         SimpleNode node = (SimpleNode) Ognl.parseExpression( "cracker.param" );
289         node.setValue( context, root, "0" );
290 
291         assertEquals( new Integer( 0 ), root.getCracker().getParam() );
292 
293         node.setValue( context, root, "10" );
294 
295         assertEquals( new Integer( 10 ), root.getCracker().getParam() );
296     }
297 
298     public void test_Get_Generic_Property()
299         throws Exception
300     {
301         GenericRoot root = new GenericRoot();
302         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
303 
304         context.setRoot( root );
305         context.setCurrentObject( root );
306 
307         SimpleNode node = (SimpleNode) Ognl.parseExpression( "cracker.param" );
308         node.setValue( context, root, "0" );
309 
310         assertEquals( new Integer( 0 ), node.getValue( context, root ) );
311 
312         node.setValue( context, root, "10" );
313 
314         assertEquals( new Integer( 10 ), node.getValue( context, root ) );
315     }
316 
317     public void test_Set_Get_Multiple_Generic_Types_Property()
318         throws Exception
319     {
320         BaseGeneric<GameGenericObject, Long> root = new GameGeneric();
321         OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
322 
323         context.setRoot( root );
324         context.setCurrentObject( root );
325 
326         SimpleNode node = (SimpleNode) Ognl.parseExpression( "ids" );
327         node.setValue( context, root, new String[] { "0", "20", "43" } );
328 
329         isEqual( new Long[] { new Long( 0 ), new Long( 20 ), new Long( 43 ) }, root.getIds() );
330         isEqual( node.getValue( context, root ), root.getIds() );
331     }
332 }