1 package org.apache.velocity.test.issues;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Method;
23 import java.security.AccessControlException;
24 import java.security.Permission;
25
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29 import org.apache.velocity.app.Velocity;
30 import org.apache.velocity.runtime.RuntimeInstance;
31 import org.apache.velocity.test.misc.TestLogChute;
32 import org.apache.velocity.test.BaseTestCase;
33 import org.apache.velocity.util.introspection.Introspector;
34
35
36
37
38 public class VelTools66TestCase
39 extends BaseTestCase
40 {
41 protected static boolean DEBUG = false;
42
43 public VelTools66TestCase(final String name)
44 throws Exception
45 {
46 super(name);
47 }
48
49 public static Test suite()
50 {
51 return new TestSuite(VelTools66TestCase.class);
52 }
53
54 public void setUp()
55 throws Exception
56 {
57 Velocity.setProperty(
58 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
59
60 Velocity.init();
61 System.setSecurityManager(new TestSecurityManager());
62
63 }
64
65 protected static void log(String out)
66 {
67 Velocity.getLog().debug(out);
68 if (DEBUG)
69 {
70 System.out.println(out);
71 }
72 }
73
74 public void tearDown()
75 {
76 System.setSecurityManager(null);
77 }
78
79 public void testVelTools66()
80 throws Exception
81 {
82 Method verifyMethod = TestInterface.class.getMethod("getTestValue", new Class[0]);
83
84
85 RuntimeInstance ri = new RuntimeInstance();
86 Introspector introspector = ri.getIntrospector();
87
88 Method testMethod = introspector.getMethod(TestObject.class, "getTestValue", new Object[0]);
89 assertNotNull(testMethod);
90 assertEquals("Method object does not match!", verifyMethod, testMethod);
91 }
92
93 public static interface TestInterface
94 {
95 String getTestValue();
96
97 void setTestValue(String testValue);
98 }
99
100 public static final class TestObject
101 implements TestInterface
102 {
103 String testValue = null;
104
105 public TestObject()
106 {
107 }
108
109 public String getTestValue()
110 {
111 return testValue;
112 }
113
114 public void setTestValue(final String testValue)
115 {
116 this.testValue = testValue;
117 }
118 }
119
120 public static final class TestSecurityManager extends SecurityManager
121 {
122 private final Class clazz = TestObject.class;
123
124 public TestSecurityManager()
125 {
126 super();
127 }
128
129 public void checkMemberAccess(final Class c, final int i)
130 {
131 log("checkMemberAccess(" + c.getName() + ", " + i + ")");
132
133 if (c.equals(clazz))
134 {
135 throw new AccessControlException("You are not allowed to access TestObject directly!");
136 }
137 }
138
139 public void checkRead(final String file)
140 {
141 log("checkRead(" + file + ")");
142 }
143
144 public void checkPackageAccess(final String s)
145 {
146 log("checkPackageAccess(" + s + ")");
147 }
148
149 public void checkPropertyAccess(final String s)
150 {
151 log("checkPropertyAccess(" + s + ")");
152 }
153
154 public void checkPermission(final Permission p)
155 {
156 log("checkPermission(" + p + ")");
157 }
158 }
159 }