1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
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
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
69 }
70
71 public static Test suite ()
72 {
73 return new TestSuite(ClassloaderChangeTestCase.class);
74 }
75
76
77
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
89
90
91 TestClassloader cl = new TestClassloader();
92 Class fooclass = cl.loadClass("Foo");
93 foo = fooclass.newInstance();
94
95
96
97
98 vc.put("foo", foo);
99
100
101
102
103
104 StringWriter writer = new StringWriter();
105 ve.evaluate( vc, writer, "test", "$foo.doIt()");
106
107
108
109
110
111
112 if ( !writer.toString().equals( OUTPUT ))
113 {
114 fail("Output from doIt() incorrect");
115 }
116
117
118
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
142
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
154
155
156 public void log(int level, String message, Throwable t)
157 {
158
159 log(level, message);
160 }
161
162 public boolean isLevelEnabled(int level)
163 {
164 return true;
165 }
166 }
167
168
169
170
171
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 }