KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.tapestry.portlet;
2
3 import javax.portlet.PortletMode;
4 import javax.portlet.PortletRequest;
5 import javax.portlet.WindowState;
6
7 import org.apache.hivemind.test.HiveMindTestCase;
8 import org.apache.tapestry.IRequestCycle;
9 import org.apache.tapestry.services.ServiceConstants;
10 import org.easymock.MockControl;
11
12 /**
13  * Test for {@link org.apache.tapestry.portlet.RenderService}.
14  *
15  * @author Howard M. Lewis Ship
16  * @since 4.0
17  */

18 public class TestRenderService extends HiveMindTestCase
19 {
20     public void testGetLinkUnsupported()
21     {
22         RenderService rs = new RenderService();
23
24         try
25         {
26             rs.getLink(null, null);
27             unreachable();
28         }
29         catch (UnsupportedOperationException JavaDoc ex)
30         {
31             // Hm. Not actually the best message!
32
assertEquals("Method getLink() is not supported for portlet requests.", ex.getMessage());
33         }
34     }
35
36     public void testNormal() throws Exception JavaDoc
37     {
38         MockControl cyclec = newControl(IRequestCycle.class);
39         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
40
41         MockControl requestc = newControl(PortletRequest.class);
42         PortletRequest request = (PortletRequest) requestc.getMock();
43
44         PortletRenderer renderer = (PortletRenderer) newMock(PortletRenderer.class);
45
46         trainGetParameter(cyclec, cycle, PortletConstants.PORTLET_MODE, "view");
47         trainGetParameter(cyclec, cycle, PortletConstants.WINDOW_STATE, "normal");
48
49         trainGetPortletMode(requestc, request, PortletMode.VIEW);
50         trainGetWindowState(requestc, request, WindowState.NORMAL);
51
52         trainGetParameter(cyclec, cycle, ServiceConstants.PAGE, "Fred");
53
54         renderer.renderPage(cycle, "Fred");
55
56         replayControls();
57
58         RenderService rs = new RenderService();
59         rs.setPortletRenderer(renderer);
60         rs.setRequest(request);
61
62         rs.service(cycle);
63
64         verifyControls();
65     }
66
67     public void testPortletModeChanged() throws Exception JavaDoc
68     {
69         MockControl cyclec = newControl(IRequestCycle.class);
70         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
71
72         MockControl requestc = newControl(PortletRequest.class);
73         PortletRequest request = (PortletRequest) requestc.getMock();
74
75         PortletRenderer renderer = (PortletRenderer) newMock(PortletRenderer.class);
76
77         PortletPageResolver resolver = newResolver(cycle, "Barney");
78
79         trainGetParameter(cyclec, cycle, PortletConstants.PORTLET_MODE, "view");
80         trainGetParameter(cyclec, cycle, PortletConstants.WINDOW_STATE, "normal");
81
82         trainGetPortletMode(requestc, request, PortletMode.EDIT);
83
84         renderer.renderPage(cycle, "Barney");
85
86         replayControls();
87
88         RenderService rs = new RenderService();
89         rs.setPortletRenderer(renderer);
90         rs.setRequest(request);
91         rs.setPageResolver(resolver);
92
93         rs.service(cycle);
94
95         verifyControls();
96     }
97
98     public void testWindowStateChanged() throws Exception JavaDoc
99     {
100         MockControl cyclec = newControl(IRequestCycle.class);
101         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
102
103         MockControl requestc = newControl(PortletRequest.class);
104         PortletRequest request = (PortletRequest) requestc.getMock();
105
106         PortletRenderer renderer = (PortletRenderer) newMock(PortletRenderer.class);
107
108         PortletPageResolver resolver = newResolver(cycle, "Wilma");
109
110         trainGetParameter(cyclec, cycle, PortletConstants.PORTLET_MODE, "view");
111         trainGetParameter(cyclec, cycle, PortletConstants.WINDOW_STATE, "normal");
112
113         trainGetPortletMode(requestc, request, PortletMode.VIEW);
114         trainGetWindowState(requestc, request, WindowState.MAXIMIZED);
115
116         renderer.renderPage(cycle, "Wilma");
117
118         replayControls();
119
120         RenderService rs = new RenderService();
121         rs.setPortletRenderer(renderer);
122         rs.setRequest(request);
123         rs.setPageResolver(resolver);
124
125         rs.service(cycle);
126
127         verifyControls();
128     }
129
130     private PortletPageResolver newResolver(IRequestCycle cycle, String JavaDoc pageName)
131     {
132         MockControl control = newControl(PortletPageResolver.class);
133         PortletPageResolver resolver = (PortletPageResolver) control.getMock();
134
135         resolver.getPageNameForRequest(cycle);
136         control.setReturnValue(pageName);
137
138         return resolver;
139     }
140
141     private void trainGetWindowState(MockControl control, PortletRequest request, WindowState state)
142     {
143         request.getWindowState();
144         control.setReturnValue(state);
145     }
146
147     private void trainGetPortletMode(MockControl control, PortletRequest request, PortletMode mode)
148     {
149         request.getPortletMode();
150         control.setReturnValue(mode);
151     }
152
153     private void trainGetParameter(MockControl control, IRequestCycle cycle, String JavaDoc name,
154             String JavaDoc value)
155     {
156         cycle.getParameter(name);
157         control.setReturnValue(value);
158     }
159 }
160
Popular Tags