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", "1", "0", "1", "1", "1", "1"); // Move down
158         moveSuccess("moveright", "1", "1", "0", "2", "0", "2", "0"); // Straight across
159     }
160     
161     public void testMoveSuccess()
162     {
163         moveSuccess("moveabs", "1", "1", "0", "1", "0", "1", "0"); // Doesn't
164                                                                     // really
165                                                                     // move
166         moveSuccess("moveabs", "1", "1", "0", "1", "1", "1", "1"); // Move down
167 
168         moveSuccess("moveabs", "2", "1", "1", "1", "0", "1", "0"); // Move up
169         moveSuccess("moveabs", "1", "1", "0", "2", "0", "2", "0"); // Move
170                                                                     // right
171         moveSuccess("moveabs", "3", "2", "0", "1", "0", "1", "0"); // Move left
172         moveSuccess("moveabs", "2", "1", "1", "2", "2", "2", "2"); // Move
173                                                                     // right &
174                                                                     // move down
175         moveSuccess("moveabs", "3", "2", "0", "1", "1", "1", "1"); // Move left
176                                                                     // & move
177                                                                     // down
178         moveSuccess("moveabs", "4", "2", "1", "1", "0", "1", "0"); // Move left
179                                                                     // & move up
180 
181         moveSuccess("moveabs", "1", "1", "0", "1", "2", "1", "1"); // Move too
182                                                                     // far down,
183                                                                     // should be
184                                                                     // at end of
185                                                                     // row
186         moveSuccess("moveabs", "2", "1", "1", "1", "2", "1", "1"); // Move too
187                                                                     // far down,
188                                                                     // should be
189                                                                     // at end of
190                                                                     // row
191         moveSuccess("moveabs", "3", "2", "0", "2", "3", "2", "2"); // Move too
192                                                                     // far down,
193                                                                     // should be
194                                                                     // at end of
195                                                                     // row
196         moveSuccess("moveabs", "4", "2", "1", "2", "3", "2", "2"); // Move too
197                                                                     // far down,
198                                                                     // should be
199                                                                     // at end of
200                                                                     // row
201         moveSuccess("moveabs", "5", "2", "2", "2", "3", "2", "2"); // Move too
202                                                                     // far down,
203                                                                     // should be
204                                                                     // at end of
205                                                                     // row
206         moveSuccess("moveabs", "1", "1", "0", "2", "4", "2", "3"); // Move too
207                                                                     // far down,
208                                                                     // should be
209                                                                     // at end of
210                                                                     // row
211         moveSuccess("moveabs", "2", "1", "1", "2", "4", "2", "3"); // Move too
212                                                                     // far down,
213                                                                     // should be
214                                                                     // at end of
215                                                                     // row
216 
217         moveSuccess("moveleft", "1", "1", "0", "0", "0", "0", "0"); // Shouldn't
218                                                                     // move
219         moveSuccess("moveleft", "6", "0", "0", "0", "0", "0", "0"); // Shouldn't
220                                                                     // move
221         moveSuccess("moveleft", "3", "2", "0", "1", "0", "1", "0"); // Straight
222                                                                     // across
223         moveSuccess("moveleft", "4", "2", "1", "1", "1", "1", "1"); // Straight
224                                                                     // across
225         moveSuccess("moveleft", "5", "2", "2", "1", "2", "1", "2"); // Straight
226                                                                     // across
227 
228         moveSuccess("moveright", "1", "1", "0", "2", "0", "2", "0"); // Straight
229                                                                         // across
230         moveSuccess("moveright", "2", "1", "1", "2", "1", "2", "1"); // Straight
231                                                                         // across
232         moveSuccess("moveright", "3", "2", "0", "3", "0", "3", "0"); // Shouldn't
233 //                                                                        // move
234         moveSuccess("moveright", "4", "2", "1", "3", "0", "3", "0"); // Shouldn't
235                                                                         // move
236         moveSuccess("moveright", "5", "2", "2", "3", "0", "3", "0"); // Shouldn't
237                                                                         // move
238 
239         moveSuccess("moveup", "2", "1", "1", "1", "0", "1", "0"); // Straight
240                                                                     // across
241         moveSuccess("moveup", "4", "2", "1", "2", "0", "2", "0"); // Straight
242                                                                     // across
243         moveSuccess("moveup", "5", "2", "2", "2", "1", "2", "1"); // Straight
244                                                                     // across
245 
246         moveSuccess("movedown", "1", "1", "0", "1", "1", "1", "1"); // Straight
247                                                                     // across
248         moveSuccess("movedown", "2", "1", "1", "1", "1", "1", "1"); // Shouldn't
249                                                                     // move
250         moveSuccess("movedown", "3", "2", "0", "2", "1", "2", "1"); // Straight
251                                                                     // across
252         moveSuccess("movedown", "4", "2", "1", "2", "2", "2", "2"); // Straight
253                                                                     // across
254         moveSuccess("movedown", "5", "2", "2", "2", "2", "2", "2"); // Shouldn't
255                                                                     // move
256     }
257 
258     public void testMoveFailure()
259     {
260         moveFailure("moveabs", "bogus", "0", "0", "0", "0"); // non integer
261                                                                 // portlet id
262         moveFailure("moveleft", "0", "0", "0", "0", "0"); // portlet not found
263         // moveFailure("moveabs", "1", "0", "0", "3", "0"); // non existent
264                                                             // column
265         moveFailure("bogus", "0", "0", "0", "0", "0"); // bogus action
266         moveFailure("moveabs", "1", "0", "0", "a", "0"); // non integer value
267         moveFailure("moveabs", "1", "0", "0", "0", "b"); // non integer value
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             // Test the success case
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             // Take a look at the response to verify a failiure
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             // Test failure case
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             //FragmentUtil.debugContentOutput(rc);
338             
339             // Take a look at the response to verify a failure            
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             // Test failure case
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             // Take a look at the response to verify a failiure
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             // Test the success case
384             RequestContext rc = null;
385 
386             rc = FragmentUtil.setupRequestContext("remove", p_sPortletId, null, null);
387 
388             valve.invoke(rc, null);
389 
390             // Take a look at the response to verify a failiure
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 }