1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
84
85
86 @Override
87 @Before
88 public void setUp()
89 {
90 super.setUp();
91
92
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 }