KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > model > test > ModelHelperTest


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.view.jsp.model.test;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11
12 import com.inversoft.junit.JspTestCase;
13 import com.inversoft.verge.mvc.MVCException;
14 import com.inversoft.verge.mvc.model.MetaData;
15 import com.inversoft.verge.mvc.model.ModelResolution;
16 import com.inversoft.verge.mvc.model.web.WebMetaData;
17 import com.inversoft.verge.mvc.view.jsp.model.ModelHelper;
18 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
19 import com.inversoft.verge.util.ScopeConstants;
20
21
22 /**
23  * <p>
24  * This class has the tests for the model helper.
25  * </p>
26  *
27  * @author Brian Pontarelli
28  * @since 2.0
29  * @version 2.0
30  */

31 public class ModelHelperTest extends JspTestCase {
32
33     /**
34      * Constructor for ModelHelperTest.
35      * @param name
36      */

37     public ModelHelperTest(String JavaDoc name) {
38         super(name);
39         setLocal(true);
40     }
41
42
43     public void testGetMetaDataExisting() {
44         
45         // Should return the one I put in
46
try {
47             MetaData md = new WebMetaData("id", "com.inversoft.verge.mvc.view.jsp.model.test.Bean",
48                 ScopeConstants.REQUEST_INT);
49             ModelResolution mr = new ModelResolution(new Object JavaDoc(), md);
50             ModelResolutionRegistry.store("key", mr, pageContext);
51             ModelResolution mr1 = ModelHelper.getModelResolution("key", null,
52                 pageContext);
53             MetaData md1 = mr1.getMetaData();
54             assertEquals("Should be the same", md, md1);
55         } catch (MVCException mvce) {
56             fail(mvce.toString());
57         } catch (JspException JavaDoc jpse) {
58             fail(jpse.toString());
59         }
60     }
61
62     public void testGetMetaDataNewRequest() {
63         // Should a new Web MD
64
try {
65             Bean bean = new Bean();
66             request.setAttribute("requestKey", bean);
67
68             ModelResolution mr = ModelHelper.getModelResolution("requestKey",
69                 "text", pageContext);
70             MetaData md = mr.getMetaData();
71             assertTrue("Should be WebMetaData", md instanceof WebMetaData);
72             WebMetaData wmd = (WebMetaData) md;
73             assertTrue("Should be request", wmd.getScope() == ScopeConstants.REQUEST_INT);
74             assertEquals("Should be requestKey.text", wmd.getDefinition(), "requestKey.text");
75             assertEquals("Should be requestKey", wmd.getID(), "requestKey");
76             assertEquals("Should be text", wmd.getProperty(), "text");
77             assertSame("Should be bean", bean, mr.getModel());
78         } catch (JspException JavaDoc jpse) {
79             fail(jpse.toString());
80         }
81     }
82
83     public void testGetMetaDataNewSession() {
84         // Should a new Web MD
85
try {
86             Bean bean = new Bean();
87             request.getSession().setAttribute("sessionKey", bean);
88
89             ModelResolution mr = ModelHelper.getModelResolution("sessionKey",
90                 "text", pageContext);
91             MetaData md = mr.getMetaData();
92             assertTrue("Should be WebMetaData", md instanceof WebMetaData);
93             WebMetaData wmd = (WebMetaData) md;
94             assertTrue("Should be session", wmd.getScope() == ScopeConstants.SESSION_INT);
95             assertEquals("Should be sessionKey.text", wmd.getDefinition(), "sessionKey.text");
96             assertEquals("Should be sessionKey", wmd.getID(), "sessionKey");
97             assertEquals("Should be text", wmd.getProperty(), "text");
98             assertSame("Should be bean", bean, mr.getModel());
99         } catch (JspException JavaDoc jpse) {
100             fail(jpse.toString());
101         }
102     }
103
104     public void testGetMetaDataNewContext() {
105         // Should a new Web MD
106
try {
107             Bean bean = new Bean();
108             context.setAttribute("appKey", bean);
109
110             ModelResolution mr = ModelHelper.getModelResolution("appKey",
111                 "text", pageContext);
112             MetaData md = mr.getMetaData();
113             assertTrue("Should be WebMetaData", md instanceof WebMetaData);
114             WebMetaData wmd = (WebMetaData) md;
115             assertTrue("Should be application", wmd.getScope() == ScopeConstants.APPLICATION_INT);
116             assertEquals("Should be appKey.text", wmd.getDefinition(), "appKey.text");
117             assertEquals("Should be appKey", wmd.getID(), "appKey");
118             assertEquals("Should be text", wmd.getProperty(), "text");
119             assertSame("Should be bean", bean, mr.getModel());
120         } catch (JspException JavaDoc jpse) {
121             fail(jpse.toString());
122         }
123     }
124
125     public void testGetValue() {
126         try {
127             Bean bean = new Bean();
128             bean.setText("testText");
129             request.getSession().setAttribute("sessionKey", bean);
130
131             ModelResolution mr = ModelHelper.getModelResolution("sessionKey",
132                 "text", pageContext);
133             Object JavaDoc value = ModelHelper.getValue(mr, pageContext);
134             
135             assertEquals("Should be testText", value, "testText");
136         } catch (JspException JavaDoc jpse) {
137             fail(jpse.toString());
138         }
139     }
140 }
Popular Tags