KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > components > BaseComponentTestCase


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.components;
16
17 import java.io.CharArrayWriter JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.apache.tapestry.IBinding;
23 import org.apache.tapestry.IComponent;
24 import org.apache.tapestry.IMarkupWriter;
25 import org.apache.tapestry.IPage;
26 import org.apache.tapestry.IRender;
27 import org.apache.tapestry.IRequestCycle;
28 import org.apache.tapestry.markup.AsciiMarkupFilter;
29 import org.apache.tapestry.markup.MarkupWriterImpl;
30 import org.apache.tapestry.spec.IComponentSpecification;
31 import org.apache.tapestry.spec.IParameterSpecification;
32 import org.apache.tapestry.test.Creator;
33 import org.easymock.MockControl;
34
35 /**
36  * @author Howard M. Lewis Ship
37  * @since 4.0
38  */

39 public abstract class BaseComponentTestCase extends HiveMindTestCase
40 {
41     private Creator _creator;
42
43     protected Creator getCreator()
44     {
45         if (_creator == null)
46             _creator = new Creator();
47
48         return _creator;
49     }
50
51     protected CharArrayWriter JavaDoc _charArrayWriter;
52
53     protected IMarkupWriter newBufferWriter()
54     {
55         _charArrayWriter = new CharArrayWriter JavaDoc();
56         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(_charArrayWriter);
57
58         return new MarkupWriterImpl("text/html", pw, new AsciiMarkupFilter());
59     }
60
61     protected void assertBuffer(String JavaDoc expected)
62     {
63         String JavaDoc actual = _charArrayWriter.toString();
64
65         assertEquals("Buffered markup writer content.", expected, actual);
66
67         _charArrayWriter.reset();
68     }
69
70     protected Object JavaDoc newInstance(Class JavaDoc componentClass)
71     {
72         return newInstance(componentClass, null);
73     }
74
75     protected Object JavaDoc newInstance(Class JavaDoc componentClass, String JavaDoc propertyName, Object JavaDoc propertyValue)
76     {
77         return getCreator().newInstance(componentClass, new Object JavaDoc[]
78         { propertyName, propertyValue });
79     }
80
81     protected Object JavaDoc newInstance(Class JavaDoc componentClass, Object JavaDoc[] properties)
82     {
83         return getCreator().newInstance(componentClass, properties);
84     }
85
86     protected IRequestCycle newCycle()
87     {
88         return (IRequestCycle) newMock(IRequestCycle.class);
89     }
90
91     protected IRequestCycle newCycle(boolean rewinding)
92     {
93         MockControl control = newControl(IRequestCycle.class);
94         IRequestCycle cycle = (IRequestCycle) control.getMock();
95
96         trainIsRewinding(control, cycle, rewinding);
97
98         return cycle;
99     }
100
101     protected void trainIsRewinding(MockControl control, IRequestCycle cycle, boolean rewinding)
102     {
103         cycle.isRewinding();
104         control.setReturnValue(rewinding);
105     }
106
107     protected IRequestCycle newCycle(String JavaDoc pageName, IPage page)
108     {
109         MockControl control = newControl(IRequestCycle.class);
110         IRequestCycle cycle = (IRequestCycle) control.getMock();
111
112         cycle.getPage(pageName);
113         control.setReturnValue(page);
114
115         return cycle;
116     }
117
118     protected IMarkupWriter newWriter()
119     {
120         return (IMarkupWriter) newMock(IMarkupWriter.class);
121     }
122
123     protected IBinding newBinding(Object JavaDoc value)
124     {
125         MockControl control = newControl(IBinding.class);
126         IBinding binding = (IBinding) control.getMock();
127
128         binding.getObject();
129         control.setReturnValue(value);
130
131         return binding;
132     }
133
134     protected IBinding newBinding(Location location)
135     {
136         MockControl control = newControl(IBinding.class);
137         IBinding binding = (IBinding) control.getMock();
138
139         binding.getLocation();
140         control.setReturnValue(location);
141
142         return binding;
143     }
144
145     protected IComponent newComponent(String JavaDoc extendedId, Location location)
146     {
147         MockControl control = newControl(IComponent.class);
148         IComponent component = (IComponent) control.getMock();
149
150         component.getExtendedId();
151         control.setReturnValue(extendedId);
152
153         component.getLocation();
154         control.setReturnValue(location);
155
156         return component;
157     }
158
159     protected IComponentSpecification newSpec(String JavaDoc parameterName, IParameterSpecification pspec)
160     {
161         MockControl control = newControl(IComponentSpecification.class);
162         IComponentSpecification spec = (IComponentSpecification) control.getMock();
163
164         spec.getParameter(parameterName);
165         control.setReturnValue(pspec);
166
167         return spec;
168     }
169
170     protected IRender newRender()
171     {
172         return (IRender) newMock(IRender.class);
173     }
174
175     protected IPage newPage()
176     {
177         return (IPage) newMock(IPage.class);
178     }
179
180     protected IPage newPage(String JavaDoc name)
181     {
182         MockControl control = newControl(IPage.class);
183         IPage page = (IPage) control.getMock();
184
185         page.getPageName();
186         control.setReturnValue(name);
187
188         return page;
189     }
190
191     protected void trainGetAttribute(MockControl cyclec, IRequestCycle cycle, String JavaDoc key, Object JavaDoc value)
192     {
193         cycle.getAttribute(key);
194         cyclec.setReturnValue(value);
195     }
196
197 }
Popular Tags