1   package org.apache.velocity.test.issues;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Test Case for <a href="https://issues.apache.org/jira/browse/VELTOOLS-66">Velocity Tools Issue 66</a>.
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 }