001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service;
016    
017    import com.liferay.portal.jcr.JCRFactoryUtil;
018    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.messaging.BaseDestination;
022    import com.liferay.portal.kernel.messaging.DestinationNames;
023    import com.liferay.portal.kernel.messaging.MessageBus;
024    import com.liferay.portal.kernel.messaging.MessageBusUtil;
025    import com.liferay.portal.kernel.messaging.SynchronousDestination;
026    import com.liferay.portal.kernel.messaging.sender.MessageSender;
027    import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
028    import com.liferay.portal.kernel.scheduler.SchedulerEngineHelperUtil;
029    import com.liferay.portal.kernel.search.SearchEngineUtil;
030    import com.liferay.portal.kernel.template.TemplateManagerUtil;
031    import com.liferay.portal.kernel.util.FileUtil;
032    import com.liferay.portal.kernel.util.LocaleThreadLocal;
033    import com.liferay.portal.kernel.util.PropsKeys;
034    import com.liferay.portal.model.Portlet;
035    import com.liferay.portal.model.Role;
036    import com.liferay.portal.model.User;
037    import com.liferay.portal.model.impl.PortletImpl;
038    import com.liferay.portal.repository.liferayrepository.LiferayRepository;
039    import com.liferay.portal.security.auth.PrincipalThreadLocal;
040    import com.liferay.portal.security.lang.DoPrivilegedUtil;
041    import com.liferay.portal.security.permission.PermissionChecker;
042    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
043    import com.liferay.portal.security.permission.PermissionThreadLocal;
044    import com.liferay.portal.security.permission.ResourceActionsUtil;
045    import com.liferay.portal.tools.DBUpgrader;
046    import com.liferay.portal.util.PortalInstances;
047    import com.liferay.portal.util.PortalUtil;
048    import com.liferay.portal.util.PropsUtil;
049    import com.liferay.portal.util.PropsValues;
050    import com.liferay.portal.util.test.RoleTestUtil;
051    import com.liferay.portal.util.test.TestPropsValues;
052    
053    import java.util.Calendar;
054    import java.util.Date;
055    import java.util.GregorianCalendar;
056    import java.util.HashMap;
057    import java.util.List;
058    import java.util.Locale;
059    import java.util.Set;
060    
061    /**
062     * @author Brian Wing Shun Chan
063     * @author Michael Young
064     * @author Alexander Chow
065     * @author Manuel de la Pe??a
066     */
067    public class ServiceTestUtil {
068    
069            public static final int THREAD_COUNT = 25;
070    
071            /**
072             * @deprecated As of 7.0.0
073             */
074            @Deprecated
075            public static void addResourcePermission(
076                            Role role, String resourceName, int scope, String primKey,
077                            String actionId)
078                    throws Exception {
079    
080                    RoleTestUtil.addResourcePermission(
081                            role, resourceName, scope, primKey, actionId);
082            }
083    
084            /**
085             * @deprecated As of 7.0.0
086             */
087            @Deprecated
088            public static void addResourcePermission(
089                            String roleName, String resourceName, int scope, String primKey,
090                            String actionId)
091                    throws Exception {
092    
093                    RoleTestUtil.addResourcePermission(
094                            roleName, resourceName, scope, primKey, actionId);
095            }
096    
097            /**
098             * @deprecated As of 7.0.0
099             */
100            @Deprecated
101            public static Role addRole(String roleName, int roleType) throws Exception {
102                    return RoleTestUtil.addRole(roleName, roleType);
103            }
104    
105            /**
106             * @deprecated As of 7.0.0
107             */
108            @Deprecated
109            public static Role addRole(
110                            String roleName, int roleType, String resourceName, int scope,
111                            String primKey, String actionId)
112                    throws Exception {
113    
114                    return RoleTestUtil.addRole(
115                            roleName, roleType, resourceName, scope, primKey, actionId);
116            }
117    
118            public static void destroyServices() {
119                    _deleteDirectories();
120            }
121    
122            public static void initPermissions() {
123                    try {
124                            PortalInstances.addCompanyId(TestPropsValues.getCompanyId());
125    
126                            setUser(TestPropsValues.getUser());
127                    }
128                    catch (Exception e) {
129                            _log.warn(e.getMessage(), e);
130                    }
131            }
132    
133            public static void initServices() {
134    
135                    // JCR
136    
137                    try {
138                            JCRFactoryUtil.prepare();
139                    }
140                    catch (Exception e) {
141                            _log.warn(e.getMessage(), e);
142                    }
143    
144                    // Template manager
145    
146                    try {
147                            TemplateManagerUtil.init();
148                    }
149                    catch (Exception e) {
150                            _log.warn(e.getMessage(), e);
151                    }
152    
153                    // Indexers
154    
155                    PortalRegisterTestUtil.registerIndexers();
156    
157                    // Upgrade
158    
159                    try {
160                            DBUpgrader.upgrade();
161                    }
162                    catch (AssertionError ae) {
163                            _log.warn(ae.getMessage(), ae);
164                    }
165                    catch (Exception e) {
166                            _log.warn(e.getMessage(), e);
167                    }
168    
169                    // Messaging
170    
171                    MessageBus messageBus = (MessageBus)PortalBeanLocatorUtil.locate(
172                            MessageBus.class.getName());
173                    MessageSender messageSender =
174                            (MessageSender)PortalBeanLocatorUtil.locate(
175                                    MessageSender.class.getName());
176                    SynchronousMessageSender synchronousMessageSender =
177                            (SynchronousMessageSender)PortalBeanLocatorUtil.locate(
178                                    SynchronousMessageSender.class.getName());
179    
180                    MessageBusUtil.init(
181                            DoPrivilegedUtil.wrap(messageBus),
182                            DoPrivilegedUtil.wrap(messageSender),
183                            DoPrivilegedUtil.wrap(synchronousMessageSender));
184    
185                    if (TestPropsValues.DL_FILE_ENTRY_PROCESSORS_TRIGGER_SYNCHRONOUSLY) {
186                            _replaceWithSynchronousDestination(
187                                    DestinationNames.DOCUMENT_LIBRARY_AUDIO_PROCESSOR);
188                            _replaceWithSynchronousDestination(
189                                    DestinationNames.DOCUMENT_LIBRARY_IMAGE_PROCESSOR);
190                            _replaceWithSynchronousDestination(
191                                    DestinationNames.DOCUMENT_LIBRARY_PDF_PROCESSOR);
192                            _replaceWithSynchronousDestination(
193                                    DestinationNames.DOCUMENT_LIBRARY_RAW_METADATA_PROCESSOR);
194                            _replaceWithSynchronousDestination(
195                                    DestinationNames.DOCUMENT_LIBRARY_VIDEO_PROCESSOR);
196                    }
197    
198                    // Scheduler
199    
200                    try {
201                            SchedulerEngineHelperUtil.start();
202                    }
203                    catch (Exception e) {
204                            _log.warn(e.getMessage(), e);
205                    }
206    
207                    // Verify
208    
209                    try {
210                            DBUpgrader.verify();
211                    }
212                    catch (Exception e) {
213                            _log.warn(e.getMessage(), e);
214                    }
215    
216                    // Class names
217    
218                    _checkClassNames();
219    
220                    // Resource actions
221    
222                    try {
223                            _checkResourceActions();
224                    }
225                    catch (Exception e) {
226                            _log.warn(e.getMessage(), e);
227                    }
228    
229                    // Trash
230    
231                    PortalRegisterTestUtil.registerTrashHandlers();
232    
233                    // Workflow
234    
235                    PortalRegisterTestUtil.registerWorkflowHandlers();
236    
237                    // Asset renderers
238    
239                    PortalRegisterTestUtil.registerAssetRendererFactories();
240    
241                    // Thread locals
242    
243                    _setThreadLocals();
244    
245                    // Company
246    
247                    try {
248                            CompanyLocalServiceUtil.checkCompany(
249                                    TestPropsValues.COMPANY_WEB_ID);
250                    }
251                    catch (Exception e) {
252                            _log.warn(e.getMessage(), e);
253                    }
254    
255                    // Directories
256    
257                    _deleteDirectories();
258    
259                    // Lucene
260    
261                    try {
262                            FileUtil.mkdirs(
263                                    PropsValues.LUCENE_DIR + TestPropsValues.getCompanyId());
264    
265                            SearchEngineUtil.initialize(TestPropsValues.getCompanyId());
266                    }
267                    catch (Exception e) {
268                            _log.warn(e.getMessage(), e);
269                    }
270            }
271    
272            public static Date newDate() throws Exception {
273                    return new Date();
274            }
275    
276            public static Date newDate(int month, int day, int year) throws Exception {
277                    Calendar calendar = new GregorianCalendar();
278    
279                    calendar.set(Calendar.MONTH, month);
280                    calendar.set(Calendar.DATE, day);
281                    calendar.set(Calendar.YEAR, year);
282    
283                    return calendar.getTime();
284            }
285    
286            public static void setUser(User user) throws Exception {
287                    if (user == null) {
288                            return;
289                    }
290    
291                    PrincipalThreadLocal.setName(user.getUserId());
292    
293                    PermissionChecker permissionChecker =
294                            PermissionCheckerFactoryUtil.create(user);
295    
296                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
297            }
298    
299            private static void _checkClassNames() {
300                    PortalUtil.getClassNameId(LiferayRepository.class.getName());
301            }
302    
303            private static void _checkResourceActions() throws Exception {
304                    for (int i = 0; i < 200; i++) {
305                            String portletId = String.valueOf(i);
306    
307                            Portlet portlet = new PortletImpl();
308    
309                            portlet.setPortletId(portletId);
310                            portlet.setPortletModes(new HashMap<String, Set<String>>());
311    
312                            List<String> portletActions =
313                                    ResourceActionsUtil.getPortletResourceActions(portletId);
314    
315                            ResourceActionLocalServiceUtil.checkResourceActions(
316                                    portletId, portletActions);
317    
318                            List<String> modelNames =
319                                    ResourceActionsUtil.getPortletModelResources(portletId);
320    
321                            for (String modelName : modelNames) {
322                                    List<String> modelActions =
323                                            ResourceActionsUtil.getModelResourceActions(modelName);
324    
325                                    ResourceActionLocalServiceUtil.checkResourceActions(
326                                            modelName, modelActions);
327                            }
328                    }
329            }
330    
331            private static void _deleteDirectories() {
332                    FileUtil.deltree(PropsValues.DL_STORE_FILE_SYSTEM_ROOT_DIR);
333    
334                    FileUtil.deltree(
335                            PropsUtil.get(PropsKeys.JCR_JACKRABBIT_REPOSITORY_ROOT));
336    
337                    try {
338                            FileUtil.deltree(
339                                    PropsValues.LUCENE_DIR + TestPropsValues.getCompanyId());
340                    }
341                    catch (Exception e) {
342                            _log.warn(e.getMessage(), e);
343                    }
344            }
345    
346            private static void _replaceWithSynchronousDestination(String name) {
347                    BaseDestination baseDestination = new SynchronousDestination();
348    
349                    baseDestination.setName(name);
350    
351                    MessageBus messageBus = MessageBusUtil.getMessageBus();
352    
353                    messageBus.replace(baseDestination);
354            }
355    
356            private static void _setThreadLocals() {
357                    LocaleThreadLocal.setThemeDisplayLocale(new Locale("en", "US"));
358    
359                    ServiceContext serviceContext = new ServiceContext();
360    
361                    serviceContext.setPathMain("path");
362                    serviceContext.setPortalURL("http://tests:8080");
363    
364                    ServiceContextThreadLocal.pushServiceContext(serviceContext);
365            }
366    
367            private static Log _log = LogFactoryUtil.getLog(ServiceTestUtil.class);
368    }