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.container.state;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  
22  import javax.portlet.PortletMode;
23  import javax.portlet.WindowState;
24  import javax.servlet.http.HttpServletRequest;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.jetspeed.PortalContext;
31  import org.apache.jetspeed.cache.JetspeedContentCache;
32  import org.apache.jetspeed.container.state.impl.NavigationalStateCodec;
33  import org.apache.jetspeed.container.state.impl.PathNavigationalState;
34  import org.apache.jetspeed.container.state.impl.SessionFullNavigationalState;
35  import org.apache.jetspeed.container.state.impl.SessionNavigationalState;
36  import org.apache.jetspeed.container.url.PortalURL;
37  import org.apache.jetspeed.container.url.impl.AbstractPortalURL;
38  import org.apache.jetspeed.container.url.impl.PathInfoEncodingPortalURL;
39  import org.apache.jetspeed.container.url.impl.QueryStringEncodingPortalURL;
40  import org.apache.jetspeed.container.window.PortletWindowAccessor;
41  import org.apache.jetspeed.engine.Engine;
42  import org.apache.jetspeed.factory.PortletFactory;
43  import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
44  import org.apache.jetspeed.om.common.portlet.PortletApplication;
45  import org.apache.jetspeed.om.window.impl.PortletWindowImpl;
46  import org.apache.jetspeed.testhelpers.SpringEngineHelper;
47  import org.apache.jetspeed.util.JetspeedLongObjectID;
48  import org.apache.pluto.om.entity.PortletEntity;
49  import org.apache.pluto.om.portlet.PortletDefinition;
50  import org.apache.pluto.om.window.PortletWindow;
51  import org.apache.pluto.om.window.PortletWindowList;
52  import org.apache.pluto.om.window.PortletWindowListCtrl;
53  import org.jmock.Mock;
54  import org.jmock.core.matcher.AnyArgumentsMatcher;
55  import org.jmock.core.stub.ReturnStub;
56  import org.jmock.core.stub.VoidStub;
57  
58  import com.mockrunner.mock.web.MockHttpServletRequest;
59  import com.mockrunner.mock.web.MockHttpSession;
60  
61  /***
62   * TestPortletContainer
63   *
64   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
65   * @version $Id: TestNavigationalState.java 550655 2007-06-26 01:41:35Z taylor $
66   */
67  
68  public class TestNavigationalState extends TestCase
69  {
70      // needed to be able to Mock PortletWindowListCtrl
71      private interface CompositeWindowList extends PortletWindowList, PortletWindowListCtrl{}
72  
73      private SpringEngineHelper engineHelper;
74      private Engine engine;
75      private NavigationalStateCodec codec;
76      private PortalContext portalContext;
77      private JetspeedContentCache cache;
78  
79      /***
80       * Defines the testcase name for JUnit.
81       *
82       * @param name the testcase's name.
83       */
84      public TestNavigationalState(String name)
85      {
86          super(name);
87      }
88  
89      /***
90       * Start the tests.
91       *
92       * @param args the arguments. Not used
93       */
94      public static void main(String args[])
95      {
96          junit.awtui.TestRunner.main(new String[] { TestNavigationalState.class.getName()});
97      }
98      
99      protected void setUp() throws Exception
100     {
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.proxy();
113         entityMock.expects(new AnyArgumentsMatcher()).method("getPortletWindowList").withNoArguments().will(new ReturnStub(windowList));
114         windowListMock.expects(new AnyArgumentsMatcher()).method("add").withAnyArguments().will(new VoidStub());
115         portletApplicationMock.expects(new AnyArgumentsMatcher()).method("getId").withNoArguments().will(new ReturnStub(new JetspeedLongObjectID(1)));
116         portletDefinitionMock.expects(new AnyArgumentsMatcher()).method("getPortletApplicationDefinition").withNoArguments().will(new ReturnStub(portletApplicationMock.proxy()));
117         entityMock.expects(new AnyArgumentsMatcher()).method("getPortletDefinition").withNoArguments().will(new ReturnStub(portletDefinitionMock.proxy()));
118         PortletWindowAccessor accessor = (PortletWindowAccessor) engine.getComponentManager().getComponent(PortletWindowAccessor.class);        
119         accessor.createPortletWindow((PortletEntity)entityMock.proxy(), "111");
120         accessor.createPortletWindow((PortletEntity)entityMock.proxy(), "222");
121         accessor.createPortletWindow((PortletEntity)entityMock.proxy(), "333");
122         
123         // register mocked PortletApplication in PortletFactory so the PortletWindowAccessor check for it won't break the tests
124         PortletFactory portletFactory = (PortletFactory)engine.getComponentManager().getComponent("portletFactory");
125         portletFactory.registerPortletApplication((PortletApplication)portletApplicationMock.proxy(),Thread.currentThread().getContextClassLoader());
126         
127         codec = (NavigationalStateCodec) engine.getComponentManager().getComponent("NavigationalStateCodec");
128         portalContext = (PortalContext) engine.getComponentManager().getComponent("PortalContext");  
129         cache = (JetspeedContentCache) engine.getComponentManager().getComponent("portletContentCache");
130     }
131 
132     public static Test suite()
133     {
134         // All methods starting with "test" will be executed in the test suite.
135         return new TestSuite(TestNavigationalState.class);
136     }
137 
138     
139     public void testSessionFullStateAndQuery()
140     {        
141         SessionFullNavigationalState navState = new SessionFullNavigationalState(codec, cache);
142         QueryStringEncodingPortalURL portalUrl = new QueryStringEncodingPortalURL(navState, portalContext);
143         HttpServletRequest request = buildRequest(portalUrl, true);
144         navState = new SessionFullNavigationalState(codec, cache);
145         portalUrl = new QueryStringEncodingPortalURL(navState, portalContext);
146         doTestUrl(portalUrl, request);
147         
148     }
149     
150     public void testSessionStateAndPathInfo()
151     {        
152         SessionNavigationalState navState = new SessionNavigationalState(codec, cache);
153         PathInfoEncodingPortalURL portalUrl = new PathInfoEncodingPortalURL(navState, portalContext);
154         HttpServletRequest request = buildRequest(portalUrl, false);
155         navState = new SessionNavigationalState(codec, cache);
156         portalUrl = new PathInfoEncodingPortalURL(navState, portalContext);
157         doTestUrl(portalUrl, request);
158     }
159     
160     public void testPathStateAndPathInfo()
161     {        
162         PathNavigationalState navState = new PathNavigationalState(codec, cache);
163         PathInfoEncodingPortalURL portalUrl = new PathInfoEncodingPortalURL(navState, portalContext);
164         HttpServletRequest request = buildRequest(portalUrl, false);
165         navState = new PathNavigationalState(codec, cache);
166         portalUrl = new PathInfoEncodingPortalURL(navState, portalContext);
167         doTestUrl(portalUrl, request);
168     }
169     
170     
171     protected HttpServletRequest buildRequest(PortalURL portalURL, boolean useQueryStringPortalURL)
172     {
173         MockHttpServletRequest request = new MockHttpServletRequest();
174         MockHttpSession session = new MockHttpSession();
175 
176         request.setSession(session);
177         request.setServerName("www.sporteportal.com");
178         request.setScheme("http");
179         request.setContextPath("/jetspeed");
180         request.setServletPath("/portal");
181         request.setPathInfo("stuff");
182         request.setRequestURI("/jetspeed/portal/stuff");
183 
184         portalURL.setRequest(request);
185         portalURL.setCharacterEncoding("UTF-8");
186 
187         PortletWindow window = new PortletWindowImpl("111");
188 
189         HashMap parameters = new HashMap();
190         parameters.put("test",new String[]{"one","two","three"});
191 
192         String portletURL = portalURL.createPortletURL(window,parameters,PortletMode.EDIT,WindowState.MAXIMIZED,true,false);
193         
194         String navStateParameterName = engine.getContext().getConfigurationProperty("portalurl.navigationalstate.parameter.name", AbstractPortalURL.DEFAULT_NAV_STATE_PARAMETER); 
195 
196         if ( useQueryStringPortalURL )
197         {
198             request.setupAddParameter(navStateParameterName,portletURL.substring(portletURL.indexOf('=')+1));            
199         }
200         else
201         {
202             request.setPathInfo(portletURL.substring(portletURL.indexOf("/portal")+7));
203         }
204         
205         return request;        
206     }
207     
208     protected void doTestUrl(PortalURL portalURL, HttpServletRequest request)
209     {             
210       portalURL.setRequest(request);
211       portalURL.setCharacterEncoding("UTF-8");
212       
213       PortletWindow window = new PortletWindowImpl("111");
214       NavigationalState nav = portalURL.getNavigationalState();
215 
216       // Check that they come out correctly
217       assertTrue("window mode is not set", nav.getMode(window).equals(PortletMode.EDIT));
218       assertTrue("window state is not set", nav.getState(window).equals(WindowState.MAXIMIZED));
219       PortletWindow target = nav.getPortletWindowOfAction();
220       assertNotNull("target window is null", target);
221       assertEquals("target window should equal window 111", target.getId(), "111");
222 
223       PortletWindow maximizedWindow = nav.getMaximizedWindow();
224       assertNotNull("maximized window is null", maximizedWindow);
225       assertEquals("maximized window should equal window 111", maximizedWindow.getId(), "111");
226 
227       Iterator iter = nav.getParameterNames(target);
228       assertTrue("There should be one parameter",iter.hasNext());
229       while ( iter.hasNext() ) {
230           assertEquals("parameter name should equals \"test\"", (String)iter.next(), "test");
231           String[] values = nav.getParameterValues(target,"test");
232           assertNotNull("parameter name has no values", values);
233           assertEquals("parameter test should have 3 values", values.length, 3);
234           assertEquals("parameter test[0] should be \"one\"", values[0], "one");
235           assertEquals("parameter test[1] should be \"two\"", values[1], "two");
236           assertEquals("parameter test[2] should be \"three\"", values[2], "three");
237       }
238         
239     }
240 
241 
242     protected void tearDown() throws Exception
243     {
244         engineHelper.tearDown();
245         super.tearDown();
246     }
247 }