1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 =  "./"; //PortalTestConstants.JETSPEED_APPLICATION_ROOT;
58          
59          MockServletConfig servletConfig = new MockServletConfig();        
60          ResourceLocatingServletContext servletContent = new ResourceLocatingServletContext(new File(appRoot));        
61          servletConfig.setServletContext(servletContent);
62          ServletConfigFactoryBean.setServletConfig(servletConfig);
63          
64          // Load the Spring configs
65          String[] bootConfigs = null;
66          String[] appConfigs =
67          { //"src/webapp/WEB-INF/assembly/layout-api.xml",
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          // Get the layout that has a null request context        
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              // Test the success case
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             // Test the success case
112             RequestContext rc = FragmentUtil.setupRequestContext("moveabs", "33", "0",
113                     "0");
114             valve.invoke(rc, null);
115 
116             // Take a look at the response to verify a failiure
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             // Test the success case
127             RequestContext rc = FragmentUtil.setupRequestContext("moveabs", "1234",
128                     null, "0");
129             valve.invoke(rc, null);
130 
131             // Take a look at the response to verify a failiure
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             // Test the success case
142             RequestContext rc = FragmentUtil.setupRequestContext("moveabs", "1234",
143                     "0", null);
144             valve.invoke(rc, null);
145 
146             // Take a look at the response to verify a failiure
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"); // Move down
158         moveSuccess("moveright", "1", "0", "0", "1", "0", "1", "0"); // Straight across
159     }
160     
161     public void testMoveSuccess()
162     {
163         moveSuccess("moveabs", "1", "0", "0", "0", "0", "-1", "-1"); // Doesn't
164                                                                     // really
165                                                                     // move
166         moveSuccess("moveabs", "1", "0", "0", "0", "1", "0", "1"); // Move down
167 
168         moveSuccess("moveabs", "2", "0", "1", "0", "0", "0", "0"); // Move up
169         moveSuccess("moveabs", "1", "0", "0", "1", "0", "1", "0"); // Move
170                                                                     // right
171         moveSuccess("moveabs", "3", "1", "0", "0", "0", "0", "0"); // Move left
172         moveSuccess("moveabs", "2", "0", "1", "1", "2", "1", "2"); // Move
173                                                                     // right &
174                                                                     // move down
175         moveSuccess("moveabs", "3", "1", "0", "0", "1", "0", "1"); // Move left
176                                                                     // & move
177                                                                     // down
178         moveSuccess("moveabs", "4", "1", "1", "0", "0", "0", "0"); // Move left
179                                                                     // & move up
180 
181         moveSuccess("moveabs", "1", "0", "0", "0", "2", "0", "1"); // Move too
182                                                                     // far down,
183                                                                     // should be
184                                                                     // at end of
185                                                                     // row
186         moveSuccess("moveabs", "2", "0", "1", "0", "2", "-1", "-1"); // Move too
187                                                                     // far down,
188                                                                     // shouldn't
189                                                                     // move
190         moveSuccess("moveabs", "3", "1", "0", "1", "3", "1", "2"); // Move too
191                                                                     // far down,
192                                                                     // should be
193                                                                     // at end of
194                                                                     // row
195         moveSuccess("moveabs", "4", "1", "1", "1", "3", "1", "2"); // Move too
196                                                                     // far down,
197                                                                     // should be
198                                                                     // at end of
199                                                                     // row
200         moveSuccess("moveabs", "5", "1", "2", "1", "3", "-1", "-1"); // Move too
201                                                                     // far down,
202                                                                     // shouldn't
203                                                                     // move
204         moveSuccess("moveabs", "1", "0", "0", "1", "4", "1", "3"); // Move too
205                                                                     // far down,
206                                                                     // should be
207                                                                     // at end of
208                                                                     // row
209         moveSuccess("moveabs", "2", "0", "1", "1", "4", "1", "3"); // Move too
210                                                                     // far down,
211                                                                     // should be
212                                                                     // at end of
213                                                                     // row
214         moveSuccess("moveleft", "1", "0", "0", "0", "0", "-1", "-1"); // Shouldn't
215                                                                     // move
216 // Root layout ("6") shouldn't/cannot be moved, so the following test doesn't make sense
217 //      moveSuccess("moveleft", "6", "0", "0", "0", "0", "-1", "-1"); // Shouldn't
218                                                                     // move
219         moveSuccess("moveleft", "3", "1", "0", "0", "0", "0", "0"); // Straight
220                                                                     // across
221         moveSuccess("moveleft", "4", "1", "1", "0", "1", "0", "1"); // Straight
222                                                                     // across
223         moveSuccess("moveleft", "5", "1", "2", "0", "2", "0", "2"); // Straight
224                                                                     // across
225 
226         moveSuccess("moveright", "1", "0", "0", "1", "0", "1", "0"); // Straight
227                                                                         // across
228         moveSuccess("moveright", "2", "0", "1", "1", "1", "1", "1"); // Straight
229                                                                         // across
230         moveSuccess("moveright", "3", "1", "0", "2", "0", "-1", "-1"); // Shouldn't
231                                                                         // move
232         moveSuccess("moveright", "4", "1", "1", "2", "0", "-1", "-1"); // Shouldn't
233                                                                         // move
234         moveSuccess("moveright", "5", "1", "2", "2", "0", "-1", "-1"); // Shouldn't
235                                                                         // move
236 
237         moveSuccess("moveup", "2", "0", "1", "0", "0", "0", "0"); // Straight
238                                                                     // across
239         moveSuccess("moveup", "4", "1", "1", "1", "0", "1", "0"); // Straight
240                                                                     // across
241         moveSuccess("moveup", "5", "1", "2", "1", "1", "1", "1"); // Straight
242                                                                     // across
243 
244         moveSuccess("movedown", "1", "0", "0", "0", "1", "0", "1"); // Straight
245                                                                     // across
246         moveSuccess("movedown", "2", "0", "1", "0", "1", "-1", "-1"); // Shouldn't
247                                                                     // move
248         moveSuccess("movedown", "3", "1", "0", "1", "1", "1", "1"); // Straight
249                                                                     // across
250         moveSuccess("movedown", "4", "1", "1", "1", "2", "1", "2"); // Straight
251                                                                     // across
252         moveSuccess("movedown", "5", "1", "2", "1", "2", "-1", "-1"); // Shouldn't
253                                                                     // move
254     }
255 
256     public void testMoveFailure()
257     {
258         moveFailure("moveabs", "bogus", "0", "0", "0", "0"); // non integer
259                                                                 // portlet id
260         moveFailure("moveleft", "0", "0", "0", "0", "0"); // portlet not found
261         // moveFailure("moveabs", "1", "0", "0", "3", "0"); // non existent
262                                                             // column
263         moveFailure("bogus", "0", "0", "0", "0", "0"); // bogus action
264         moveFailure("moveabs", "1", "0", "0", "a", "0"); // non integer value
265         moveFailure("moveabs", "1", "0", "0", "0", "b"); // non integer value
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             // Test the success case
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             // Take a look at the response to verify a failiure
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             // Test failure case
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             //FragmentUtil.debugContentOutput(rc);
336             
337             // Take a look at the response to verify a failure            
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             // Test failure case
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             // Take a look at the response to verify a failiure
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             // Test the success case
382             RequestContext rc = null;
383 
384             rc = FragmentUtil.setupRequestContext("remove", p_sPortletId, null, null);
385 
386             valve.invoke(rc, null);
387 
388             // Take a look at the response to verify a failiure
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 }