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.lar.test;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.workflow.WorkflowThreadLocal;
019    import com.liferay.portal.kernel.xml.Element;
020    import com.liferay.portal.kernel.xml.SAXReaderUtil;
021    import com.liferay.portal.kernel.xml.XPath;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.model.StagedModel;
024    import com.liferay.portal.model.WorkflowedModel;
025    import com.liferay.portlet.exportimport.lar.ExportImportPathUtil;
026    import com.liferay.portlet.exportimport.lar.PortletDataContext;
027    import com.liferay.portlet.exportimport.lar.PortletDataException;
028    import com.liferay.portlet.exportimport.lar.StagedModelDataHandler;
029    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerRegistryUtil;
030    import com.liferay.portlet.exportimport.lar.StagedModelDataHandlerUtil;
031    
032    import java.util.List;
033    
034    import org.junit.Assert;
035    import org.junit.Test;
036    
037    /**
038     * @author Daniel Kocsis
039     */
040    public abstract class BaseWorkflowedStagedModelDataHandlerTestCase
041            extends BaseStagedModelDataHandlerTestCase {
042    
043            @Test
044            public void testExportWorkflowedStagedModels() throws Exception {
045                    initExport();
046    
047                    List<StagedModel> stagedModels = null;
048    
049                    boolean workflowEnabled = WorkflowThreadLocal.isEnabled();
050    
051                    try {
052                            WorkflowThreadLocal.setEnabled(true);
053    
054                            stagedModels = addWorkflowedStagedModels(stagingGroup);
055                    }
056                    finally {
057                            WorkflowThreadLocal.setEnabled(workflowEnabled);
058                    }
059    
060                    for (StagedModel stagedModel : stagedModels) {
061                            Assert.assertTrue(
062                                    "Staged model is not a workflowed model",
063                                    stagedModel instanceof WorkflowedModel);
064    
065                            try {
066                                    StagedModelDataHandlerUtil.exportStagedModel(
067                                            portletDataContext, stagedModel);
068                            }
069                            catch (PortletDataException pde) {
070                                    Assert.assertEquals(
071                                            "An unexpected error occurred during the export",
072                                            PortletDataException.STATUS_UNAVAILABLE, pde.getType());
073                            }
074    
075                            validateWorkflowedExport(portletDataContext, stagedModel);
076                    }
077            }
078    
079            protected abstract List<StagedModel> addWorkflowedStagedModels(Group group)
080                    throws Exception;
081    
082            protected Element getExportStagedModelElement(
083                    PortletDataContext portletDataContext, StagedModel stagedModel) {
084    
085                    Element rootElement = portletDataContext.getExportDataRootElement();
086    
087                    Class<?> modelClass = stagedModel.getModelClass();
088    
089                    Element modelElement = rootElement.element(modelClass.getSimpleName());
090    
091                    Assert.assertNotNull("Unable to find model element", modelElement);
092    
093                    XPath xPath = SAXReaderUtil.createXPath(
094                            "staged-model[@path ='" +
095                                    ExportImportPathUtil.getModelPath(stagedModel) + "']");
096    
097                    return (Element)xPath.selectSingleNode(modelElement);
098            }
099    
100            protected void validateWorkflowedExport(
101                            PortletDataContext portletDataContext, StagedModel stagedModel)
102                    throws Exception {
103    
104                    StagedModelDataHandler<?> stagedModelDataHandler =
105                            StagedModelDataHandlerRegistryUtil.getStagedModelDataHandler(
106                                    stagedModel.getModelClassName());
107    
108                    WorkflowedModel workflowedModel = (WorkflowedModel)stagedModel;
109    
110                    Element exportStagedModelElement = getExportStagedModelElement(
111                            portletDataContext, stagedModel);
112    
113                    if (ArrayUtil.contains(
114                                    stagedModelDataHandler.getExportableStatuses(),
115                                    workflowedModel.getStatus())) {
116    
117                            Assert.assertNotNull(
118                                    "Staged model should be exported", exportStagedModelElement);
119                    }
120                    else {
121                            Assert.assertNull(
122                                    "Staged model should not be exported",
123                                    exportStagedModelElement);
124                    }
125            }
126    
127    }