KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > config > test > ActionBuilderTest


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.form.config.test;
8
9
10 import org.jdom.Element;
11
12 import com.inversoft.config.ConfigurationException;
13 import com.inversoft.junit.WebTestCase;
14 import com.inversoft.verge.mvc.controller.WebBeanHandle;
15 import com.inversoft.verge.mvc.controller.form.FormMVCConstants;
16 import com.inversoft.verge.mvc.controller.form.config.ActionConfig;
17 import com.inversoft.verge.mvc.controller.form.config.ActionConfigBuilder;
18 import com.inversoft.verge.mvc.controller.form.config.Constants;
19 import com.inversoft.verge.repository.RepositoryBean;
20 import com.inversoft.verge.repository.config.RepositoryConfigRegistry;
21 import com.inversoft.verge.repository.test.RepositoryTest;
22 import com.inversoft.verge.util.ScopeConstants;
23
24
25 /**
26  * <p>
27  * This class is a test of the ActionConfig builder
28  * </p>
29  *
30  * @author Brian Pontarelli
31  */

32 public class ActionBuilderTest extends WebTestCase {
33
34     /**
35      * Instantiates a new <code>ActionBuilderTest</code>
36      */

37     public ActionBuilderTest(String JavaDoc name) {
38         super(name);
39         setLocal(true);
40     }
41
42
43     /**
44      * Tests the build method correctly builds an action config object
45      */

46     public void testBuild() {
47         Element element = new Element("action");
48         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
49         element.setAttribute(Constants.HANDLER_ATTRIBUTE,
50             "com.inversoft.verge.mvc.controller.form.config.test.TestActionHandler");
51
52         ActionConfigBuilder builder = new ActionConfigBuilder();
53         try {
54             ActionConfig config = (ActionConfig) builder.build(element);
55             assertTrue("Name should be test", config.getName().equals("testAction"));
56             assertTrue("Should have model enabled", config.isModelEnabled());
57             assertTrue("Should have validation enabled", config.isValidationEnabled());
58             assertTrue("WebBeanHandle should not be null", config.getHandle() != null);
59
60             WebBeanHandle handle = config.getHandle();
61             assertTrue("handle should be testAction",
62                 handle.getHandleName().equals("testAction"));
63             assertTrue("handle should be TestActionHandler",
64                 handle.getWebBean().getClassName().equals(
65                     "com.inversoft.verge.mvc.controller.form.config.test.TestActionHandler"));
66             assertTrue("handle should be TestActionHandler Class",
67                 handle.getWebBean().getBeanClass() == TestActionHandler.class);
68             assertTrue("handle have id of test",
69                 handle.getWebBean().getID().equals(
70                     FormMVCConstants.ACTION_HANDLER_REQUEST_KEY));
71             assertTrue("handle should request scoped",
72                 handle.getWebBean().getScope() == ScopeConstants.REQUEST_INT);
73             assertFalse("longTxnEnabled should be false", config.isLongTxnEnabled());
74         } catch (Exception JavaDoc e) {
75             fail(e.toString());
76         }
77     }
78
79     /**
80      * Tests the build method correctly builds an action config object
81      */

82     public void testFailure() {
83         Element element = new Element("action");
84         //element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
85
element.setAttribute(Constants.HANDLER_ATTRIBUTE,
86             "com.inversoft.verge.mvc.controller.form.config.test.TestActionHandler");
87
88         ActionConfigBuilder builder = new ActionConfigBuilder();
89         try {
90             builder.build(element);
91             fail("Should have failed because no name");
92         } catch (Exception JavaDoc e) {
93             assertTrue("Should be a ConfigurationException",
94                 e instanceof ConfigurationException);
95             assertTrue("Should have errorlist",
96                 ((ConfigurationException) e).getErrors() != null &&
97                 !((ConfigurationException) e).getErrors().isEmpty());
98         }
99
100         element = new Element("action");
101         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
102         //element.setAttribute(Constants.HANDLER_ATTRIBUTE,
103
//"com.inversoft.verge.config.form.test.TestActionHandler");
104

105         try {
106             builder.build(element);
107             fail("Should have failed because no handler");
108         } catch (Exception JavaDoc e) {
109             assertTrue("Should be a ConfigurationException",
110                 e instanceof ConfigurationException);
111             assertTrue("Should have errorlist",
112                 ((ConfigurationException) e).getErrors() != null &&
113                 !((ConfigurationException) e).getErrors().isEmpty());
114         }
115
116         element = new Element("action");
117         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
118         element.setAttribute(Constants.HANDLER_ATTRIBUTE,
119             "com.inversoft.verge.mvc.controller.form.config.test.NotAClass");
120
121         try {
122             builder.build(element);
123             fail("Should have failed because invalid class");
124         } catch (Exception JavaDoc e) {
125             assertTrue("Should be a ConfigurationException",
126                 e instanceof ConfigurationException);
127             assertTrue("Should have errorlist",
128                 ((ConfigurationException) e).getErrors() != null &&
129                 !((ConfigurationException) e).getErrors().isEmpty());
130         }
131     }
132
133     /**
134      * Test repository build
135      */

136     public void testRepositoryBuildPartial() {
137
138         // Tests that the build correctly ignores the repository stuff on the
139
// initial pass
140
Element element = new Element("action");
141         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
142         element.setAttribute(Constants.HANDLER_ATTRIBUTE, "repositoryItem");
143         element.setAttribute(Constants.IS_REPOSITORY_ATTRIBUTE, "true");
144
145         ActionConfigBuilder builder = new ActionConfigBuilder();
146         try {
147             ActionConfig config = (ActionConfig) builder.build(element);
148             assertTrue("Name should be test", config.getName().equals("testAction"));
149             assertTrue("WebBeanHandle should be null", config.getHandle() == null);
150             assertTrue("Should be repository", config.isRepository());
151             assertTrue("Should have model enabled", config.isModelEnabled());
152             assertTrue("Should have validation enabled", config.isValidationEnabled());
153             assertFalse("longTxnEnabled should be false", config.isLongTxnEnabled());
154         } catch (Exception JavaDoc e) {
155             fail(e.toString());
156         }
157     }
158
159     /**
160      * Tests a full repository build
161      */

162     public void testRepositoryBuild() {
163
164         // Tests that repository items work correctly
165
RepositoryTest tester = new RepositoryTest("testInitialization");
166         tester.setContext(context);
167         tester.testInitialization();
168
169         Element element = new Element("action");
170         element.setAttribute(Constants.NAME_ATTRIBUTE, "action");
171         element.setAttribute(Constants.HANDLER_ATTRIBUTE, "item1");
172         element.setAttribute(Constants.IS_REPOSITORY_ATTRIBUTE, "true");
173
174         ActionConfigBuilder builder = new ActionConfigBuilder();
175         try {
176             ActionConfig config = (ActionConfig) builder.build(element);
177             assertTrue("Name should be test", config.getName().equals("action"));
178             assertTrue("WebBeanHandle should be null", config.getHandle() == null);
179             assertTrue("Should be repository", config.isRepository());
180             assertTrue("Should have model enabled", config.isModelEnabled());
181             assertTrue("Should have validation enabled", config.isValidationEnabled());
182
183             RepositoryConfigRegistry registry =
184                 RepositoryConfigRegistry.getInstance(request);
185             builder.validate(config, registry);
186
187             assertTrue("Name should be test", config.getName().equals("action"));
188             assertTrue("WebBeanHandle not should be null", config.getHandle() != null);
189             assertTrue("Should be repository", config.isRepository());
190
191             WebBeanHandle handle = config.getHandle();
192             assertTrue("handle should be action",
193                 handle.getHandleName().equals("action"));
194             assertTrue("handle should be SimpleItem",
195                 handle.getWebBean().getClassName().equals(
196                     "com.inversoft.verge.repository.test.SimpleItem"));
197             assertTrue("handle should be SimpleItem Class",
198                 handle.getWebBean().getBeanClass() ==
199                 com.inversoft.verge.repository.test.SimpleItem.class);
200             assertTrue("handle have id of item1",
201                 handle.getWebBean().getID().equals("item1"));
202             assertTrue("Should be a repository bean",
203                 handle.getWebBean() instanceof RepositoryBean);
204             assertFalse("longTxnEnabled should be false", config.isLongTxnEnabled());
205         } catch (Exception JavaDoc e) {
206             fail(e.toString());
207         }
208     }
209
210     /**
211      * Test repository build failure
212      */

213     public void testRepositoryFailurePartial() {
214
215         // Tests that the build correctly ignores the repository stuff on the
216
// initial pass
217
Element element = new Element("action");
218         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
219         element.setAttribute(Constants.HANDLER_ATTRIBUTE, "repositoryItem");
220         element.setAttribute(Constants.IS_REPOSITORY_ATTRIBUTE, "bob");
221
222         ActionConfigBuilder builder = new ActionConfigBuilder();
223         try {
224             /*ActionConfig config = (ActionConfig)*/ builder.build(element);
225             fail("Should have failed bcause repository boolean is bob");
226         } catch (Exception JavaDoc e) {
227             assertTrue("Should be a ConfigurationException",
228                 e instanceof ConfigurationException);
229             assertTrue("Should have errorlist",
230                 ((ConfigurationException) e).getErrors() != null &&
231                 !((ConfigurationException) e).getErrors().isEmpty());
232             System.out.println(e.toString());
233         }
234     }
235
236     /**
237      * Tests a full repository build failure
238      */

239     public void testRepositoryFailure() {
240
241         // Tests that repository items work correctly
242
RepositoryTest tester = new RepositoryTest("testInitialization");
243         tester.setContext(context);
244         tester.testInitialization();
245
246         Element element = new Element("action");
247         element.setAttribute(Constants.NAME_ATTRIBUTE, "action");
248         element.setAttribute(Constants.HANDLER_ATTRIBUTE, "doesNotExist");
249         element.setAttribute(Constants.IS_REPOSITORY_ATTRIBUTE, "true");
250
251         ActionConfigBuilder builder = new ActionConfigBuilder();
252         try {
253             ActionConfig config = (ActionConfig) builder.build(element);
254             assertTrue("Name should be test", config.getName().equals("action"));
255             assertTrue("WebBeanHandle should be null", config.getHandle() == null);
256             assertTrue("Should be repository", config.isRepository());
257             assertTrue("Should have model enabled", config.isModelEnabled());
258             assertTrue("Should have validation enabled", config.isValidationEnabled());
259
260             RepositoryConfigRegistry registry =
261                 RepositoryConfigRegistry.getInstance(request);
262             builder.validate(config, registry);
263
264             fail("Should have failed bcause repository item does not exist");
265         } catch (Exception JavaDoc e) {
266             assertTrue("Should be a ConfigurationException",
267                 e instanceof ConfigurationException);
268             assertTrue("Should have errorlist",
269                 ((ConfigurationException) e).getErrors() != null &&
270                 !((ConfigurationException) e).getErrors().isEmpty());
271             System.out.println(e.toString());
272         }
273     }
274
275     /**
276      * Tests disabling of model and validation
277      */

278     public void testModelValidationDisable() {
279
280         Element element = new Element("action");
281         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
282         element.setAttribute(Constants.HANDLER_ATTRIBUTE,
283             "com.inversoft.verge.mvc.controller.form.config.test.TestActionHandler");
284         element.setAttribute(Constants.IS_REPOSITORY_ATTRIBUTE, "false");
285         element.setAttribute(Constants.MODEL_ENABLED_ATTRIBUTE, "false");
286         element.setAttribute(Constants.VALIDATION_ENABLED_ATTRIBUTE, "false");
287
288         ActionConfigBuilder builder = new ActionConfigBuilder();
289         try {
290             ActionConfig config = (ActionConfig) builder.build(element);
291             assertEquals("Name should be testAction", config.getName(), "testAction");
292             assertNotNull("WebBeanHandle should not be null", config.getHandle());
293             assertTrue("Should not be repository", !config.isRepository());
294             assertTrue("Should not have model enabled", !config.isModelEnabled());
295             assertTrue("Should not have validation enabled", !config.isValidationEnabled());
296             assertFalse("longTxnEnabled should be false", config.isLongTxnEnabled());
297         } catch (Exception JavaDoc e) {
298             fail(e.toString());
299         }
300     }
301
302     /**
303      * Tests enabling of the long transaction support
304      */

305     public void testLongTxnEnable() {
306
307         Element element = new Element("action");
308         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
309         element.setAttribute(Constants.HANDLER_ATTRIBUTE,
310             "com.inversoft.verge.mvc.controller.form.config.test.TestActionHandler");
311         element.setAttribute(Constants.IS_REPOSITORY_ATTRIBUTE, "false");
312         element.setAttribute(Constants.MODEL_ENABLED_ATTRIBUTE, "false");
313         element.setAttribute(Constants.VALIDATION_ENABLED_ATTRIBUTE, "false");
314         element.setAttribute(Constants.LONG_TXN_ENABLED_ATTRIBUTE, "true");
315
316         ActionConfigBuilder builder = new ActionConfigBuilder();
317         try {
318             ActionConfig config = (ActionConfig) builder.build(element);
319             assertEquals("Name should be testAction", config.getName(), "testAction");
320             assertNotNull("WebBeanHandle should not be null", config.getHandle());
321             assertTrue("Should not be repository", !config.isRepository());
322             assertTrue("Should not have model enabled", !config.isModelEnabled());
323             assertTrue("Should not have validation enabled", !config.isValidationEnabled());
324             assertTrue("longTxnEnabled should be true", config.isLongTxnEnabled());
325         } catch (Exception JavaDoc e) {
326             fail(e.toString());
327         }
328     }
329
330     /**
331      * Tests the long transaction support URLs
332      */

333     public void testLongTxnURLs() {
334
335         Element element = new Element("action");
336         element.setAttribute(Constants.NAME_ATTRIBUTE, "testAction");
337         element.setAttribute(Constants.HANDLER_ATTRIBUTE,
338                 "com.inversoft.verge.mvc.controller.form.config.test.TestActionHandler");
339         element.setAttribute(Constants.IS_REPOSITORY_ATTRIBUTE, "false");
340         element.setAttribute(Constants.MODEL_ENABLED_ATTRIBUTE, "false");
341         element.setAttribute(Constants.VALIDATION_ENABLED_ATTRIBUTE, "false");
342         element.setAttribute(Constants.LONG_TXN_ENABLED_ATTRIBUTE, "true");
343         element.setAttribute(Constants.LONG_TXN_START_URL_ATTRIBUTE, "/start.jsp");
344         element.setAttribute(Constants.LONG_TXN_END_URL_ATTRIBUTE, "/end.jsp");
345         element.setAttribute(Constants.LONG_TXN_CATEGORY_ATTRIBUTE, "cat1");
346
347         ActionConfigBuilder builder = new ActionConfigBuilder();
348         try {
349             ActionConfig config = (ActionConfig) builder.build(element);
350             assertEquals("Name should be testAction", config.getName(), "testAction");
351             assertNotNull("WebBeanHandle should not be null", config.getHandle());
352             assertTrue("Should not be repository", !config.isRepository());
353             assertTrue("Should not have model enabled", !config.isModelEnabled());
354             assertTrue("Should not have validation enabled", !config.isValidationEnabled());
355             assertTrue("longTxnEnabled should be true", config.isLongTxnEnabled());
356             assertEquals("/start.jsp", config.getLongTxnStartURL());
357             assertEquals("/end.jsp", config.getLongTxnEndURL());
358             assertEquals("cat1", config.getLongTxnCategory());
359         } catch (Exception JavaDoc e) {
360             fail(e.toString());
361         }
362     }
363 }
Popular Tags