001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.layout;
018:
019: import javax.security.auth.Subject;
020:
021: import org.apache.jetspeed.om.page.ContentPage;
022: import org.apache.jetspeed.om.page.Fragment;
023: import org.apache.jetspeed.om.page.Page;
024: import org.apache.jetspeed.om.page.ContentPageImpl;
025: import org.apache.jetspeed.om.page.psml.PageImpl;
026: import org.apache.jetspeed.request.JetspeedRequestContext;
027: import org.apache.jetspeed.request.RequestContext;
028:
029: import com.mockrunner.mock.web.MockHttpServletRequest;
030: import com.mockrunner.mock.web.MockHttpServletResponse;
031: import com.mockrunner.mock.web.MockHttpSession;
032: import com.mockrunner.mock.web.MockServletConfig;
033: import com.mockrunner.mock.web.MockServletContext;
034:
035: /**
036: * Test for Fragment placement
037: *
038: * @author <a>David Gurney </a>
039: * @version $Id: $
040: */
041: public class FragmentUtil {
042:
043: public static RequestContext buildFullRequestContext() {
044: // Build a request object and populate it with fragments
045: RequestContext a_oRC = setupRequestContext("remove", "1234",
046: "0", "0");
047:
048: ContentPage a_oContentPage = new ContentPageImpl(setupPage());
049: a_oRC.setPage(a_oContentPage);
050:
051: return a_oRC;
052: }
053:
054: // Helper method to find a string within the response
055: public static boolean findValue(RequestContext p_oRequestContext,
056: String p_sValue) {
057: MockHttpServletResponse mr = (MockHttpServletResponse) p_oRequestContext
058: .getResponse();
059: String a_sContent = mr.getOutputStreamContent();
060: boolean a_bResults = a_sContent.indexOf(p_sValue) >= 0;
061: return a_bResults;
062: }
063:
064: // Helper method
065: public static RequestContext setupRequestContext(String p_sAction,
066: String p_sPortletId, String p_sCol, String p_sRow) {
067: MockServletConfig config = new MockServletConfig();
068: MockServletContext context = new MockServletContext();
069: MockHttpSession session = new MockHttpSession();
070: session.setupServletContext(context);
071: MockHttpServletRequest request = new MockHttpServletRequest();
072: request.setupAddParameter("action", p_sAction);
073: request.setupAddParameter("id", p_sPortletId);
074: if (p_sRow != null) {
075: request.setupAddParameter("row", p_sRow);
076: }
077: if (p_sCol != null) {
078: request.setupAddParameter("col", p_sCol);
079: }
080:
081: request.setSession(session);
082: MockHttpServletResponse response = new MockHttpServletResponse();
083:
084: RequestContext a_oRC = new JetspeedRequestContext(request,
085: response, config, null);
086:
087: a_oRC.setSubject(new Subject());
088:
089: Page a_oPage = setupPage();
090: ContentPage a_oContentPage = new ContentPageImpl(a_oPage);
091:
092: a_oRC.setPage(a_oContentPage);
093:
094: return a_oRC;
095: }
096:
097: // Helper method
098: public static Page setupPage() {
099: // Prepare some fragments
100: Fragment a_oLayout = buildFragment("layout", "6", "layout", 0,
101: 0);
102: Fragment a_oFrag1 = buildFragment("frag1", "1", "portlet", 0, 0);
103: Fragment a_oFrag2 = buildFragment("frag2", "2", "portlet", 0, 1);
104: Fragment a_oFrag3 = buildFragment("frag3", "3", "portlet", 1, 0);
105: Fragment a_oFrag4 = buildFragment("frag4", "4", "portlet", 1, 1);
106: Fragment a_oFrag5 = buildFragment("frag5", "5", "portlet", 1, 2);
107:
108: LocalFragmentImpl a_oLocalLayout = (LocalFragmentImpl) a_oLayout;
109: a_oLocalLayout.addFragment(a_oFrag1);
110: a_oLocalLayout.addFragment(a_oFrag2);
111: a_oLocalLayout.addFragment(a_oFrag3);
112: a_oLocalLayout.addFragment(a_oFrag4);
113: a_oLocalLayout.addFragment(a_oFrag5);
114:
115: Page a_oPage = new PageImpl();
116: a_oPage.setRootFragment(a_oLayout);
117:
118: return a_oPage;
119: }
120:
121: public static Fragment buildFragment(String p_sName, String p_sId,
122: String p_sType, int p_iCol, int p_iRow) {
123: LocalFragmentImpl a_oFrag = new LocalFragmentImpl();
124: a_oFrag.setName(p_sName);
125: a_oFrag.setType(p_sType);
126: a_oFrag.setLayoutColumn(p_iCol);
127: a_oFrag.setLayoutRow(p_iRow);
128: a_oFrag.setId(p_sId);
129: return a_oFrag;
130: }
131:
132: public static void debugContentOutput(RequestContext rc) {
133: MockHttpServletResponse mr = (MockHttpServletResponse) rc
134: .getResponse();
135: String content = mr.getOutputStreamContent();
136: System.out.println("content = " + content);
137: }
138:
139: public static String getContentOutput(RequestContext rc) {
140: MockHttpServletResponse mr = (MockHttpServletResponse) rc
141: .getResponse();
142: return mr.getOutputStreamContent();
143: }
144:
145: }
|