KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.wsrp.test;
2
3 import java.io.InputStream JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.rmi.RemoteException JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.GregorianCalendar JavaDoc;
9 import org.exoplatform.Constants;
10 import org.exoplatform.commons.Environment;
11 import org.exoplatform.commons.utils.IOUtil;
12 import org.exoplatform.container.PortalContainer;
13 import org.exoplatform.container.SessionContainer;
14 import org.exoplatform.services.database.HibernateService;
15 import org.exoplatform.services.log.LogUtil;
16 import org.exoplatform.services.organization.OrganizationService;
17 import org.exoplatform.services.organization.User;
18 import org.exoplatform.services.portal.PortalConfigService;
19 import org.exoplatform.services.portletcontainer.PortletApplicationRegister;
20 import org.exoplatform.services.portletcontainer.PortletContainerService;
21 import org.exoplatform.services.portletcontainer.impl.PortletApplicationsHolder;
22 import org.exoplatform.services.portletcontainer.pci.model.PortletApp;
23 import org.exoplatform.services.portletcontainer.pci.model.XMLParser;
24 import org.exoplatform.services.wsrp.bind.WSRP_v1_Markup_Binding_SOAPImpl;
25 import org.exoplatform.services.wsrp.bind.WSRP_v1_PortletManagement_Binding_SOAPImpl;
26 import org.exoplatform.services.wsrp.bind.WSRP_v1_Registration_Binding_SOAPImpl;
27 import org.exoplatform.services.wsrp.bind.WSRP_v1_ServiceDescription_Binding_SOAPImpl;
28 import org.exoplatform.services.wsrp.intf.WSRP_v1_Markup_PortType;
29 import org.exoplatform.services.wsrp.intf.WSRP_v1_PortletManagement_PortType;
30 import org.exoplatform.services.wsrp.intf.WSRP_v1_Registration_PortType;
31 import org.exoplatform.services.wsrp.intf.WSRP_v1_ServiceDescription_PortType;
32 import org.exoplatform.services.wsrp.producer.PersistentStateManager;
33 import org.exoplatform.services.wsrp.type.*;
34 import org.exoplatform.services.wsrp.wsdl.WSRPServiceLocator;
35 import org.exoplatform.test.mocks.servlet.MockServletContext;
36
37 import junit.framework.TestCase;
38
39
40
41 /**
42  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
43  * Please look at license.txt in info directory for more license detail.
44  **/

45
46 /**
47  * Created by the Exo Development team.
48  * Author : Tuan Nguyen
49  * tuan08@users.sourceforge.net
50  * Date: 11 nov. 2003
51  * Time: 22:08:31
52  */

53 public class BaseTest extends TestCase {
54
55   protected static final String JavaDoc SERVICE_URL = "http://localhost:8081/portal/services/";
56   protected static final String JavaDoc PORTLET_APP_PATH = "file:./war_template";
57   static boolean initService_ = true;
58   protected PortletContainerService portletContainer;
59   protected PortletApplicationsHolder holder;
60   protected PortletApp portletApp_;
61   protected Collection JavaDoc roles;
62
63   protected WSRP_v1_ServiceDescription_PortType serviceDescriptionInterface;
64   protected WSRP_v1_Registration_PortType registrationOperationsInterface;
65   protected WSRP_v1_Markup_PortType markupOperationsInterface;
66   protected WSRP_v1_PortletManagement_PortType portletManagementOperationsInterface;
67
68   protected PersonName personName;
69   protected UserContext userContext;
70   protected UserProfile userProfile;
71
72   protected RegistrationData registrationData;
73   protected RuntimeContext runtimeContext;
74   protected Templates templates;
75   protected ClientData clientData;
76   protected MarkupParams markupParams;
77   protected static final String JavaDoc[] USER_CATEGORIES_ARRAY = {
78     "full", "standard", "minimal"
79   };
80   public static String JavaDoc[] localesArray = {"en"};
81   public static String JavaDoc[] markupCharacterSets = {"UF-08", "ISO-8859-1"};
82   public static String JavaDoc[] mimeTypes = {"text/html", "text/xhtml"};
83
84   public static final String JavaDoc BASE_URL = "/portal/faces/portal/portal.jsp?portal:ctx=" + Constants.DEFAUL_PORTAL_OWNER;
85   public static final String JavaDoc DEFAULT_TEMPLATE = BASE_URL + "&portal:windowState={wsrp-windowState}" +
86       "&_mode={wsrp-mode}" +
87       "&_isSecure={wsrp-secureURL}" +
88       "&_component={wsrp-portletHandle}";
89   public static final String JavaDoc RENDER_TEMPLATE = DEFAULT_TEMPLATE + "&portal:type={wsrp-urlType}" +
90       "&ns={wsrp-navigationalState}";
91   public static final String JavaDoc BLOCKING_TEMPLATE = DEFAULT_TEMPLATE + "&portal:type={wsrp-urlType}" +
92       "&ns={wsrp-navigationalState}" +
93       "&is={wsrp-interactionState}";
94
95   public static final String JavaDoc[] CONSUMER_MODES = {"wsrp:view", "wsrp:edit"};
96   public static final String JavaDoc[] CONSUMER_STATES = {"wsrp:normal", "wsrp:maximized"};
97   public static final String JavaDoc[] CONSUMER_SCOPES = {"chunk_data"};
98   public static final String JavaDoc[] CONSUMER_CUSTOM_PROFILES = {"what_more"};
99   private MockServletContext mockServletContext;
100   private OrganizationService orgService_;
101   private PortletApplicationRegister portletApplicationRegister;
102
103   public BaseTest(String JavaDoc s) {
104     super(s);
105   }
106
107   public void setUp() throws Exception JavaDoc {
108     LogUtil.setLevel("org.exoplatform.services.wsrp", org.exoplatform.services.log.LogService.DEBUG , true) ;
109     LogUtil.setLevel("org.exoplatform.services.database", org.exoplatform.services.log.LogService.DEBUG , true) ;
110     if (Environment.getInstance().getPlatform() == Environment.STAND_ALONE) {
111       PortalContainer manager = PortalContainer.getInstance();
112       manager.getComponentInstanceOfType(PortalConfigService.class);
113       manager.getComponentInstanceOfType(PersistentStateManager.class);
114       orgService_ =
115           (OrganizationService) manager.getComponentInstanceOfType(OrganizationService.class);
116       User user = orgService_.findUserByName("exotest");
117       if (user == null) {
118         user = orgService_.createUserInstance();
119         user.setUserName("exotest");
120         user.setPassword("exo");
121         user.setFirstName("Exo");
122         user.setLastName("Platform");
123         user.setEmail("exo@exoportal.org");
124         orgService_.createUser(user);
125       }
126       URL JavaDoc url = new URL JavaDoc(PORTLET_APP_PATH + "/WEB-INF/portlet.xml");
127
128       InputStream JavaDoc is = url.openStream();
129       portletApp_ = XMLParser.parse(is) ;
130
131       Collection JavaDoc roles = new ArrayList JavaDoc();
132       roles.add("auth-user");
133
134
135       mockServletContext = new MockServletContext("hello", "./war_template");
136       mockServletContext.setInitParameter("test-param", "test-parame-value");
137
138       portletContainer = (PortletContainerService) manager.
139           getComponentInstanceOfType(PortletContainerService.class);
140
141       portletApplicationRegister = (PortletApplicationRegister) manager.
142         getComponentInstanceOfType(PortletApplicationRegister.class);
143       
144       portletApplicationRegister.registerPortletApplication(mockServletContext, portletApp_, roles);
145
146       serviceDescriptionInterface = new WSRP_v1_ServiceDescription_Binding_SOAPImpl();
147       registrationOperationsInterface = new WSRP_v1_Registration_Binding_SOAPImpl();
148       markupOperationsInterface = new WSRP_v1_Markup_Binding_SOAPImpl();
149       portletManagementOperationsInterface = new WSRP_v1_PortletManagement_Binding_SOAPImpl();
150     } else {
151       WSRPServiceLocator serviceLocator = new WSRPServiceLocator();
152       serviceDescriptionInterface = serviceLocator.
153           getWSRPServiceDescriptionService(new URL JavaDoc(SERVICE_URL + "WSRPServiceDescriptionService"));
154       registrationOperationsInterface = serviceLocator.
155           getWSRPRegistrationService(new URL JavaDoc(SERVICE_URL + "WSRPRegistrationService"));
156       markupOperationsInterface = serviceLocator.
157           getWSRPBaseService(new URL JavaDoc(SERVICE_URL + "WSRPBaseService"));
158       portletManagementOperationsInterface = serviceLocator.
159           getWSRPPortletManagementService(new URL JavaDoc(SERVICE_URL + "WSRPPortletManagementService"));
160     }
161
162     registrationData = new RegistrationData();
163     registrationData.setConsumerName("www.exoplatform.com");
164     registrationData.setConsumerAgent("exoplatform.1.0");
165     registrationData.setMethodGetSupported(false);
166     registrationData.setConsumerModes(CONSUMER_MODES);
167     registrationData.setConsumerWindowStates(CONSUMER_STATES);
168     registrationData.setConsumerUserScopes(CONSUMER_SCOPES);
169     registrationData.setCustomUserProfileData(CONSUMER_CUSTOM_PROFILES);
170     registrationData.setRegistrationProperties(null);//allows extension of the specs
171
registrationData.setExtensions(null);//allows extension of the specs
172

173     personName = new PersonName();
174     personName.setNickname("exotest");
175
176     userProfile = new UserProfile();
177     userProfile.setBdate(new GregorianCalendar JavaDoc());
178     userProfile.setGender("male");
179     userProfile.setName(personName);
180
181     userContext = new UserContext();
182     userContext.setUserCategories(USER_CATEGORIES_ARRAY);
183     userContext.setProfile(userProfile);
184     userContext.setUserContextKey("exotest");
185
186     templates = new Templates();
187     templates.setDefaultTemplate(DEFAULT_TEMPLATE);
188     templates.setRenderTemplate(RENDER_TEMPLATE);
189     templates.setRenderTemplate(BLOCKING_TEMPLATE);
190
191     runtimeContext = new RuntimeContext();
192     runtimeContext.setNamespacePrefix("NamespacePrefix");
193 //runtimeContext.setPortletInstanceKey("windowID");
194
runtimeContext.setSessionID(null);
195     runtimeContext.setTemplates(templates);
196     runtimeContext.setUserAuthentication("none");
197
198     clientData = new ClientData();
199     clientData.setUserAgent("PC");
200
201     markupParams = new MarkupParams();
202     markupParams.setClientData(clientData);
203     markupParams.setLocales(localesArray);
204     markupParams.setMarkupCharacterSets(markupCharacterSets);
205     markupParams.setNavigationalState("");
206     markupParams.setSecureClientCommunication(false);
207     markupParams.setValidateTag(null);
208     markupParams.setValidNewModes(null);
209     markupParams.setValidNewWindowStates(null);
210     markupParams.setMimeTypes(mimeTypes);
211     markupParams.setMode("wsrp:view");
212     markupParams.setWindowState("wsrp:normal");
213   }
214
215   public void tearDown() throws Exception JavaDoc {
216     try {
217       portletApplicationRegister.removePortletApplication(mockServletContext);
218       PortalContainer manager = PortalContainer.getInstance();
219       HibernateService hservice =
220           (HibernateService) manager.getComponentInstanceOfType(HibernateService.class) ;
221       hservice.closeSession();
222     } catch (Exception JavaDoc e) {
223       e.printStackTrace();
224     }
225   }
226
227   protected ServiceDescription getServiceDescription(String JavaDoc[] locales) throws RemoteException JavaDoc {
228     ServiceDescriptionRequest getServiceDescription = new ServiceDescriptionRequest();
229     getServiceDescription.setDesiredLocales(locales);
230     return serviceDescriptionInterface.getServiceDescription(getServiceDescription);
231   }
232
233   protected MarkupRequest getMarkup(RegistrationContext rc, PortletContext portletContext) {
234     MarkupRequest getMarkup = new MarkupRequest();
235     getMarkup.setRegistrationContext(rc);
236     getMarkup.setPortletContext(portletContext);
237     getMarkup.setRuntimeContext(runtimeContext);
238     getMarkup.setUserContext(userContext);
239     getMarkup.setMarkupParams(markupParams);
240     return getMarkup;
241   }
242
243   protected void manageTemplatesOptimization(ServiceDescription sd, String JavaDoc portletHandle) {
244     PortletDescription[] array = sd.getOfferedPortlets();
245     for (int i = 0; i < array.length; i++) {
246       PortletDescription portletDescription = array[i];
247       if (portletDescription.getPortletHandle().equals(portletHandle)) {
248         System.out.println("[test] use of portlet handle : " + portletHandle);
249         if (portletDescription.getTemplatesStoredInSession().booleanValue()) {
250           System.out.println("[test] set templates to null ");
251           runtimeContext.setTemplates(null);
252         }
253       }
254     }
255   }
256
257   protected void manageUserContextOptimization(ServiceDescription sd, String JavaDoc portletHandle, MarkupRequest getMarkup) {
258     PortletDescription[] array = sd.getOfferedPortlets();
259     for (int i = 0; i < array.length; i++) {
260       PortletDescription portletDescription = array[i];
261       if (portletDescription.getPortletHandle().equals(portletHandle)) {
262         System.out.println("[test] use of portlet handle : " + portletHandle);
263         if (portletDescription.getUserContextStoredInSession().booleanValue()) {
264           System.out.println("[test] set user context to null ");
265           getMarkup.setUserContext(null);
266         }
267       }
268     }
269   }
270
271   protected void manageUserContextOptimization(ServiceDescription sd, String JavaDoc portletHandle,
272                                                BlockingInteractionRequest performBlockingInteraction) {
273     PortletDescription[] array = sd.getOfferedPortlets();
274     for (int i = 0; i < array.length; i++) {
275       PortletDescription portletDescription = array[i];
276       if (portletDescription.getPortletHandle().equals(portletHandle)) {
277         System.out.println("[test] use of portlet handle : " + portletHandle);
278         if (portletDescription.getUserContextStoredInSession().booleanValue()) {
279           System.out.println("[test] set user context to null ");
280           performBlockingInteraction.setUserContext(null);
281         }
282       }
283     }
284   }
285
286   protected void resolveRegistrationContext(RegistrationContext registrationContext)
287       throws Exception JavaDoc {
288     byte[] registrationState = registrationContext.getRegistrationState();
289     if (registrationState != null) {
290       System.out.println("[test] Save registration state on consumer");
291       Object JavaDoc o = IOUtil.deserialize(registrationState);
292       if (!(o instanceof RegistrationData))
293         fail("The deserialized object should be of type RegistrationData");
294       assertEquals(((RegistrationData) o).getConsumerName(), registrationData.getConsumerName());
295     }
296   }
297
298 }
299
Popular Tags