001: //--------------------------------------------------------------------------
002: // Copyright (c) 2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package org.ognl.test;
032:
033: import java.lang.reflect.*;
034: import java.util.*;
035: import junit.framework.TestSuite;
036: import ognl.OgnlException;
037: import ognl.DefaultMemberAccess;
038: import org.ognl.test.objects.Simple;
039:
040: public class MemberAccessTest extends OgnlTestCase {
041: private static Simple ROOT = new Simple();
042: private static Object[][] TESTS = {
043: { "@Runtime@getRuntime()", OgnlException.class },
044: { "@System@getProperty('java.specification.version')",
045: System.getProperty("java.specification.version") },
046: { "bigIntValue", OgnlException.class },
047: { "bigIntValue", OgnlException.class, new Integer(25),
048: OgnlException.class },
049: { "getBigIntValue()", OgnlException.class },
050: { "stringValue", ROOT.getStringValue() }, };
051:
052: /*===================================================================
053: Public static methods
054: ===================================================================*/
055: public static TestSuite suite() {
056: TestSuite result = new TestSuite();
057:
058: for (int i = 0; i < TESTS.length; i++) {
059: result.addTest(new MemberAccessTest((String) TESTS[i][0]
060: + " (" + TESTS[i][1] + ")", ROOT,
061: (String) TESTS[i][0], TESTS[i][1]));
062: }
063: return result;
064: }
065:
066: /*===================================================================
067: Constructors
068: ===================================================================*/
069: public MemberAccessTest() {
070: super ();
071: }
072:
073: public MemberAccessTest(String name) {
074: super (name);
075: }
076:
077: public MemberAccessTest(String name, Object root,
078: String expressionString, Object expectedResult,
079: Object setValue, Object expectedAfterSetResult) {
080: super (name, root, expressionString, expectedResult, setValue,
081: expectedAfterSetResult);
082: }
083:
084: public MemberAccessTest(String name, Object root,
085: String expressionString, Object expectedResult,
086: Object setValue) {
087: super (name, root, expressionString, expectedResult, setValue);
088: }
089:
090: public MemberAccessTest(String name, Object root,
091: String expressionString, Object expectedResult) {
092: super (name, root, expressionString, expectedResult);
093: }
094:
095: /*===================================================================
096: Overridden methods
097: ===================================================================*/
098: public void setUp() {
099: super .setUp();
100: /* Should allow access at all to the Simple class except for the bigIntValue property */
101: context.setMemberAccess(new DefaultMemberAccess(false) {
102: public boolean isAccessible(Map context, Object target,
103: Member member, String propertyName) {
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,
111: member, propertyName);
112: } else {
113: if (member instanceof Method) {
114: return !member.getName().equals(
115: "getBigIntValue")
116: && !member.getName().equals(
117: "setBigIntValue")
118: && super.isAccessible(context,
119: target, member,
120: propertyName);
121: }
122: }
123: }
124: return super.isAccessible(context, target, member,
125: propertyName);
126: }
127: });
128: }
129: }
|