KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > test > BeanHandleTest


1 /*
2  * Copyright (c) 2003-2004, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.test;
8
9
10 import java.lang.reflect.Method JavaDoc;
11
12 import com.inversoft.beans.BeanException;
13 import com.inversoft.junit.WebTestCase;
14 import com.inversoft.util.ReflectionException;
15 import com.inversoft.util.ReflectionTools;
16 import com.inversoft.verge.mvc.controller.Action;
17 import com.inversoft.verge.mvc.controller.BeanHandle;
18
19
20 /**
21  * <p>
22  * This class tests the BeanHandle class.
23  * </p>
24  *
25  * @author Brian Pontarelli
26  */

27 public class BeanHandleTest extends WebTestCase {
28
29     /**
30      * Constructs a new <code>BeanHandleTest</code>.
31      */

32     public BeanHandleTest(String JavaDoc name) {
33         super(name);
34         setLocal(true);
35     }
36
37
38     /**
39      * Tests the bean handle calls the handle method correctly.
40      */

41     public void testCallHandle() {
42         try {
43             BeanHandle bh = new BeanHandle("testSimple", TestActionHandler.class,
44                 new Class JavaDoc[]{Action.class});
45             Method JavaDoc method = ReflectionTools.getMethod(TestActionHandler.class,
46                 "handleTestSimple", new Class JavaDoc[]{Action.class});
47             assertEquals("testSimple", bh.getHandleName());
48             assertEquals(method, bh.getHandleMethod());
49
50             TestActionHandler tah = new TestActionHandler();
51
52             tah.simple = false;
53             bh.invokeHandle(tah, new Object JavaDoc[]{null});
54             assertTrue(tah.simple);
55
56             tah.simple = false;
57             bh.invokeHandle(tah, new Object JavaDoc[]{new Action("foo", request, response)});
58             assertTrue(tah.simple);
59         } catch (BeanException be) {
60             fail(be.toString());
61         } catch (ReflectionException re) {
62             fail(re.toString());
63         }
64     }
65
66     /**
67      * Tests the bean handle calls that throw exceptions
68      */

69     public void testException() {
70         try {
71             BeanHandle bh = new BeanHandle("exception", TestActionHandler.class,
72                 new Class JavaDoc[]{Action.class});
73             TestActionHandler tah = new TestActionHandler();
74
75             bh.invokeHandle(tah, new Object JavaDoc[]{null});
76             fail("Should have thrown exception");
77         } catch (BeanException be) {
78             assertEquals(Exception JavaDoc.class, be.getCause().getClass());
79         }
80     }
81
82     /**
83      * Tests the bean handle calls that throw RuntimeException
84      */

85     public void testRuntimeException() {
86         try {
87             BeanHandle bh = new BeanHandle("runtimeException", TestActionHandler.class,
88                 new Class JavaDoc[]{Action.class});
89             TestActionHandler tah = new TestActionHandler();
90
91             bh.invokeHandle(tah, new Object JavaDoc[]{null});
92             fail("Should have thrown RuntimeException");
93         } catch (BeanException be) {
94             fail("Should not have thrown this");
95         } catch (RuntimeException JavaDoc re) {
96             assertEquals("test", re.getMessage());
97         }
98     }
99
100     /**
101      * Tests the bean handle calls that throw error
102      */

103     public void testError() {
104         try {
105             BeanHandle bh = new BeanHandle("error", TestActionHandler.class,
106                 new Class JavaDoc[]{Action.class});
107             TestActionHandler tah = new TestActionHandler();
108
109             bh.invokeHandle(tah, new Object JavaDoc[]{null});
110             fail("Should have thrown Error");
111         } catch (BeanException be) {
112             fail("Should not have thrown this");
113         } catch (Error JavaDoc e) {
114             assertEquals("test", e.getMessage());
115         }
116     }
117
118     /**
119      * Tests the bean handle calls that throw Throwable
120      */

121     public void testThrowable() {
122         try {
123             BeanHandle bh = new BeanHandle("throwable", TestActionHandler.class,
124                 new Class JavaDoc[]{Action.class});
125             TestActionHandler tah = new TestActionHandler();
126
127             bh.invokeHandle(tah, new Object JavaDoc[]{null});
128             fail("Should have thrown Throwable");
129         } catch (BeanException be) {
130             assertEquals(Throwable JavaDoc.class, be.getCause().getClass());
131         }
132     }
133 }
Popular Tags