Source code: org/ognl/test/MemberAccessTest.java
1 //--------------------------------------------------------------------------
2 // Copyright (c) 2004, Drew Davidson and Luke Blanshard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // Neither the name of the Drew Davidson nor the names of its contributors
15 // may be used to endorse or promote products derived from this software
16 // without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //--------------------------------------------------------------------------
31 package org.ognl.test;
32
33 import java.lang.reflect.*;
34 import java.util.*;
35 import junit.framework.TestSuite;
36 import ognl.OgnlException;
37 import ognl.DefaultMemberAccess;
38 import org.ognl.test.objects.Simple;
39
40 public class MemberAccessTest extends OgnlTestCase
41 {
42 private static Simple ROOT = new Simple();
43 private static Object[][] TESTS = {
44 { "@Runtime@getRuntime()", OgnlException.class },
45 { "@System@getProperty('java.specification.version')", System.getProperty("java.specification.version") },
46 { "bigIntValue", OgnlException.class },
47 { "bigIntValue", OgnlException.class, new Integer(25), OgnlException.class },
48 { "getBigIntValue()", OgnlException.class },
49 { "stringValue", ROOT.getStringValue() },
50 };
51
52 /*===================================================================
53 Public static methods
54 ===================================================================*/
55 public static TestSuite suite()
56 {
57 TestSuite result = new TestSuite();
58
59 for (int i = 0; i < TESTS.length; i++) {
60 result.addTest(new MemberAccessTest((String)TESTS[i][0] + " (" + TESTS[i][1] + ")", ROOT, (String)TESTS[i][0], TESTS[i][1]));
61 }
62 return result;
63 }
64
65 /*===================================================================
66 Constructors
67 ===================================================================*/
68 public MemberAccessTest()
69 {
70 super();
71 }
72
73 public MemberAccessTest(String name)
74 {
75 super(name);
76 }
77
78 public MemberAccessTest(String name, Object root, String expressionString, Object expectedResult, Object setValue, Object expectedAfterSetResult)
79 {
80 super(name, root, expressionString, expectedResult, setValue, expectedAfterSetResult);
81 }
82
83 public MemberAccessTest(String name, Object root, String expressionString, Object expectedResult, Object setValue)
84 {
85 super(name, root, expressionString, expectedResult, setValue);
86 }
87
88 public MemberAccessTest(String name, Object root, String expressionString, Object expectedResult)
89 {
90 super(name, root, expressionString, expectedResult);
91 }
92
93 /*===================================================================
94 Overridden methods
95 ===================================================================*/
96 public void setUp()
97 {
98 super.setUp();
99 /* Should allow access at all to the Simple class except for the bigIntValue property */
100 context.setMemberAccess(new DefaultMemberAccess(false)
101 {
102 public boolean isAccessible(Map context, Object target, Member member, String propertyName)
103 {
104 if (target == Runtime.class) {
105 return false;
106 }
107 if (target instanceof Simple) {
108 if (propertyName != null) {
109 return !propertyName.equals("bigIntValue") &&
110 super.isAccessible(context, target, member, propertyName);
111 } else {
112 if (member instanceof Method) {
113 return !member.getName().equals("getBigIntValue") &&
114 !member.getName().equals("setBigIntValue") &&
115 super.isAccessible(context, target, member, propertyName);
116 }
117 }
118 }
119 return super.isAccessible(context, target, member, propertyName);
120 }
121 });
122 }
123 }