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 org.apache.jetspeed.om.page.ContentPage;
20  import org.apache.jetspeed.om.page.Fragment;
21  import org.apache.jetspeed.om.page.Page;
22  import org.apache.jetspeed.om.page.ContentPageImpl;
23  import org.apache.jetspeed.om.page.psml.PageImpl;
24  import org.apache.jetspeed.request.JetspeedRequestContext;
25  import org.apache.jetspeed.request.RequestContext;
26  
27  import com.mockrunner.mock.web.MockHttpServletRequest;
28  import com.mockrunner.mock.web.MockHttpServletResponse;
29  import com.mockrunner.mock.web.MockHttpSession;
30  import com.mockrunner.mock.web.MockServletConfig;
31  import com.mockrunner.mock.web.MockServletContext;
32  
33  /***
34   * Test for Fragment placement
35   * 
36   * @author <a>David Gurney </a>
37   * @version $Id: $
38   */
39  public class FragmentUtil
40  {
41  
42      public static RequestContext buildFullRequestContext()
43      {
44          // Build a request object and populate it with fragments
45          RequestContext a_oRC = setupRequestContext("remove", "1234", "0", "0");
46  
47          // Build some fragments and add them to the request context
48          // Prepare some fragments
49          Fragment a_oLayout = buildFragment("layout", "6", "layout", 0, 0);
50          Fragment a_oFrag1 = buildFragment("frag1", "1", "portlet", 1, 0);
51          Fragment a_oFrag2 = buildFragment("frag2", "2", "portlet", 1, 1);
52          Fragment a_oFrag3 = buildFragment("frag3", "3", "portlet", 2, 0);
53          Fragment a_oFrag4 = buildFragment("frag4", "4", "portlet", 2, 1);
54          Fragment a_oFrag5 = buildFragment("frag5", "5", "portlet", 2, 2);
55  
56          LocalFragmentImpl a_oLocalLayout = (LocalFragmentImpl) a_oLayout;
57          a_oLocalLayout.addFragment(a_oFrag1);
58          a_oLocalLayout.addFragment(a_oFrag2);
59          a_oLocalLayout.addFragment(a_oFrag3);
60          a_oLocalLayout.addFragment(a_oFrag4);
61          a_oLocalLayout.addFragment(a_oFrag5);
62  
63          Page a_oPage = new PageImpl();
64          a_oPage.setRootFragment(a_oLayout);
65          ContentPage a_oContentPage = new ContentPageImpl(a_oPage);
66          a_oRC.setPage(a_oContentPage);
67  
68          return a_oRC;
69      }
70  
71      // Helper method to find a string within the response
72      public static boolean findValue(RequestContext p_oRequestContext,
73              String p_sValue)
74      {
75          MockHttpServletResponse mr = (MockHttpServletResponse) p_oRequestContext
76                  .getResponse();
77          String a_sContent = mr.getOutputStreamContent();
78          boolean a_bResults = a_sContent.indexOf(p_sValue) >= 0;
79          return a_bResults;
80      }
81  
82      // Helper method
83      public static RequestContext setupRequestContext(String p_sAction,
84              String p_sPortletId, String p_sCol, String p_sRow)
85      {
86          MockServletConfig config = new MockServletConfig();
87          MockServletContext context = new MockServletContext();
88          MockHttpSession session = new MockHttpSession();
89          session.setupServletContext(context);
90          MockHttpServletRequest request = new MockHttpServletRequest();
91          request.setupAddParameter("action", p_sAction);
92          request.setupAddParameter("id", p_sPortletId);
93          if (p_sRow != null)
94          {
95              request.setupAddParameter("row", p_sRow);
96          }
97          if (p_sCol != null)
98          {
99              request.setupAddParameter("col", p_sCol);
100         }
101 
102         request.setSession(session);
103         MockHttpServletResponse response = new MockHttpServletResponse();
104 
105         RequestContext a_oRC = new JetspeedRequestContext(request, response,
106                 config, null);
107 
108         Page a_oPage = setupPage();
109         ContentPage a_oContentPage = new ContentPageImpl(a_oPage);
110 
111         a_oRC.setPage(a_oContentPage);
112 
113         return a_oRC;
114     }
115 
116     // Helper method
117     public static Page setupPage()
118     {
119         // Prepare some fragments
120         Fragment a_oLayout = buildFragment("layout", "6", "layout", 0, 0);
121         Fragment a_oFrag1 = buildFragment("frag1", "1", "portlet", 1, 0);
122         Fragment a_oFrag2 = buildFragment("frag2", "2", "portlet", 1, 1);
123         Fragment a_oFrag3 = buildFragment("frag3", "3", "portlet", 2, 0);
124         Fragment a_oFrag4 = buildFragment("frag4", "4", "portlet", 2, 1);
125         Fragment a_oFrag5 = buildFragment("frag5", "5", "portlet", 2, 2);
126 
127         LocalFragmentImpl a_oLocalLayout = (LocalFragmentImpl) a_oLayout;
128         a_oLocalLayout.addFragment(a_oFrag1);
129         a_oLocalLayout.addFragment(a_oFrag2);
130         a_oLocalLayout.addFragment(a_oFrag3);
131         a_oLocalLayout.addFragment(a_oFrag4);
132         a_oLocalLayout.addFragment(a_oFrag5);
133 
134         Page a_oPage = new PageImpl();
135         a_oPage.setRootFragment(a_oLayout);
136 
137         return a_oPage;
138     }
139 
140     public static Fragment buildFragment(String p_sName, String p_sId,
141             String p_sType, int p_iCol, int p_iRow)
142     {
143         LocalFragmentImpl a_oFrag = new LocalFragmentImpl();
144         a_oFrag.setName(p_sName);
145         a_oFrag.setType(p_sType);
146         a_oFrag.setLayoutColumn(p_iCol);
147         a_oFrag.setLayoutRow(p_iRow);
148         a_oFrag.setId(p_sId);
149         return a_oFrag;
150     }
151     
152     public static void debugContentOutput(RequestContext rc)
153     {
154         MockHttpServletResponse mr = (MockHttpServletResponse) rc.getResponse();        
155         String content = mr.getOutputStreamContent();
156         System.out.println("content = " + content);
157     }
158 
159 }