1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.layout;
18
19 import java.io.File;
20
21 import junit.framework.TestCase;
22
23 import org.apache.jetspeed.components.ComponentManager;
24 import org.apache.jetspeed.components.SpringComponentManager;
25 import org.apache.jetspeed.components.factorybeans.ServletConfigFactoryBean;
26 import org.apache.jetspeed.layout.impl.LayoutValve;
27 import org.apache.jetspeed.mocks.ResourceLocatingServletContext;
28 import org.apache.jetspeed.pipeline.PipelineException;
29 import org.apache.jetspeed.request.RequestContext;
30 import com.mockrunner.mock.web.MockServletConfig;
31
32 /***
33 * Test for Fragment placement
34 *
35 * @author <a>David Gurney </a>
36 * @version $Id: $
37 */
38 public class TestLayout extends TestCase
39 {
40
41 private ComponentManager cm;
42
43 private LayoutValve valve;
44
45 public static void main(String[] args)
46 {
47 junit.swingui.TestRunner.run(TestLayout.class);
48 }
49
50 /***
51 * Setup the request context
52 */
53 protected void setUp() throws Exception
54 {
55 super.setUp();
56
57 String appRoot = "./";
58
59 MockServletConfig servletConfig = new MockServletConfig();
60 ResourceLocatingServletContext servletContent = new ResourceLocatingServletContext(new File(appRoot));
61 servletConfig.setServletContext(servletContent);
62 ServletConfigFactoryBean.setServletConfig(servletConfig);
63
64
65 String[] bootConfigs = null;
66 String[] appConfigs =
67 {
68 "src/test/resources/assembly/test-layout-api.xml"};
69
70
71 cm = new SpringComponentManager(bootConfigs, appConfigs, servletContent, ".");
72 cm.start();
73 valve = (LayoutValve) cm.getComponent("layoutValve");
74 }
75
76 protected void tearDown() throws Exception
77 {
78 cm.stop();
79 }
80
81 public void testNullRequestContext()
82 {
83
84 try
85 {
86 valve.invoke(null, null);
87 TestLayout.fail("should have thrown an exception");
88 } catch (PipelineException e)
89 {
90 TestLayout.assertTrue("detected null request context", true);
91 }
92 }
93
94 public void testNullParameters()
95 {
96 try
97 {
98
99 RequestContext rc = FragmentUtil
100 .setupRequestContext(null, "1234", "0", "0");
101 valve.invoke(rc, null);
102 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
103 "failure"));
104 } catch (PipelineException e)
105 {
106 TestLayout.fail("unexpected exception");
107 }
108
109 try
110 {
111
112 RequestContext rc = FragmentUtil.setupRequestContext("moveabs", "33", "0",
113 "0");
114 valve.invoke(rc, null);
115
116
117 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
118 "failure"));
119 } catch (PipelineException e)
120 {
121 TestLayout.fail("unexpected exception");
122 }
123
124 try
125 {
126
127 RequestContext rc = FragmentUtil.setupRequestContext("moveabs", "1234",
128 null, "0");
129 valve.invoke(rc, null);
130
131
132 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
133 "failure"));
134 } catch (PipelineException e)
135 {
136 TestLayout.fail("unexpected exception");
137 }
138
139 try
140 {
141
142 RequestContext rc = FragmentUtil.setupRequestContext("moveabs", "1234",
143 "0", null);
144 valve.invoke(rc, null);
145
146
147 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
148 "failure"));
149 } catch (PipelineException e)
150 {
151 TestLayout.fail("unexpected exception");
152 }
153 }
154
155 public void testEasy()
156 {
157 moveSuccess("moveabs", "1", "0", "0", "0", "1", "0", "1");
158 moveSuccess("moveright", "1", "0", "0", "1", "0", "1", "0");
159 }
160
161 public void testMoveSuccess()
162 {
163 moveSuccess("moveabs", "1", "0", "0", "0", "0", "-1", "-1");
164
165
166 moveSuccess("moveabs", "1", "0", "0", "0", "1", "0", "1");
167
168 moveSuccess("moveabs", "2", "0", "1", "0", "0", "0", "0");
169 moveSuccess("moveabs", "1", "0", "0", "1", "0", "1", "0");
170
171 moveSuccess("moveabs", "3", "1", "0", "0", "0", "0", "0");
172 moveSuccess("moveabs", "2", "0", "1", "1", "2", "1", "2");
173
174
175 moveSuccess("moveabs", "3", "1", "0", "0", "1", "0", "1");
176
177
178 moveSuccess("moveabs", "4", "1", "1", "0", "0", "0", "0");
179
180
181 moveSuccess("moveabs", "1", "0", "0", "0", "2", "0", "1");
182
183
184
185
186 moveSuccess("moveabs", "2", "0", "1", "0", "2", "-1", "-1");
187
188
189
190 moveSuccess("moveabs", "3", "1", "0", "1", "3", "1", "2");
191
192
193
194
195 moveSuccess("moveabs", "4", "1", "1", "1", "3", "1", "2");
196
197
198
199
200 moveSuccess("moveabs", "5", "1", "2", "1", "3", "-1", "-1");
201
202
203
204 moveSuccess("moveabs", "1", "0", "0", "1", "4", "1", "3");
205
206
207
208
209 moveSuccess("moveabs", "2", "0", "1", "1", "4", "1", "3");
210
211
212
213
214 moveSuccess("moveleft", "1", "0", "0", "0", "0", "-1", "-1");
215
216
217
218
219 moveSuccess("moveleft", "3", "1", "0", "0", "0", "0", "0");
220
221 moveSuccess("moveleft", "4", "1", "1", "0", "1", "0", "1");
222
223 moveSuccess("moveleft", "5", "1", "2", "0", "2", "0", "2");
224
225
226 moveSuccess("moveright", "1", "0", "0", "1", "0", "1", "0");
227
228 moveSuccess("moveright", "2", "0", "1", "1", "1", "1", "1");
229
230 moveSuccess("moveright", "3", "1", "0", "2", "0", "-1", "-1");
231
232 moveSuccess("moveright", "4", "1", "1", "2", "0", "-1", "-1");
233
234 moveSuccess("moveright", "5", "1", "2", "2", "0", "-1", "-1");
235
236
237 moveSuccess("moveup", "2", "0", "1", "0", "0", "0", "0");
238
239 moveSuccess("moveup", "4", "1", "1", "1", "0", "1", "0");
240
241 moveSuccess("moveup", "5", "1", "2", "1", "1", "1", "1");
242
243
244 moveSuccess("movedown", "1", "0", "0", "0", "1", "0", "1");
245
246 moveSuccess("movedown", "2", "0", "1", "0", "1", "-1", "-1");
247
248 moveSuccess("movedown", "3", "1", "0", "1", "1", "1", "1");
249
250 moveSuccess("movedown", "4", "1", "1", "1", "2", "1", "2");
251
252 moveSuccess("movedown", "5", "1", "2", "1", "2", "-1", "-1");
253
254 }
255
256 public void testMoveFailure()
257 {
258 moveFailure("moveabs", "bogus", "0", "0", "0", "0");
259
260 moveFailure("moveleft", "0", "0", "0", "0", "0");
261
262
263 moveFailure("bogus", "0", "0", "0", "0", "0");
264 moveFailure("moveabs", "1", "0", "0", "a", "0");
265 moveFailure("moveabs", "1", "0", "0", "0", "b");
266 }
267
268 public void moveSuccess(String a_sMoveType, String p_sPortletId,
269 String p_sOldCol, String p_sOldRow, String p_sNewCol,
270 String p_sNewRow, String p_sExpectedNewCol, String p_sExpectedNewRow)
271 {
272 try
273 {
274
275 RequestContext rc = null;
276
277 if (a_sMoveType.equalsIgnoreCase("moveabs"))
278 {
279 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId,
280 p_sNewCol, p_sNewRow);
281 } else
282 {
283 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId, null,
284 null);
285 }
286
287 valve.invoke(rc, null);
288
289
290 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
291 "success"));
292 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
293 "<js>"));
294 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
295 "<status>success</status>"));
296 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
297 "<action>" + a_sMoveType + "</action>"));
298 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
299 "<id>" + p_sPortletId + "</id>"));
300 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
301 "<old_position><col>" + p_sOldCol + "</col><row>"
302 + p_sOldRow + "</row></old_position>"));
303 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
304 "<new_position><col>" + p_sExpectedNewCol + "</col><row>"
305 + p_sExpectedNewRow + "</row></new_position>"));
306 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
307 "</js>"));
308 } catch (PipelineException e)
309 {
310 e.printStackTrace();
311 TestLayout.fail("layout valve failed");
312 }
313 }
314
315 public void moveFailure(String a_sMoveType, String p_sPortletId,
316 String p_sOldCol, String p_sOldRow, String p_sNewCol,
317 String p_sNewRow)
318 {
319 try
320 {
321
322 RequestContext rc = null;
323
324 if (a_sMoveType.equalsIgnoreCase("moveabs"))
325 {
326 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId,
327 p_sNewCol, p_sNewRow);
328 } else
329 {
330 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId, null,
331 null);
332 }
333 valve.invoke(rc, null);
334
335
336
337
338 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
339 "<status>failure</status>"));
340 } catch (PipelineException p)
341 {
342 TestLayout.fail("unexpected exception");
343 }
344
345 try
346 {
347
348 RequestContext rc = null;
349
350 if (a_sMoveType.equalsIgnoreCase("moveabs"))
351 {
352 rc = FragmentUtil.setupRequestContext(a_sMoveType, "1234", "0", "foo");
353 } else
354 {
355 rc = FragmentUtil.setupRequestContext(a_sMoveType, "1234", null, null);
356 }
357
358 valve.invoke(rc, null);
359
360
361 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
362 "<status>failure</status>"));
363 } catch (PipelineException p)
364 {
365 TestLayout.fail("unexpected exception");
366 }
367 }
368
369 public void testRemove()
370 {
371 remove("1");
372 remove("2");
373 remove("3");
374 remove("4");
375 }
376
377 private void remove(String p_sPortletId)
378 {
379 try
380 {
381
382 RequestContext rc = null;
383
384 rc = FragmentUtil.setupRequestContext("remove", p_sPortletId, null, null);
385
386 valve.invoke(rc, null);
387
388
389 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
390 "success"));
391 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
392 "<js>"));
393 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
394 "<status>success</status>"));
395 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
396 "<action>" + "remove" + "</action>"));
397 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
398 "<id>" + p_sPortletId + "</id>"));
399 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
400 "<old_position>"));
401 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
402 "<col>"));
403 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
404 "<row>"));
405 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
406 "</old_position>"));
407 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
408 "</js>"));
409 } catch (PipelineException e)
410 {
411 e.printStackTrace();
412 TestLayout.fail("layout valve failed");
413 }
414
415 }
416 }