View Javadoc

1   /*
2    * $Id: MemberAccessTest.java 1104112 2011-05-17 10:25:53Z mcucchiara $
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 org.apache.commons.ognl.DefaultMemberAccess;
23  import org.apache.commons.ognl.OgnlException;
24  import org.apache.commons.ognl.test.objects.Simple;
25  import org.junit.Before;
26  import org.junit.runner.RunWith;
27  import org.junit.runners.Parameterized;
28  import org.junit.runners.Parameterized.Parameters;
29  
30  import java.lang.reflect.Member;
31  import java.lang.reflect.Method;
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.Map;
35  
36  @RunWith(value = Parameterized.class)
37  public class MemberAccessTest
38      extends OgnlTestCase
39  {
40  
41      private static Simple ROOT = new Simple();
42  
43      private static Object[][] TESTS = { { "@Runtime@getRuntime()", OgnlException.class },
44          { "@System@getProperty('java.specification.version')", System.getProperty( "java.specification.version" ) },
45          { "bigIntValue", OgnlException.class },
46          { "bigIntValue", OgnlException.class, 25, OgnlException.class },
47          { "getBigIntValue()", OgnlException.class }, { "stringValue", ROOT.getStringValue() }, };
48  
49      /*
50       * =================================================================== Public static methods
51       * ===================================================================
52       */
53      @Parameters
54      public static Collection<Object[]> data()
55      {
56          Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length);
57          for ( Object[] TEST : TESTS )
58          {
59              Object[] tmp = new Object[6];
60              tmp[0] = TEST[0] + " (" + TEST[1] + ")";
61              tmp[1] = ROOT;
62              tmp[2] = TEST[0];
63              tmp[3] = TEST[1];
64              tmp[4] = null;
65              tmp[5] = null;
66  
67              data.add( tmp );
68          }
69          return data;
70      }
71  
72      /*
73       * =================================================================== Constructors
74       * ===================================================================
75       */
76      public MemberAccessTest( String name, Object root, String expressionString, Object expectedResult, Object setValue,
77                               Object expectedAfterSetResult )
78      {
79          super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult );
80      }
81  
82      /*
83       * =================================================================== Overridden methods
84       * ===================================================================
85       */
86      @Override
87      @Before
88      public void setUp()
89      {
90          super.setUp();
91  
92          /* Should allow access at all to the Simple class except for the bigIntValue property */
93          _context.setMemberAccess( new DefaultMemberAccess( false )
94          {
95  
96              @Override
97              public boolean isAccessible( Map context, Object target, Member member, String propertyName )
98              {
99                  if ( target == Runtime.class )
100                 {
101                     return false;
102                 }
103                 if ( target instanceof Simple )
104                 {
105                     if ( propertyName != null )
106                     {
107                         return !propertyName.equals( "bigIntValue" )
108                             && super.isAccessible( context, target, member, propertyName );
109                     }
110                     if ( member instanceof Method )
111                     {
112                         return !member.getName().equals( "getBigIntValue" )
113                             && !member.getName().equals( "setBigIntValue" )
114                             && super.isAccessible( context, target, member, propertyName );
115                     }
116                 }
117                 return super.isAccessible( context, target, member, propertyName );
118             }
119         } );
120     }
121 }