001    /*
002     * $Id: ASTPropertyTest.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.ASTChain;
024    import org.apache.commons.ognl.ASTConst;
025    import org.apache.commons.ognl.ASTProperty;
026    import org.apache.commons.ognl.Ognl;
027    import org.apache.commons.ognl.OgnlContext;
028    import org.apache.commons.ognl.OgnlRuntime;
029    import org.apache.commons.ognl.SimpleNode;
030    import org.apache.commons.ognl.test.objects.BaseGeneric;
031    import org.apache.commons.ognl.test.objects.GameGeneric;
032    import org.apache.commons.ognl.test.objects.GameGenericObject;
033    import org.apache.commons.ognl.test.objects.GenericRoot;
034    import org.apache.commons.ognl.test.objects.Root;
035    
036    import java.util.List;
037    import java.util.Map;
038    
039    import static org.apache.commons.ognl.test.OgnlTestCase.isEqual;
040    
041    /**
042     * Tests functionality of {@link org.apache.commons.ognl.ASTProperty}.
043     */
044    public class ASTPropertyTest
045        extends TestCase
046    {
047    
048        public void test_Get_Indexed_Property_Type()
049            throws Exception
050        {
051            ASTProperty p = new ASTProperty( 0 );
052            p.setIndexedAccess( false );
053            ASTConst pRef = new ASTConst( 0 );
054            pRef.setValue( "nested" );
055            pRef.jjtSetParent( p );
056            p.jjtAddChild( pRef, 0 );
057    
058            Map root = new Root().getMap();
059    
060            OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
061            context.setRoot( root );
062            context.setCurrentObject( root );
063            context.setCurrentNode( pRef );
064    
065            assertEquals( root.getClass(), context.getCurrentType() );
066            assertEquals( null, context.getPreviousType() );
067            assertEquals( root, context.getCurrentObject() );
068            assertEquals( null, context.getCurrentAccessor() );
069            assertEquals( null, context.getPreviousAccessor() );
070    
071            int type = p.getIndexedPropertyType( context, root );
072    
073            assertEquals( OgnlRuntime.INDEXED_PROPERTY_NONE, type );
074            assertEquals( root.getClass(), context.getCurrentType() );
075            assertEquals( null, context.getPreviousType() );
076            assertEquals( null, context.getCurrentAccessor() );
077            assertEquals( null, context.getPreviousAccessor() );
078        }
079    
080        public void test_Get_Value_Body()
081            throws Exception
082        {
083            ASTProperty p = new ASTProperty( 0 );
084            p.setIndexedAccess( false );
085            ASTConst pRef = new ASTConst( 0 );
086            pRef.setValue( "nested" );
087            pRef.jjtSetParent( p );
088            p.jjtAddChild( pRef, 0 );
089    
090            Map root = new Root().getMap();
091    
092            OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
093            context.setRoot( root );
094            context.setCurrentObject( root );
095            context.setCurrentNode( pRef );
096    
097            assertEquals( root.getClass(), context.getCurrentType() );
098            assertEquals( null, context.getPreviousType() );
099            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    }