1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.StringWriter;
23  
24  import junit.framework.Test;
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  import org.apache.velocity.VelocityContext;
29  import org.apache.velocity.app.Velocity;
30  import org.apache.velocity.exception.MethodInvocationException;
31  import org.apache.velocity.runtime.log.NullLogChute;
32  
33  /**
34   * Tests if we can hand Velocity an arbitrary class for logging.
35   *
36   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
37   * @version $Id: MethodInvocationExceptionTestCase.java 477002 2006-11-20 01:07:43Z henning $
38   */
39  public class MethodInvocationExceptionTestCase extends TestCase
40  {
41     /**
42       * Default constructor.
43       * @param name
44       */
45      public MethodInvocationExceptionTestCase(String name)
46      {
47          super(name);
48      }
49  
50      public void setUp()
51              throws Exception
52      {
53          /*
54           *  init() Runtime with defaults
55           */
56  
57          Velocity.setProperty(
58                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
59  
60          Velocity.init();
61      }
62  
63      public static Test suite ()
64      {
65          return new TestSuite(MethodInvocationExceptionTestCase.class);
66      }
67  
68      /**
69       * Runs the test :
70       *
71       *  uses the Velocity class to eval a string
72       *  which accesses a method that throws an
73       *  exception.
74       *  @throws Exception
75       */
76      public void testNormalMethodInvocationException ()
77              throws Exception
78      {
79          String template = "$woogie.doException() boing!";
80  
81          VelocityContext vc = new VelocityContext();
82  
83          vc.put("woogie", this );
84  
85          StringWriter w = new StringWriter();
86  
87          try
88          {
89              Velocity.evaluate( vc,  w, "test", template );
90              fail("No exception thrown");
91          }
92          catch( MethodInvocationException mie )
93          {
94              System.out.println("Caught MIE (good!) :" );
95              System.out.println("  reference = " + mie.getReferenceName() );
96              System.out.println("  method    = " + mie.getMethodName() );
97  
98              Throwable t = mie.getWrappedThrowable();
99              System.out.println("  throwable = " + t );
100 
101             if( t instanceof Exception)
102             {
103                 System.out.println("  exception = " + ( (Exception) t).getMessage() );
104             }
105         }
106     }
107 
108 
109     public void testGetterMethodInvocationException ()
110             throws Exception
111     {
112         VelocityContext vc = new VelocityContext();
113         vc.put("woogie", this );
114 
115         StringWriter w = new StringWriter();
116 
117         /*
118          *  second test - to ensure that methods accessed via get+ construction
119          *  also work
120          */
121 
122         String template = "$woogie.foo boing!";
123 
124         try
125         {
126             Velocity. evaluate( vc,  w, "test", template );
127             fail("No exception thrown, second test.");
128         }
129         catch( MethodInvocationException mie )
130         {
131             System.out.println("Caught MIE (good!) :" );
132             System.out.println("  reference = " + mie.getReferenceName() );
133             System.out.println("  method    = " + mie.getMethodName() );
134 
135             Throwable t = mie.getWrappedThrowable();
136             System.out.println("  throwable = " + t );
137 
138             if( t instanceof Exception)
139             {
140                 System.out.println("  exception = " + ( (Exception) t).getMessage() );
141             }
142         }
143     }
144 
145 
146     public void testCapitalizedGetterMethodInvocationException ()
147             throws Exception
148     {
149         VelocityContext vc = new VelocityContext();
150         vc.put("woogie", this );
151 
152         StringWriter w = new StringWriter();
153 
154         String template = "$woogie.Foo boing!";
155 
156         try
157         {
158             Velocity. evaluate( vc,  w, "test", template );
159             fail("No exception thrown, third test.");
160         }
161         catch( MethodInvocationException mie )
162         {
163             System.out.println("Caught MIE (good!) :" );
164             System.out.println("  reference = " + mie.getReferenceName() );
165             System.out.println("  method    = " + mie.getMethodName() );
166 
167             Throwable t = mie.getWrappedThrowable();
168             System.out.println("  throwable = " + t );
169 
170             if( t instanceof Exception)
171             {
172                 System.out.println("  exception = " + ( (Exception) t).getMessage() );
173             }
174         }
175     }
176 
177     public void testSetterMethodInvocationException ()
178             throws Exception
179     {
180         VelocityContext vc = new VelocityContext();
181         vc.put("woogie", this );
182 
183         StringWriter w = new StringWriter();
184 
185         String template = "#set($woogie.foo = 'lala') boing!";
186 
187         try
188         {
189             Velocity. evaluate( vc,  w, "test", template );
190             fail("No exception thrown, set test.");
191         }
192         catch( MethodInvocationException mie )
193         {
194             System.out.println("Caught MIE (good!) :" );
195             System.out.println("  reference = " + mie.getReferenceName() );
196             System.out.println("  method    = " + mie.getMethodName() );
197 
198             Throwable t = mie.getWrappedThrowable();
199             System.out.println("  throwable = " + t );
200 
201             if( t instanceof Exception)
202             {
203                 System.out.println("  exception = " + ( (Exception) t).getMessage() );
204             }
205         }
206     }
207 
208 
209     /**
210      * test that no exception is thrown when in parameter to macro.
211      * This is the way we expect the system to work, but it would be better
212      * to throw an exception.
213      * @throws Exception
214      */
215     public void testMacroInvocationException ()
216             throws Exception
217     {
218         VelocityContext vc = new VelocityContext();
219         vc.put("woogie", this );
220 
221         StringWriter w = new StringWriter();
222 
223         String template = "#macro (macro1 $param) $param #end  #macro1($woogie.getFoo())";
224 
225         try
226         {
227             Velocity. evaluate( vc,  w, "test", template );
228             fail("No exception thrown, macro invocation test.");
229         }
230         catch( MethodInvocationException mie )
231         {
232             System.out.println("Caught MIE (good!) :" );
233             System.out.println("  reference = " + mie.getReferenceName() );
234             System.out.println("  method    = " + mie.getMethodName() );
235 
236             Throwable t = mie.getWrappedThrowable();
237             System.out.println("  throwable = " + t );
238 
239             if( t instanceof Exception)
240             {
241                 System.out.println("  exception = " + ( (Exception) t).getMessage() );
242             }
243         }
244         catch( Exception e)
245         {
246             fail("Wrong exception thrown, test of exception within macro parameter");
247         }
248     }
249 
250     public void doException()
251         throws Exception
252     {
253         throw new NullPointerException();
254     }
255 
256     public void getFoo()
257         throws Exception
258     {
259         throw new Exception("Hello from getFoo()" );
260     }
261 
262     public void  setFoo( String foo )
263         throws Exception
264     {
265         throw new Exception("Hello from setFoo()");
266     }
267 }