KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > util > test > WebBeanTest


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.util.test;
8
9
10 import com.inversoft.beans.BeanException;
11 import com.inversoft.junit.WebTestCase;
12 import com.inversoft.verge.util.ScopeConstants;
13 import com.inversoft.verge.util.WebBean;
14 import com.inversoft.verge.util.WebBeanProperty;
15
16
17 /**
18  * <p>
19  * This class tests the web bean class.
20  * </p>
21  *
22  * @author Brian Pontarelli
23  */

24 public class WebBeanTest extends WebTestCase {
25
26     /**
27      * Instantiates a new <code>WebBeanTest</code>
28      */

29     public WebBeanTest(String JavaDoc name) {
30         super(name);
31         setLocal(true);
32     }
33
34
35     /**
36      * Tests that the WebBean class instantiates correctly
37      */

38     public void testInstantiation() {
39
40         try {
41             /*WebBean wb =*/ new WebBean("test", ScopeConstants.PAGE_INT, Bean1.class);
42             /*wb =*/ new WebBean("test", ScopeConstants.REQUEST_INT, Bean1.class);
43             /*wb =*/ new WebBean("test", ScopeConstants.SESSION_INT, Bean1.class);
44             /*wb =*/ new WebBean("test", ScopeConstants.APPLICATION_INT, Bean1.class);
45         } catch (Exception JavaDoc e) {
46             fail(e.toString());
47         }
48     }
49
50     /**
51      * Tests that the WebBean class instantiates incorrectly
52      */

53     public void testInstantiationFailure() {
54
55         try {
56             /*WebBean wb =*/ new WebBean("test", 10, Bean1.class);
57             // This will only fail if assertions are enabled
58
fail("Should have failed");
59         } catch (Throwable JavaDoc t) {
60             // okay
61
}
62     }
63
64     /**
65      * Tests that the WebBean class finds the bean correctly
66      */

67     public void testFind() {
68
69         try {
70             Bean1 bean1_request = new Bean1();
71             request.setAttribute("test-request", bean1_request);
72             Bean1 bean1_session = new Bean1();
73             request.getSession().setAttribute("test-session", bean1_session);
74             Bean1 bean1_context = new Bean1();
75             context.setAttribute("test-context", bean1_context);
76
77             WebBean wb = new WebBean("test-request", ScopeConstants.REQUEST_INT, Bean1.class);
78             assertTrue("Should be same instance from request",
79                 wb.getInstance(request) == bean1_request);
80             wb = new WebBean("test-session", ScopeConstants.SESSION_INT, Bean1.class);
81             assertTrue("Should be same instance from session",
82                 wb.getInstance(request) == bean1_session);
83             wb = new WebBean("test-context", ScopeConstants.APPLICATION_INT, Bean1.class);
84             assertTrue("Should be same instance from context",
85                 wb.getInstance(request) == bean1_context);
86         } catch (Exception JavaDoc e) {
87             fail(e.toString());
88         }
89     }
90
91     /**
92      * Tests that the WebBean resolves nested properties correctly
93      */

94     public void testWebBeanProperty() {
95
96         try {
97             Bean1 bean1 = new Bean1();
98             Bean2 bean2 = new Bean2();
99             Bean3 bean3 = new Bean3();
100             request.setAttribute("test-request", bean1);
101             bean1.setProperty1(bean2);
102             bean2.setProperty2(bean3);
103             bean3.setProperty3("testFoo");
104
105             WebBean wb = new WebBean("test-request", ScopeConstants.REQUEST_INT, Bean1.class);
106             WebBeanProperty wbp = wb.getWebBeanProperty("property1.property2.property3");
107
108             assertSame("Should be same instance from request", bean1,
109                 wb.getInstance(request));
110             assertSame("Should be same BeanProperty", wb.getBeanProperty("property1"),
111                 wbp.getRootProperty());
112             assertSame("Should have test value", "testFoo", wbp.getPropertyValue(request));
113         } catch (BeanException be) {
114             fail(be.toString());
115         }
116     }
117 }
Popular Tags