001    /*
002     * $Id: ASTMethodTest.java 1187867 2011-10-23 11:35:10Z 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.*;
024    import org.apache.commons.ognl.enhance.ExpressionCompiler;
025    import org.apache.commons.ognl.test.objects.Bean2;
026    import org.apache.commons.ognl.test.objects.Bean3;
027    import org.apache.commons.ognl.test.objects.Root;
028    
029    import java.util.Map;
030    
031    /**
032     * Tests {@link org.apache.commons.ognl.ASTMethod}.
033     */
034    public class ASTMethodTest
035        extends TestCase
036    {
037    
038        public void test_Context_Types()
039            throws Throwable
040        {
041            ASTMethod p = new ASTMethod( 0 );
042            p.setMethodName( "get" );
043    
044            ASTConst pRef = new ASTConst( 0 );
045            pRef.setValue( "value" );
046            p.jjtAddChild( pRef, 0 );
047    
048            Root root = new Root();
049    
050            OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
051            context.setRoot( root.getMap() );
052            context.setCurrentObject( root.getMap() );
053            context.setCurrentType( root.getMap().getClass() );
054    
055            assertEquals( p.toGetSourceString( context, root.getMap() ), ".get(\"value\")" );
056            assertEquals( context.getCurrentType(), Object.class );
057            assertEquals( root.getMap().get( "value" ), context.getCurrentObject() );
058            assert Map.class.isAssignableFrom( context.getCurrentAccessor() );
059            assert Map.class.isAssignableFrom( context.getPreviousType() );
060            assert context.getPreviousAccessor() == null;
061    
062            assertEquals( OgnlRuntime.getCompiler( context ).castExpression( context, p, ".get(\"value\")" ), ".get(\"value\")" );
063            assert context.get( ExpressionCompiler.PRE_CAST ) == null;
064    
065            // now test one context level further to see casting work properly on base object types
066    
067            ASTProperty prop = new ASTProperty( 0 );
068            ASTConst propRef = new ASTConst( 0 );
069            propRef.setValue( "bean3" );
070            prop.jjtAddChild( propRef, 0 );
071    
072            Bean2 val = (Bean2) root.getMap().get( "value" );
073    
074            assertEquals( prop.toGetSourceString( context, root.getMap().get( "value" ) ), ".getBean3()" );
075    
076            assertEquals( context.getCurrentObject(), val.getBean3() );
077            assertEquals( context.getCurrentType(), Bean3.class );
078            assertEquals( context.getCurrentAccessor(), Bean2.class );
079            assertEquals( Object.class, context.getPreviousType() );
080            assert Map.class.isAssignableFrom( context.getPreviousAccessor() );
081    
082            assertEquals( OgnlRuntime.getCompiler( context ).castExpression( context, prop, ".getBean3()" ), ").getBean3()" );
083    
084        }
085    }