KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > ClassloaderChangeTest


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.lang.ClassLoader JavaDoc;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.StringWriter JavaDoc;
24
25 import org.apache.velocity.app.VelocityEngine;
26 import org.apache.velocity.runtime.RuntimeServices;
27 import org.apache.velocity.VelocityContext;
28
29 import org.apache.velocity.runtime.log.LogSystem;
30
31 import org.apache.velocity.util.introspection.Introspector;
32
33 import junit.framework.TestCase;
34
35 /**
36  * Tests if we can hand Velocity an arbitrary class for logging.
37  *
38  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
39  * @version $Id: ClassloaderChangeTest.java,v 1.1.10.1 2004/03/03 23:23:04 geirm Exp $
40  */

41 public class ClassloaderChangeTest extends TestCase implements LogSystem
42 {
43     private VelocityEngine ve = null;
44     private boolean sawCacheDump = false;
45     
46     private static String JavaDoc OUTPUT = "Hello From Foo";
47     
48     
49     /**
50      * Default constructor.
51      */

52     public ClassloaderChangeTest()
53     {
54         super("ClassloaderChangeTest");
55
56         try
57         {
58             /*
59              * use an alternative logger. Set it up here and pass it in.
60              */

61             
62             ve = new VelocityEngine();
63             ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this );
64             ve.init();
65         }
66         catch (Exception JavaDoc e)
67         {
68             System.err.println("Cannot setup ClassloaderChnageTest : " + e);
69             System.exit(1);
70         }
71     }
72
73     public void init( RuntimeServices rs )
74     {
75         // do nothing with it
76
}
77
78     public static junit.framework.Test suite ()
79     {
80         return new ClassloaderChangeTest();
81     }
82
83     /**
84      * Runs the test.
85      */

86     public void runTest ()
87     {
88         sawCacheDump = false;
89                         
90         try
91         {
92             VelocityContext vc = new VelocityContext();
93             Object JavaDoc foo = null;
94
95             /*
96              * first, we need a classloader to make our foo object
97              */

98
99             TestClassloader cl = new TestClassloader();
100             Class JavaDoc fooclass = cl.loadClass("Foo");
101             foo = fooclass.newInstance();
102
103             /*
104              * put it into the context
105              */

106             vc.put("foo", foo);
107         
108             /*
109              * and render something that would use it
110              * that will get it into the introspector cache
111              */

112             StringWriter JavaDoc writer = new StringWriter JavaDoc();
113             ve.evaluate( vc, writer, "test", "$foo.doIt()");
114
115             /*
116              * Check to make sure ok. note the obvious
117              * dependency on the Foo class...
118              */

119              
120             if ( !writer.toString().equals( OUTPUT ))
121             {
122                fail("Output from doIt() incorrect");
123             }
124              
125             /*
126              * and do it again :)
127              */

128             cl = new TestClassloader();
129             fooclass = cl.loadClass("Foo");
130             foo = fooclass.newInstance();
131             
132             vc.put("foo", foo);
133         
134             writer = new StringWriter JavaDoc();
135             ve.evaluate( vc, writer, "test", "$foo.doIt()");
136
137             if ( !writer.toString().equals( OUTPUT ))
138             {
139                fail("Output from doIt() incorrect");
140             }
141         }
142         catch( Exception JavaDoc ee )
143         {
144             System.out.println("ClassloaderChangeTest : " + ee );
145         }
146         
147         if (!sawCacheDump)
148         {
149             fail("Didn't see introspector cache dump.");
150         }
151     }
152
153     /**
154      * method to catch Velocity log messages. When we
155      * see the introspector dump message, then set the flag
156      */

157     public void logVelocityMessage(int level, String JavaDoc message)
158     {
159         if (message.equals( Introspector.CACHEDUMP_MSG) )
160         {
161             sawCacheDump = true;
162         }
163     }
164 }
165
166 /**
167  * Simple (real simple...) classloader that depends
168  * on a Foo.class being located in the classloader
169  * directory under test
170  */

171 class TestClassloader extends ClassLoader JavaDoc
172 {
173     private final static String JavaDoc testclass =
174         "../test/classloader/Foo.class";
175         
176     private Class JavaDoc fooClass = null;
177     
178     public TestClassloader()
179     {
180         try
181         {
182             File JavaDoc f = new File JavaDoc( testclass );
183             
184             byte[] barr = new byte[ (int) f.length() ];
185                  
186             FileInputStream JavaDoc fis = new FileInputStream JavaDoc( f );
187             fis.read( barr );
188             fis.close();
189         
190             fooClass = defineClass("Foo", barr, 0, barr.length);
191         }
192         catch( Exception JavaDoc e )
193         {
194             System.out.println("TestClassloader : exception : " + e );
195         }
196     }
197     
198     
199     public Class JavaDoc findClass(String JavaDoc name)
200     {
201         return fooClass;
202     }
203 }
204
Popular Tags