KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > TestFormComponentContributorContext


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import java.util.Locale JavaDoc;
18
19 import org.apache.hivemind.ClassResolver;
20 import org.apache.hivemind.Resource;
21 import org.apache.hivemind.util.ClasspathResource;
22 import org.apache.tapestry.IForm;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.PageRenderSupport;
25 import org.apache.tapestry.TapestryUtils;
26 import org.apache.tapestry.components.BaseComponentTestCase;
27 import org.apache.tapestry.services.Infrastructure;
28 import org.easymock.MockControl;
29
30 /**
31  * Tests for {@link org.apache.tapestry.form.FormComponentContributorContextImpl}.
32  *
33  * @author Howard Lewis Ship
34  * @since 4.0
35  */

36 public class TestFormComponentContributorContext extends BaseComponentTestCase
37 {
38     private IForm newForm(String JavaDoc name)
39     {
40         MockControl control = newControl(IForm.class);
41         IForm form = (IForm) control.getMock();
42
43         form.getName();
44         control.setReturnValue(name);
45
46         return form;
47     }
48
49     private IFormComponent newField(IForm form, String JavaDoc name)
50     {
51         MockControl control = newControl(IFormComponent.class);
52         IFormComponent field = (IFormComponent) control.getMock();
53
54         field.getForm();
55         control.setReturnValue(form);
56
57         field.getName();
58         control.setReturnValue(name);
59
60         return field;
61     }
62
63     private ClassResolver newResolver()
64     {
65         return (ClassResolver) newMock(ClassResolver.class);
66     }
67
68     private Infrastructure newInfrastructure(ClassResolver resolver)
69     {
70         MockControl control = newControl(Infrastructure.class);
71         Infrastructure inf = (Infrastructure) control.getMock();
72
73         inf.getClassResolver();
74         control.setReturnValue(resolver);
75
76         return inf;
77     }
78
79     public void testGetFieldDOM()
80     {
81         IForm form = newForm("myform");
82         IFormComponent field = newField(form, "myfield");
83         ClassResolver resolver = newResolver();
84
85         MockControl cyclec = newControl(IRequestCycle.class);
86         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
87
88         Infrastructure inf = newInfrastructure(resolver);
89
90         cycle.getInfrastructure();
91         cyclec.setReturnValue(inf);
92
93         PageRenderSupport prs = newSupport();
94
95         trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
96
97         replayControls();
98
99         FormComponentContributorContext context = new FormComponentContributorContextImpl(
100                 Locale.ENGLISH, cycle, field);
101
102         assertEquals("document.myform.myfield", context.getFieldDOM());
103
104         verifyControls();
105     }
106
107     public void testIncludeClasspathScript()
108     {
109         IForm form = newForm("myform");
110         IFormComponent field = newField(form, "myfield");
111         ClassResolver resolver = newResolver();
112
113         MockControl cyclec = newControl(IRequestCycle.class);
114         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
115
116         Infrastructure inf = newInfrastructure(resolver);
117
118         cycle.getInfrastructure();
119         cyclec.setReturnValue(inf);
120
121         PageRenderSupport prs = newSupport();
122
123         Resource expected = new ClasspathResource(resolver, "/foo.js");
124
125         prs.addExternalScript(expected);
126
127         trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
128
129         replayControls();
130
131         FormComponentContributorContext context = new FormComponentContributorContextImpl(
132                 Locale.ENGLISH, cycle, field);
133
134         context.includeClasspathScript("/foo.js");
135
136         verifyControls();
137     }
138
139     public void testAddSubmitListener()
140     {
141
142         IForm form = newForm("myform");
143         IFormComponent field = newField(form, "myfield");
144         ClassResolver resolver = newResolver();
145
146         MockControl cyclec = newControl(IRequestCycle.class);
147         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
148
149         Infrastructure inf = newInfrastructure(resolver);
150
151         cycle.getInfrastructure();
152         cyclec.setReturnValue(inf);
153
154         PageRenderSupport prs = newSupport();
155
156         prs.addInitializationScript("document.myform.events.addSubmitListener(foo);");
157
158         trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
159
160         replayControls();
161
162         FormComponentContributorContext context = new FormComponentContributorContextImpl(
163                 Locale.ENGLISH, cycle, field);
164
165         context.addSubmitListener("foo");
166
167         verifyControls();
168     }
169
170     private PageRenderSupport newSupport()
171     {
172         return (PageRenderSupport) newMock(PageRenderSupport.class);
173     }
174 }
175
Popular Tags