Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd1afcd0b34e5aa0e324d6ab164ad911dd87b1e5 (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
/*****************************************************************************
 * Copyright (c) 2014, 2015 CEA LIST, Christian W. Damus, 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:
 *  Anatoliy Tischenko - Initial API and implementation
 *  Christian W. Damus - bug 451230
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.activity.tests.edit.helper;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.rmi.UnexpectedException;
import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.TransactionalEditingDomainImpl;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.papyrus.infra.gmfdiag.common.commands.CreateEditBasedElementCommand;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.uml.diagram.activity.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.service.types.helper.ExpansionNodeHelper;
import org.eclipse.papyrus.uml.service.types.helper.ExpansionRegionHelper;
import org.eclipse.uml2.uml.ExpansionRegion;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.editor.presentation.UMLEditor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class ExpansionRegionHelperTest extends AbstractPapyrusTest {

	public static final String TEST_UML_MODEL = "platform:/plugin/org.eclipse.papyrus.uml.diagram.activity.tests/content/UMLActivityTestModel.uml";

	private MockExpansionRegionHelper myHelperInstance;

	private UMLEditor myUMLEditor;

	private TransactionalEditingDomain myEditingDomain;

	@Before
	public void setUp() throws Exception {
		myHelperInstance = new MockExpansionRegionHelper();
		myUMLEditor = new UMLEditor();
		myEditingDomain = new TransactionalEditingDomainImpl(myUMLEditor.getAdapterFactory()) {

			@Override
			public boolean isReadOnly(Resource resource) {
				return false;
			}
		};
	}

	@Test
	public void testExpansionNodeCreateCommand() throws Exception {
		CreateElementRequest expansionNodeCreateRequest = initCreateElementRequest(UMLElementTypes.ExpansionNode_InputShape, UMLPackage.eINSTANCE.getExpansionRegion_InputElement());
		ICommand command = myHelperInstance.getCreateCommand(expansionNodeCreateRequest);
		commonExpansionRegionCreateChildTest(command);
		CreateEditBasedElementCommand createCommand = (CreateEditBasedElementCommand) command;
		assertNotEquals(expansionNodeCreateRequest, createCommand.getCreateRequest());
		CreateElementRequest changedRequest = createCommand.getCreateRequest();
		assertEquals(changedRequest.getContainer(), expansionNodeCreateRequest.getContainer());
		assertEquals(changedRequest.getContainmentFeature(), UMLPackage.eINSTANCE.getStructuredActivityNode_Node());
		assertEquals(changedRequest.getParameter(ExpansionNodeHelper.IN_EXPANSION_REGION), changedRequest.getContainer());
		assertEquals(changedRequest.getParameter(ExpansionNodeHelper.EXPANSION_REGION_FEATURE), UMLPackage.eINSTANCE.getExpansionRegion_InputElement());
	}

	@Test
	public void testExpansionRegionContainmentChildCreateCommand() throws Exception {
		CreateElementRequest expansionRegionContainmentCreateRequest = initCreateElementRequest(UMLElementTypes.StructuredActivityNode_Shape, UMLPackage.eINSTANCE.getStructuredActivityNode_Node());
		ICommand command = myHelperInstance.getCreateCommand(expansionRegionContainmentCreateRequest);
		commonExpansionRegionCreateChildTest(command);
		CreateEditBasedElementCommand createCommand = (CreateEditBasedElementCommand) command;
		assertNotEquals(expansionRegionContainmentCreateRequest, createCommand.getCreateRequest());
	}

	@Test
	public void testExpansionRegionBadCreateCommand() throws Exception {
		CreateElementRequest badCreateCommandRequest = initCreateElementRequest(UMLElementTypes.ExpansionNode_InputShape, UMLPackage.eINSTANCE.getStructuredActivityNode_Edge());
		ICommand command = myHelperInstance.getCreateCommand(badCreateCommandRequest);
		assertFalse(command.canExecute());
	}

	private void commonExpansionRegionCreateChildTest(ICommand command) throws Exception {
		assertNotNull(command);
		assertTrue(command.canExecute());
		assertEquals(CreateEditBasedElementCommand.class, command.getClass());
	}

	private CreateElementRequest initCreateElementRequest(IElementType childType, EReference feature) throws UnexpectedException {
		CreateElementRequest req = new CreateElementRequest(childType);
		req.setContainer(loadExpansionRegion());
		req.setContainmentFeature(feature);
		req.setEditingDomain(myEditingDomain);
		return req;
	}

	private ExpansionRegion loadExpansionRegion() throws UnexpectedException {
		ResourceSet resourceSet = myUMLEditor.getEditingDomain().getResourceSet();
		Resource resource = resourceSet.getResource(URI.createURI(TEST_UML_MODEL), true);
		for (Iterator<EObject> i = resource.getAllContents(); i.hasNext();) {
			EObject content = i.next();
			if (content instanceof ExpansionRegion) {
				return (ExpansionRegion) content;
			}
		}
		throw new UnexpectedException("ExpansionRegion element was not fount in " + TEST_UML_MODEL);
	}

	@After
	public void tearDown() throws Exception {
		myEditingDomain = null;
		myUMLEditor = null;
		myHelperInstance = null;
	}

	private static class MockExpansionRegionHelper extends ExpansionRegionHelper {

		@Override
		public ICommand getCreateCommand(CreateElementRequest req) {
			return super.getCreateCommand(req);
		}

	}
}

Back to the top