View Javadoc

1   /*
2    * $Id: ASTMethodTest.java 1187867 2011-10-23 11:35:10Z 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.*;
24  import org.apache.commons.ognl.enhance.ExpressionCompiler;
25  import org.apache.commons.ognl.test.objects.Bean2;
26  import org.apache.commons.ognl.test.objects.Bean3;
27  import org.apache.commons.ognl.test.objects.Root;
28  
29  import java.util.Map;
30  
31  /**
32   * Tests {@link org.apache.commons.ognl.ASTMethod}.
33   */
34  public class ASTMethodTest
35      extends TestCase
36  {
37  
38      public void test_Context_Types()
39          throws Throwable
40      {
41          ASTMethod p = new ASTMethod( 0 );
42          p.setMethodName( "get" );
43  
44          ASTConst pRef = new ASTConst( 0 );
45          pRef.setValue( "value" );
46          p.jjtAddChild( pRef, 0 );
47  
48          Root root = new Root();
49  
50          OgnlContext context = (OgnlContext) Ognl.createDefaultContext( null );
51          context.setRoot( root.getMap() );
52          context.setCurrentObject( root.getMap() );
53          context.setCurrentType( root.getMap().getClass() );
54  
55          assertEquals( p.toGetSourceString( context, root.getMap() ), ".get(\"value\")" );
56          assertEquals( context.getCurrentType(), Object.class );
57          assertEquals( root.getMap().get( "value" ), context.getCurrentObject() );
58          assert Map.class.isAssignableFrom( context.getCurrentAccessor() );
59          assert Map.class.isAssignableFrom( context.getPreviousType() );
60          assert context.getPreviousAccessor() == null;
61  
62          assertEquals( OgnlRuntime.getCompiler( context ).castExpression( context, p, ".get(\"value\")" ), ".get(\"value\")" );
63          assert context.get( ExpressionCompiler.PRE_CAST ) == null;
64  
65          // now test one context level further to see casting work properly on base object types
66  
67          ASTProperty prop = new ASTProperty( 0 );
68          ASTConst propRef = new ASTConst( 0 );
69          propRef.setValue( "bean3" );
70          prop.jjtAddChild( propRef, 0 );
71  
72          Bean2 val = (Bean2) root.getMap().get( "value" );
73  
74          assertEquals( prop.toGetSourceString( context, root.getMap().get( "value" ) ), ".getBean3()" );
75  
76          assertEquals( context.getCurrentObject(), val.getBean3() );
77          assertEquals( context.getCurrentType(), Bean3.class );
78          assertEquals( context.getCurrentAccessor(), Bean2.class );
79          assertEquals( Object.class, context.getPreviousType() );
80          assert Map.class.isAssignableFrom( context.getPreviousAccessor() );
81  
82          assertEquals( OgnlRuntime.getCompiler( context ).castExpression( context, prop, ".getBean3()" ), ").getBean3()" );
83  
84      }
85  }