1 7 package org.exoplatform.services.wsrp.test; 8 9 import java.rmi.RemoteException ; 10 import javax.portlet.PortletMode; 11 import javax.portlet.PortletPreferences; 12 import javax.portlet.WindowState; 13 import org.exoplatform.commons.utils.IOUtil; 14 import org.exoplatform.services.wsrp.WSRPConstants; 15 import org.exoplatform.services.wsrp.type.*; 16 17 18 19 23 public class TestPerformBlockingInteraction extends BaseTest{ 24 25 public TestPerformBlockingInteraction(String s) { 26 super(s); 27 } 28 29 public void testSimplePerformBlockingInteraction() throws Exception { 30 ServiceDescription sd = getServiceDescription(new String []{"en"}) ; 31 RegistrationContext rc = null; 32 if(sd.isRequiresRegistration()) 33 rc = new RegistrationContext(); 34 String portletHandle = "hello/PortletToTestPerformBlockingInteraction"; 35 PortletContext portletContext = new PortletContext(); 36 portletContext.setPortletHandle(portletHandle); 37 NamedString nS1 = new NamedString(); 38 nS1.setName("name1"); 39 nS1.setValue("value1"); 40 NamedString nS2 = new NamedString(); 41 nS2.setName("name2"); 42 nS2.setValue("value2"); 43 NamedString[] array = new NamedString[2]; 44 array[0] = nS1; 45 array[1] = nS2; 46 InteractionParams params = new InteractionParams(); 47 params.setPortletStateChange(StateChange.readWrite); 48 params.setFormParameters(array); 49 BlockingInteractionRequest performBlockingInteraction = getPerformBlockingInteraction(rc, 50 portletContext, params); 51 BlockingInteractionResponse response = markupOperationsInterface. 52 performBlockingInteraction(performBlockingInteraction); 53 UpdateResponse updateResponse = response.getUpdateResponse(); 54 assertEquals(WSRPConstants.WSRP_PREFIX + WindowState.MAXIMIZED.toString(), 55 updateResponse.getNewWindowState()); 56 assertEquals(WSRPConstants.WSRP_PREFIX + PortletMode.EDIT.toString(), 57 updateResponse.getNewMode()); 58 String navigationalState = updateResponse.getNavigationalState(); 59 markupParams.setNavigationalState(navigationalState); 60 byte[] portletState = updateResponse.getPortletContext().getPortletState(); 62 if( portletState != null){ 63 System.out.println("[test] consumer save state"); 64 portletContext.setPortletState(portletState); 65 } 66 runtimeContext.setSessionID(updateResponse.getSessionContext().getSessionID()); 67 MarkupRequest getMarkup = getMarkup(rc, portletContext); 68 manageTemplatesOptimization(sd, portletHandle); 69 manageUserContextOptimization(sd, portletHandle, getMarkup); 70 MarkupResponse responseMarkup = markupOperationsInterface.getMarkup(getMarkup); 71 assertEquals("value1", responseMarkup.getMarkupContext().getMarkupString()); 72 if(updateResponse.getMarkupContext() != null) 74 assertEquals(responseMarkup.getMarkupContext().getMarkupString(), 75 updateResponse.getMarkupContext().getMarkupString()); 76 } 77 78 public void testSendRedirect() throws RemoteException { 79 ServiceDescription sd = getServiceDescription(new String []{"en"}) ; 80 RegistrationContext rc = null; 81 if(sd.isRequiresRegistration()) 82 rc = new RegistrationContext(); 83 PortletContext portletContext = new PortletContext(); 84 portletContext.setPortletHandle("hello/Portlet2TestSendRedirect"); 85 portletContext.setPortletState(null); InteractionParams params = new InteractionParams(); 87 params.setPortletStateChange(StateChange.readWrite); 88 BlockingInteractionRequest performBlockingInteraction = getPerformBlockingInteraction(rc, 89 portletContext, params); 90 BlockingInteractionResponse response = markupOperationsInterface. 91 performBlockingInteraction(performBlockingInteraction); 92 assertEquals("/path/to/redirect/to.jsp", response.getRedirectURL()); 93 } 94 95 96 public void testReadOnlyStateChange() throws RemoteException { 97 ServiceDescription sd = getServiceDescription(new String []{"en"}) ; 98 RegistrationContext rc = null; 99 if(sd.isRequiresRegistration()) 100 rc = new RegistrationContext(); 101 PortletContext portletContext = new PortletContext(); 102 portletContext.setPortletHandle("hello/Portlet2TestStateUser"); 103 InteractionParams params = new InteractionParams(); 104 params.setPortletStateChange(StateChange.readOnly); 105 BlockingInteractionRequest performBlockingInteraction = getPerformBlockingInteraction(rc, 106 portletContext, params); 107 try { 108 markupOperationsInterface.performBlockingInteraction(performBlockingInteraction); 109 fail("The portlet is in read only state!!!"); 110 } catch (RemoteException e) { 111 e.printStackTrace(); 112 } 113 } 114 115 public void testCloneBeforeWriteStateChange() throws RemoteException { 116 RegistrationContext rc = registrationOperationsInterface.register(registrationData); 117 PortletContext portletContext = new PortletContext(); 118 portletContext.setPortletHandle("hello/Portlet2TestStateUser2"); 119 InteractionParams params = new InteractionParams(); 120 params.setPortletStateChange(StateChange.cloneBeforeWrite); 121 BlockingInteractionRequest performBlockingInteraction = getPerformBlockingInteraction(rc, 122 portletContext, params); 123 BlockingInteractionResponse response = markupOperationsInterface. 124 performBlockingInteraction(performBlockingInteraction); 125 assertNotSame("hello/Portlet2TestStateUser/windowID", 126 response.getUpdateResponse().getPortletContext().getPortletHandle()); 127 } 128 129 public void testStateSaveOnConsumer() throws RemoteException { 130 ServiceDescription sd = getServiceDescription(new String []{"en"}) ; 131 RegistrationContext rc = null; 132 if(sd.isRequiresRegistration()) 133 rc = new RegistrationContext(); 134 String portletHandle = "hello/Portlet2TestStateUser3"; 135 PortletContext portletContext = new PortletContext(); 136 portletContext.setPortletHandle(portletHandle); 137 InteractionParams params = new InteractionParams(); 138 params.setPortletStateChange(StateChange.readWrite); 139 BlockingInteractionRequest performBlockingInteraction = getPerformBlockingInteraction(rc, 140 portletContext, params); 141 UpdateResponse updateResponse = markupOperationsInterface. 142 performBlockingInteraction(performBlockingInteraction).getUpdateResponse(); 143 byte[] portletState = updateResponse.getPortletContext().getPortletState(); 145 if( portletState != null){ 146 System.out.println("[test] consumer save state : " + portletState); 147 } else { 148 return; } 150 Object o = null; 151 try { 152 o = IOUtil.deserialize(portletState); 153 } catch (Exception e) { 154 fail("The object should be deserializable"); 155 } 156 assertTrue(o instanceof PortletPreferences); 157 PortletPreferences pref = (PortletPreferences) o; 158 String element = (String ) pref.getNames().nextElement(); 159 assertEquals("attName", element); 160 assertEquals("attValue", pref.getValue(element, "default")); 161 runtimeContext.setSessionID(updateResponse.getSessionContext().getSessionID()); 162 manageTemplatesOptimization(sd, portletHandle); 163 manageUserContextOptimization(sd, portletHandle, performBlockingInteraction); 164 portletContext.setPortletState(portletState); 165 updateResponse = markupOperationsInterface. 166 performBlockingInteraction(performBlockingInteraction).getUpdateResponse(); 167 portletState = updateResponse.getPortletContext().getPortletState(); 168 try { 169 o = IOUtil.deserialize(portletState); 170 } catch (Exception e) { 171 fail("The object should be deserializable"); 172 } 173 assertTrue(o instanceof PortletPreferences); 174 assertEquals("attValue2", ((PortletPreferences) o).getValue("attName2", "default")); 175 } 176 177 private BlockingInteractionRequest getPerformBlockingInteraction(RegistrationContext rc, 178 PortletContext portletContext, 179 InteractionParams params) { 180 BlockingInteractionRequest performBlockingInteraction = new BlockingInteractionRequest(); 181 performBlockingInteraction.setRegistrationContext(rc); 182 performBlockingInteraction.setPortletContext(portletContext); 183 performBlockingInteraction.setRuntimeContext(runtimeContext); 184 performBlockingInteraction.setUserContext(userContext); 185 performBlockingInteraction.setMarkupParams(markupParams); 186 performBlockingInteraction.setInteractionParams(params); 187 return performBlockingInteraction; 188 } 189 190 } 191 | Popular Tags |