KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.portlet.PortletMode;
22 import javax.portlet.PortletRequest;
23 import javax.portlet.WindowState;
24
25 import org.apache.hivemind.test.HiveMindTestCase;
26 import org.apache.tapestry.IPage;
27 import org.apache.tapestry.IRequestCycle;
28 import org.apache.tapestry.PageNotFoundException;
29 import org.easymock.MockControl;
30
31 /**
32  * Tests for {@link org.apache.tapestry.portlet.MatchingPortletPageResolver}.
33  *
34  * @author Howard M. Lewis Ship
35  * @since 4.0
36  */

37 public class TestMatchingPortletPageResolver extends HiveMindTestCase
38 {
39     private IRequestCycle newCycle()
40     {
41         return (IRequestCycle) newMock(IRequestCycle.class);
42     }
43
44     private IPage newPage()
45     {
46         return (IPage) newMock(IPage.class);
47     }
48
49     private IRequestCycle newCycle(String JavaDoc pageName)
50     {
51         IPage page = newPage();
52
53         MockControl control = newControl(IRequestCycle.class);
54         IRequestCycle cycle = (IRequestCycle) control.getMock();
55
56         cycle.getPage(pageName);
57         control.setReturnValue(page);
58
59         return cycle;
60     }
61
62     private PageResolverContribution newContribution(String JavaDoc mimeType, String JavaDoc portletMode,
63             String JavaDoc windowState, String JavaDoc pageName)
64     {
65         PageResolverContribution result = new PageResolverContribution();
66         result.setMimeType(mimeType);
67         result.setPortletMode(portletMode);
68         result.setWindowState(windowState);
69         result.setPageName(pageName);
70
71         return result;
72     }
73
74     public void testNoMatchOnContentType()
75     {
76         IRequestCycle cycle = newCycle();
77
78         MockControl requestc = newControl(PortletRequest.class);
79         PortletRequest request = (PortletRequest) requestc.getMock();
80
81         trainContentType(requestc, request, "text/xml");
82
83         List JavaDoc l = Collections.singletonList(newContribution("text/html", null, "wierd", "Wierd"));
84
85         replayControls();
86
87         MatchingPortletPageResolver resolver = new MatchingPortletPageResolver();
88         resolver.setContributions(l);
89         resolver.initializeService();
90         resolver.setRequest(request);
91
92         assertNull(resolver.getPageNameForRequest(cycle));
93
94         verifyControls();
95     }
96
97     public void testNoMatchOnPortletMode()
98     {
99         IRequestCycle cycle = newCycle();
100
101         MockControl requestc = newControl(PortletRequest.class);
102         PortletRequest request = (PortletRequest) requestc.getMock();
103
104         trainPortletMode(requestc, request, "edit");
105
106         List JavaDoc l = Collections.singletonList(newContribution(null, "view", null, "Wierd"));
107
108         replayControls();
109
110         MatchingPortletPageResolver resolver = new MatchingPortletPageResolver();
111         resolver.setContributions(l);
112         resolver.initializeService();
113         resolver.setRequest(request);
114
115         assertNull(resolver.getPageNameForRequest(cycle));
116
117         verifyControls();
118     }
119
120     public void testNoMatchOnWindowState()
121     {
122         IRequestCycle cycle = newCycle();
123
124         MockControl requestc = newControl(PortletRequest.class);
125         PortletRequest request = (PortletRequest) requestc.getMock();
126
127         trainWindowState(requestc, request, "huge");
128
129         List JavaDoc l = Collections.singletonList(newContribution(null, null, "tiny", "Wierd"));
130
131         replayControls();
132
133         MatchingPortletPageResolver resolver = new MatchingPortletPageResolver();
134         resolver.setContributions(l);
135         resolver.initializeService();
136         resolver.setRequest(request);
137
138         assertNull(resolver.getPageNameForRequest(cycle));
139
140         verifyControls();
141     }
142
143     public void testSortingCounts()
144     {
145         IRequestCycle cycle = newCycle("EditHuge");
146
147         MockControl requestc = newControl(PortletRequest.class);
148         PortletRequest request = (PortletRequest) requestc.getMock();
149
150         List JavaDoc l = new ArrayList JavaDoc();
151         l.add(newContribution(null, "edit", null, "EditNormal"));
152         l.add(newContribution(null, "edit", "huge", "EditHuge"));
153
154         trainPortletMode(requestc, request, "edit");
155         trainWindowState(requestc, request, "huge");
156
157         replayControls();
158
159         MatchingPortletPageResolver resolver = new MatchingPortletPageResolver();
160         resolver.setContributions(l);
161         resolver.initializeService();
162         resolver.setRequest(request);
163
164         assertEquals("EditHuge", resolver.getPageNameForRequest(cycle));
165
166         verifyControls();
167     }
168
169     public void testPageMissing()
170     {
171         IPage page = newPage();
172
173         MockControl cyclec = newControl(IRequestCycle.class);
174         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
175
176         MockControl requestc = newControl(PortletRequest.class);
177         PortletRequest request = (PortletRequest) requestc.getMock();
178
179         List JavaDoc l = new ArrayList JavaDoc();
180         l.add(newContribution(null, "edit", null, "EditNormal"));
181         l.add(newContribution(null, "edit", "huge", "EditHuge"));
182
183         trainPortletMode(requestc, request, "edit");
184         trainWindowState(requestc, request, "huge");
185
186         cycle.getPage("EditHuge");
187         cyclec.setThrowable(new PageNotFoundException("missing!"));
188
189         trainPortletMode(requestc, request, "edit");
190
191         cycle.getPage("EditNormal");
192         cyclec.setReturnValue(page);
193
194         replayControls();
195
196         MatchingPortletPageResolver resolver = new MatchingPortletPageResolver();
197         resolver.setContributions(l);
198         resolver.initializeService();
199         resolver.setRequest(request);
200
201         assertEquals("EditNormal", resolver.getPageNameForRequest(cycle));
202
203         verifyControls();
204     }
205
206     private void trainContentType(MockControl control, PortletRequest request, String JavaDoc contentType)
207     {
208         request.getResponseContentType();
209         control.setReturnValue(contentType);
210     }
211
212     private void trainPortletMode(MockControl control, PortletRequest request, String JavaDoc modeName)
213     {
214         PortletMode mode = new PortletMode(modeName);
215
216         request.getPortletMode();
217         control.setReturnValue(mode);
218     }
219
220     private void trainWindowState(MockControl control, PortletRequest request, String JavaDoc stateName)
221     {
222         WindowState state = new WindowState(stateName);
223
224         request.getWindowState();
225         control.setReturnValue(state);
226     }
227 }
Popular Tags