1 package org.apache.velocity.test; 2 3 18 19 import java.io.StringWriter ; 20 21 import junit.framework.TestCase; 22 23 import org.apache.velocity.app.Velocity; 24 import org.apache.velocity.VelocityContext; 25 import org.apache.velocity.runtime.log.LogSystem; 26 27 import org.apache.velocity.exception.ParseErrorException; 28 import org.apache.velocity.exception.MethodInvocationException; 29 30 import org.apache.velocity.app.event.EventCartridge; 31 import org.apache.velocity.app.event.ReferenceInsertionEventHandler; 32 import org.apache.velocity.app.event.MethodExceptionEventHandler; 33 import org.apache.velocity.app.event.NullSetEventHandler; 34 35 import org.apache.velocity.runtime.RuntimeServices; 36 37 43 public class EventHandlingTestCase extends TestCase implements ReferenceInsertionEventHandler, 44 NullSetEventHandler, MethodExceptionEventHandler, 45 LogSystem 46 { 47 48 private String logString = null; 49 private boolean exceptionSwitch = true; 50 private static String NO_REFERENCE_VALUE = "<no reference value>"; 51 private static String REFERENCE_VALUE = "<reference value>"; 52 53 56 public EventHandlingTestCase() 57 { 58 super("EventHandlingTestCase"); 59 60 try 61 { 62 65 66 Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, this ); 67 Velocity.init(); 68 } 69 catch (Exception e) 70 { 71 System.err.println("Cannot setup event handling test : " + e); 72 System.exit(1); 73 } 74 } 75 76 public void init( RuntimeServices rs ) 77 { 78 79 } 80 81 public static junit.framework.Test suite () 82 { 83 return new EventHandlingTestCase(); 84 } 85 86 89 public void runTest () 90 { 91 92 95 96 VelocityContext inner = new VelocityContext(); 97 98 103 104 EventCartridge ec = new EventCartridge(); 105 ec.addEventHandler(this); 106 ec.attachToContext( inner ); 107 108 112 113 VelocityContext context = new VelocityContext( inner ); 114 115 context.put("name", "Velocity"); 116 117 try 118 { 119 122 123 String s = "$name"; 124 125 StringWriter w = new StringWriter (); 126 Velocity.evaluate( context, w, "mystring", s ); 127 128 if ( !w.toString().equals( REFERENCE_VALUE )) 129 { 130 fail( "Reference insertion test 1"); 131 } 132 133 137 138 s = "$floobie"; 139 140 w = new StringWriter (); 141 Velocity.evaluate( context, w, "mystring", s ); 142 143 if ( !w.toString().equals( NO_REFERENCE_VALUE )) 144 { 145 fail( "Reference insertion test 2"); 146 } 147 148 152 153 s = "#set($settest = $NotAReference)"; 154 w = new StringWriter (); 155 logString = null; 156 Velocity.evaluate( context, w, "mystring", s ); 157 158 if( logString != null) 159 { 160 fail( "NullSetEventHandler test 1"); 161 } 162 163 167 168 s = "#set($logthis = $NotAReference)"; 169 w = new StringWriter (); 170 logString = null; 171 Velocity.evaluate( context, w, "mystring", s ); 172 173 if( logString == null) 174 { 175 fail( "NullSetEventHandler test 1"); 176 } 177 178 188 189 exceptionSwitch = true; 190 191 context.put("this", this ); 192 193 s = " $this.throwException()"; 194 w = new StringWriter (); 195 196 try 197 { 198 Velocity.evaluate( context, w, "mystring", s ); 199 } 200 catch( MethodInvocationException mee ) 201 { 202 fail("MethodExceptionEvent test 1"); 203 } 204 catch( Exception e ) 205 { 206 fail("MethodExceptionEvent test 1"); 207 } 208 209 214 215 exceptionSwitch = false; 216 217 s = " $this.throwException()"; 218 w = new StringWriter (); 219 220 try 221 { 222 Velocity.evaluate( context, w, "mystring", s ); 223 fail("MethodExceptionEvent test 2"); 224 } 225 catch( MethodInvocationException mee ) 226 { 227 230 } 231 catch( Exception e ) 232 { 233 fail("MethodExceptionEvent test 2"); 234 } 235 } 236 catch( ParseErrorException pee ) 237 { 238 fail("ParseErrorException" + pee); 239 } 240 catch( MethodInvocationException mee ) 241 { 242 fail("MethodInvocationException" + mee); 243 } 244 catch( Exception e ) 245 { 246 fail("Exception" + e); 247 } 248 } 249 250 254 public void throwException() 255 throws Exception 256 { 257 throw new Exception ("Hello from throwException()"); 258 } 259 260 263 public Object referenceInsert( String reference, Object value ) 264 { 265 269 270 String s = null; 271 272 if( value != null ) 273 { 274 s = REFERENCE_VALUE; 275 } 276 else 277 { 278 282 if ( reference.equals("$floobie") ) 283 { 284 s = NO_REFERENCE_VALUE; 285 } 286 } 287 return s; 288 } 289 290 296 public boolean shouldLogOnNullSet( String lhs, String rhs ) 297 { 298 if (lhs.equals("$settest")) 299 return false; 300 301 return true; 302 } 303 304 307 public Object methodException( Class claz, String method, Exception e ) 308 throws Exception 309 { 310 313 314 if( exceptionSwitch && method.equals("throwException")) 315 { 316 return "handler"; 317 } 318 319 throw e; 320 } 321 322 325 public void logVelocityMessage(int level, String message) 326 { 327 logString = message; 328 } 329 330 } 331 | Popular Tags |