1   package org.apache.velocity.test;
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.io.File;
23  import java.io.FileInputStream;
24  import java.io.StringWriter;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.velocity.VelocityContext;
31  import org.apache.velocity.app.VelocityEngine;
32  import org.apache.velocity.runtime.RuntimeServices;
33  import org.apache.velocity.runtime.log.LogChute;
34  import org.apache.velocity.util.introspection.Introspector;
35  
36  /**
37   * Tests if we can hand Velocity an arbitrary class for logging.
38   *
39   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
40   * @version $Id: ClassloaderChangeTestCase.java 463298 2006-10-12 16:10:32Z henning $
41   */
42  public class ClassloaderChangeTestCase extends TestCase implements LogChute
43  {
44      private VelocityEngine ve = null;
45      private boolean sawCacheDump = false;
46  
47      private static String OUTPUT = "Hello From Foo";
48  
49  
50      /**
51       * Default constructor.
52       */
53      public ClassloaderChangeTestCase(String name)
54      {
55          super(name);
56      }
57  
58      public void setUp()
59              throws Exception
60      {
61          ve = new VelocityEngine();
62          ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
63          ve.init();
64      }
65  
66      public void init( RuntimeServices rs )
67      {
68          // do nothing with it
69      }
70  
71      public static Test suite ()
72      {
73          return new TestSuite(ClassloaderChangeTestCase.class);
74      }
75  
76      /**
77       * Runs the test.
78       */
79      public void testClassloaderChange()
80          throws Exception
81      {
82          sawCacheDump = false;
83  
84          VelocityContext vc = new VelocityContext();
85          Object foo = null;
86  
87          /*
88           *  first, we need a classloader to make our foo object
89           */
90  
91          TestClassloader cl = new TestClassloader();
92          Class fooclass = cl.loadClass("Foo");
93          foo = fooclass.newInstance();
94  
95          /*
96           *  put it into the context
97           */
98          vc.put("foo", foo);
99  
100         /*
101          *  and render something that would use it
102          *  that will get it into the introspector cache
103          */
104         StringWriter writer = new StringWriter();
105         ve.evaluate( vc, writer, "test", "$foo.doIt()");
106 
107         /*
108          *  Check to make sure ok.  note the obvious
109          *  dependency on the Foo class...
110          */
111 
112         if ( !writer.toString().equals( OUTPUT ))
113         {
114             fail("Output from doIt() incorrect");
115         }
116 
117         /*
118          * and do it again :)
119          */
120         cl = new TestClassloader();
121         fooclass = cl.loadClass("Foo");
122         foo = fooclass.newInstance();
123 
124         vc.put("foo", foo);
125 
126         writer = new StringWriter();
127         ve.evaluate( vc, writer, "test", "$foo.doIt()");
128 
129         if ( !writer.toString().equals( OUTPUT ))
130         {
131             fail("Output from doIt() incorrect");
132         }
133 
134         if (!sawCacheDump)
135         {
136             fail("Didn't see introspector cache dump.");
137         }
138     }
139 
140     /**
141      *  method to catch Velocity log messages.  When we
142      *  see the introspector dump message, then set the flag
143      */
144     public void log(int level, String message)
145     {
146         if (message.equals( Introspector.CACHEDUMP_MSG) )
147         {
148             sawCacheDump = true;
149         }
150     }
151 
152     /**
153      *  method to catch Velocity log messages.  When we
154      *  see the introspector dump message, then set the flag
155      */
156     public void log(int level, String message, Throwable t)
157     {
158         // ignore the Throwable for this test
159         log(level, message);
160     }
161 
162     public boolean isLevelEnabled(int level)
163     {
164         return true;
165     }
166 }
167 
168 /**
169  *  Simple (real simple...) classloader that depends
170  *  on a Foo.class being located in the classloader
171  *  directory under test
172  */
173 class TestClassloader extends ClassLoader
174 {
175     private final static String testclass =
176         "test/classloader/Foo.class";
177 
178     private Class fooClass = null;
179 
180     public TestClassloader()
181             throws Exception
182     {
183         File f = new File( testclass );
184 
185         byte[] barr = new byte[ (int) f.length() ];
186 
187         FileInputStream fis = new FileInputStream( f );
188         fis.read( barr );
189         fis.close();
190 
191         fooClass = defineClass("Foo", barr, 0, barr.length);
192     }
193 
194 
195     public Class findClass(String name)
196     {
197         return fooClass;
198     }
199 }