001 /* 002 * $Id: MemberAccessTest.java 1104112 2011-05-17 10:25:53Z mcucchiara $ 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 org.apache.commons.ognl.DefaultMemberAccess; 023 import org.apache.commons.ognl.OgnlException; 024 import org.apache.commons.ognl.test.objects.Simple; 025 import org.junit.Before; 026 import org.junit.runner.RunWith; 027 import org.junit.runners.Parameterized; 028 import org.junit.runners.Parameterized.Parameters; 029 030 import java.lang.reflect.Member; 031 import java.lang.reflect.Method; 032 import java.util.ArrayList; 033 import java.util.Collection; 034 import java.util.Map; 035 036 @RunWith(value = Parameterized.class) 037 public class MemberAccessTest 038 extends OgnlTestCase 039 { 040 041 private static Simple ROOT = new Simple(); 042 043 private static Object[][] TESTS = { { "@Runtime@getRuntime()", OgnlException.class }, 044 { "@System@getProperty('java.specification.version')", System.getProperty( "java.specification.version" ) }, 045 { "bigIntValue", OgnlException.class }, 046 { "bigIntValue", OgnlException.class, 25, OgnlException.class }, 047 { "getBigIntValue()", OgnlException.class }, { "stringValue", ROOT.getStringValue() }, }; 048 049 /* 050 * =================================================================== Public static methods 051 * =================================================================== 052 */ 053 @Parameters 054 public static Collection<Object[]> data() 055 { 056 Collection<Object[]> data = new ArrayList<Object[]>(TESTS.length); 057 for ( Object[] TEST : TESTS ) 058 { 059 Object[] tmp = new Object[6]; 060 tmp[0] = TEST[0] + " (" + TEST[1] + ")"; 061 tmp[1] = ROOT; 062 tmp[2] = TEST[0]; 063 tmp[3] = TEST[1]; 064 tmp[4] = null; 065 tmp[5] = null; 066 067 data.add( tmp ); 068 } 069 return data; 070 } 071 072 /* 073 * =================================================================== Constructors 074 * =================================================================== 075 */ 076 public MemberAccessTest( String name, Object root, String expressionString, Object expectedResult, Object setValue, 077 Object expectedAfterSetResult ) 078 { 079 super( name, root, expressionString, expectedResult, setValue, expectedAfterSetResult ); 080 } 081 082 /* 083 * =================================================================== Overridden methods 084 * =================================================================== 085 */ 086 @Override 087 @Before 088 public void setUp() 089 { 090 super.setUp(); 091 092 /* Should allow access at all to the Simple class except for the bigIntValue property */ 093 _context.setMemberAccess( new DefaultMemberAccess( false ) 094 { 095 096 @Override 097 public boolean isAccessible( Map context, Object target, Member member, String propertyName ) 098 { 099 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 }