Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb7a60b3f3f355fedab323cb7c9a44696c328f38 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*****************************************************************************
 * Copyright (c) 2014 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.papyrusrt.umlrt.core.tests;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.resource.ModelUtils;
import org.eclipse.papyrus.infra.core.resource.NotFoundException;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.types.core.registries.ElementTypeSetConfigurationRegistry;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusConfiguration;
import org.eclipse.papyrus.infra.viewpoints.configuration.PapyrusViewpoint;
import org.eclipse.papyrus.infra.viewpoints.policy.PolicyChecker;
import org.eclipse.papyrus.junit.framework.classification.tests.AbstractPapyrusTest;
import org.eclipse.papyrus.junit.utils.PapyrusProjectUtils;
import org.eclipse.papyrus.junit.utils.rules.HouseKeeper;
import org.eclipse.papyrus.uml.diagram.statemachine.providers.UMLElementTypes;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.papyrus.uml.tools.model.UmlUtils;
import org.eclipse.papyrusrt.umlrt.core.types.UMLRTElementTypesEnumerator;
import org.eclipse.papyrusrt.umlrt.core.utils.ProtocolUtils;
import org.eclipse.swt.widgets.Display;
import org.eclipse.uml2.uml.Collaboration;
import org.eclipse.uml2.uml.Interface;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;

/**
 * Generic class test for edition tests
 */
public abstract class AbstractPapyrusRTCoreTest extends AbstractPapyrusTest {

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

	protected static IProject createProject;

	protected static IFile copyPapyrusModel;

	protected static IMultiDiagramEditor openPapyrusEditor;

	protected static ModelSet modelset;

	protected static UmlModel umlIModel;

	protected static Model rootModel;

	protected static TransactionalEditingDomain transactionalEditingDomain;

	protected static Package protocolContainer;

	protected static Collaboration protocol;
	
	protected static org.eclipse.uml2.uml.Class capsule;

	protected static Interface messageSetIn;

	/**
	 * Init test class
	 */
	@BeforeClass
	public static void initCreateElementTest() throws Exception {

		// create Project
		createProject = houseKeeper.createProject("UMLRTCoreTests");

		// import test model
		try {
			copyPapyrusModel = PapyrusProjectUtils.copyPapyrusModel(createProject, Platform.getBundle("org.eclipse.papyrusrt.umlrt.core.tests"), "/resource/", "TestModel");
		} catch (CoreException e) {
			fail(e.getMessage());
		} catch (IOException e) {
			fail(e.getMessage());
		}

		// ensure viewpoint...
		PapyrusConfiguration loadConfigurationFrom = PolicyChecker.loadConfigurationFrom("platform:/plugin/org.eclipse.papyrusrt.umlrt.tooling.diagram.common/configuration/UMLRT.configuration");
		PolicyChecker.setCurrent(new PolicyChecker(loadConfigurationFrom, (PapyrusViewpoint) loadConfigurationFrom.getStakeholders().get(0).getViewpoints().get(0), false));
		Display.getDefault().syncExec(new Runnable() {

			@Override
			public void run() {
				// force loading of the state machine plugin, but in UI thread, as the element types require some UI...
				ElementTypeSetConfigurationRegistry registry = ElementTypeSetConfigurationRegistry.getInstance();
				System.err.println(registry.getElementTypeSetConfigurations());
				System.err.println(UMLElementTypes.Region_Shape);
				
			}
		});

		Assert.assertTrue("Invalid viewpoint", PolicyChecker.getCurrent().getConfiguration().equals(loadConfigurationFrom));

		// open project
		openPapyrusEditor = houseKeeper.openPapyrusEditor(copyPapyrusModel);

		transactionalEditingDomain = openPapyrusEditor.getAdapter(TransactionalEditingDomain.class);
		assertTrue("Impossible to init editing domain", transactionalEditingDomain instanceof TransactionalEditingDomain);

		// retrieve UML model from this editor
		try {
			modelset = ModelUtils.getModelSetChecked(openPapyrusEditor.getServicesRegistry());
			umlIModel = UmlUtils.getUmlModel(modelset);
			rootModel = (Model) umlIModel.lookupRoot();
		} catch (ServiceException e) {
			fail(e.getMessage());
		} catch (NotFoundException e) {
			fail(e.getMessage());
		} catch (ClassCastException e) {
			fail(e.getMessage());
		}
		try {
			initExistingElements();
		} catch (Exception e) {
			fail(e.getMessage());
		}

		// force load of the element type registry. Will need to load in in UI thread because of some advice in communication diag: see org.eclipse.gmf.tooling.runtime.providers.DiagramElementTypeImages
		Display.getDefault().syncExec(new Runnable() {

			@Override
			public void run() {
				ElementTypeSetConfigurationRegistry registry = ElementTypeSetConfigurationRegistry.getInstance();
				Assert.assertNotNull("registry should not be null after init", registry);
				Assert.assertNotNull("element type should not be null", UMLRTElementTypesEnumerator.CAPSULE);
			}
		});
	}

	/**
	 * Init fields corresponding to element in the test model
	 */
	protected static void initExistingElements() throws Exception {
		protocolContainer = (Package) rootModel.getPackagedElement("Protocol0", true, UMLPackage.eINSTANCE.getPackage(), false);
		Assert.assertNotNull(protocolContainer);

		protocol = (Collaboration) protocolContainer.getPackagedElement("Protocol0", true, UMLPackage.eINSTANCE.getCollaboration(), false);
		Assert.assertNotNull(protocol);

		messageSetIn = (Interface) protocolContainer.getPackagedElement("Protocol0", true, UMLPackage.eINSTANCE.getInterface(), false);
		Assert.assertNotNull(messageSetIn);
		Assert.assertEquals("MessageSet In does not correspond to the one from utility methods", ProtocolUtils.getMessageSetIn(protocol), messageSetIn);
		
		capsule = (org.eclipse.uml2.uml.Class) rootModel.getPackagedElement("Capsule0", true, UMLPackage.eINSTANCE.getClass_(), false);
		Assert.assertNotNull(capsule);
	}
}

Back to the top