KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > portlet > TestPortletHomeService


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.portlet;
16
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.hivemind.test.HiveMindTestCase;
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.Tapestry;
23 import org.apache.tapestry.engine.ILink;
24 import org.apache.tapestry.services.LinkFactory;
25 import org.apache.tapestry.services.ResponseRenderer;
26 import org.apache.tapestry.services.ServiceConstants;
27 import org.easymock.MockControl;
28
29 /**
30  * Tests for {@link org.apache.tapestry.portlet.PortletHomeService}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class TestPortletHomeService extends HiveMindTestCase
36 {
37     private ResponseRenderer newResponseRenderer()
38     {
39         return (ResponseRenderer) newMock(ResponseRenderer.class);
40     }
41
42     private IRequestCycle newCycle()
43     {
44         return (IRequestCycle) newMock(IRequestCycle.class);
45     }
46
47     private PortletRequestGlobals newRequestGlobals(boolean isRender)
48     {
49         MockControl control = newControl(PortletRequestGlobals.class);
50         PortletRequestGlobals globals = (PortletRequestGlobals) control.getMock();
51
52         globals.isRenderRequest();
53         control.setReturnValue(isRender);
54
55         return globals;
56     }
57
58     private PortletRenderer newPortletRenderer()
59     {
60         return (PortletRenderer) newMock(PortletRenderer.class);
61     }
62
63     private PortletPageResolver newResolver(IRequestCycle cycle, String JavaDoc pageName)
64     {
65         MockControl control = newControl(PortletPageResolver.class);
66         PortletPageResolver resolver = (PortletPageResolver) control.getMock();
67
68         resolver.getPageNameForRequest(cycle);
69         control.setReturnValue(pageName);
70
71         return resolver;
72     }
73
74     public void testIsRenderRequest() throws Exception JavaDoc
75     {
76         IRequestCycle cycle = newCycle();
77         PortletRequestGlobals globals = newRequestGlobals(true);
78         PortletRenderer renderer = newPortletRenderer();
79         PortletPageResolver resolver = newResolver(cycle, "ZePage");
80
81         renderer.renderPage(cycle, "ZePage");
82
83         replayControls();
84
85         PortletHomeService phs = new PortletHomeService();
86         phs.setPageResolver(resolver);
87         phs.setPortletRenderer(renderer);
88         phs.setRequestGlobals(globals);
89
90         phs.service(cycle);
91
92         verifyControls();
93     }
94
95     public void testIsActionRequest() throws Exception JavaDoc
96     {
97         IRequestCycle cycle = newCycle();
98         PortletRequestGlobals globals = newRequestGlobals(false);
99         ResponseRenderer renderer = newResponseRenderer();
100         PortletPageResolver resolver = newResolver(cycle, "ZePage");
101
102         cycle.activate("ZePage");
103         renderer.renderResponse(cycle);
104
105         replayControls();
106
107         PortletHomeService phs = new PortletHomeService();
108         phs.setResponseRenderer(renderer);
109         phs.setRequestGlobals(globals);
110         phs.setPageResolver(resolver);
111
112         phs.service(cycle);
113
114         verifyControls();
115     }
116
117     public void testGetLink()
118     {
119         Map JavaDoc parameters = new HashMap JavaDoc();
120         parameters.put(ServiceConstants.SERVICE, Tapestry.HOME_SERVICE);
121
122         MockControl factoryc = newControl(LinkFactory.class);
123         LinkFactory factory = (LinkFactory) factoryc.getMock();
124
125         ILink link = (ILink) newMock(ILink.class);
126         IRequestCycle cycle = newCycle();
127
128         factory.constructLink(cycle, parameters, true);
129         factoryc.setReturnValue(link);
130
131         replayControls();
132
133         PortletHomeService phs = new PortletHomeService();
134         phs.setLinkFactory(factory);
135
136         assertSame(link, phs.getLink(cycle, null));
137
138         verifyControls();
139     }
140
141     public void testGetLinkWithParameter()
142     {
143         try
144         {
145             new PortletHomeService().getLink(null, "PARAMETER");
146             unreachable();
147         }
148         catch (IllegalArgumentException JavaDoc ex)
149         {
150             assertEquals("The home service does not require a parameter object.", ex.getMessage());
151         }
152     }
153 }
Popular Tags