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.container.state;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021:
022: import javax.portlet.PortletMode;
023: import javax.portlet.WindowState;
024: import javax.servlet.http.HttpServletRequest;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.jetspeed.PortalContext;
031: import org.apache.jetspeed.cache.JetspeedContentCache;
032: import org.apache.jetspeed.container.state.impl.NavigationalStateCodec;
033: import org.apache.jetspeed.container.state.impl.PathNavigationalState;
034: import org.apache.jetspeed.container.state.impl.SessionFullNavigationalState;
035: import org.apache.jetspeed.container.state.impl.SessionNavigationalState;
036: import org.apache.jetspeed.container.url.PortalURL;
037: import org.apache.jetspeed.container.url.impl.AbstractPortalURL;
038: import org.apache.jetspeed.container.url.impl.PathInfoEncodingPortalURL;
039: import org.apache.jetspeed.container.url.impl.QueryStringEncodingPortalURL;
040: import org.apache.jetspeed.container.window.PortletWindowAccessor;
041: import org.apache.jetspeed.engine.Engine;
042: import org.apache.jetspeed.factory.PortletFactory;
043: import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
044: import org.apache.jetspeed.om.common.portlet.PortletApplication;
045: import org.apache.jetspeed.om.window.impl.PortletWindowImpl;
046: import org.apache.jetspeed.testhelpers.SpringEngineHelper;
047: import org.apache.jetspeed.util.JetspeedLongObjectID;
048: import org.apache.pluto.om.entity.PortletEntity;
049: import org.apache.pluto.om.portlet.PortletDefinition;
050: import org.apache.pluto.om.window.PortletWindow;
051: import org.apache.pluto.om.window.PortletWindowList;
052: import org.apache.pluto.om.window.PortletWindowListCtrl;
053: import org.jmock.Mock;
054: import org.jmock.core.matcher.AnyArgumentsMatcher;
055: import org.jmock.core.stub.ReturnStub;
056: import org.jmock.core.stub.VoidStub;
057:
058: import com.mockrunner.mock.web.MockHttpServletRequest;
059: import com.mockrunner.mock.web.MockHttpSession;
060:
061: /**
062: * TestPortletContainer
063: *
064: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
065: * @version $Id: TestNavigationalState.java 550655 2007-06-26 01:41:35Z taylor $
066: */
067:
068: public class TestNavigationalState extends TestCase {
069: // needed to be able to Mock PortletWindowListCtrl
070: private interface CompositeWindowList extends PortletWindowList,
071: PortletWindowListCtrl {
072: }
073:
074: private SpringEngineHelper engineHelper;
075: private Engine engine;
076: private NavigationalStateCodec codec;
077: private PortalContext portalContext;
078: private JetspeedContentCache cache;
079:
080: /**
081: * Defines the testcase name for JUnit.
082: *
083: * @param name the testcase's name.
084: */
085: public TestNavigationalState(String name) {
086: super (name);
087: }
088:
089: /**
090: * Start the tests.
091: *
092: * @param args the arguments. Not used
093: */
094: public static void main(String args[]) {
095: junit.awtui.TestRunner
096: .main(new String[] { TestNavigationalState.class
097: .getName() });
098: }
099:
100: protected void setUp() throws Exception {
101: super .setUp();
102:
103: HashMap context = new HashMap();
104: engineHelper = new SpringEngineHelper(context);
105: engineHelper.setUp();
106: engine = (Engine) context.get(SpringEngineHelper.ENGINE_ATTR);
107: // mock test PortletWindow, PortletEntity, PortletDefinition and PortletApplication
108: Mock entityMock = new Mock(MutablePortletEntity.class);
109: Mock portletDefinitionMock = new Mock(PortletDefinition.class);
110: Mock portletApplicationMock = new Mock(PortletApplication.class);
111: Mock windowListMock = new Mock(CompositeWindowList.class);
112: PortletWindowListCtrl windowList = (PortletWindowListCtrl) windowListMock
113: .proxy();
114: entityMock.expects(new AnyArgumentsMatcher()).method(
115: "getPortletWindowList").withNoArguments().will(
116: new ReturnStub(windowList));
117: windowListMock.expects(new AnyArgumentsMatcher()).method("add")
118: .withAnyArguments().will(new VoidStub());
119: portletApplicationMock.expects(new AnyArgumentsMatcher())
120: .method("getId").withNoArguments().will(
121: new ReturnStub(new JetspeedLongObjectID(1)));
122: portletDefinitionMock.expects(new AnyArgumentsMatcher())
123: .method("getPortletApplicationDefinition")
124: .withNoArguments().will(
125: new ReturnStub(portletApplicationMock.proxy()));
126: entityMock.expects(new AnyArgumentsMatcher()).method(
127: "getPortletDefinition").withNoArguments().will(
128: new ReturnStub(portletDefinitionMock.proxy()));
129: PortletWindowAccessor accessor = (PortletWindowAccessor) engine
130: .getComponentManager().getComponent(
131: PortletWindowAccessor.class);
132: accessor.createPortletWindow(
133: (PortletEntity) entityMock.proxy(), "111");
134: accessor.createPortletWindow(
135: (PortletEntity) entityMock.proxy(), "222");
136: accessor.createPortletWindow(
137: (PortletEntity) entityMock.proxy(), "333");
138:
139: // register mocked PortletApplication in PortletFactory so the PortletWindowAccessor check for it won't break the tests
140: PortletFactory portletFactory = (PortletFactory) engine
141: .getComponentManager().getComponent("portletFactory");
142: portletFactory.registerPortletApplication(
143: (PortletApplication) portletApplicationMock.proxy(),
144: Thread.currentThread().getContextClassLoader());
145:
146: codec = (NavigationalStateCodec) engine.getComponentManager()
147: .getComponent("NavigationalStateCodec");
148: portalContext = (PortalContext) engine.getComponentManager()
149: .getComponent("PortalContext");
150: cache = (JetspeedContentCache) engine.getComponentManager()
151: .getComponent("portletContentCache");
152: }
153:
154: public static Test suite() {
155: // All methods starting with "test" will be executed in the test suite.
156: return new TestSuite(TestNavigationalState.class);
157: }
158:
159: public void testSessionFullStateAndQuery() {
160: SessionFullNavigationalState navState = new SessionFullNavigationalState(
161: codec, cache);
162: QueryStringEncodingPortalURL portalUrl = new QueryStringEncodingPortalURL(
163: navState, portalContext);
164: HttpServletRequest request = buildRequest(portalUrl, true);
165: navState = new SessionFullNavigationalState(codec, cache);
166: portalUrl = new QueryStringEncodingPortalURL(navState,
167: portalContext);
168: doTestUrl(portalUrl, request);
169:
170: }
171:
172: public void testSessionStateAndPathInfo() {
173: SessionNavigationalState navState = new SessionNavigationalState(
174: codec, cache);
175: PathInfoEncodingPortalURL portalUrl = new PathInfoEncodingPortalURL(
176: navState, portalContext);
177: HttpServletRequest request = buildRequest(portalUrl, false);
178: navState = new SessionNavigationalState(codec, cache);
179: portalUrl = new PathInfoEncodingPortalURL(navState,
180: portalContext);
181: doTestUrl(portalUrl, request);
182: }
183:
184: public void testPathStateAndPathInfo() {
185: PathNavigationalState navState = new PathNavigationalState(
186: codec, cache);
187: PathInfoEncodingPortalURL portalUrl = new PathInfoEncodingPortalURL(
188: navState, portalContext);
189: HttpServletRequest request = buildRequest(portalUrl, false);
190: navState = new PathNavigationalState(codec, cache);
191: portalUrl = new PathInfoEncodingPortalURL(navState,
192: portalContext);
193: doTestUrl(portalUrl, request);
194: }
195:
196: protected HttpServletRequest buildRequest(PortalURL portalURL,
197: boolean useQueryStringPortalURL) {
198: MockHttpServletRequest request = new MockHttpServletRequest();
199: MockHttpSession session = new MockHttpSession();
200:
201: request.setSession(session);
202: request.setServerName("www.sporteportal.com");
203: request.setScheme("http");
204: request.setContextPath("/jetspeed");
205: request.setServletPath("/portal");
206: request.setPathInfo("stuff");
207: request.setRequestURI("/jetspeed/portal/stuff");
208:
209: portalURL.setRequest(request);
210: portalURL.setCharacterEncoding("UTF-8");
211:
212: PortletWindow window = new PortletWindowImpl("111");
213:
214: HashMap parameters = new HashMap();
215: parameters.put("test", new String[] { "one", "two", "three" });
216:
217: String portletURL = portalURL.createPortletURL(window,
218: parameters, PortletMode.EDIT, WindowState.MAXIMIZED,
219: true, false);
220:
221: String navStateParameterName = engine.getContext()
222: .getConfigurationProperty(
223: "portalurl.navigationalstate.parameter.name",
224: AbstractPortalURL.DEFAULT_NAV_STATE_PARAMETER);
225:
226: if (useQueryStringPortalURL) {
227: request.setupAddParameter(navStateParameterName, portletURL
228: .substring(portletURL.indexOf('=') + 1));
229: } else {
230: request.setPathInfo(portletURL.substring(portletURL
231: .indexOf("/portal") + 7));
232: }
233:
234: return request;
235: }
236:
237: protected void doTestUrl(PortalURL portalURL,
238: HttpServletRequest request) {
239: portalURL.setRequest(request);
240: portalURL.setCharacterEncoding("UTF-8");
241:
242: PortletWindow window = new PortletWindowImpl("111");
243: NavigationalState nav = portalURL.getNavigationalState();
244:
245: // Check that they come out correctly
246: assertTrue("window mode is not set", nav.getMode(window)
247: .equals(PortletMode.EDIT));
248: assertTrue("window state is not set", nav.getState(window)
249: .equals(WindowState.MAXIMIZED));
250: PortletWindow target = nav.getPortletWindowOfAction();
251: assertNotNull("target window is null", target);
252: assertEquals("target window should equal window 111", target
253: .getId(), "111");
254:
255: PortletWindow maximizedWindow = nav.getMaximizedWindow();
256: assertNotNull("maximized window is null", maximizedWindow);
257: assertEquals("maximized window should equal window 111",
258: maximizedWindow.getId(), "111");
259:
260: Iterator iter = nav.getParameterNames(target);
261: assertTrue("There should be one parameter", iter.hasNext());
262: while (iter.hasNext()) {
263: assertEquals("parameter name should equals \"test\"",
264: (String) iter.next(), "test");
265: String[] values = nav.getParameterValues(target, "test");
266: assertNotNull("parameter name has no values", values);
267: assertEquals("parameter test should have 3 values",
268: values.length, 3);
269: assertEquals("parameter test[0] should be \"one\"",
270: values[0], "one");
271: assertEquals("parameter test[1] should be \"two\"",
272: values[1], "two");
273: assertEquals("parameter test[2] should be \"three\"",
274: values[2], "three");
275: }
276:
277: }
278:
279: protected void tearDown() throws Exception {
280: engineHelper.tearDown();
281: super.tearDown();
282: }
283: }
|