1 package org.apache.velocity.test; 2 3 18 19 import java.io.StringWriter ; 20 21 import org.apache.velocity.app.Velocity; 22 import org.apache.velocity.VelocityContext; 23 import org.apache.velocity.runtime.log.LogSystem; 24 25 import org.apache.velocity.exception.MethodInvocationException; 26 27 import junit.framework.TestCase; 28 29 35 public class MethodInvocationExceptionTest extends TestCase 36 { 37 40 public MethodInvocationExceptionTest() 41 { 42 super("MethodInvocationExceptionTest"); 43 44 try 45 { 46 49 Velocity.init(); 50 51 } 52 catch (Exception e) 53 { 54 System.err.println("Cannot setup MethodInvocationExceptionTest : " + e); 55 System.exit(1); 56 } 57 } 58 59 public static junit.framework.Test suite () 60 { 61 return new MethodInvocationExceptionTest(); 62 } 63 64 71 public void runTest () 72 { 73 String template = "$woogie.doException() boing!"; 74 75 VelocityContext vc = new VelocityContext(); 76 77 vc.put("woogie", this ); 78 79 StringWriter w = new StringWriter (); 80 81 try 82 { 83 Velocity.evaluate( vc, w, "test", template ); 84 fail("No exception thrown"); 85 } 86 catch( MethodInvocationException mie ) 87 { 88 System.out.println("Caught MIE (good!) :" ); 89 System.out.println(" reference = " + mie.getReferenceName() ); 90 System.out.println(" method = " + mie.getMethodName() ); 91 92 Throwable t = mie.getWrappedThrowable(); 93 System.out.println(" throwable = " + t ); 94 95 if( t instanceof Exception ) 96 { 97 System.out.println(" exception = " + ( (Exception ) t).getMessage() ); 98 } 99 } 100 catch( Exception e) 101 { 102 fail("Wrong exception thrown, first test." + e); 103 e.printStackTrace(); 104 } 105 106 110 111 template = "$woogie.foo boing!"; 112 113 try 114 { 115 Velocity. evaluate( vc, w, "test", template ); 116 fail("No exception thrown, second test."); 117 } 118 catch( MethodInvocationException mie ) 119 { 120 System.out.println("Caught MIE (good!) :" ); 121 System.out.println(" reference = " + mie.getReferenceName() ); 122 System.out.println(" method = " + mie.getMethodName() ); 123 124 Throwable t = mie.getWrappedThrowable(); 125 System.out.println(" throwable = " + t ); 126 127 if( t instanceof Exception ) 128 { 129 System.out.println(" exception = " + ( (Exception ) t).getMessage() ); 130 } 131 } 132 catch( Exception e) 133 { 134 fail("Wrong exception thrown, second test"); 135 } 136 137 template = "$woogie.Foo boing!"; 138 139 try 140 { 141 Velocity. evaluate( vc, w, "test", template ); 142 fail("No exception thrown, third test."); 143 } 144 catch( MethodInvocationException mie ) 145 { 146 System.out.println("Caught MIE (good!) :" ); 147 System.out.println(" reference = " + mie.getReferenceName() ); 148 System.out.println(" method = " + mie.getMethodName() ); 149 150 Throwable t = mie.getWrappedThrowable(); 151 System.out.println(" throwable = " + t ); 152 153 if( t instanceof Exception ) 154 { 155 System.out.println(" exception = " + ( (Exception ) t).getMessage() ); 156 } 157 } 158 catch( Exception e) 159 { 160 fail("Wrong exception thrown, third test"); 161 } 162 163 template = "#set($woogie.foo = 'lala') boing!"; 164 165 try 166 { 167 Velocity. evaluate( vc, w, "test", template ); 168 fail("No exception thrown, set test."); 169 } 170 catch( MethodInvocationException mie ) 171 { 172 System.out.println("Caught MIE (good!) :" ); 173 System.out.println(" reference = " + mie.getReferenceName() ); 174 System.out.println(" method = " + mie.getMethodName() ); 175 176 Throwable t = mie.getWrappedThrowable(); 177 System.out.println(" throwable = " + t ); 178 179 if( t instanceof Exception ) 180 { 181 System.out.println(" exception = " + ( (Exception ) t).getMessage() ); 182 } 183 } 184 catch( Exception e) 185 { 186 fail("Wrong exception thrown, set test"); 187 } 188 } 189 190 public void doException() 191 throws Exception 192 { 193 throw new NullPointerException (); 194 } 195 196 public void getFoo() 197 throws Exception 198 { 199 throw new Exception ("Hello from getFoo()" ); 200 } 201 202 public void setFoo( String foo ) 203 throws Exception 204 { 205 throw new Exception ("Hello from setFoo()"); 206 } 207 } 208 | Popular Tags |