Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5189172d88ea2b38f855f7e001c4a3f7b79ec6e9 (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
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * 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.diagram.stereotypeproperty;

import java.util.ArrayList;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequest;
import org.eclipse.gmf.runtime.diagram.ui.requests.CreateViewRequestFactory;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.uml.diagram.clazz.edit.parts.ClassEditPart;
import org.eclipse.papyrus.uml.diagram.clazz.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.diagram.stereotype.edition.editpart.AppliedStereotypeConpartmentEditPart;
import org.eclipse.papyrus.uml.extensionpoints.profile.RegisteredProfile;
import org.eclipse.papyrus.uml.extensionpoints.utils.Util;
import org.eclipse.papyrus.uml.tools.commands.ApplyStereotypeCommand;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.junit.Before;
import org.junit.Test;


public class TestStereotypeApplication extends AbstractPapyrusTestCase {


	private static final String TEST_PROFILE_STEREOTYPE1 = "testProfile::Stereotype1";

	@Test
	public void testToManageModel() {
		testToCreateANode(UMLElementTypes.Class_2008);

	}

	/**
	 * Test to create a node.
	 * 
	 * @param type
	 *        the type
	 */
	public void testToCreateANode(IElementType type) {

		//CREATION
		assertTrue(CREATION + INITIALIZATION_TEST, getDiagramEditPart().getChildren().size() == 0);

		assertTrue(CREATION + INITIALIZATION_TEST, getRootSemanticModel().getOwnedElements().size() == 1);
		//1 element element due to the reference to the profile
		EList<Profile> list = ((Model)getRootSemanticModel()).getAllAppliedProfiles();
		assertTrue(CREATION + INITIALIZATION_TEST, ((Model)getRootSemanticModel()).getAllAppliedProfiles().size() == 1);

		CreateViewRequest requestcreation = CreateViewRequestFactory.getCreateShapeRequest(type, getDiagramEditPart().getDiagramPreferencesHint());
		Command command = getDiagramEditPart().getCommand(requestcreation);
		assertNotNull(CREATION + COMMAND_NULL, command);
		assertTrue(CREATION + TEST_IF_THE_COMMAND_IS_CREATED, command != UnexecutableCommand.INSTANCE);
		assertTrue("CREATION: " + TEST_IF_THE_COMMAND_CAN_BE_EXECUTED, command.canExecute() == true);
		diagramEditor.getDiagramEditDomain().getDiagramCommandStack().execute(command);
		assertTrue(CREATION + TEST_THE_EXECUTION, getRootView().getChildren().size() == 1);
		Element element = getRootSemanticModel().getOwnedElements().get(1);
		GraphicalEditPart classEditPart = (ClassEditPart)getDiagramEditPart().getChildren().get(0);
		assertTrue(CREATION + TEST_THE_EXECUTION, element.getApplicableStereotype(TEST_PROFILE_STEREOTYPE1) != null);
		ArrayList<Stereotype> stereotypeslist = new ArrayList<Stereotype>();
		stereotypeslist.add(element.getApplicableStereotype(TEST_PROFILE_STEREOTYPE1));

		ApplyStereotypeCommand applyStereotypeCommand = new ApplyStereotypeCommand(((Element)classEditPart.resolveSemanticElement()), stereotypeslist, diagramEditor.getEditingDomain());
		diagramEditor.getEditingDomain().getCommandStack().execute(applyStereotypeCommand);
		//diagramEditor.getDiagramEditDomain().getDiagramCommandStack().execute(new ICommandProxy(new EMFtoGMFCommandWrapper(applyStereotypeCommand)));
		Element semanticElement = (Element)classEditPart.resolveSemanticElement();
		assertTrue(CREATION + TEST_THE_EXECUTION + "No applied stereotype found on the element ", semanticElement.getAppliedStereotypes().size() != 0);

		View appliedStereotypeMultilinePropertyView = null;
		for(int i = 0; i < classEditPart.getNotationView().getChildren().size(); i++) {
			View view = ((View)classEditPart.getNotationView().getChildren().get(i));
			if(view.getType().equals(AppliedStereotypeConpartmentEditPart.ID)) {
				appliedStereotypeMultilinePropertyView = view;

			}
		}
		//the thread is asynchronous
		assertTrue(CREATION + TEST_THE_EXECUTION + "No stereotype Compartment found in the notation", appliedStereotypeMultilinePropertyView == null);

		//		AppliedStereotypeMultilinePropertyEditPart appliedStereotypeMultilinePropertyEditPart=null;
		//		for(int i=0; i<classEditPart.getChildren().size();i++){
		//			GraphicalEditPart editPart= ((GraphicalEditPart)classEditPart.getChildren().get(i));
		//			if (editPart.getNotationView().getType().equals(AppliedStereotypeMultilinePropertyEditPart.ID)){
		//				appliedStereotypeMultilinePropertyEditPart=(AppliedStereotypeMultilinePropertyEditPart)editPart;
		//				
		//			}
		//		}
		//		assertTrue(CREATION+TEST_THE_EXECUTION,appliedStereotypeMultilinePropertyEditPart!=null);
	}

	@Before
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		RegisteredProfile registeredProfile = RegisteredProfile.getRegisteredProfile("TestProfile");
		final Model root = ((Model)getDiagramEditPart().resolveSemanticElement());
		URI modelUri = registeredProfile.uri;
		final Resource modelResource = Util.getResourceSet(root).getResource(modelUri, true);
		final Profile profile = (Profile)modelResource.getContents().get(0);
		//	PackageUtil.applyProfile(root,profile, false);
		final TransactionalEditingDomain domain = ServiceUtils.getInstance().getTransactionalEditingDomain(papyrusEditor.getServicesRegistry());
		AppliedProfileCommand appliedProfileCommand = new AppliedProfileCommand(domain, root, profile);
		domain.getCommandStack().execute(new GMFtoEMFCommandWrapper(appliedProfileCommand));


	}
}

Back to the top