blob: 07637383a6304afc26c582b9d865520886cedc8a [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2021 CEA LIST and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.model2doc.integration.ieee.requirements.sysml16.odt.architecture.tests;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.emf.gmf.command.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.infra.types.ElementTypeSetConfiguration;
import org.eclipse.papyrus.infra.types.core.registries.ElementTypeSetConfigurationRegistry;
import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture;
import org.eclipse.papyrus.junit.utils.rules.PluginResource;
import org.eclipse.papyrus.model2doc.emf.documentstructuretemplate.DocumentTemplate;
import org.eclipse.papyrus.model2doc.emf.documentstructuretemplate.utils.DocumentStructureTemplateConstants;
import org.eclipse.papyrus.model2doc.integration.emf.documentstructuretemplate.types.internal.advices.DocumentTemplateElementTypesConstants;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@SuppressWarnings("nls")
@PluginResource("resources/DocumentTemplateElementType/SysML16_DocumentTemplateSemanticOwnerDeletion.di")
public class SysML16_DocumentTemplateElementTypeTest {
/**
* The editor fixture
*/
@Rule
public final PapyrusEditorFixture fixture = new PapyrusEditorFixture(Collections.singletonList("pdst"));
/**
* This tests checks the elements available for the creation throw the
* Architecture Framework
*/
@Test
public void checkDocumentTemplateOwnerDeletion() {
final Package root = this.fixture.getModel();
final Resource pdstRes = getPDSTResourceForCurrentModel();
Assert.assertNotNull("The pdst resource has not been found.", pdstRes);
Assert.assertEquals("The pdst resource doesn't contains the expected elements.", 2, pdstRes.getContents().size());
NamedElement subPackage = root.getOwnedMember("SubPackage");
Assert.assertNotNull("The subpackage has not been found.", subPackage);
for (final EObject current : pdstRes.getContents()) {
Assert.assertTrue("The pdst resource doesn't contains the expected elements.", current instanceof DocumentTemplate);
Assert.assertTrue("The owner of the DocumentTemplate is not the expected one.", subPackage == ((DocumentTemplate) current).getSemanticContext());
}
DestroyElementRequest destroyRequest = new DestroyElementRequest(subPackage, false);
final IElementEditService provider = ElementEditServiceUtils.getCommandProvider(subPackage);
final ICommand cmd = provider.getEditCommand(destroyRequest);
Assert.assertNotNull("The destroy command is null", cmd);
Assert.assertTrue("The destroy command is not executable", cmd.canExecute());
// we check the command
final CommandStack commandStack = this.fixture.getEditingDomain().getCommandStack();
commandStack.execute(GMFtoEMFCommandWrapper.wrap(cmd));
this.fixture.save();
Assert.assertNull("The subpackage has not been deleted", root.getOwnedMember("SubPackage"));
// we check the contents of the pdst file
Assert.assertEquals("The pdst resource is not empty after the deletion of the semantic context", 0, pdstRes.getContents().size());
commandStack.undo();
Assert.assertEquals("The pdst resource is empty after the undo of the deletion of the semantic context", 2, pdstRes.getContents().size());
commandStack.redo();
this.fixture.save();
Assert.assertEquals("The pdst resource is not empty after the redo of the deletion of the semantic context", 0, pdstRes.getContents().size());
}
@Test
public void checkElementTypeConfiguration() {
final Map<String, Map<String, ElementTypeSetConfiguration>> elementTypeSetConfigurationsArchitecture = ElementTypeSetConfigurationRegistry.getInstance().getElementTypeSetConfigurations();
Assert.assertNotNull("The method ElementTypeSetConfigurationRegistry.getInstance().getElementTypeSetConfigurations() returns null", elementTypeSetConfigurationsArchitecture);
Assert.assertFalse("The method ElementTypeSetConfigurationRegistry.getInstance().getElementTypeSetConfigurations() returns an empty map", elementTypeSetConfigurationsArchitecture.isEmpty());
final Map<String, ElementTypeSetConfiguration> elementTypeSetConfigurations = elementTypeSetConfigurationsArchitecture.get(DocumentTemplateElementTypesConstants.PAPYRUS_ELEMENT_TYPE_CONTEXT);
Assert.assertNotNull(NLS.bind("The map of ElementTypeSetConfiguration is null for the context {0}", DocumentTemplateElementTypesConstants.PAPYRUS_ELEMENT_TYPE_CONTEXT), elementTypeSetConfigurations);
Assert.assertFalse(NLS.bind("The map of ElementTypeSetConfiguration is empty the context {0}", DocumentTemplateElementTypesConstants.PAPYRUS_ELEMENT_TYPE_CONTEXT), elementTypeSetConfigurations.isEmpty());
boolean findRequiredTypeSet = false;
final Iterator<Entry<String, ElementTypeSetConfiguration>> iter = elementTypeSetConfigurations.entrySet().iterator();
while (iter.hasNext() && !findRequiredTypeSet) {
findRequiredTypeSet = DocumentTemplateElementTypesConstants.DOCUMENT_TEMPLATE_ELEMENT_TYPE_SET_ID.equals(iter.next().getValue().getIdentifier());
}
Assert.assertTrue(findRequiredTypeSet);
}
/**
*
* @return the *.pdst resource associated to the current model
*/
protected final Resource getPDSTResourceForCurrentModel() {
for (final Resource current : this.fixture.getResourceSet().getResources()) {
if (DocumentStructureTemplateConstants.DOCUMENT_STRUCTURE_TEMPLATE_FILE_EXTENSION
.equals(current.getURI().fileExtension())) {
if (this.fixture.getModelResourceURI().trimFileExtension()
.equals(current.getURI().trimFileExtension())) {
return current;
}
}
}
return null;
}
}