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