KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > wsrp > test > TestPerformBlockingInteraction


1 /*
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  *
5  * Created on 11 janv. 2004
6  */

7 package org.exoplatform.services.wsrp.test;
8
9 import java.rmi.RemoteException JavaDoc;
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 /**
20  * @author Mestrallet Benjamin
21  * benjmestrallet@users.sourceforge.net
22  */

23 public class TestPerformBlockingInteraction extends BaseTest{
24
25   public TestPerformBlockingInteraction(String JavaDoc s) {
26     super(s);
27   }
28
29   public void testSimplePerformBlockingInteraction() throws Exception JavaDoc {
30     ServiceDescription sd = getServiceDescription(new String JavaDoc[]{"en"}) ;
31     RegistrationContext rc = null;
32     if(sd.isRequiresRegistration())
33       rc = new RegistrationContext();
34     String JavaDoc 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 JavaDoc navigationalState = updateResponse.getNavigationalState();
59     markupParams.setNavigationalState(navigationalState);
60     //look if we obtain the portlet state (case of consumer save state)
61
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     //test optimization cases
73
if(updateResponse.getMarkupContext() != null)
74       assertEquals(responseMarkup.getMarkupContext().getMarkupString(),
75                    updateResponse.getMarkupContext().getMarkupString());
76   }
77
78   public void testSendRedirect() throws RemoteException JavaDoc{
79     ServiceDescription sd = getServiceDescription(new String JavaDoc[]{"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);//TODO
86
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 JavaDoc {
97     ServiceDescription sd = getServiceDescription(new String JavaDoc[]{"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 JavaDoc e) {
111       e.printStackTrace();
112     }
113   }
114
115   public void testCloneBeforeWriteStateChange() throws RemoteException JavaDoc {
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 JavaDoc {
130     ServiceDescription sd = getServiceDescription(new String JavaDoc[]{"en"}) ;
131     RegistrationContext rc = null;
132     if(sd.isRequiresRegistration())
133       rc = new RegistrationContext();
134     String JavaDoc 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     //look if we obtain the portlet state (case of consumer save state)
144
byte[] portletState = updateResponse.getPortletContext().getPortletState();
145     if( portletState != null){
146       System.out.println("[test] consumer save state : " + portletState);
147     } else {
148       return; //we don't test portlet test save on producer here
149
}
150     Object JavaDoc o = null;
151     try {
152       o = IOUtil.deserialize(portletState);
153     } catch (Exception JavaDoc e) {
154       fail("The object should be deserializable");
155     }
156     assertTrue(o instanceof PortletPreferences);
157     PortletPreferences pref = (PortletPreferences) o;
158     String JavaDoc element = (String JavaDoc) 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 JavaDoc 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