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.runtime.log.NullLogChute;
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      public VelTools66TestCase(final String name)
42              throws Exception
43      {
44          super(name);
45      }
46  
47      public static Test suite()
48      {
49          return new TestSuite(VelTools66TestCase.class);
50      }
51  
52      public void setUp()
53              throws Exception
54      {
55          Velocity.setProperty(
56                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
57  
58          Velocity.init();
59          System.setSecurityManager(new TestSecurityManager());
60  
61      }
62  
63      public void tearDown()
64      {
65          System.setSecurityManager(null);
66      }
67  
68      public void testVelTools66()
69              throws Exception
70      {
71          Method verifyMethod = TestInterface.class.getMethod("getTestValue", new Class[0]);
72  
73  
74          RuntimeInstance ri = new RuntimeInstance();
75          Introspector introspector = ri.getIntrospector();
76  
77          Method testMethod = introspector.getMethod(TestObject.class, "getTestValue", new Object[0]);
78          assertNotNull(testMethod);
79          assertEquals("Method object does not match!", verifyMethod, testMethod);
80      }
81  
82      public static interface TestInterface
83      {
84          String getTestValue();
85  
86          void setTestValue(String testValue);
87      }
88  
89      public static final class TestObject
90              implements TestInterface
91      {
92          String testValue = null;
93  
94          public TestObject()
95          {
96          }
97  
98          public String getTestValue()
99          {
100             return testValue;
101         }
102 
103         public void setTestValue(final String testValue)
104         {
105             this.testValue = testValue;
106         }
107     }
108 
109     public static final class TestSecurityManager extends SecurityManager
110     {
111         private final Class clazz = TestObject.class;
112 
113         public TestSecurityManager()
114         {
115             super();
116         }
117 
118         public void checkMemberAccess(final Class c, final int i)
119         {
120             System.out.println("checkMemberAccess(" + c.getName() + ", " + i + ")");
121 
122             if (c.equals(clazz))
123             {
124                 throw new AccessControlException("You are not allowed to access TestObject directly!");
125             }
126         }
127 
128         public void checkRead(final String file)
129         {
130             System.out.println("checkRead(" + file + ")");
131         }
132 
133         public void checkPackageAccess(final String s)
134         {
135             System.out.println("checkPackageAccess(" + s + ")");
136         }
137 
138         public void checkPropertyAccess(final String s)
139         {
140             System.out.println("checkPropertyAccess(" + s + ")");
141         }
142 
143         public void checkPermission(final Permission p)
144         {
145             System.out.println("checkPermission(" + p + ")");
146         }
147     }
148 }