001
014
015 package com.liferay.portal.lar.test;
016
017 import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
018 import com.liferay.portal.kernel.test.util.GroupTestUtil;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.kernel.util.Time;
022 import com.liferay.portal.kernel.xml.Element;
023 import com.liferay.portal.kernel.xml.SAXReaderUtil;
024 import com.liferay.portal.kernel.zip.ZipWriter;
025 import com.liferay.portal.kernel.zip.ZipWriterFactoryUtil;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portlet.PortletPreferencesImpl;
028 import com.liferay.portlet.exportimport.lar.ManifestSummary;
029 import com.liferay.portlet.exportimport.lar.PortletDataContext;
030 import com.liferay.portlet.exportimport.lar.PortletDataContextFactoryUtil;
031 import com.liferay.portlet.exportimport.lar.PortletDataHandler;
032 import com.liferay.portlet.exportimport.lar.PortletDataHandlerBoolean;
033 import com.liferay.portlet.exportimport.lar.StagedModelType;
034 import com.liferay.registry.Registry;
035 import com.liferay.registry.RegistryUtil;
036
037 import java.util.Collection;
038 import java.util.Date;
039 import java.util.Iterator;
040 import java.util.LinkedHashMap;
041 import java.util.Map;
042
043 import org.junit.Assert;
044 import org.junit.Before;
045 import org.junit.Test;
046
047
050 public abstract class BasePortletDataHandlerTestCase {
051
052 @Before
053 public void setUp() throws Exception {
054 stagingGroup = GroupTestUtil.addGroup();
055
056 portletId = getPortletId();
057
058 portletDataHandler = getPortletDataHandler(portletId);
059 }
060
061 @Test
062 public void testPrepareManifestSummary() throws Exception {
063 initExport();
064
065 addStagedModels();
066
067 portletDataContext.setEndDate(getEndDate());
068
069 portletDataHandler.prepareManifestSummary(portletDataContext);
070
071 ManifestSummary manifestSummary =
072 portletDataContext.getManifestSummary();
073
074 ManifestSummary expectedManifestSummary =
075 (ManifestSummary)manifestSummary.clone();
076
077 manifestSummary.resetCounters();
078
079 portletDataHandler.exportData(
080 portletDataContext, portletId, new PortletPreferencesImpl());
081
082 checkManifestSummary(expectedManifestSummary);
083 }
084
085 protected void addBooleanParameter(
086 Map<String, String[]> parameterMap, String namespace, String name,
087 boolean value) {
088
089 PortletDataHandlerBoolean portletDataHandlerBoolean =
090 new PortletDataHandlerBoolean(namespace, name);
091
092 parameterMap.put(
093 portletDataHandlerBoolean.getNamespacedControlName(),
094 new String[] {String.valueOf(value)});
095 }
096
097 protected void addParameters(Map<String, String[]> parameterMap) {
098 }
099
100 protected abstract void addStagedModels() throws Exception;
101
102 protected void checkManifestSummary(
103 ManifestSummary expectedManifestSummary) {
104
105 ManifestSummary manifestSummary =
106 portletDataContext.getManifestSummary();
107
108 for (String manifestSummaryKey :
109 manifestSummary.getManifestSummaryKeys()) {
110
111 Assert.assertFalse(
112 manifestSummaryKey.endsWith(
113 StagedModelType.REFERRER_CLASS_NAME_ALL));
114 Assert.assertFalse(
115 manifestSummaryKey.endsWith(
116 StagedModelType.REFERRER_CLASS_NAME_ANY));
117 }
118
119 for (String manifestSummaryKey :
120 expectedManifestSummary.getManifestSummaryKeys()) {
121
122 String[] keyParts = StringUtil.split(
123 manifestSummaryKey, StringPool.POUND);
124
125 long expectedModelAdditionCount =
126 expectedManifestSummary.getModelAdditionCount(
127 manifestSummaryKey);
128
129 StagedModelType stagedModelType = new StagedModelType(keyParts[0]);
130
131 if (keyParts.length > 1) {
132 stagedModelType = new StagedModelType(keyParts[0], keyParts[1]);
133 }
134
135 long modelAdditionCount = manifestSummary.getModelAdditionCount(
136 stagedModelType);
137
138 if (expectedModelAdditionCount == 0) {
139 Assert.assertFalse(modelAdditionCount > 0);
140 }
141 else {
142 Assert.assertEquals(
143 expectedModelAdditionCount, modelAdditionCount);
144 }
145 }
146 }
147
148 protected Date getEndDate() {
149 return new Date();
150 }
151
152 protected PortletDataHandler getPortletDataHandler(String portletId) {
153 try {
154 Registry registry = RegistryUtil.getRegistry();
155
156 Collection<PortletDataHandler> portletDataHandlers =
157 registry.getServices(
158 PortletDataHandler.class,
159 "(javax.portlet.name=" + portletId + ")");
160
161 Iterator<PortletDataHandler> iterator =
162 portletDataHandlers.iterator();
163
164 return iterator.next();
165 }
166 catch (Exception e) {
167 throw new RuntimeException(e);
168 }
169 }
170
171 protected abstract String getPortletId();
172
173 protected Date getStartDate() {
174 return new Date(System.currentTimeMillis() - Time.HOUR);
175 }
176
177 protected void initExport() throws Exception {
178 Map<String, String[]> parameterMap = new LinkedHashMap<>();
179
180 addParameters(parameterMap);
181
182 zipWriter = ZipWriterFactoryUtil.getZipWriter();
183
184 portletDataContext =
185 PortletDataContextFactoryUtil.createExportPortletDataContext(
186 stagingGroup.getCompanyId(), stagingGroup.getGroupId(),
187 parameterMap, getStartDate(), getEndDate(), zipWriter);
188
189 rootElement = SAXReaderUtil.createElement("root");
190
191 portletDataContext.setExportDataRootElement(rootElement);
192
193 missingReferencesElement = SAXReaderUtil.createElement(
194 "missing-references");
195
196 portletDataContext.setMissingReferencesElement(
197 missingReferencesElement);
198 }
199
200 protected Element missingReferencesElement;
201 protected PortletDataContext portletDataContext;
202 protected PortletDataHandler portletDataHandler;
203 protected String portletId;
204 protected Element rootElement;
205
206 @DeleteAfterTestRun
207 protected Group stagingGroup;
208
209 protected ZipWriter zipWriter;
210
211 }