KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > MethodInvocationExceptionTest


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.StringWriter JavaDoc;
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 /**
30  * Tests if we can hand Velocity an arbitrary class for logging.
31  *
32  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
33  * @version $Id: MethodInvocationExceptionTest.java,v 1.6.10.1 2004/03/03 23:23:04 geirm Exp $
34  */

35 public class MethodInvocationExceptionTest extends TestCase
36 {
37    /**
38      * Default constructor.
39      */

40     public MethodInvocationExceptionTest()
41     {
42         super("MethodInvocationExceptionTest");
43
44         try
45         {
46             /*
47              * init() Runtime with defaults
48              */

49             Velocity.init();
50
51         }
52         catch (Exception JavaDoc 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     /**
65      * Runs the test :
66      *
67      * uses the Velocity class to eval a string
68      * which accesses a method that throws an
69      * exception.
70      */

71     public void runTest ()
72     {
73         String JavaDoc template = "$woogie.doException() boing!";
74
75         VelocityContext vc = new VelocityContext();
76         
77         vc.put("woogie", this );
78
79         StringWriter JavaDoc w = new StringWriter JavaDoc();
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 JavaDoc t = mie.getWrappedThrowable();
93             System.out.println(" throwable = " + t );
94
95             if( t instanceof Exception JavaDoc)
96             {
97                 System.out.println(" exception = " + ( (Exception JavaDoc) t).getMessage() );
98             }
99         }
100         catch( Exception JavaDoc e)
101         {
102             fail("Wrong exception thrown, first test." + e);
103             e.printStackTrace();
104         }
105
106         /*
107          * second test - to ensure that methods accessed via get+ construction
108          * also work
109          */

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 JavaDoc t = mie.getWrappedThrowable();
125             System.out.println(" throwable = " + t );
126
127             if( t instanceof Exception JavaDoc)
128             {
129                 System.out.println(" exception = " + ( (Exception JavaDoc) t).getMessage() );
130             }
131         }
132         catch( Exception JavaDoc 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 JavaDoc t = mie.getWrappedThrowable();
151             System.out.println(" throwable = " + t );
152
153             if( t instanceof Exception JavaDoc)
154             {
155                 System.out.println(" exception = " + ( (Exception JavaDoc) t).getMessage() );
156             }
157         }
158         catch( Exception JavaDoc 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 JavaDoc t = mie.getWrappedThrowable();
177             System.out.println(" throwable = " + t );
178
179             if( t instanceof Exception JavaDoc)
180             {
181                 System.out.println(" exception = " + ( (Exception JavaDoc) t).getMessage() );
182             }
183         }
184         catch( Exception JavaDoc e)
185         {
186             fail("Wrong exception thrown, set test");
187         }
188     }
189
190     public void doException()
191         throws Exception JavaDoc
192     {
193         throw new NullPointerException JavaDoc();
194     }
195
196     public void getFoo()
197         throws Exception JavaDoc
198     {
199         throw new Exception JavaDoc("Hello from getFoo()" );
200     }
201
202     public void setFoo( String JavaDoc foo )
203         throws Exception JavaDoc
204     {
205         throw new Exception JavaDoc("Hello from setFoo()");
206     }
207 }
208
Popular Tags