KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (c) 2003, 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 com.inversoft.beans.BeanException;
11 import com.inversoft.junit.WebTestCase;
12 import com.inversoft.verge.mvc.controller.Action;
13 import com.inversoft.verge.mvc.controller.ActionHandlerException;
14 import com.inversoft.verge.mvc.controller.BeanHandle;
15 import com.inversoft.verge.mvc.controller.WebBeanHandle;
16 import com.inversoft.verge.util.ScopeConstants;
17
18
19 /**
20  * <p>
21  * This class contains the tests for the WebBeanHandle object
22  * </p>
23  *
24  * @author Brian Pontarelli
25  */

26 public class WebBeanHandleTest extends WebTestCase {
27
28     /**
29      * Instantiates a new <code>WebBeanHandleTest</code>
30      */

31     public WebBeanHandleTest(String JavaDoc name) {
32         super(name);
33         setLocal(true);
34     }
35
36
37     /**
38      * Tests the handle loads correctly.
39      */

40     public void testCreate() {
41
42         try {
43             WebBeanHandle handle = new WebBeanHandle("testId.testSimple",
44                 ScopeConstants.REQUEST_INT,
45                 "com.inversoft.verge.mvc.controller.test.TestActionHandler");
46             assertTrue("Should have id of testId", handle.getWebBean().getID().equals("testId"));
47             assertTrue("Should have handle of testSimple", handle.getHandleName().equals("testSimple"));
48             assertTrue("Should request scoped", handle.getWebBean().getScope() == ScopeConstants.REQUEST_INT);
49             assertTrue("Should have correct className",
50                 handle.getWebBean().getClassName().equals(
51                     "com.inversoft.verge.mvc.controller.test.TestActionHandler"));
52             assertTrue("Should have bean class",
53                 handle.getWebBean().getBeanClass() == TestActionHandler.class);
54         } catch (BeanException be) {
55             fail(be.toString());
56         }
57
58         try {
59             WebBeanHandle handle = new WebBeanHandle("testId.testSimple",
60                 ScopeConstants.REQUEST_INT, TestActionHandler.class);
61             assertTrue("Should have id of testId", handle.getWebBean().getID().equals("testId"));
62             assertTrue("Should have handle of testSimple", handle.getHandleName().equals("testSimple"));
63             assertTrue("Should request scoped", handle.getWebBean().getScope() == ScopeConstants.REQUEST_INT);
64             assertTrue("Should have correct className",
65                 handle.getWebBean().getClassName().equals(
66                     "com.inversoft.verge.mvc.controller.test.TestActionHandler"));
67             assertTrue("Should have bean class",
68                 handle.getWebBean().getBeanClass() == TestActionHandler.class);
69         } catch (BeanException be) {
70             fail(be.toString());
71         }
72
73         try {
74             WebBeanHandle handle = new WebBeanHandle("testId.property.testSimple2",
75                 ScopeConstants.REQUEST_INT,
76                 "com.inversoft.verge.mvc.controller.test.TestActionHandler");
77             assertTrue("Should have id of testId", handle.getWebBean().getID().equals("testId"));
78             assertTrue("Should have handle of testSimple2", handle.getHandleName().equals("testSimple2"));
79             assertTrue("Should request scoped", handle.getWebBean().getScope() == ScopeConstants.REQUEST_INT);
80             assertTrue("Should have correct className",
81                 handle.getWebBean().getClassName().equals(
82                     "com.inversoft.verge.mvc.controller.test.TestActionHandler"));
83             assertTrue("Should have bean class",
84                 handle.getWebBean().getBeanClass() == TestActionHandler.class);
85         } catch (BeanException be) {
86             fail(be.toString());
87         }
88     }
89
90     /**
91      * Tests the handle calls correctly.
92      */

93     public void testCall() {
94
95         try {
96             WebBeanHandle handle = new WebBeanHandle("testId.testSimple",
97                 ScopeConstants.REQUEST_INT,
98                 "com.inversoft.verge.mvc.controller.test.TestActionHandler");
99             handle.invokeHandle(new Action("", request, response), request);
100             TestActionHandler handler = (TestActionHandler) request.getAttribute("testId");
101
102             assertTrue("Should be in request", handler != null);
103             assertTrue("Should have called handle method", handler.simple);
104         } catch (BeanException be) {
105             fail(be.toString());
106         }
107
108         try {
109             WebBeanHandle handle = new WebBeanHandle("testId.property.testSimple2",
110                 ScopeConstants.REQUEST_INT,
111                 "com.inversoft.verge.mvc.controller.test.TestActionHandler");
112             handle.invokeHandle(new Action("", request, response), request);
113             TestActionHandler handler = (TestActionHandler) request.getAttribute("testId");
114             TestActionHandler2 handler2 = handler.getProperty();
115
116             assertTrue("Should not be null", handler2 != null);
117             assertTrue("Should have called second handle method", handler2.simple2);
118         } catch (Exception JavaDoc e) {
119             fail(e.toString());
120         }
121     }
122
123     /**
124      * Tests the handle calls correctly and throws an exception
125      */

126     public void testException() {
127
128         try {
129             WebBeanHandle handle = new WebBeanHandle("testId.testException",
130                 ScopeConstants.REQUEST_INT,
131                 "com.inversoft.verge.mvc.controller.test.TestActionHandler");
132             handle.invokeHandle(new Action("", request, response), request);
133             fail("Should have thrown a ActionHandlerException");
134         } catch (Exception JavaDoc e) {
135             TestActionHandler handler = (TestActionHandler) request.getAttribute("testId");
136             assertTrue("Should have called exception handle", handler.exception);
137             assertTrue("Should be an ActionHandlerException",
138                 e.getCause() instanceof ActionHandlerException);
139         }
140
141         try {
142             WebBeanHandle handle = new WebBeanHandle("testId.property.testException2",
143                 ScopeConstants.REQUEST_INT,
144                 "com.inversoft.verge.mvc.controller.test.TestActionHandler");
145             handle.invokeHandle(new Action("", request, response), request);
146             fail("Should have thrown a ActionHandlerException");
147         } catch (Exception JavaDoc e) {
148             TestActionHandler handler = (TestActionHandler) request.getAttribute("testId");
149             TestActionHandler2 handler2 = handler.getProperty();
150             assertTrue("Should have called exception handle2", handler2.exception2);
151             assertTrue("Should be an ActionHandlerException",
152                 e.getCause() instanceof ActionHandlerException);
153         }
154     }
155
156     /**
157      * Tests the handle fails correctly
158      */

159     public void testFailure() {
160
161         try {
162             /*BeanHandle bh =*/ new BeanHandle("notValid", TestActionHandler.class,
163                 new Class JavaDoc[]{Action.class});
164             fail("Should have failed to find handle method");
165         } catch (Exception JavaDoc e) {
166             assertTrue("Should be an BeanException", e instanceof BeanException);
167         }
168
169         try {
170             WebBeanHandle handle = new WebBeanHandle("testId.property.notValid",
171                 ScopeConstants.REQUEST_INT,
172                 "com.inversoft.verge.mvc.controller.form.test.TestActionHandler");
173             handle.invokeHandle((Action) null, request);
174             fail("Should have failed to invoke the handler");
175         } catch (Exception JavaDoc e) {
176             assertTrue("Should be an BeanException", e instanceof BeanException);
177         }
178         
179         try {
180             WebBeanHandle handle = new WebBeanHandle("testId.notValid",
181                 ScopeConstants.REQUEST_INT,
182                 "com.inversoft.verge.mvc.controller.form.test.TestActionHandler");
183             handle.invokeHandle((Action) null, request);
184             fail("Should have failed to invoke the handler");
185         } catch (Exception JavaDoc e) {
186             assertTrue("Should be an BeanException", e instanceof BeanException);
187         }
188     }
189 }
190
Popular Tags