1 package org.apache.velocity.test; 2 3 18 19 import java.lang.ClassLoader ; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.StringWriter ; 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 41 public class ClassloaderChangeTest extends TestCase implements LogSystem 42 { 43 private VelocityEngine ve = null; 44 private boolean sawCacheDump = false; 45 46 private static String OUTPUT = "Hello From Foo"; 47 48 49 52 public ClassloaderChangeTest() 53 { 54 super("ClassloaderChangeTest"); 55 56 try 57 { 58 61 62 ve = new VelocityEngine(); 63 ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, this ); 64 ve.init(); 65 } 66 catch (Exception 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 } 77 78 public static junit.framework.Test suite () 79 { 80 return new ClassloaderChangeTest(); 81 } 82 83 86 public void runTest () 87 { 88 sawCacheDump = false; 89 90 try 91 { 92 VelocityContext vc = new VelocityContext(); 93 Object foo = null; 94 95 98 99 TestClassloader cl = new TestClassloader(); 100 Class fooclass = cl.loadClass("Foo"); 101 foo = fooclass.newInstance(); 102 103 106 vc.put("foo", foo); 107 108 112 StringWriter writer = new StringWriter (); 113 ve.evaluate( vc, writer, "test", "$foo.doIt()"); 114 115 119 120 if ( !writer.toString().equals( OUTPUT )) 121 { 122 fail("Output from doIt() incorrect"); 123 } 124 125 128 cl = new TestClassloader(); 129 fooclass = cl.loadClass("Foo"); 130 foo = fooclass.newInstance(); 131 132 vc.put("foo", foo); 133 134 writer = new StringWriter (); 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 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 157 public void logVelocityMessage(int level, String message) 158 { 159 if (message.equals( Introspector.CACHEDUMP_MSG) ) 160 { 161 sawCacheDump = true; 162 } 163 } 164 } 165 166 171 class TestClassloader extends ClassLoader 172 { 173 private final static String testclass = 174 "../test/classloader/Foo.class"; 175 176 private Class fooClass = null; 177 178 public TestClassloader() 179 { 180 try 181 { 182 File f = new File ( testclass ); 183 184 byte[] barr = new byte[ (int) f.length() ]; 185 186 FileInputStream fis = new FileInputStream ( f ); 187 fis.read( barr ); 188 fis.close(); 189 190 fooClass = defineClass("Foo", barr, 0, barr.length); 191 } 192 catch( Exception e ) 193 { 194 System.out.println("TestClassloader : exception : " + e ); 195 } 196 } 197 198 199 public Class findClass(String name) 200 { 201 return fooClass; 202 } 203 } 204 | Popular Tags |