Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5a85f4e4ebc608c14bc1f2709cb5fa3d207d1d95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.service.types.ui.tests.deletion;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.requests.DestroyElementRequest;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.ElementEditServiceUtils;
import org.eclipse.papyrus.infra.services.edit.service.IElementEditService;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Property;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

/**
 * @author Camille Letavernier
 *
 */
public class DeleteAssociationTest {

	@ClassRule
	public static final HouseKeeper.Static staticHouseKeeper = new HouseKeeper.Static();

	@Rule
	public HouseKeeper houseKeeper = new HouseKeeper();

	private static IProject createProject;

	private static IFile copyPapyrusModel;

	private Model rootModel;

	private Resource resource;

	private TransactionalEditingDomain domain;

	private Association association;

	private Property end1, end2;

	private IMultiDiagramEditor papyrusEditor;

	@BeforeClass
	public static void initProject() {
		// create Project
		createProject = staticHouseKeeper.createProject("AssociationTestModel");

		// create UML resource
		// import test model
		staticHouseKeeper.createFile(createProject, "AssociationTestModel.uml", "/resource/AssociationTestModel.uml");
		staticHouseKeeper.createFile(createProject, "AssociationTestModel.notation", "/resource/AssociationTestModel.notation");
		copyPapyrusModel = staticHouseKeeper.createFile(createProject, "AssociationTestModel.di", "/resource/AssociationTestModel.di");
	}

	@Before
	public void openModel() throws Exception {
		papyrusEditor = houseKeeper.openPapyrusEditor(copyPapyrusModel);
		ModelSet modelSet = ServiceUtils.getInstance().getModelSet(papyrusEditor.getServicesRegistry());
		resource = ((UmlModel) modelSet.getModel(UmlModel.MODEL_ID)).getResource();

		rootModel = (Model) resource.getContents().get(0);

		domain = ServiceUtils.getInstance().getTransactionalEditingDomain(papyrusEditor.getServicesRegistry());

		association = (Association) rootModel.getPackagedElement("TestAssociation");

		end1 = association.getMemberEnds().get(0);
		end2 = association.getMemberEnds().get(1);
	}

	@Test
	public void deleteAssociation() {
		deleteAndTest(association);
	}

	@Test
	public void deleteAssociationTarget() {
		Classifier source = (Classifier) rootModel.getPackagedElement("Class2"); // Delete class2, which doesn't own any member end
		deleteAndTest(source);
	}

	// Deletes the element, and verify that the Association and its properties have been removed from model. Also test Undo/Redo
	protected void deleteAndTest(EObject elementToDelete) {

		assertInitialState();

		IElementEditService elementEditService = ElementEditServiceUtils.getCommandProvider(elementToDelete);
		ICommand command = elementEditService.getEditCommand(new DestroyElementRequest(elementToDelete, false));

		Command emfCommand = new GMFtoEMFCommandWrapper(command);
		domain.getCommandStack().execute(emfCommand);

		assertFinalState();

		domain.getCommandStack().undo();

		assertInitialState();

		domain.getCommandStack().redo();

		assertFinalState();
	}

	protected void assertInitialState() {
		Assert.assertEquals(resource, association.eResource());
		Assert.assertEquals(resource, end1.eResource());
		Assert.assertEquals(resource, end2.eResource());

		Assert.assertEquals(association.getMemberEnds().get(0), end1);
		Assert.assertEquals(association.getMemberEnds().get(1), end2);

		Assert.assertEquals(association, end1.eContainer());
		Assert.assertEquals("Class1", ((Classifier) end2.eContainer()).getName());
	}

	protected void assertFinalState() {
		Assert.assertNull(association.eResource());
		Assert.assertNull(end1.eResource());
		Assert.assertNull(end2.eResource());
	}
}

Back to the top