001
014
015 package com.liferay.portal.lar;
016
017 import com.liferay.portal.LocaleException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.lar.ExportImportClassedModelUtil;
020 import com.liferay.portal.kernel.lar.ExportImportDateUtil;
021 import com.liferay.portal.kernel.lar.PortletDataHandler;
022 import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
023 import com.liferay.portal.kernel.template.TemplateHandler;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.LocaleUtil;
026 import com.liferay.portal.kernel.util.MapUtil;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.util.Time;
029 import com.liferay.portal.model.Group;
030 import com.liferay.portal.model.Portlet;
031 import com.liferay.portal.model.StagedModel;
032 import com.liferay.portal.service.GroupLocalServiceUtil;
033 import com.liferay.portal.service.LayoutLocalServiceUtil;
034 import com.liferay.portal.service.PortletLocalServiceUtil;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.test.GroupTestUtil;
037 import com.liferay.portal.util.test.LayoutTestUtil;
038 import com.liferay.portal.util.test.TestPropsValues;
039 import com.liferay.portlet.PortletPreferencesFactoryUtil;
040 import com.liferay.portlet.asset.model.AssetEntry;
041 import com.liferay.portlet.asset.model.AssetLink;
042 import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
043 import com.liferay.portlet.asset.service.AssetLinkLocalServiceUtil;
044 import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
045 import com.liferay.portlet.dynamicdatamapping.util.test.DDMTemplateTestUtil;
046 import com.liferay.portlet.portletdisplaytemplate.util.PortletDisplayTemplate;
047
048 import java.util.Date;
049 import java.util.HashMap;
050 import java.util.Iterator;
051 import java.util.LinkedHashMap;
052 import java.util.List;
053 import java.util.Locale;
054 import java.util.Map;
055
056 import javax.portlet.PortletPreferences;
057
058 import org.junit.Assert;
059 import org.junit.Test;
060
061
064 public abstract class BasePortletExportImportTestCase
065 extends BaseExportImportTestCase {
066
067 public String getNamespace() {
068 return null;
069 }
070
071 public String getPortletId() throws Exception {
072 return null;
073 }
074
075 @Test
076 public void testExportImportAssetLinks() throws Exception {
077 StagedModel stagedModel = addStagedModel(group.getGroupId());
078
079 StagedModel relatedStagedModel1 = addStagedModel(group.getGroupId());
080 StagedModel relatedStagedModel2 = addStagedModel(group.getGroupId());
081
082 addAssetLink(stagedModel, relatedStagedModel1, 1);
083 addAssetLink(stagedModel, relatedStagedModel2, 2);
084
085 exportImportPortlet(getPortletId());
086
087 StagedModel importedStagedModel = getStagedModel(
088 getStagedModelUuid(stagedModel), importedGroup.getGroupId());
089
090 Assert.assertNotNull(importedStagedModel);
091
092 validateImportedLinks(stagedModel, importedStagedModel);
093 }
094
095 @Test
096 public void testExportImportDeletions() throws Exception {
097 StagedModel stagedModel = addStagedModel(group.getGroupId());
098
099 if (stagedModel == null) {
100 return;
101 }
102
103 String stagedModelUuid = getStagedModelUuid(stagedModel);
104
105 exportImportPortlet(getPortletId());
106
107 deleteStagedModel(stagedModel);
108
109 exportImportPortlet(getPortletId());
110
111 StagedModel importedStagedModel = getStagedModel(
112 stagedModelUuid, importedGroup.getGroupId());
113
114 Assert.assertNotNull(importedStagedModel);
115
116 Map<String, String[]> exportParameterMap =
117 new LinkedHashMap<String, String[]>();
118
119 exportParameterMap.put(
120 PortletDataHandlerKeys.DELETIONS,
121 new String[] {String.valueOf(true)});
122
123 exportImportPortlet(
124 getPortletId(), exportParameterMap, getImportParameterMap());
125
126 importedStagedModel = getStagedModel(
127 stagedModelUuid, importedGroup.getGroupId());
128
129 Assert.assertNotNull(importedStagedModel);
130
131 Map<String, String[]> importParameterMap =
132 new LinkedHashMap<String, String[]>();
133
134 importParameterMap.put(
135 PortletDataHandlerKeys.DELETIONS,
136 new String[] {String.valueOf(true)});
137
138 exportImportPortlet(
139 getPortletId(), exportParameterMap, importParameterMap);
140
141 try {
142 importedStagedModel = getStagedModel(
143 stagedModelUuid, importedGroup.getGroupId());
144
145 Assert.assertNull(importedStagedModel);
146 }
147 catch (Exception e) {
148 }
149 }
150
151 @Test
152 public void testExportImportDisplayStyleFromCurrentGroup()
153 throws Exception {
154
155 testExportImportDisplayStyle(group.getGroupId(), StringPool.BLANK);
156 }
157
158 @Test
159 public void testExportImportDisplayStyleFromDifferentGroup()
160 throws Exception {
161
162 Group group2 = GroupTestUtil.addGroup();
163
164 testExportImportDisplayStyle(group2.getGroupId(), StringPool.BLANK);
165 }
166
167 @Test
168 public void testExportImportDisplayStyleFromGlobalScope() throws Exception {
169 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
170 group.getCompanyId());
171
172 testExportImportDisplayStyle(companyGroup.getGroupId(), "company");
173 }
174
175 @Test
176 public void testExportImportDisplayStyleFromLayoutScope() throws Exception {
177 testExportImportDisplayStyle(group.getGroupId(), "layout");
178 }
179
180 @Test
181 public void testExportImportInvalidAvailableLocales() throws Exception {
182 testExportImportAvailableLocales(
183 new Locale[] {LocaleUtil.US, LocaleUtil.SPAIN},
184 new Locale[] {LocaleUtil.US, LocaleUtil.GERMANY}, true);
185 }
186
187 @Test
188 public void testExportImportValidAvailableLocales() throws Exception {
189 testExportImportAvailableLocales(
190 new Locale[] {LocaleUtil.US, LocaleUtil.SPAIN},
191 new Locale[] {LocaleUtil.US, LocaleUtil.SPAIN, LocaleUtil.GERMANY},
192 false);
193 }
194
195 @Test
196 public void testUpdateLastPublishDate() throws Exception {
197 Date lastPublishDate = new Date(System.currentTimeMillis() - Time.HOUR);
198
199 Date stagedModelCreationDate = new Date(
200 lastPublishDate.getTime() + Time.MINUTE);
201
202 StagedModel stagedModel = addStagedModel(
203 group.getGroupId(), stagedModelCreationDate);
204
205 if (stagedModel == null) {
206 return;
207 }
208
209 LayoutTestUtil.addPortletToLayout(
210 TestPropsValues.getUserId(), layout, getPortletId(), "column-1",
211 new HashMap<String, String[]>());
212
213 PortletPreferences portletPreferences =
214 PortletPreferencesFactoryUtil.getStrictPortletSetup(
215 layout, getPortletId());
216
217 portletPreferences.setValue(
218 "last-publish-date", String.valueOf(lastPublishDate.getTime()));
219
220 portletPreferences.store();
221
222 Map<String, String[]> exportParameterMap =
223 new LinkedHashMap<String, String[]>();
224
225 exportParameterMap.put(
226 PortletDataHandlerKeys.UPDATE_LAST_PUBLISH_DATE,
227 new String[] {String.valueOf(true)});
228 exportParameterMap.put(
229 "range",
230 new String[] {ExportImportDateUtil.RANGE_FROM_LAST_PUBLISH_DATE});
231
232 Map<String, String[]> importParameterMap =
233 new LinkedHashMap<String, String[]>();
234
235 Date startDate = new Date(
236 stagedModelCreationDate.getTime() + Time.MINUTE);
237 Date endDate = new Date();
238
239 exportImportPortlet(
240 getPortletId(), exportParameterMap, importParameterMap, startDate,
241 endDate);
242
243 portletPreferences =
244 PortletPreferencesFactoryUtil.getStrictPortletSetup(
245 layout, getPortletId());
246
247 lastPublishDate = ExportImportDateUtil.getLastPublishDate(
248 portletPreferences);
249
250 Assert.assertEquals(endDate.getTime(), lastPublishDate.getTime());
251
252 StagedModel importedStagedModel = getStagedModel(
253 getStagedModelUuid(stagedModel), importedGroup.getGroupId());
254
255 Assert.assertNotNull(importedStagedModel);
256 }
257
258 protected AssetLink addAssetLink(
259 StagedModel sourceStagedModel, StagedModel targetStagedModel,
260 int weight)
261 throws PortalException {
262
263 AssetEntry originAssetEntry = getAssetEntry(sourceStagedModel);
264 AssetEntry targetAssetEntry = getAssetEntry(targetStagedModel);
265
266 return AssetLinkLocalServiceUtil.addLink(
267 TestPropsValues.getUserId(), originAssetEntry.getEntryId(),
268 targetAssetEntry.getEntryId(), 0, weight);
269 }
270
271 protected void addParameter(
272 Map<String, String[]> parameterMap, String name, boolean value) {
273
274 addParameter(parameterMap, getNamespace(), name, value);
275 }
276
277 protected void exportImportPortlet(String portletId) throws Exception {
278 exportImportPortlet(
279 portletId, new LinkedHashMap<String, String[]>(),
280 new LinkedHashMap<String, String[]>());
281 }
282
283 protected void exportImportPortlet(
284 String portletId, Map<String, String[]> exportParameterMap,
285 Map<String, String[]> importParameterMap)
286 throws Exception {
287
288 exportImportPortlet(
289 portletId, exportParameterMap, importParameterMap, null, null);
290 }
291
292 protected void exportImportPortlet(
293 String portletId, Map<String, String[]> exportParameterMap,
294 Map<String, String[]> importParameterMap, Date startDate,
295 Date endDate)
296 throws Exception {
297
298 MapUtil.merge(getExportParameterMap(), exportParameterMap);
299
300 larFile = LayoutLocalServiceUtil.exportPortletInfoAsFile(
301 layout.getPlid(), layout.getGroupId(), portletId,
302 exportParameterMap, startDate, endDate);
303
304 importedLayout = LayoutTestUtil.addLayout(importedGroup);
305
306 MapUtil.merge(getImportParameterMap(), importParameterMap);
307
308 LayoutLocalServiceUtil.importPortletInfo(
309 TestPropsValues.getUserId(), importedLayout.getPlid(),
310 importedGroup.getGroupId(), portletId, importParameterMap, larFile);
311 }
312
313 protected AssetEntry getAssetEntry(StagedModel stagedModel)
314 throws PortalException {
315
316 return AssetEntryLocalServiceUtil.getEntry(
317 ExportImportClassedModelUtil.getClassName(stagedModel),
318 ExportImportClassedModelUtil.getClassPK(stagedModel));
319 }
320
321 protected PortletPreferences getImportedPortletPreferences(
322 Map<String, String[]> preferenceMap)
323 throws Exception {
324
325 String portletId = LayoutTestUtil.addPortletToLayout(
326 TestPropsValues.getUserId(), this.layout, getPortletId(),
327 "column-1", preferenceMap);
328
329 exportImportPortlet(portletId);
330
331 return LayoutTestUtil.getPortletPreferences(importedLayout, portletId);
332 }
333
334 protected void testExportImportAvailableLocales(
335 Locale[] sourceAvailableLocales, Locale[] targetAvailableLocales,
336 boolean expectFailure)
337 throws Exception {
338
339 Portlet portlet = PortletLocalServiceUtil.getPortletById(
340 group.getCompanyId(), getPortletId());
341
342 if (portlet == null) {
343 return;
344 }
345
346 PortletDataHandler portletDataHandler =
347 portlet.getPortletDataHandlerInstance();
348
349 if (!portletDataHandler.isDataLocalized()) {
350 Assert.assertTrue("This test does not apply", true);
351
352 return;
353 }
354
355 GroupTestUtil.updateDisplaySettings(
356 group.getGroupId(), sourceAvailableLocales, null);
357 GroupTestUtil.updateDisplaySettings(
358 importedGroup.getGroupId(), targetAvailableLocales, null);
359
360 try {
361 exportImportPortlet(getPortletId());
362
363 if (expectFailure) {
364 Assert.fail();
365 }
366 }
367 catch (LocaleException le) {
368 if (!expectFailure) {
369 Assert.fail();
370 }
371 }
372 }
373
374 protected void testExportImportDisplayStyle(
375 long displayStyleGroupId, String scopeType)
376 throws Exception {
377
378 Portlet portlet = PortletLocalServiceUtil.getPortletById(
379 group.getCompanyId(), getPortletId());
380
381 if (portlet == null) {
382 return;
383 }
384
385 if (scopeType.equals("layout") && !portlet.isScopeable()) {
386 Assert.assertTrue("This test does not apply", true);
387
388 return;
389 }
390
391 TemplateHandler templateHandler = portlet.getTemplateHandlerInstance();
392
393 if (templateHandler == null) {
394 Assert.assertTrue("This test does not apply", true);
395
396 return;
397 }
398
399 String className = templateHandler.getClassName();
400
401 DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate(
402 displayStyleGroupId, PortalUtil.getClassNameId(className), 0);
403
404 Map<String, String[]> preferenceMap = new HashMap<String, String[]>();
405
406 String displayStyle =
407 PortletDisplayTemplate.DISPLAY_STYLE_PREFIX + ddmTemplate.getUuid();
408
409 preferenceMap.put("displayStyle", new String[] {displayStyle});
410
411 preferenceMap.put(
412 "displayStyleGroupId",
413 new String[] {String.valueOf(ddmTemplate.getGroupId())});
414
415 if (scopeType.equals("layout")) {
416 preferenceMap.put(
417 "lfrScopeLayoutUuid", new String[] {this.layout.getUuid()});
418 }
419
420 preferenceMap.put("lfrScopeType", new String[] {scopeType});
421
422 PortletPreferences portletPreferences = getImportedPortletPreferences(
423 preferenceMap);
424
425 String importedDisplayStyle = portletPreferences.getValue(
426 "displayStyle", StringPool.BLANK);
427
428 Assert.assertEquals(displayStyle, importedDisplayStyle);
429
430 long importedDisplayStyleGroupId = GetterUtil.getLong(
431 portletPreferences.getValue("displayStyleGroupId", null));
432
433 long expectedDisplayStyleGroupId = importedGroup.getGroupId();
434
435 if (scopeType.equals("company")) {
436 Group companyGroup = GroupLocalServiceUtil.getCompanyGroup(
437 importedGroup.getCompanyId());
438
439 expectedDisplayStyleGroupId = companyGroup.getGroupId();
440 }
441 else if (displayStyleGroupId != group.getGroupId()) {
442 expectedDisplayStyleGroupId = displayStyleGroupId;
443 }
444
445 Assert.assertEquals(
446 expectedDisplayStyleGroupId, importedDisplayStyleGroupId);
447 }
448
449 protected void validateImportedLinks(
450 StagedModel originalStagedModel, StagedModel importedStagedModel)
451 throws PortalException {
452
453 AssetEntry originalAssetEntry = getAssetEntry(originalStagedModel);
454
455 List<AssetLink> originalAssetLinks = AssetLinkLocalServiceUtil.getLinks(
456 originalAssetEntry.getEntryId());
457
458 AssetEntry importedAssetEntry = getAssetEntry(importedStagedModel);
459
460 List<AssetLink> importedAssetLinks = AssetLinkLocalServiceUtil.getLinks(
461 importedAssetEntry.getEntryId());
462
463 Assert.assertEquals(
464 originalAssetLinks.size(), importedAssetLinks.size());
465
466 for (AssetLink originalLink : originalAssetLinks) {
467 AssetEntry sourceAssetEntry = AssetEntryLocalServiceUtil.getEntry(
468 originalLink.getEntryId1());
469
470 AssetEntry targetAssetEntry = AssetEntryLocalServiceUtil.getEntry(
471 originalLink.getEntryId2());
472
473 Iterator<AssetLink> iterator = importedAssetLinks.iterator();
474
475 while (iterator.hasNext()) {
476 AssetLink importedLink = iterator.next();
477
478 AssetEntry importedLinkSourceAssetEntry =
479 AssetEntryLocalServiceUtil.getEntry(
480 importedLink.getEntryId1());
481 AssetEntry importedLinkTargetAssetEntry =
482 AssetEntryLocalServiceUtil.getEntry(
483 importedLink.getEntryId2());
484
485 if (!sourceAssetEntry.getClassUuid().equals(
486 importedLinkSourceAssetEntry.getClassUuid())) {
487
488 continue;
489 }
490
491 if (!targetAssetEntry.getClassUuid().equals(
492 importedLinkTargetAssetEntry.getClassUuid())) {
493
494 continue;
495 }
496
497 Assert.assertEquals(
498 originalLink.getWeight(), importedLink.getWeight());
499 Assert.assertEquals(
500 originalLink.getType(), importedLink.getType());
501
502 iterator.remove();
503
504 break;
505 }
506 }
507
508 Assert.assertEquals(0, importedAssetLinks.size());
509 }
510
511 }