001: package org.apache.velocity.test.issues;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.lang.reflect.Method;
023: import java.security.AccessControlException;
024: import java.security.Permission;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028:
029: import org.apache.velocity.app.Velocity;
030: import org.apache.velocity.runtime.RuntimeInstance;
031: import org.apache.velocity.runtime.log.NullLogChute;
032: import org.apache.velocity.test.BaseTestCase;
033: import org.apache.velocity.util.introspection.Introspector;
034:
035: /**
036: * Test Case for <a href="https://issues.apache.org/jira/browse/VELTOOLS-66">Velocity Tools Issue 66</a>.
037: */
038: public class VelTools66TestCase extends BaseTestCase {
039: public VelTools66TestCase(final String name) throws Exception {
040: super (name);
041: }
042:
043: public static Test suite() {
044: return new TestSuite(VelTools66TestCase.class);
045: }
046:
047: public void setUp() throws Exception {
048: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
049: NullLogChute.class.getName());
050:
051: Velocity.init();
052: System.setSecurityManager(new TestSecurityManager());
053:
054: }
055:
056: public void tearDown() {
057: System.setSecurityManager(null);
058: }
059:
060: public void testVelTools66() throws Exception {
061: Method verifyMethod = TestInterface.class.getMethod(
062: "getTestValue", new Class[0]);
063:
064: RuntimeInstance ri = new RuntimeInstance();
065: Introspector introspector = ri.getIntrospector();
066:
067: Method testMethod = introspector.getMethod(TestObject.class,
068: "getTestValue", new Object[0]);
069: assertNotNull(testMethod);
070: assertEquals("Method object does not match!", verifyMethod,
071: testMethod);
072: }
073:
074: public static interface TestInterface {
075: String getTestValue();
076:
077: void setTestValue(String testValue);
078: }
079:
080: public static final class TestObject implements TestInterface {
081: String testValue = null;
082:
083: public TestObject() {
084: }
085:
086: public String getTestValue() {
087: return testValue;
088: }
089:
090: public void setTestValue(final String testValue) {
091: this .testValue = testValue;
092: }
093: }
094:
095: public static final class TestSecurityManager extends
096: SecurityManager {
097: private final Class clazz = TestObject.class;
098:
099: public TestSecurityManager() {
100: super ();
101: }
102:
103: public void checkMemberAccess(final Class c, final int i) {
104: System.out.println("checkMemberAccess(" + c.getName()
105: + ", " + i + ")");
106:
107: if (c.equals(clazz)) {
108: throw new AccessControlException(
109: "You are not allowed to access TestObject directly!");
110: }
111: }
112:
113: public void checkRead(final String file) {
114: System.out.println("checkRead(" + file + ")");
115: }
116:
117: public void checkPackageAccess(final String s) {
118: System.out.println("checkPackageAccess(" + s + ")");
119: }
120:
121: public void checkPropertyAccess(final String s) {
122: System.out.println("checkPropertyAccess(" + s + ")");
123: }
124:
125: public void checkPermission(final Permission p) {
126: System.out.println("checkPermission(" + p + ")");
127: }
128: }
129: }
|