001: package org.apache.velocity.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.StringWriter;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: import org.apache.velocity.VelocityContext;
029: import org.apache.velocity.app.Velocity;
030: import org.apache.velocity.exception.MethodInvocationException;
031: import org.apache.velocity.runtime.log.NullLogChute;
032:
033: /**
034: * Tests if we can hand Velocity an arbitrary class for logging.
035: *
036: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
037: * @version $Id: MethodInvocationExceptionTestCase.java 477002 2006-11-20 01:07:43Z henning $
038: */
039: public class MethodInvocationExceptionTestCase extends TestCase {
040: /**
041: * Default constructor.
042: * @param name
043: */
044: public MethodInvocationExceptionTestCase(String name) {
045: super (name);
046: }
047:
048: public void setUp() throws Exception {
049: /*
050: * init() Runtime with defaults
051: */
052:
053: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
054: NullLogChute.class.getName());
055:
056: Velocity.init();
057: }
058:
059: public static Test suite() {
060: return new TestSuite(MethodInvocationExceptionTestCase.class);
061: }
062:
063: /**
064: * Runs the test :
065: *
066: * uses the Velocity class to eval a string
067: * which accesses a method that throws an
068: * exception.
069: * @throws Exception
070: */
071: public void testNormalMethodInvocationException() throws Exception {
072: String template = "$woogie.doException() boing!";
073:
074: VelocityContext vc = new VelocityContext();
075:
076: vc.put("woogie", this );
077:
078: StringWriter w = new StringWriter();
079:
080: try {
081: Velocity.evaluate(vc, w, "test", template);
082: fail("No exception thrown");
083: } catch (MethodInvocationException mie) {
084: System.out.println("Caught MIE (good!) :");
085: System.out.println(" reference = "
086: + mie.getReferenceName());
087: System.out.println(" method = " + mie.getMethodName());
088:
089: Throwable t = mie.getWrappedThrowable();
090: System.out.println(" throwable = " + t);
091:
092: if (t instanceof Exception) {
093: System.out.println(" exception = "
094: + ((Exception) t).getMessage());
095: }
096: }
097: }
098:
099: public void testGetterMethodInvocationException() throws Exception {
100: VelocityContext vc = new VelocityContext();
101: vc.put("woogie", this );
102:
103: StringWriter w = new StringWriter();
104:
105: /*
106: * second test - to ensure that methods accessed via get+ construction
107: * also work
108: */
109:
110: String template = "$woogie.foo boing!";
111:
112: try {
113: Velocity.evaluate(vc, w, "test", template);
114: fail("No exception thrown, second test.");
115: } catch (MethodInvocationException mie) {
116: System.out.println("Caught MIE (good!) :");
117: System.out.println(" reference = "
118: + mie.getReferenceName());
119: System.out.println(" method = " + mie.getMethodName());
120:
121: Throwable t = mie.getWrappedThrowable();
122: System.out.println(" throwable = " + t);
123:
124: if (t instanceof Exception) {
125: System.out.println(" exception = "
126: + ((Exception) t).getMessage());
127: }
128: }
129: }
130:
131: public void testCapitalizedGetterMethodInvocationException()
132: throws Exception {
133: VelocityContext vc = new VelocityContext();
134: vc.put("woogie", this );
135:
136: StringWriter w = new StringWriter();
137:
138: String template = "$woogie.Foo boing!";
139:
140: try {
141: Velocity.evaluate(vc, w, "test", template);
142: fail("No exception thrown, third test.");
143: } catch (MethodInvocationException mie) {
144: System.out.println("Caught MIE (good!) :");
145: System.out.println(" reference = "
146: + mie.getReferenceName());
147: System.out.println(" method = " + mie.getMethodName());
148:
149: Throwable t = mie.getWrappedThrowable();
150: System.out.println(" throwable = " + t);
151:
152: if (t instanceof Exception) {
153: System.out.println(" exception = "
154: + ((Exception) t).getMessage());
155: }
156: }
157: }
158:
159: public void testSetterMethodInvocationException() throws Exception {
160: VelocityContext vc = new VelocityContext();
161: vc.put("woogie", this );
162:
163: StringWriter w = new StringWriter();
164:
165: String template = "#set($woogie.foo = 'lala') boing!";
166:
167: try {
168: Velocity.evaluate(vc, w, "test", template);
169: fail("No exception thrown, set test.");
170: } catch (MethodInvocationException mie) {
171: System.out.println("Caught MIE (good!) :");
172: System.out.println(" reference = "
173: + 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: System.out.println(" exception = "
181: + ((Exception) t).getMessage());
182: }
183: }
184: }
185:
186: /**
187: * test that no exception is thrown when in parameter to macro.
188: * This is the way we expect the system to work, but it would be better
189: * to throw an exception.
190: * @throws Exception
191: */
192: public void testMacroInvocationException() throws Exception {
193: VelocityContext vc = new VelocityContext();
194: vc.put("woogie", this );
195:
196: StringWriter w = new StringWriter();
197:
198: String template = "#macro (macro1 $param) $param #end #macro1($woogie.getFoo())";
199:
200: try {
201: Velocity.evaluate(vc, w, "test", template);
202: fail("No exception thrown, macro invocation test.");
203: } catch (MethodInvocationException mie) {
204: System.out.println("Caught MIE (good!) :");
205: System.out.println(" reference = "
206: + mie.getReferenceName());
207: System.out.println(" method = " + mie.getMethodName());
208:
209: Throwable t = mie.getWrappedThrowable();
210: System.out.println(" throwable = " + t);
211:
212: if (t instanceof Exception) {
213: System.out.println(" exception = "
214: + ((Exception) t).getMessage());
215: }
216: } catch (Exception e) {
217: fail("Wrong exception thrown, test of exception within macro parameter");
218: }
219: }
220:
221: public void doException() throws Exception {
222: throw new NullPointerException();
223: }
224:
225: public void getFoo() throws Exception {
226: throw new Exception("Hello from getFoo()");
227: }
228:
229: public void setFoo(String foo) throws Exception {
230: throw new Exception("Hello from setFoo()");
231: }
232: }
|