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", "1", "0", "1", "1", "1", "1");
158 moveSuccess("moveright", "1", "1", "0", "2", "0", "2", "0");
159 }
160
161 public void testMoveSuccess()
162 {
163 moveSuccess("moveabs", "1", "1", "0", "1", "0", "1", "0");
164
165
166 moveSuccess("moveabs", "1", "1", "0", "1", "1", "1", "1");
167
168 moveSuccess("moveabs", "2", "1", "1", "1", "0", "1", "0");
169 moveSuccess("moveabs", "1", "1", "0", "2", "0", "2", "0");
170
171 moveSuccess("moveabs", "3", "2", "0", "1", "0", "1", "0");
172 moveSuccess("moveabs", "2", "1", "1", "2", "2", "2", "2");
173
174
175 moveSuccess("moveabs", "3", "2", "0", "1", "1", "1", "1");
176
177
178 moveSuccess("moveabs", "4", "2", "1", "1", "0", "1", "0");
179
180
181 moveSuccess("moveabs", "1", "1", "0", "1", "2", "1", "1");
182
183
184
185
186 moveSuccess("moveabs", "2", "1", "1", "1", "2", "1", "1");
187
188
189
190
191 moveSuccess("moveabs", "3", "2", "0", "2", "3", "2", "2");
192
193
194
195
196 moveSuccess("moveabs", "4", "2", "1", "2", "3", "2", "2");
197
198
199
200
201 moveSuccess("moveabs", "5", "2", "2", "2", "3", "2", "2");
202
203
204
205
206 moveSuccess("moveabs", "1", "1", "0", "2", "4", "2", "3");
207
208
209
210
211 moveSuccess("moveabs", "2", "1", "1", "2", "4", "2", "3");
212
213
214
215
216
217 moveSuccess("moveleft", "1", "1", "0", "0", "0", "0", "0");
218
219 moveSuccess("moveleft", "6", "0", "0", "0", "0", "0", "0");
220
221 moveSuccess("moveleft", "3", "2", "0", "1", "0", "1", "0");
222
223 moveSuccess("moveleft", "4", "2", "1", "1", "1", "1", "1");
224
225 moveSuccess("moveleft", "5", "2", "2", "1", "2", "1", "2");
226
227
228 moveSuccess("moveright", "1", "1", "0", "2", "0", "2", "0");
229
230 moveSuccess("moveright", "2", "1", "1", "2", "1", "2", "1");
231
232 moveSuccess("moveright", "3", "2", "0", "3", "0", "3", "0");
233
234 moveSuccess("moveright", "4", "2", "1", "3", "0", "3", "0");
235
236 moveSuccess("moveright", "5", "2", "2", "3", "0", "3", "0");
237
238
239 moveSuccess("moveup", "2", "1", "1", "1", "0", "1", "0");
240
241 moveSuccess("moveup", "4", "2", "1", "2", "0", "2", "0");
242
243 moveSuccess("moveup", "5", "2", "2", "2", "1", "2", "1");
244
245
246 moveSuccess("movedown", "1", "1", "0", "1", "1", "1", "1");
247
248 moveSuccess("movedown", "2", "1", "1", "1", "1", "1", "1");
249
250 moveSuccess("movedown", "3", "2", "0", "2", "1", "2", "1");
251
252 moveSuccess("movedown", "4", "2", "1", "2", "2", "2", "2");
253
254 moveSuccess("movedown", "5", "2", "2", "2", "2", "2", "2");
255
256 }
257
258 public void testMoveFailure()
259 {
260 moveFailure("moveabs", "bogus", "0", "0", "0", "0");
261
262 moveFailure("moveleft", "0", "0", "0", "0", "0");
263
264
265 moveFailure("bogus", "0", "0", "0", "0", "0");
266 moveFailure("moveabs", "1", "0", "0", "a", "0");
267 moveFailure("moveabs", "1", "0", "0", "0", "b");
268 }
269
270 public void moveSuccess(String a_sMoveType, String p_sPortletId,
271 String p_sOldCol, String p_sOldRow, String p_sNewCol,
272 String p_sNewRow, String p_sExpectedNewCol, String p_sExpectedNewRow)
273 {
274 try
275 {
276
277 RequestContext rc = null;
278
279 if (a_sMoveType.equalsIgnoreCase("moveabs"))
280 {
281 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId,
282 p_sNewCol, p_sNewRow);
283 } else
284 {
285 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId, null,
286 null);
287 }
288
289 valve.invoke(rc, null);
290
291
292 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
293 "success"));
294 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
295 "<js>"));
296 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
297 "<status>success</status>"));
298 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
299 "<action>" + a_sMoveType + "</action>"));
300 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
301 "<id>" + p_sPortletId + "</id>"));
302 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
303 "<old_position><col>" + p_sOldCol + "</col><row>"
304 + p_sOldRow + "</row></old_position>"));
305 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
306 "<new_position><col>" + p_sExpectedNewCol + "</col><row>"
307 + p_sExpectedNewRow + "</row></new_position>"));
308 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
309 "</js>"));
310 } catch (PipelineException e)
311 {
312 e.printStackTrace();
313 TestLayout.fail("layout valve failed");
314 }
315 }
316
317 public void moveFailure(String a_sMoveType, String p_sPortletId,
318 String p_sOldCol, String p_sOldRow, String p_sNewCol,
319 String p_sNewRow)
320 {
321 try
322 {
323
324 RequestContext rc = null;
325
326 if (a_sMoveType.equalsIgnoreCase("moveabs"))
327 {
328 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId,
329 p_sNewCol, p_sNewRow);
330 } else
331 {
332 rc = FragmentUtil.setupRequestContext(a_sMoveType, p_sPortletId, null,
333 null);
334 }
335 valve.invoke(rc, null);
336
337
338
339
340 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
341 "<status>failure</status>"));
342 } catch (PipelineException p)
343 {
344 TestLayout.fail("unexpected exception");
345 }
346
347 try
348 {
349
350 RequestContext rc = null;
351
352 if (a_sMoveType.equalsIgnoreCase("moveabs"))
353 {
354 rc = FragmentUtil.setupRequestContext(a_sMoveType, "1234", "0", "foo");
355 } else
356 {
357 rc = FragmentUtil.setupRequestContext(a_sMoveType, "1234", null, null);
358 }
359
360 valve.invoke(rc, null);
361
362
363 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
364 "<status>failure</status>"));
365 } catch (PipelineException p)
366 {
367 TestLayout.fail("unexpected exception");
368 }
369 }
370
371 public void testRemove()
372 {
373 remove("1");
374 remove("2");
375 remove("3");
376 remove("4");
377 }
378
379 private void remove(String p_sPortletId)
380 {
381 try
382 {
383
384 RequestContext rc = null;
385
386 rc = FragmentUtil.setupRequestContext("remove", p_sPortletId, null, null);
387
388 valve.invoke(rc, null);
389
390
391 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
392 "success"));
393 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
394 "<js>"));
395 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
396 "<status>success</status>"));
397 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
398 "<action>" + "remove" + "</action>"));
399 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
400 "<id>" + p_sPortletId + "</id>"));
401 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
402 "<old_position>"));
403 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
404 "<col>"));
405 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
406 "<row>"));
407 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
408 "</old_position>"));
409 TestLayout.assertTrue("couldn't find value", FragmentUtil.findValue(rc,
410 "</js>"));
411 } catch (PipelineException e)
412 {
413 e.printStackTrace();
414 TestLayout.fail("layout valve failed");
415 }
416
417 }
418 }